python寫的的語音天氣預報

來源:互聯網
上載者:User

標籤:

最近犯懶,但這麼長時間沒點成果實在過意不去。朋友研究樹莓派,前幾天幫他寫個語音天氣預報的程式。就讓我用來湊個數吧。

 

#coding=utf-8
import urllib2,json,subprocess,time

‘‘‘使用雲聚資料的介面擷取天氣資料,介面文檔:
    即時天氣介面:http://www.36wu.com/Service/Details/1
    生活氣象指數:http://www.36wu.com/Service/Details/1?cid=5

  需要安裝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 #說出文本後不會停頓,加空格也不行
    return fd

if __name__ == ‘__main__‘:
    tts = tts_head()
    tts.say(‘天氣預報‘)
    weather = get_weather_data()
    if weather[‘status‘] != 200:
        tts.say(‘天氣資料擷取錯誤‘)
    for i in [‘district‘,‘dateTime‘,[‘平均溫度‘,‘temp‘,‘度‘]]:#在這裡定製想要的資訊
        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‘))

python寫的的語音天氣預報

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.