ラズパイがしゃべってくれたので、今度はWebから天気予報を取得してしゃべってもらいたいと思います。
全体はこちらを参考にしました。
raspi.seesaa.net
jsayコマンドの作成は、上記リンク内のこちらを参考にしました。
raspi.seesaa.net
jsayコマンドの作成
スクリプト作成
$ sudo vi /usr/local/bin/jsay #!/bin/sh CMDNAME=`basename $0` sleep 1 if [ $# -lt 1 ]; then echo "Usage: ${CMDNAME} [ text ]" 1>&2 exit 1 fi # 定数定義(出力ファイル名、辞書の場所、音声データの場所) TMPFILE=`mktemp /tmp/tmp.XXXXXXXX.wav` DIC=/usr/local/share/open_jtalk/open_jtalk_dic_utf_8-1.08/ VOICE=/usr/share/hts-voice/mei/mei_normal.htsvoice ### こちらでも良いです #DIC=/var/lib/mecab/dic/open-jtalk/naist-jdic # 音声データ生成 echo "$1" | open_jtalk \ -x ${DIC} \ -m ${VOICE} \ -ow ${TMPFILE} && \ # 生成した音声データを再生する aplay --quiet ${TMPFILE} # 生成した音声データを削除する rm -f ${TMPFILE} # 終了 exit 0
しゃべる
$ sudo chmod +x /usr/local/bin/jsay $ jsay こんにちは
しゃべった!OK!
天気予報をしゃべるスクリプト作成
スクリプト作成
$ sudo vi talk_weather.py #!/usr/bin/env python # -*- coding:utf-8 -*- import shlex import subprocess from datetime import datetime import urllib2 import json CMD_SAY = 'jsay' def main(): say_datetime() say_weather() return def say_datetime(): d = datetime.now() text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second) text = CMD_SAY + ' ' + text print '**********************' print text print '**********************' proc = subprocess.Popen(shlex.split(text)) #proc = subprocess.Popen(shlex.split(text), shell=True) proc.communicate() return def say_weather(): city = '120010' json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' weather_text = u'%sの天気は%sです。' temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。' try: r = urllib2.urlopen('%s?city=%s' % (json_url, city) ) obj = json.loads( unicode(r.read()) ) title = obj['title'] forecasts = obj['forecasts'] # TODAY cast = forecasts[0] today_w_txt = weather_text % (cast['dateLabel'], cast['telop']) # TOMMOROW cast = forecasts[1] temperature = cast['temperature'] tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop']) tommorow_t_txt = temperature_text % (cast['dateLabel'], temperature['max']['celsius'], temperature['min']['celsius']) # SAY weather_str = title + ' ' + today_w_txt + ' ' + tommorow_w_txt + ' ' + tommorow_t_txt weather_str = weather_str.encode('utf-8') text = '''%s '%s' ''' % (CMD_SAY, weather_str) print text proc = subprocess.Popen(shlex.split(text)) #proc = subprocess.Popen(shlex.split(text), shell=True) proc.communicate() finally: r.close() return ### Execute if __name__ == "__main__": main()
実行
$ python talk_weather.py ********************** jsay 7月1日、14時29分0秒 ********************** jsay '千葉県 千葉 の天気 今日の天気は曇時々雨です。 明日の天気は曇りです。 明日 の予想最高気温、28度、予想最低気温、23度です。'
やったー。Webから取ってきた天気予報をしゃべったーー。
さて、次は何をしようかな。