Create a gist now

Instantly share code, notes, and snippets.

@masaponto /app.py
Last active Apr 22, 2017

What would you like to do?
MESHボタン押すとラズパイが天気しゃべるよ (http://masaponto.hatenablog.com/entry/2017/04/21/234752)
from bottledaemon import daemon_run
from bottle import route, run, get
from talk_weather import talk_weather, talk_datetime
@get('/test')
def test():
return 'Hello Raspbery Pi!'
@get('/talk_weather')
def talk_today_weather():
talk_datetime()
talk_weather()
if __name__ == "__main__":
daemon_run(host='0.0.0.0', port=8080)
#!/bin/sh
# Reference: http://raspi.seesaa.net/article/415482141.html
# 引数チェック
CMDNAME=`basename $0`
if [ $# -lt 1 ]; then
echo "Usage: ${CMDNAME} [ text ]" 1>&2
exit 1
fi
# 定数定義(出力ファイル名、辞書の場所、音声データの場所)
TMPFILE=`mktemp /tmp/tmp.XXXXXXXX.wav`
DIC=/var/lib/mecab/dic/open-jtalk/naist-jdic
VOICE=/usr/share/hts-voice/mei/mei_normal.htsvoice
# 音声データ生成
echo "$1" | open_jtalk \
-x ${DIC} \
-m ${VOICE} \
-ow ${TMPFILE} && \
# 生成した音声データを再生する
aplay --quiet ${TMPFILE}
# 生成した音声データを削除する
rm -f ${TMPFILE}
# 終了
exit 0
var apiURL = "http://<your-host>/talk_weather";
ajax ({
url : apiURL,
type : "get",
timeout : 10000,
success : function ( contents ) {
callbackSuccess( {
resultType : "continue",
} );
},
error : function ( request, errorMessage ) {
log("Day : Network error");
callbackSuccess( {
resultType : "continue",
} );
}
});
return {
resultType : "continue"
};
return {
resultType : "continue"
};
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Reference: http://raspi.seesaa.net/article/415530289.html
"""
import shlex
import subprocess
from datetime import datetime
import urllib2
import json
CMD_TALK = 'jtalk'
def talk_datetime():
d = datetime.now()
text = '%s%s日、%s%s' % (d.month, d.day, d.hour, d.minute)
text = CMD_TALK + ' ' + text
print text
proc = subprocess.Popen(shlex.split(text))
proc.communicate()
def talk_weather():
city = '070030' # Aizu
json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1'
weather_text = u'%sの天気は%sです。'
temperature_text = u'予想最高気温は、%s度、予想最低気温は、%s度です。'
try:
r = urllib2.urlopen('%s?city=%s' % (json_url, city))
obj = json.loads(unicode(r.read()))
forecasts = obj['forecasts']
cast = forecasts[0]
temperature = cast['temperature']
today_w_txt = weather_text % (cast['dateLabel'], cast['telop'])
if temperature['max'] is not None and temperature['min'] is not None:
today_t_txt = temperature_text % (
temperature['max']['celsius'], temperature['min']['celsius'])
weather_str = today_w_txt + ' ' + today_t_txt
else:
weather_str = today_w_txt
weather_str = weather_str.encode('utf-8')
text = CMD_TALK + ' ' + weather_str
print text
proc = subprocess.Popen(shlex.split(text))
proc.communicate()
finally:
r.close()
def main():
talk_datetime()
talk_weather()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment