Skip to content

Commit

Permalink
Make googleweather support unicode in en OS
Browse files Browse the repository at this point in the history
Make googleweather support unicode in en OS
Fix a typo error in fetion class
  • Loading branch information
unknown authored and unknown committed May 8, 2012
1 parent 8de0214 commit 297c01b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion python/fetion/fetion.py
Expand Up @@ -52,7 +52,7 @@ def LogIn(self):
raw_input('Press any key to exit.')
return False
else:
print '登录成果!'
print '登录成功!'
return True

def SendMe(self, msg):
Expand Down
30 changes: 16 additions & 14 deletions python/fetion/googleweather.py
@@ -1,41 +1,42 @@
#coding:utf-8
#coding:utf-8
import urllib2
from xml.dom import minidom
import getopt
import sys
import fetionini

# The google weather api is not official may close in the future but it's simple.
GOOGLE_WEATHER_URL = 'http://www.google.com/ig/api?weather=上海&hl=zh-cn'
GOOGLE_WEATHER_URL = 'http://www.google.com/ig/api?weather=Shanghai&hl=zh-cn'

def getweather():
proc = lambda tnlist, conds : [
[temp.getAttribute('data') for temp in conds.getElementsByTagName(tagname)][0]
for tagname in (tnlist) ]

# Note: the encoding is important to parse correctly
data = urllib2.urlopen(GOOGLE_WEATHER_URL.decode('gbk').encode('utf-8')).read().decode('gbk').encode('utf-8')
data = urllib2.urlopen(GOOGLE_WEATHER_URL).read().decode('gbk').encode('utf-8')
xmldoc = minidom.parseString(data)
curconds = xmldoc.getElementsByTagName("current_conditions")
(t, h, c, w) = proc(('temp_c', 'humidity', 'condition', 'wind_condition'), curconds[0])
print t
print h
print c
print w

# so far the current conditions are not expected
# curconds = xmldoc.getElementsByTagName("current_conditions")
# (t, h, c, w) = proc(('temp_c', 'humidity', 'condition', 'wind_condition'), curconds[0])
# print t
# print h
# print c
# print w

msg = ''
for cond in xmldoc.getElementsByTagName("forecast_conditions"):
(d, l, h, c) = proc(('day_of_week', 'low', 'high', 'condition'), cond)
# print '%s: %s/%s %s' % (d, l, h, c)
msg += '%s: %s/%s %s' % (d, l, h, c) + '\n'
print msg
return msg

def usage():
file = sys.argv[0]
print 'python ' + file + ' 输出结果到当前屏幕'
print 'python ' + file + ' -s 输出结果到当前屏幕并发送短信'
print 'python ' + file + ' -h 帮助'
print 'python ' + file + u' 输出结果到当前屏幕'
print 'python ' + file + u' -s 输出结果到当前屏幕并发送短信'
print 'python ' + file + u' -h 帮助'

def run():
need_send_message = False
Expand All @@ -52,7 +53,8 @@ def run():
if o.lower() in ('-h','--help'):
usage()
sys.exit()
msg = getweather().decode('utf-8').encode('gbk')
msg = getweather().encode('gbk')
print msg
if need_send_message:
fetionini.fetionINI(msg)

Expand Down

0 comments on commit 297c01b

Please sign in to comment.