質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

732閲覧

LINEでお天気通知

shinya-ta

総合スコア31

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/12/06 08:51

このコードで兵庫県加古川市の通知に変えたいのですが、CITY Codeが無くて困っています。
とりあえず、神戸市でやりましたが、神戸ではあまり役に立たないので…。
加古川市が出てるCITY Codeはありますか?

# -*- coding: utf-8 -*- import json import sys import urllib.parse import urllib.request # weather's API WEATHER_URL="http://weather.livedoor.com/forecast/webservice/json/v1?city=%s" CITY_CODE="280010" # kobe TODAY=0 TOMMOROW=1 # LINE notify's API LINE_TOKEN="#アクセストークン" LINE_NOTIFY_URL="https://notify-api.line.me/api/notify" def get_weather_info(): try: url = WEATHER_URL % CITY_CODE html = urllib.request.urlopen(url) html_json = json.loads(html.read().decode('utf-8')) except Exception as e: print ("Exception Error: ", e) sys.exit(1) return html_json def set_weather_info(weather_json, day): min_temperature = None max_temperature = None try: date = weather_json['forecasts'][day]['date'] weather = weather_json['forecasts'][day]['telop'] max_temperature = weather_json['forecasts'][day]['temperature']['max']['celsius'] min_temperature = weather_json['forecasts'][day]['temperature']['min']['celsius'] except TypeError: # temperature data is None etc... pass msg = "%s\nweather: %s\nmin: %s\nmax: %s" % \ (date, weather, min_temperature, max_temperature) return msg def send_weather_info(msg): method = "POST" headers = {"Authorization": "Bearer %s" % LINE_TOKEN} payload = {"message": msg} try: payload = urllib.parse.urlencode(payload).encode("utf-8") req = urllib.request.Request( url=LINE_NOTIFY_URL, data=payload, method=method, headers=headers) urllib.request.urlopen(req) except Exception as e: print ("Exception Error: ", e) sys.exit(1) def main(): weather_json = get_weather_info() for day in [TODAY, TOMMOROW]: msg = set_weather_info(weather_json, day) send_weather_info(msg) if __name__ == '__main__': main() コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

OpenWeatherMap は、対応している都市の一覧が公開されています。
http://bulk.openweathermap.org/sample/city.list.json.gz に 加古川のコードがありましたので、データは取得できそうですね。

{ "id": 1860704, "name": "Kakogawa", "country": "JP", "coord": { "lon": 134.850006, "lat": 34.76667 } },

ということで、5 day weather forecast でためしてみました。

https://api.openweathermap.org/data/2.5/forecast?id=1860704&APPID={APP_ID}

を実行して見ると...

{ "cod": "200", "message": 0.0087, "cnt": 40, "list": [ { "dt": 1544097600, "main": { "temp": 283.71, "temp_min": 282.456, "temp_max": 283.71, "pressure": 1014.31, "sea_level": 1029.39, "grnd_level": 1014.31, "humidity": 90, "temp_kf": 1.26 }, "weather": [ { "id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04n" } ], "clouds": { "all": 56 }, "wind": { "speed": 2.22, "deg": 270.508 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-12-06 12:00:00" }, { "dt": 1544108400, "main": { "temp": 282.66, "temp_min": 281.717, "temp_max": 282.66, "pressure": 1014.38, "sea_level": 1029.41, "grnd_level": 1014.38, "humidity": 87, "temp_kf": 0.94 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" } ], "clouds": { "all": 48 }, "wind": { "speed": 2.43, "deg": 266.004 }, "rain": { "3h": 0.0075000000000003 }, "sys": { "pod": "n" }, "dt_txt": "2018-12-06 15:00:00" }, { "dt": 1544119200, "main": { "temp": 283.06, "temp_min": 282.43, "temp_max": 283.06, "pressure": 1015.6, "sea_level": 1030.72, "grnd_level": 1015.6, "humidity": 90, "temp_kf": 0.63 }, "weather": [ { "id": 804, "main": "Clouds", "description": "overcast clouds", "icon": "04n" } ], "clouds": { "all": 92 }, "wind": { "speed": 3.91, "deg": 277.008 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-12-06 18:00:00" }, (中略) ], "city": { "id": 1860704, "name": "Kakogawa", "coord": { "lat": 34.7667, "lon": 134.85 }, "country": "JP" } }

な感じのデータが取得されました。

投稿2018/12/06 11:46

CHERRY

総合スコア25171

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shinya-ta

2018/12/06 12:29

返答ありがとうございます???? プログラミング自体、初心者なのでよくわからないのですが、パッと見た感じ、pythonのコードに見えないのですが、これはpythonのコードですか??????
CHERRY

2018/12/06 12:36 編集

API で、データが取得できることの確認のテストをしただけなので、回答に記載したのは、アクセスするエンドポイントの URL 例と取得できる JSON データの例です。 実際のプログラムは、書いていません。
shinya-ta

2018/12/06 14:08

そうなのですね???? 返答ありがとうございます????
shinya-ta

2018/12/06 14:14

外国のサイトなのですね???? 日本のサイトには、ないのでしょうか? 翻訳をかけて読むと、内容が読みにくくなってたりしますので????
guest

0

お天気Webサービス仕様によれば全国の地点定義表(RSS)に載っていない市町村は指定できないようです。どうしてもであれば別サービスを探すしかないと思います。

投稿2018/12/06 09:01

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shinya-ta

2018/12/06 09:04

他のサービスでも構いませんので、どこか知っているところがあれば、教えて下さい。
can110

2018/12/06 09:08

ごめんなさい。調べてないので私は知りません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問