python语音天气预报

作者: 匿了名的米老鼠 | 来源:发表于2017-12-14 23:13 被阅读0次

刚学习python,看大神各种操作,想着自己也写一个小程序。每天早上起来第一件事就是开电脑,于是就写了一个语音播报天气的小程序。

看他人都是用的爬虫抓取的天气,自己还不会,于是就用了api,在京东花一分钱买的api,

开始访问api,用到了 requests,特别简单

代码:

import requests

r = requests.get('https://way.jd.com/jisuapi/weather?city=吉林&cityid=111&citycode=101260301&appkey=6f9f4bf8c5d672c29aee8b5992bc20a9')

r.encoding = 'utf-8'  #不加这行有可能会乱码

print(r.text)

打印返回数据,得到如下结果

返回的事json格式,于是用了一下json在线解析

json解析

这样就好看多了

然后对里面的内容进行提取

date=r.json()["result"]['result']["date"]

templow=r.json()["result"]['result']["templow"]

temphigh=r.json()["result"]['result']["temphigh"]

tempnow=r.json()["result"]['result']["temp"]

week=r.json()["result"]['result']["week"]

tip=r.json()["result"]['result']["index"][3]["detail"]

weather=r.json()["result"]['result']['daily'][0]['night']['weather']

add=r.json()["result"]['result']["city"]

wind=r.json()["result"]['result']['winddirect']

WS=r.json()["result"]['result']["windpower"]

再导入时间模块,获取当前时间

import time

t = time.localtime() # 当前时间的纪元值

fmt = "%H %M"

now = time.strftime(fmt, t)  # 将纪元值转化为包含时、分的字符串

now = now.split(' ') #以空格切割,将时、分放入名为now的列表中

hour = now[0]

minute = now[1]

整理一下,

wea='你好,,今天是%s%s,现在北京时间%s时%s分,%s天气%s,气温%s摄氏度~%s摄氏度,现在为%s摄氏度,%s,风力%s,%s'%(date,week,hour,minute,add,weather,templow,temphigh,tempnow,wind,WS,tip)

运行的结果就是:

你好,,今天是2017-12-14星期四,现在北京时间22时54分,吉林市天气晴,气温-19摄氏度~-13摄氏度,现在为-13摄氏度,南风,风力2级,昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。

这一步已经做好,现在开始将文字转化为语音。

这里用的事百度的语音合成api百度在线语音合成 Python SDK参考文档

from aip import AipSpeech

APP_ID = '***************'

API_KEY = '*******************************'

SECRET_KEY = '*******************************'

aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result  = aipSpeech.synthesis(wea, 'zh', 1, {

'vol': 10,'per':4,'spd':5

})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码

if not isinstance(result, dict):

with open('auido.mp3', 'wb') as f:

f.write(result)

播放mp3文件没找很好的方式,学的太次了,用的pygame

#播放语音的pygame

pygame.mixer.init(frequency=16000,size=-16,channels=4)

track = pygame.mixer.music.load('auido.mp3')

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

pygame.time.delay(100)

pygame.mixer.quit()

整个程序就写完了,很少,但是好像让我写的很麻烦,第一次写。

但是怎么能让电脑开机播放呢,这里用到了任务计划

任务计划

这个在控制面板->管理工具->任务计划程序,创建任务,在操作中要做如下配置

任务计划配置

程序那里要填python的安装目录,添加参数要填程序的目录,起始于空着就好。

触发器

触发器我是这样配置的,每天早上7点45准时播报,还有每次输入密码的时候也就是开机的时候,但是我每次电脑关机之前都是静音的状态,所以,在开机之后拖十分钟启动,保证网络的顺畅。

于是,开机语音播报天气的功能就实现了。

文中有错误的地方,敬请指正。

希望可以和各位一起学习。

放完整源码:

import requests

import time

import pygame

#抓取中国天气网吉林天气

r = requests.get('https://way.jd.com/jisuapi/weather?city=吉林&cityid=111&citycode=101260301&appkey=6f9f4bf8c5d672c29aee8b5992bc20a9')

r.encoding = 'utf-8'

# print(r.text)

date=r.json()["result"]['result']["date"]

templow=r.json()["result"]['result']["templow"]

temphigh=r.json()["result"]['result']["temphigh"]

tempnow=r.json()["result"]['result']["temp"]

week=r.json()["result"]['result']["week"]

tip=r.json()["result"]['result']["index"][3]["detail"]

weather=r.json()["result"]['result']['daily'][0]['night']['weather']

add=r.json()["result"]['result']["city"]

wind=r.json()["result"]['result']['winddirect']

WS=r.json()["result"]['result']["windpower"]

t = time.localtime()  # 当前时间的纪元值

fmt = "%H %M"

now = time.strftime(fmt, t)  # 将纪元值转化为包含时、分的字符串

now = now.split(' ') #以空格切割,将时、分放入名为now的列表中

hour = now[0]

minute = now[1]

wea='你好,庞树博,今天是%s%s,现在北京时间%s时%s分,%s天气%s,气温%s摄氏度~%s摄氏度,现在为%s摄氏度,%s,风力%s,%s'%(date,week,hour,minute,add,weather,templow,temphigh,tempnow,wind,WS,tip)

print(wea)

#百度语音合成apI

from aip import AipSpeech

""" App ID: 9946043

API Key: WpcSAMtagA6HtrNsmTQi1GFa

Secret Key: a89f277902098c1ed872f566c2e0e50c """

APP_ID = '9946043'

API_KEY = 'WpcSAMtagA6HtrNsmTQi1GFa'

SECRET_KEY = 'a89f277902098c1ed872f566c2e0e50c'

aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result  = aipSpeech.synthesis(wea, 'zh', 1, {

'vol': 10,'per':4,'spd':5

})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码

if not isinstance(result, dict):

with open('auido.mp3', 'wb') as f:

f.write(result)

#播放语音的pygame

pygame.mixer.init(frequency=16000,size=-16,channels=4)

track = pygame.mixer.music.load('auido.mp3')

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

pygame.time.delay(100)

pygame.mixer.quit()

“我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其实我是一个程序员”

相关文章

网友评论

    本文标题:python语音天气预报

    本文链接:https://www.haomeiwen.com/subject/nailwxtx.html