码迷,mamicode.com
首页 > 其他好文 > 详细

树莓派播放天气

时间:2018-03-07 19:07:30      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:result   成语   blog   tle   file   game   logs   lib   __name__   

https://files.cnblogs.com/files/littlehb/%E6%A0%91%E8%8E%93%E6%B4%BE%E6%92%AD%E6%94%BE%E5%A4%A9%E6%B0%94.zip

from aip import AipSpeech
import urllib.request
import json
import time
import pygame

# pip install pygame
# cd python-sdk-master
# python3 setup.py install

# 注册的baidu帐号
APP_ID = 10892266
API_KEY = 1qVkEPMsjr7G0lswIqqDegHT
SECRET_KEY = xxxxxxxxxxxxxxxxxx

# 获取天气
def getWeather():
    ApiUrl = "http://www.weather.com.cn/data/sk/101060101.html"
    html = urllib.request.urlopen(ApiUrl)
    # 读取并解码
    data = html.read().decode("utf-8")
    # 将JSON编码的字符串转换回Python数据结构
    ss = json.loads(data)
    info = ss[weatherinfo]
    return info

if __name__ == __main__:
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    # 获取天气信息
    info=getWeather()
    speakString=现在播报天气:城市,%s % info[city]
    speakString=speakString+( ,温度:%s度 % info[temp])
    speakString = speakString +(,风速:%s % info[WD])
    speakString = speakString + info[WS]
    speakString = speakString +(,湿度:%s % info[SD])
    speakString = speakString + (,天气播报完毕!)

    print(speakString)
    # 调用百度进行生成语音
    result = client.synthesis(speakString, zh, 1, {
        vol: 5,
    })
    # 保存文件
    file=weather.mp3
    if not isinstance(result, dict):
        with open(file, wb) as f:
            f.write(result)
        # 播放音乐
        # 让树莓派播放:mplayer shero.mp3
        pygame.mixer.init()
        track = pygame.mixer.music.load(file)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            time.sleep(1)

 

树莓派播放天气

标签:result   成语   blog   tle   file   game   logs   lib   __name__   

原文地址:https://www.cnblogs.com/littlehb/p/8523798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!