SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
Python に
天気予報を聞いてみた
@kawatomo.0331
はじめに
• 自己紹介
• 千葉県で Unity(c#) 関係の仕事をしています
• LTに参加したきっかけ
• Python 入門者向けハンズオンの懇親会で巻き込まれた
天気予報の取得
• お天気Webサービス(Livedoor Weather Web Service)
• 千葉の天気情報を取得
• http://weather.livedoor.com/forecast/webservice/json/v1?
city=120010
• 結果は JSON 形式
• 上のリンクをブラウザから見てみると…
{'pinpointLocations': [{'name': '千葉市', 'link': 'http://weather.livedoor.com/area/forecast/1210000'}, {'name': '市川市', 'link':
'http://weather.livedoor.com/area/forecast/1220300'}, {'name': '船橋市', 'link': 'http://weather.livedoor.com/area/forecast/
1220400'}, {'name': '松戸市', 'link': 'http://weather.livedoor.com/area/forecast/1220700'}, {'name': '野田市', 'link': 'http://
weather.livedoor.com/area/forecast/1220800'}, {'name': '成田市', 'link': 'http://weather.livedoor.com/area/forecast/1221100'},
{'name': '佐倉市', 'link': 'http://weather.livedoor.com/area/forecast/1221200'}, {'name': '習志野市', 'link': 'http://
weather.livedoor.com/area/forecast/1221600'}, {'name': '柏市', 'link': 'http://weather.livedoor.com/area/forecast/1221700'}, {'name':
'市原市', 'link': 'http://weather.livedoor.com/area/forecast/1221900'}, {'name': '流山市', 'link': 'http://weather.livedoor.com/area/
forecast/1222000'}, {'name': '八千代市', 'link': 'http://weather.livedoor.com/area/forecast/1222100'}, {'name': '我孫子市', 'link':
'http://weather.livedoor.com/area/forecast/1222200'}, {'name': '鎌ケ谷市', 'link': 'http://weather.livedoor.com/area/forecast/
1222400'}, {'name': '浦安市', 'link': 'http://weather.livedoor.com/area/forecast/1222700'}, {'name': '四街道市', 'link': 'http://
weather.livedoor.com/area/forecast/1222800'}, {'name': '八街市', 'link': 'http://weather.livedoor.com/area/forecast/1223000'},
{'name': '印西市', 'link': 'http://weather.livedoor.com/area/forecast/1223100'}, {'name': '白井市', 'link': 'http://
weather.livedoor.com/area/forecast/1223200'}, {'name': '富里市', 'link': 'http://weather.livedoor.com/area/forecast/1223300'},
{'name': '酒々井町', 'link': 'http://weather.livedoor.com/area/forecast/1232200'}, {'name': '栄町', 'link': 'http://
weather.livedoor.com/area/forecast/1232900'}], 'title': '千葉県 千葉 の天気', 'copyright': {'title': '(C) LINE Corporation', 'link':
'http://weather.livedoor.com/', 'image': {'title': 'livedoor 天気情報', 'height': 26, 'url': 'http://weather.livedoor.com/img/cmn/
livedoor.gif', 'width': 118, 'link': 'http://weather.livedoor.com/'}, 'provider': [{'name': '日本気象協会', 'link': 'http://
tenki.jp/'}]}, 'forecasts': [{'image': {'title': '晴れ', 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'width': 50, 'height':
31}, 'temperature': {'min': None, 'max': {'celsius': '11', 'fahrenheit': '51.8'}}, 'date': '2016-01-27', 'dateLabel': '今日',
'telop': '晴れ'}, {'image': {'title': '晴れ', 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'width': 50, 'height': 31},
'temperature': {'min': {'celsius': '2', 'fahrenheit': '35.6'}, 'max': {'celsius': '13', 'fahrenheit': '55.4'}}, 'date':
'2016-01-28', 'dateLabel': '明日', 'telop': '晴れ'}], 'location': {'city': '千葉', 'prefecture': '千葉県', 'area': '関東'}, 'link':
'http://weather.livedoor.com/area/forecast/120010', 'publicTime': '2016-01-27T05:00:00+0900', 'description': {'text': ' 西日本から東日
本は、東シナ海に中心を持つ高気圧に覆われています。nn 千葉県は、晴れています。nn 27日は、高気圧に覆われて、晴れる見込みです。nn 28日は、高気圧に覆われ
て晴れますが、夜は気圧の谷が接近してくるたnめ、次第に雲が広がるでしょう。nn 太平洋沿岸では、27日から28日にかけて、波がやや高い見込みです。',
'publicTime': '2016-01-27T04:32:00+0900'}}
Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1'
# 地域コード:千葉県
payload = {'city': '120010'}
response = requests.get(json_url, params=payload)
json = response.json()
text = json['description']['text']
print(text)
実行するためには事前に pip install requests が必要になります
実行結果
西日本から東日本は、東シナ海に中心を持つ高気圧に覆われています。
千葉県は、晴れています。
27日は、高気圧に覆われて、晴れる見込みです。
28日は、高気圧に覆われて晴れますが、夜は気圧の谷が接近してくるた
め、次第に雲が広がるでしょう。
太平洋沿岸では、27日から28日にかけて、波がやや高い見込みです。
Python で天気予報が取得できました
おまけ
天気予報を喋らせてみた
Python 成分が少し減ります…
Raspberry Pi を用意 
Raspberry Pi で喋らせる
• 最初から Python が入っているけど少し古いので新しくする
• pyenv, pyenv-virtualenv
• たぷん日記のラズパイに喋ってもらう作戦を参考に環境構築
• http://www.tapun.net/raspi/raspberry-pi-speaks-japanese
• Open Jtalk というオープンソースの日本語音声合成エンジンを利用
• mplayer をインストール (任意)
←の部分 (jsay.sh) Python にできないかな…
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import subprocess
import shlex
import tempfile
import os
def debug_print(tag,obj):
if __debug__:
print('[%s]:%s'% (tag, obj))
def execute(message):
with tempfile.NamedTemporaryFile() as temp_text_f, tempfile.NamedTemporaryFile() as temp_wav_f:
temp_text_f.write(message.encode('utf-8'))
temp_text_f.seek(0)
debug_print('text path',temp_text_f.name)
debug_print('wav path', temp_wav_f.name)
debug_print('text',temp_text_f.read().decode('utf-8'))
jtalk_dic_path = '/usr/local/share/open_jtalk/open_jtalk_dic_utf_8-1.09/'
voice_path = '/usr/local/share/hts_voice/mei/mei_normal.htsvoice'
jtalk_command = '/usr/local/bin/open_jtalk -x %s -m %s -ow %s %s' % (jtalk_dic_path, voice_path, temp_wav_f.name, temp_text_f.name)
debug_print('jtalk command', jtalk_command)
subprocess.check_call(shlex.split(jtalk_command))
mplayer_command = '/usr/bin/mplayer %s' % (temp_wav_f.name)
if __debug__:
subprocess.check_call(shlex.split(mplayer_command))
else:
with open(os.devnull, 'w') as fnull:
result = subprocess.check_call(shlex.split(mplayer_command), stdout = fnull, stderr = fnull)
if __name__ == '__main__':
execute('こんにちは')
jsay.sh -> jsay.py
jsay.py を呼ぶ
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import jsay
json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1'
payload = {'city': '120010'}
response = requests.get(json_url, params=payload)
json = response.json()
text = json['description']['text']
speech = text.replace('n','').replace('r','').replace(' ','')
jsay.execute(speech)
赤枠は変更・追加部分です
天気予報を喋らせてみた

Mais conteúdo relacionado

Destaque

Pynyumon03 LT
Pynyumon03 LTPynyumon03 LT
Pynyumon03 LTdrillan
 
VBAにおける配列の データ構造と行列
VBAにおける配列の データ構造と行列VBAにおける配列の データ構造と行列
VBAにおける配列の データ構造と行列Katsuhiro Morishita
 
素振りのススメ at Python入門者の集い
素振りのススメ at Python入門者の集い素振りのススメ at Python入門者の集い
素振りのススメ at Python入門者の集いTakayuki Shimizukawa
 
データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831
 データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831 データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831
データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831Yusaku Kinoshita
 
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)schoowebcampus
 

Destaque (6)

Python LT
Python LTPython LT
Python LT
 
Pynyumon03 LT
Pynyumon03 LTPynyumon03 LT
Pynyumon03 LT
 
VBAにおける配列の データ構造と行列
VBAにおける配列の データ構造と行列VBAにおける配列の データ構造と行列
VBAにおける配列の データ構造と行列
 
素振りのススメ at Python入門者の集い
素振りのススメ at Python入門者の集い素振りのススメ at Python入門者の集い
素振りのススメ at Python入門者の集い
 
データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831
 データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831 データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831
データ分析-の波乗り遅れた気がしてる人のための Python×データ分析の超基礎の基礎 v1.0-20160831
 
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)
誰も教えてくれない、Excelマクロ/VBAの使い方 先生:田中亨(Office TANAKA)
 

Python に天気予報を聞いてみた

  • 2. はじめに • 自己紹介 • 千葉県で Unity(c#) 関係の仕事をしています • LTに参加したきっかけ • Python 入門者向けハンズオンの懇親会で巻き込まれた
  • 3. 天気予報の取得 • お天気Webサービス(Livedoor Weather Web Service) • 千葉の天気情報を取得 • http://weather.livedoor.com/forecast/webservice/json/v1? city=120010 • 結果は JSON 形式 • 上のリンクをブラウザから見てみると…
  • 4. {'pinpointLocations': [{'name': '千葉市', 'link': 'http://weather.livedoor.com/area/forecast/1210000'}, {'name': '市川市', 'link': 'http://weather.livedoor.com/area/forecast/1220300'}, {'name': '船橋市', 'link': 'http://weather.livedoor.com/area/forecast/ 1220400'}, {'name': '松戸市', 'link': 'http://weather.livedoor.com/area/forecast/1220700'}, {'name': '野田市', 'link': 'http:// weather.livedoor.com/area/forecast/1220800'}, {'name': '成田市', 'link': 'http://weather.livedoor.com/area/forecast/1221100'}, {'name': '佐倉市', 'link': 'http://weather.livedoor.com/area/forecast/1221200'}, {'name': '習志野市', 'link': 'http:// weather.livedoor.com/area/forecast/1221600'}, {'name': '柏市', 'link': 'http://weather.livedoor.com/area/forecast/1221700'}, {'name': '市原市', 'link': 'http://weather.livedoor.com/area/forecast/1221900'}, {'name': '流山市', 'link': 'http://weather.livedoor.com/area/ forecast/1222000'}, {'name': '八千代市', 'link': 'http://weather.livedoor.com/area/forecast/1222100'}, {'name': '我孫子市', 'link': 'http://weather.livedoor.com/area/forecast/1222200'}, {'name': '鎌ケ谷市', 'link': 'http://weather.livedoor.com/area/forecast/ 1222400'}, {'name': '浦安市', 'link': 'http://weather.livedoor.com/area/forecast/1222700'}, {'name': '四街道市', 'link': 'http:// weather.livedoor.com/area/forecast/1222800'}, {'name': '八街市', 'link': 'http://weather.livedoor.com/area/forecast/1223000'}, {'name': '印西市', 'link': 'http://weather.livedoor.com/area/forecast/1223100'}, {'name': '白井市', 'link': 'http:// weather.livedoor.com/area/forecast/1223200'}, {'name': '富里市', 'link': 'http://weather.livedoor.com/area/forecast/1223300'}, {'name': '酒々井町', 'link': 'http://weather.livedoor.com/area/forecast/1232200'}, {'name': '栄町', 'link': 'http:// weather.livedoor.com/area/forecast/1232900'}], 'title': '千葉県 千葉 の天気', 'copyright': {'title': '(C) LINE Corporation', 'link': 'http://weather.livedoor.com/', 'image': {'title': 'livedoor 天気情報', 'height': 26, 'url': 'http://weather.livedoor.com/img/cmn/ livedoor.gif', 'width': 118, 'link': 'http://weather.livedoor.com/'}, 'provider': [{'name': '日本気象協会', 'link': 'http:// tenki.jp/'}]}, 'forecasts': [{'image': {'title': '晴れ', 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'width': 50, 'height': 31}, 'temperature': {'min': None, 'max': {'celsius': '11', 'fahrenheit': '51.8'}}, 'date': '2016-01-27', 'dateLabel': '今日', 'telop': '晴れ'}, {'image': {'title': '晴れ', 'url': 'http://weather.livedoor.com/img/icon/1.gif', 'width': 50, 'height': 31}, 'temperature': {'min': {'celsius': '2', 'fahrenheit': '35.6'}, 'max': {'celsius': '13', 'fahrenheit': '55.4'}}, 'date': '2016-01-28', 'dateLabel': '明日', 'telop': '晴れ'}], 'location': {'city': '千葉', 'prefecture': '千葉県', 'area': '関東'}, 'link': 'http://weather.livedoor.com/area/forecast/120010', 'publicTime': '2016-01-27T05:00:00+0900', 'description': {'text': ' 西日本から東日 本は、東シナ海に中心を持つ高気圧に覆われています。nn 千葉県は、晴れています。nn 27日は、高気圧に覆われて、晴れる見込みです。nn 28日は、高気圧に覆われ て晴れますが、夜は気圧の谷が接近してくるたnめ、次第に雲が広がるでしょう。nn 太平洋沿岸では、27日から28日にかけて、波がやや高い見込みです。', 'publicTime': '2016-01-27T04:32:00+0900'}}
  • 5. Python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' # 地域コード:千葉県 payload = {'city': '120010'} response = requests.get(json_url, params=payload) json = response.json() text = json['description']['text'] print(text) 実行するためには事前に pip install requests が必要になります
  • 9. Raspberry Pi で喋らせる • 最初から Python が入っているけど少し古いので新しくする • pyenv, pyenv-virtualenv • たぷん日記のラズパイに喋ってもらう作戦を参考に環境構築 • http://www.tapun.net/raspi/raspberry-pi-speaks-japanese • Open Jtalk というオープンソースの日本語音声合成エンジンを利用 • mplayer をインストール (任意)
  • 10. ←の部分 (jsay.sh) Python にできないかな…
  • 11. #!/usr/bin/env python # -*- coding:utf-8 -*- import subprocess import shlex import tempfile import os def debug_print(tag,obj): if __debug__: print('[%s]:%s'% (tag, obj)) def execute(message): with tempfile.NamedTemporaryFile() as temp_text_f, tempfile.NamedTemporaryFile() as temp_wav_f: temp_text_f.write(message.encode('utf-8')) temp_text_f.seek(0) debug_print('text path',temp_text_f.name) debug_print('wav path', temp_wav_f.name) debug_print('text',temp_text_f.read().decode('utf-8')) jtalk_dic_path = '/usr/local/share/open_jtalk/open_jtalk_dic_utf_8-1.09/' voice_path = '/usr/local/share/hts_voice/mei/mei_normal.htsvoice' jtalk_command = '/usr/local/bin/open_jtalk -x %s -m %s -ow %s %s' % (jtalk_dic_path, voice_path, temp_wav_f.name, temp_text_f.name) debug_print('jtalk command', jtalk_command) subprocess.check_call(shlex.split(jtalk_command)) mplayer_command = '/usr/bin/mplayer %s' % (temp_wav_f.name) if __debug__: subprocess.check_call(shlex.split(mplayer_command)) else: with open(os.devnull, 'w') as fnull: result = subprocess.check_call(shlex.split(mplayer_command), stdout = fnull, stderr = fnull) if __name__ == '__main__': execute('こんにちは') jsay.sh -> jsay.py
  • 12. jsay.py を呼ぶ #!/usr/bin/env python # -*- coding:utf-8 -*- import requests import jsay json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' payload = {'city': '120010'} response = requests.get(json_url, params=payload) json = response.json() text = json['description']['text'] speech = text.replace('n','').replace('r','').replace(' ','') jsay.execute(speech) 赤枠は変更・追加部分です