Skip to content

Instantly share code, notes, and snippets.

@jackyshan
Created February 22, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackyshan/e50ad6ebe7c927ffc63d to your computer and use it in GitHub Desktop.
Save jackyshan/e50ad6ebe7c927ffc63d to your computer and use it in GitHub Desktop.
起床闹钟天气小程序
# -*- 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
@jackyshan
Copy link
Author

crontab定时

28 7 * * * python /Users/jackyshan/Desktop/Project/Script/SayWeather.py

@jackyshan
Copy link
Author

空格
28 9 * * * osascript -e 'tell application "System Events" to keystroke " "'

@jackyshan
Copy link
Author

28 9 * * * osascript -e 'set volume 0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment