From 0132332df417cd58e700c0536c40361ef426f0bf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 04:53:17 +0000 Subject: [PATCH 1/9] feat: Add Los Angeles weather retrieval in main.go --- main.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b503518..06060a4 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,65 @@ package main import ( + "encoding/json" "fmt" + "io" + "net/http" "time" ) -// 時間を表示する簡単なgo言語のプログラム +// OpenWeatherMap APIのレスポンス構造体 +type WeatherResponse struct { + Weather []struct { + Description string `json:"description"` + } `json:"weather"` + Main struct { + Temp float64 `json:"temp"` + } `json:"main"` + Name string `json:"name"` +} + +// ロサンゼルスの天気を取得して表示するプログラム func main() { - fmt.Println("The time is", time.Now()) + // ロサンゼルスの天気を取得 + weather, err := getLAWeather() + if err != nil { + fmt.Println("天気情報の取得に失敗しました:", err) + return + } + + // 結果を表示 + fmt.Printf("日時: %s\n", time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("場所: %s\n", weather.Name) + if len(weather.Weather) > 0 { + fmt.Printf("天気: %s\n", weather.Weather[0].Description) + } + fmt.Printf("気温: %.1f°C\n", weather.Main.Temp) +} + +// ロサンゼルスの天気情報を取得する関数 +func getLAWeather() (*WeatherResponse, error) { + // OpenWeatherMap APIのエンドポイント (注: 実際の使用にはAPIキーが必要です) + url := "https://api.openweathermap.org/data/2.5/weather?q=Los%20Angeles&units=metric&appid=YOUR_API_KEY" + + // HTTPリクエスト + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + // レスポンスの読み取り + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + // JSONのデコード + var weather WeatherResponse + if err := json.Unmarshal(body, &weather); err != nil { + return nil, err + } + + return &weather, nil } From cbc464b906af227c6c03fa5449c188b24a262a47 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 04:59:34 +0000 Subject: [PATCH 2/9] feat: Use dotenv for API key retrieval --- main.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 06060a4..0cff125 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,12 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" + "os" "time" + + "github.com/joho/godotenv" ) // OpenWeatherMap APIのレスポンス構造体 @@ -21,6 +25,12 @@ type WeatherResponse struct { // ロサンゼルスの天気を取得して表示するプログラム func main() { + // .envファイルから環境変数を読み込む + err := godotenv.Load() + if err != nil { + log.Println("Warning: .envファイルの読み込みに失敗しました。環境変数から直接APIキーを取得します。") + } + // ロサンゼルスの天気を取得 weather, err := getLAWeather() if err != nil { @@ -39,8 +49,14 @@ func main() { // ロサンゼルスの天気情報を取得する関数 func getLAWeather() (*WeatherResponse, error) { - // OpenWeatherMap APIのエンドポイント (注: 実際の使用にはAPIキーが必要です) - url := "https://api.openweathermap.org/data/2.5/weather?q=Los%20Angeles&units=metric&appid=YOUR_API_KEY" + // 環境変数からAPIキーを取得 + apiKey := os.Getenv("OPENWEATHERMAP_API_KEY") + if apiKey == "" { + return nil, fmt.Errorf("APIキーが設定されていません。.envファイルまたは環境変数にOPENWEATHERMAP_API_KEYを設定してください") + } + + // OpenWeatherMap APIのエンドポイント + url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=Los%%20Angeles&units=metric&appid=%s", apiKey) // HTTPリクエスト resp, err := http.Get(url) From 44416dd9e82fd924078aafae8b688bd3d0a27771 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 05:06:45 +0000 Subject: [PATCH 3/9] fix: change time zone to Japan time --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 0cff125..2bdfed6 100644 --- a/main.go +++ b/main.go @@ -38,8 +38,12 @@ func main() { return } + // 日本のタイムゾーンを設定 + jst := time.FixedZone("JST", 9*60*60) + now := time.Now().In(jst) + // 結果を表示 - fmt.Printf("日時: %s\n", time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("日時: %s\n", now.Format("2006-01-02 15:04:05")) fmt.Printf("場所: %s\n", weather.Name) if len(weather.Weather) > 0 { fmt.Printf("天気: %s\n", weather.Weather[0].Description) From fd16d212d9dfb674bcc880c2ca6e7247f4ab02f0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 05:11:30 +0000 Subject: [PATCH 4/9] Add weather feature for Japan location --- main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2bdfed6..31f80c8 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ type WeatherResponse struct { Name string `json:"name"` } -// ロサンゼルスの天気を取得して表示するプログラム +// 東京の天気を取得して表示するプログラム func main() { // .envファイルから環境変数を読み込む err := godotenv.Load() @@ -31,8 +31,8 @@ func main() { log.Println("Warning: .envファイルの読み込みに失敗しました。環境変数から直接APIキーを取得します。") } - // ロサンゼルスの天気を取得 - weather, err := getLAWeather() + // 東京の天気を取得 + weather, err := getTokyoWeather() if err != nil { fmt.Println("天気情報の取得に失敗しました:", err) return @@ -51,8 +51,8 @@ func main() { fmt.Printf("気温: %.1f°C\n", weather.Main.Temp) } -// ロサンゼルスの天気情報を取得する関数 -func getLAWeather() (*WeatherResponse, error) { +// 東京の天気情報を取得する関数 +func getTokyoWeather() (*WeatherResponse, error) { // 環境変数からAPIキーを取得 apiKey := os.Getenv("OPENWEATHERMAP_API_KEY") if apiKey == "" { @@ -60,7 +60,7 @@ func getLAWeather() (*WeatherResponse, error) { } // OpenWeatherMap APIのエンドポイント - url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=Los%%20Angeles&units=metric&appid=%s", apiKey) + url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=Tokyo&units=metric&appid=%s", apiKey) // HTTPリクエスト resp, err := http.Get(url) From 0dfbf6080e924c80274af38d352cce8c010489fc Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 05:22:17 +0000 Subject: [PATCH 5/9] feat: Add location selection for LA and Tokyo --- main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 31f80c8..eb4c9e7 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "io" "log" @@ -23,16 +24,20 @@ type WeatherResponse struct { Name string `json:"name"` } -// 東京の天気を取得して表示するプログラム +// 天気を取得して表示するプログラム func main() { + // コマンドライン引数の解析 + location := flag.String("location", "Tokyo", "天気を取得する都市名") + flag.Parse() + // .envファイルから環境変数を読み込む err := godotenv.Load() if err != nil { log.Println("Warning: .envファイルの読み込みに失敗しました。環境変数から直接APIキーを取得します。") } - // 東京の天気を取得 - weather, err := getTokyoWeather() + // 指定された場所の天気を取得 + weather, err := getWeather(*location) if err != nil { fmt.Println("天気情報の取得に失敗しました:", err) return @@ -51,8 +56,8 @@ func main() { fmt.Printf("気温: %.1f°C\n", weather.Main.Temp) } -// 東京の天気情報を取得する関数 -func getTokyoWeather() (*WeatherResponse, error) { +// 指定された場所の天気情報を取得する関数 +func getWeather(location string) (*WeatherResponse, error) { // 環境変数からAPIキーを取得 apiKey := os.Getenv("OPENWEATHERMAP_API_KEY") if apiKey == "" { @@ -60,7 +65,7 @@ func getTokyoWeather() (*WeatherResponse, error) { } // OpenWeatherMap APIのエンドポイント - url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=Tokyo&units=metric&appid=%s", apiKey) + url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=%s", location, apiKey) // HTTPリクエスト resp, err := http.Get(url) From 1191271e5caea9988c93c7342d8a412d82f582d6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 05:31:39 +0000 Subject: [PATCH 6/9] feat: Add Osaka to the project --- main.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index eb4c9e7..0bb505f 100644 --- a/main.go +++ b/main.go @@ -27,9 +27,35 @@ type WeatherResponse struct { // 天気を取得して表示するプログラム func main() { // コマンドライン引数の解析 - location := flag.String("location", "Tokyo", "天気を取得する都市名") + var locationName string + flag.StringVar(&locationName, "location", "Tokyo", "天気を取得する都市名") + + // 場所選択 + locations := map[string]string{ + "tokyo": "Tokyo", + "la": "Los Angeles", + "osaka": "Osaka", + } + + locationsHelp := "利用可能な場所: " + for k, v := range locations { + locationsHelp += fmt.Sprintf("%s (%s) ", k, v) + } + + location := flag.String("city", "", locationsHelp) flag.Parse() + // 場所が指定されていれば、それを使用 + if *location != "" { + if cityName, ok := locations[*location]; ok { + locationName = cityName + } else { + fmt.Printf("未知の場所です: %s\n", *location) + fmt.Println(locationsHelp) + return + } + } + // .envファイルから環境変数を読み込む err := godotenv.Load() if err != nil { @@ -37,7 +63,7 @@ func main() { } // 指定された場所の天気を取得 - weather, err := getWeather(*location) + weather, err := getWeather(locationName) if err != nil { fmt.Println("天気情報の取得に失敗しました:", err) return From 0a28ee35c6b835474f540e73fbb27e0892ee1df4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 06:13:30 +0000 Subject: [PATCH 7/9] feat: Add support for Hokkaido region --- main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 0bb505f..0c75e9d 100644 --- a/main.go +++ b/main.go @@ -32,9 +32,10 @@ func main() { // 場所選択 locations := map[string]string{ - "tokyo": "Tokyo", - "la": "Los Angeles", - "osaka": "Osaka", + "tokyo": "Tokyo", + "la": "Los Angeles", + "osaka": "Osaka", + "hokkaido": "Sapporo", } locationsHelp := "利用可能な場所: " From a3c7ac9e5537f562c6fc7823f3dded6faeb878be Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 06:39:53 +0000 Subject: [PATCH 8/9] Rewrite main.go in JavaScript --- main.go | 186 ++++++++++++++++++++++++-------------------------------- 1 file changed, 79 insertions(+), 107 deletions(-) diff --git a/main.go b/main.go index 0c75e9d..368168e 100644 --- a/main.go +++ b/main.go @@ -1,117 +1,89 @@ -package main +// index.js -import ( - "encoding/json" - "flag" - "fmt" - "io" - "log" - "net/http" - "os" - "time" - - "github.com/joho/godotenv" -) +require('dotenv').config(); +const axios = require('axios'); +const yargs = require('yargs/yargs'); +const { hideBin } = require('yargs/helpers'); -// OpenWeatherMap APIのレスポンス構造体 -type WeatherResponse struct { - Weather []struct { - Description string `json:"description"` - } `json:"weather"` - Main struct { - Temp float64 `json:"temp"` - } `json:"main"` - Name string `json:"name"` -} +// コマンドライン引数の設定 +const argv = yargs(hideBin(process.argv)) + .option('city', { + alias: 'c', + description: '天気を取得する都市名', + type: 'string', + default: '' + }) + .option('location', { + alias: 'l', + description: '天気を取得する都市名 (完全名)', + type: 'string', + default: 'Tokyo' + }) + .help() + .alias('help', 'h') + .argv; -// 天気を取得して表示するプログラム -func main() { - // コマンドライン引数の解析 - var locationName string - flag.StringVar(&locationName, "location", "Tokyo", "天気を取得する都市名") - - // 場所選択 - locations := map[string]string{ - "tokyo": "Tokyo", - "la": "Los Angeles", - "osaka": "Osaka", - "hokkaido": "Sapporo", - } - - locationsHelp := "利用可能な場所: " - for k, v := range locations { - locationsHelp += fmt.Sprintf("%s (%s) ", k, v) - } - - location := flag.String("city", "", locationsHelp) - flag.Parse() - - // 場所が指定されていれば、それを使用 - if *location != "" { - if cityName, ok := locations[*location]; ok { - locationName = cityName - } else { - fmt.Printf("未知の場所です: %s\n", *location) - fmt.Println(locationsHelp) - return - } - } - - // .envファイルから環境変数を読み込む - err := godotenv.Load() - if err != nil { - log.Println("Warning: .envファイルの読み込みに失敗しました。環境変数から直接APIキーを取得します。") - } - - // 指定された場所の天気を取得 - weather, err := getWeather(locationName) - if err != nil { - fmt.Println("天気情報の取得に失敗しました:", err) - return - } +// メイン関数 +async function main() { + // 場所選択 + const locations = { + 'tokyo': 'Tokyo', + 'la': 'Los Angeles', + 'osaka': 'Osaka', + 'hokkaido': 'Sapporo' + }; - // 日本のタイムゾーンを設定 - jst := time.FixedZone("JST", 9*60*60) - now := time.Now().In(jst) - - // 結果を表示 - fmt.Printf("日時: %s\n", now.Format("2006-01-02 15:04:05")) - fmt.Printf("場所: %s\n", weather.Name) - if len(weather.Weather) > 0 { - fmt.Printf("天気: %s\n", weather.Weather[0].Description) - } - fmt.Printf("気温: %.1f°C\n", weather.Main.Temp) -} + let locationName = argv.location; -// 指定された場所の天気情報を取得する関数 -func getWeather(location string) (*WeatherResponse, error) { - // 環境変数からAPIキーを取得 - apiKey := os.Getenv("OPENWEATHERMAP_API_KEY") - if apiKey == "" { - return nil, fmt.Errorf("APIキーが設定されていません。.envファイルまたは環境変数にOPENWEATHERMAP_API_KEYを設定してください") - } - - // OpenWeatherMap APIのエンドポイント - url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=%s", location, apiKey) + // city引数が指定されていれば、それを使用 + if (argv.city) { + if (locations[argv.city]) { + locationName = locations[argv.city]; + } else { + console.log(`未知の場所です: ${argv.city}`); + console.log('利用可能な場所: ' + Object.entries(locations).map(([k, v]) => `${k} (${v})`).join(' ')); + return; + } + } - // HTTPリクエスト - resp, err := http.Get(url) - if err != nil { - return nil, err - } - defer resp.Body.Close() + try { + // 指定された場所の天気を取得 + const weather = await getWeather(locationName); - // レスポンスの読み取り - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } + // 日本のタイムゾーンを設定 + const now = new Date(); + const jstTime = new Date(now.getTime() + (9 * 60 * 60 * 1000)); + + // 結果を表示 + console.log(`日時: ${jstTime.toISOString().replace('T', ' ').substring(0, 19)}`); + console.log(`場所: ${weather.name}`); + if (weather.weather && weather.weather.length > 0) { + console.log(`天気: ${weather.weather[0].description}`); + } + console.log(`気温: ${weather.main.temp.toFixed(1)}°C`); + } catch (error) { + console.error('天気情報の取得に失敗しました:', error.message); + } +} - // JSONのデコード - var weather WeatherResponse - if err := json.Unmarshal(body, &weather); err != nil { - return nil, err - } +// 指定された場所の天気情報を取得する関数 +async function getWeather(location) { + // 環境変数からAPIキーを取得 + const apiKey = process.env.OPENWEATHERMAP_API_KEY; + if (!apiKey) { + throw new Error('APIキーが設定されていません。.envファイルまたは環境変数にOPENWEATHERMAP_API_KEYを設定してください'); + } + + // OpenWeatherMap APIのエンドポイント + const url = `https://api.openweathermap.org/data/2.5/weather?q=${location}&units=metric&appid=${apiKey}`; - return &weather, nil + // HTTPリクエスト + const response = await axios.get(url); + return response.data; } + +// プログラム実行 +main().catch(err => { + console.error(err); + process.exit(1); +}); \ No newline at end of file From b48024746c5f39f65249422a0c5539e06e6dae2e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 6 Apr 2025 06:42:26 +0000 Subject: [PATCH 9/9] Rename files to use .js extension --- main.go => index.js | 2 -- 1 file changed, 2 deletions(-) rename main.go => index.js (99%) diff --git a/main.go b/index.js similarity index 99% rename from main.go rename to index.js index 368168e..d21a439 100644 --- a/main.go +++ b/index.js @@ -1,5 +1,3 @@ -// index.js - require('dotenv').config(); const axios = require('axios'); const yargs = require('yargs/yargs');