weapho单机执行
gae再度抛锚,我心憔悴啊
还是自个儿的服务器用着省心,就是不敢动作太大,毕竟不是我一人的。
1、用来执行定时任务的脚本
# -*- coding:utf-8 -*-
# by fatway#gmail.com
# May 18,2010
__version__ = 4.0
"""天气预报发送
因GAE上的weapho项目无法正常运行,采用离线形式编写脚本来实现
调用新浪提供的天气预报信息向目标用户发送
"""
import sys
import time
import sqlite3
from PyFetion import *
from SinaWeather import Sina
def Make_Temp_Msg():
"""先根据user表,为每个用户生成要发送的信息内容
by Lee
May 18,2010
"""
db_conn = sqlite3.connect("weapho.cfg")
db_cur_selete = db_conn.cursor() # 用于查询用户清单
db_cur_update = db_conn.cursor() # 用于更新信息
for cell in db_cur_selete.execute("select * from user").fetchall():
user_sip = cell[2] # 用户飞信号
citys = cell[3].split(",") # 城市列表清单
print user_sip, citys
user_msg = ["天气预报%s" % time.strftime('%m.%d')] # 针对用户的内容
#user_msg = ["天气预报"]
for city in citys:
#获取城市的天气内容
city = city.encode('utf-8','ignore')
weather_4_user = Sina(city, 2)
user_msg.append(weather_4_user.weapho_format())
user_msg_format = "\n".join(user_msg) # 定制出的短信消息
print user_msg_format.decode("utf-8","ignore").encode("cp936","ignore")
# 更新用户定制内容
# 首次运行需要先insert
# t = (user_sip, user_msg_format)
# db_cur_update.execute("insert into weatherinfo values(?,?)", t)
db_cur_update.execute("update weatherinfo set MSG=? where SIP=?",
(user_msg_format, user_sip,)
)
db_conn.commit()
def Send_Msg():
"""按用户定制表发送信息
by Lee
May 18,2010
"""
db_conn = sqlite3.connect("weapho.cfg")
db_cur_selete = db_conn.cursor() # 用于查询用户清单
# 查询管理员帐号
user = db_cur_selete.execute("select * from admin").fetchone()
fetion_id = user[0]
fetion_pwd = user[1]
fetion = PyFetion(fetion_id, fetion_pwd, "TCP")
fetion.login(FetionHidden)
# 查询定制信息并依次发送
tt = db_cur_selete.execute("select * from weatherinfo").fetchall()
for t in tt:
sip = t[0]
msg = t[1].encode('utf-8')
fetion.send_sms(msg, sip, True)
fetion.logout()
print "---Done---"
if __name__ == "__main__":
Make_Temp_Msg()
Send_Msg()
sys.exit()
2、SinaWeather组件
#coding=utf-8
#file:SinaWeather.py
#by: fatway#gmail.com
import sys
from urllib import urlencode, urlopen
from utility import weather, weather_4_lin
class Sina():
"""获取新浪今明后三天天气预报信息
调用方法 sth = sina("东莞", 2)
参数为城市名和要获取的未来天数(今天为0)
weapho返回列表信息:
['多云,20-29℃,≤3', '雷阵雨转中雨,20-26℃,≤3', '小雨,14-18℃,≤3']
"""
def __init__(self, city_name="东莞", day_num=0):
self.city_name = city_name
self.day_num = self.__check_day(day_num)
self.base_url = "http://php.weather.sina.com.cn/xml.php"
def __u2g(self, str_utf8):
return str_utf8.decode('utf-8').encode('gb2312')
def __check_day(self, day_num):
try:
num = int(day_num)
if num in xrange(5):
return num + 1
else:
return 1
except ValueError:
return 1
def _xml_msg(self):
"""获取xml信息"""
result = []
for i in xrange(self.day_num):
params = urlencode({
"city": self.__u2g(self.city_name),
"password": "DJOYnieT8234jlsK",
"day": i
})
#reader = urlopen(self.base_url, params).read() #post method
reader = urlopen(self.base_url + "?%s" % params).read()
result.append(reader)
return result
def weapho(self):
"""格式化为输出列表"""
return map(lambda x: weather(x), self._xml_msg())
def weapho_format(self):
info = self.weapho()
result = ["%s" % self.city_name]
result.append("明天:%s" % info[1].encode('utf8'))
result.append("后天:%s" % info[2].encode('utf8'))
return "\n".join(result)
def weapho_4_lin(self):
"""格式化为专用输出列表"""
return map(lambda x:weather_4_lin(x), self._xml_msg())
if __name__ == "__main__":
sth = Sina("东莞", 2)
# print sth.weapho()
print sth.weapho_format().decode('utf8').encode('cp936')
print
print u"给老婆的穿衣推荐:".encode('gbk')
print sth.weapho_4_lin()[0].encode('gbk')
3、Ulitily组件
#coding=utf-8
#file:place.py
#by: fatway#gmail.com
from xml2dict import XML2Dict
def _xml_2_dict(w_xml):
xml = XML2Dict()
w_dict = xml.fromstring(w_xml)
return w_dict
def _get_weather(w_xml):
w_info = _xml_2_dict(w_xml)
result = {}
if 'Weather' not in w_info.Profiles:
return result
weather = w_info.Profiles.Weather
city = weather.city # 城市
status1 = weather.status1 # 白天天气
status2 = weather.status2 # 夜间天气
power = weather.power1 # 风力
temperature1 = weather.temperature1 # 白天温度
temperature2 = weather.temperature2 # 夜间温度
chy = weather.chy_shuoming # 穿衣说明(Calin专用)
result["city"] = city
if status1 == status2:
result["status"] = status1
else:
result["status"] = status1 + u"\u8f6c" + status2
result["power"] = power
result["temperature"] = str(temperature2) \
+ u"\uff5e" \
+ str(temperature1) \
+ u"\u2103"
result["chy"] = chy
return result
def weather(w_xml):
weather_info = _get_weather(w_xml)
temp = []
# temp.append(weather_info["city"])
temp.append(weather_info["status"])
temp.append(weather_info["temperature"])
temp.append(weather_info["power"])
return ",".join(temp)
def weather_4_lin(w_xml):
'''明日穿衣推荐'''
weather_info = _get_weather(w_xml)
temp = [u"明日天气"]
temp.append(weather_info["status"] + "," + weather_info["temperature"])
temp.append(u"穿衣指数:" + weather_info["chy"])
temp.append(u"(来源:新浪天气)")
return "\n".join(temp)
if __name__ == "__main__":
msg = """<Profiles>
<Weather>
<city>东莞</city>
<status1>多云</status1>
<status2>晴</status2>
<figure1>duoyun</figure1>
<figure2>duoyun</figure2>
<direction1>无持续风向</direction1>
<direction2>无持续风向</direction2>
<power1>≤3</power1>
<power2>≤3</power2>
<temperature1>27</temperature1>
<temperature2>18</temperature2>
<ssd>0</ssd>
<tgd1>28</tgd1>
<tgd2>28</tgd2>
<zwx>1</zwx>
<ktk>4</ktk>
<pollution>3</pollution>
<xcz>4</xcz>
<zho></zho>
<diy></diy>
<fas></fas>
<chy>1</chy>
<zho_shuoming>暂无</zho_shuoming>
<diy_shuoming>暂无</diy_shuoming>
<fas_shuoming>暂无</fas_shuoming>
<chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming>
<pollution_l>一般</pollution_l>
<zwx_l>最弱</zwx_l>
<ssd_l>舒适</ssd_l>
<fas_l>暂无</fas_l>
<zho_l>暂无</zho_l>
<chy_l>薄短袖类</chy_l>
<ktk_l>不需要开启</ktk_l>
<xcz_l>不太适宜</xcz_l>
<diy_l>暂无</diy_l>
<pollution_s>对空气污染物扩散无明显影响</pollution_s>
<zwx_s>紫外线最弱</zwx_s>
<ssd_s>适宜在自然环境及露天场所活动。</ssd_s>
<ktk_s>不需要开启空调</ktk_s>
<xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,或洗车当日气温太低容易结冰。不太适宜洗车。</xcz_s>
<savedate_weather>2010-03-19</savedate_weather>
<savedate_life>2010-03-19</savedate_life>
<savedate_zhishu>2010-03-19</savedate_zhishu>
</Weather>
</Profiles>
"""
print weather(msg)
#东莞,多云转晴,18-27℃,≤3
print weather_4_lin(msg)
#明日天气
#多云转晴,18-27℃
#穿衣指数:短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫
4、xml2dict和PyFetion不赘述了
5、sqlite3数据表
1)admin 管理员帐户,记录登陆飞信的用户、密码
2)user 用户清单表,记录用户的昵称、飞信号、定制的地市
3)weatherinfo 临时消息表,根据用户表生成需要发送内容
浙公网安备 33010602011771号