Recently I have been lazy, but I feel sorry for the long time. Friends Study Raspberry Pi, help him write a voice weather forecast program a few days ago. Let me use the number.
#coding =utf-8
Import Urllib2,json,subprocess,time
"' Get weather data, interface documentation using cloud-gathered data interfaces:
Real-time Weather interface: HTTP://WWW.36WU.COM/SERVICE/DETAILS/1
Living Weather Index: http://www.36wu.com/Service/Details/1?cid=5
Need to install Espeak
‘‘‘
Def get_weather_data ():
Header = {' user-agent ': ' mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6 '}
req = Urllib2. Request (url= ' Http://api.36wu.com/Weather/GetWeatherIndex?district=%E5%8C%97%E4%BA%AC&format=json ', headers= Header
Weather = Json.loads (Urllib2.urlopen (req). Read ())
req = Urllib2. Request (url= ' Http://api.36wu.com/Weather/GetWeather?district=%E5%8C%97%E4%BA%AC&format=json ', headers= Header
weather[' data '].update (Json.loads (Urllib2.urlopen (req). read ()) [' Data '])
Return weather
Def tts_head ():
FD = subprocess. Popen ([' Espeak ', '-vzh '],stdin=subprocess. Pipe,stdout=subprocess. Pipe,stderr=subprocess. PIPE) #为了不让它输出任何东西:)
def sayn (str):
Fd.stdin.write (' \ n ')
Fd.stdin.write (str)
#犯懒:
Fd.sayn = Sayn #sayn方法说出文本后会停顿
Fd.say = Fd.stdin.write #说出文本后不会停顿, plus a space
return FD
if __name__ = = ' __main__ ':
TTS = Tts_head ()
Tts.say (' Weather forecast ')
Weather = Get_weather_data ()
If weather[' status ']! = 200:
Tts.say (' Weather data get error ')
For i in [' District ', ' DateTime ', [' Average temperature ', ' temp ', ' degrees ']: #在这里定制想要的信息
If Isinstance (i,list):
Tts.sayn (I[0])
Tts.sayn (weather[' Data '][i[1]].encode (' utf-8 '))
Tts.say (I[2])
Else
Tts.sayn (weather[' Data '][i].encode (' utf-8 '))
The voice weather forecast written by python