The voice weather forecast written by python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.