Created
February 22, 2016 15:51
-
-
Save jackyshan/e50ad6ebe7c927ffc63d to your computer and use it in GitHub Desktop.
起床闹钟天气小程序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import urllib2 | |
import json | |
import time | |
from BaiduTrans import translate | |
while True: | |
url = 'http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%3D%222161838%22&format=json' | |
req = urllib2.Request(url) | |
try: | |
resp = urllib2.urlopen(req) | |
except Exception, e: | |
print e | |
time.sleep(3) | |
continue | |
content = resp.read() | |
data = json.loads(content) | |
try: | |
info = data['query']['results']['channel']['item']['forecast'][0] | |
except Exception, e: | |
print e | |
time.sleep(3) | |
continue | |
year = time.strftime('%Y', time.localtime(time.time())) | |
month = time.strftime('%m', time.localtime(time.time())) | |
day = time.strftime('%d', time.localtime(time.time())) | |
week = time.strftime("%w",time.localtime()) | |
low = int(info['low']) | |
low = (low - 32) * 5 / 9 | |
hig = int(info['high']) | |
hig = (hig - 32) * 5 / 9 | |
text = info['text'].encode('utf-8') | |
trans = translate(text) | |
if trans: | |
text = trans | |
text = text.encode('utf-8') | |
weekstr = '天' if week == '0' else week | |
cityinfo = 'Good Morning,今天是'+year+'年'+month+'月'+day+'号'+'星期'+weekstr+\ | |
',今天的天气情况如下,最低温度'+str(low)+'摄氏度,最高温度'+str(hig)+'摄氏度,'+text | |
if low < 10: | |
cityinfo += ',天气寒冷,多穿衣服呀' | |
if '雨' in text: | |
cityinfo += ',记得带伞呀' | |
if int(week) > 5 or int(week) == 0: | |
cityinfo += ',今天是周末,没事可以多睡会儿呢' | |
print cityinfo | |
#设置音量 | |
os.system('osascript -e "set volume %s"' % '1') | |
#播报天气 | |
os.system('say '+ cityinfo) | |
#打开fm | |
fmurl = 'http://douban.fm' | |
os.system('open ' + fmurl) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
28 9 * * * osascript -e 'set volume 0'