#!/usr/bin/env python
#coding=utf-8
import urllib, urllib2
import re
import time
#from PyFetion import *
proxy=urllib2.ProxyHandler({'http': 'http://lk:2002@172.17.5.53:80'})
opener=urllib2.build_opener(proxy)
urllib2.install_opener(opener)
def GetWeather():
sock = urllib2.urlopen("http://qq.ip138.com/weather/guangdong/DongGuan.htm")
strhtml = sock.read()
strhtml = unicode(strhtml, 'gb2312','ignore').encode('utf-8','ignore')
#strhtml = unicode(strhtml, 'gb2312','ignore').encode('big5','ignore')
#print strhtml
theGrades = re.findall('<td>(.*℃)</td>', strhtml)
weathers = re.findall('<td><img src="/image/b/d+.gif" alt=".*" /><br/>(.*)</td>', strhtml)
# 定???格式
this_date = str(time.strftime("%Y/%m/%d %a"))
now = int(time.time())
sec = 24*60*60
day_today = "今天(%s號)" % str(time.strftime("%d", time.localtime(now+0*sec)))
day_tommo = "明天(%s號)" % str(time.strftime("%d", time.localtime(now+1*sec)))
day_aftom = "後天(%s號)" % str(time.strftime("%d", time.localtime(now+2*sec)))
# 定?簡訊本文
#sms = [this_date]
sms = []
sms.append("東莞天氣")
sms.append("%s:%s, %s" % (day_today, weathers[0], theGrades[0]))
sms.append("%s:%s, %s" % (day_tommo, weathers[1], theGrades[1]))
sms.append("%s:%s, %s" % (day_aftom, weathers[2], theGrades[2]))
sms.append("天氣有冷暖,關懷永不變!")
smscontent = '/n'.join(sms)
return smscontent
def SendSMS(sms):
myphone = '1365024****' # 手機號
mypwd = 'xxxx' # 登入密碼
destphone = ["62487xxxx", "70078xxxx", "69451xxxx"] # 發送對象飛訊號
# 發送目的地改為飛訊號,之前用的手機號做目的地發送,別人的收不到簡訊
print "/nwaiting for login fetion..."
fetion = PyFetion(myphone, mypwd, 'TCP')
fetion.login(FetionHidden)
for phone in destphone:
print "sending to", phone
fetion.send_sms(sms, phone, True)
print "OK"
fetion.logout()
return True
def main():
print "getting out the weather code..."
msg = GetWeather()
print "/n", msg
#SendSMS("測試天氣預報")
#SendSMS(msg)
print "Done."
if __name__ == "__main__":
main()