#!/usr/bin/env python
# -*- encoding:utf-8 -*-
#-------------------------------------------------------------------------
#FileName: weather_broadcast.py
#Description: automatically get the weather information from internet,deal with it and send it out everyday to my sisters,best friends and myself^_^. Actually, It can be sent out as short messages by the ChinaMobile Fetion which will be useful(Because of some reasons, I use chinaunicom now. I use email everyday,so It's enough for me...)
#Author: Xiang Guo(gx2008758@gmail.com)
#Time : Wed Mar 24 23:44:34 CST 2009
#Modified: Sun Apr 19 01:04:18 CST 2009
#(1).Fix the IOError and socket.gaierror
#(2).Make sure tomorrow's date displayed correctly
#(3).replace the handling of the file with pure python
#(4).Write a class to include the all functions
#-------------------------------------------------------------------------
import os,sys
import commands
import urllib,smtplib,email
import socket
import re
#urlbase = ""
urlbase = ""
cities={'苏州':'101190401','徐州':'101190801','南京':'101190101'}
class Weather:
def __init__(self):
pass
#remove the old pages,not necessary
#def rm_pages(city):
# localfile=city + '.shtml'
# if os.path.isfile(localfile):
# cmd='rm -f ./%s' %localfile
# os.popen(cmd)
#get_weather_message
def get_message(self,city):
url=urlbase + cities[city] + '.shtml'
localfile=city+'.shtml'
while 1:
try:
urllib.urlretrieve(url,localfile)
except IOError,e:
continue
else:
break
#delete the useless information
f=open(localfile,"r")
content = f.read()
a=re.sub('\r\n','',content)
a=re.sub('','',a)
a=re.sub('c_1_2.*dd_0','',a,)
a=re.sub(''',a)
a=re.sub('紫外线.*舒适指数','',a)
a=re.sub('<[^>]*>','',a)
a=re.sub('  ','',a)
a=re.sub('\s*','',a)
a=re.sub('星期.{3}','',a)
a=re.sub('高温:','',a)
a=re.sub('低温:','',a)
a=re.sub('/\s*',' -',a)
a=re.match(';([^;]*);([^;]*);([^;]*);',a)
b0=re.match('(.*级)(\d*日)',a.group(1))
tomorrow='%s日: ' %(commands.getoutput('date -d"tomorrow" +%d'))
t1=re.match('([^\d]*)(\d*℃\s-\d*℃)(.*)',b0.group(1))#here
b1=tomorrow+t1.group(1)+','+t1.group(2)+','+t1.group(3)
b2=re.match('(.*级)(\d*日)',a.group(2))
t2=re.match('([^\d]*)(\d*℃)(\d*℃)(.*)',b2.group(1))
b3=b0.group(2)+": "+t2.group(1)+','+t2.group(3)+'-'+t2.group(2)+','+t2.group(4)
b4=re.match('(.*级)(\d*日)',a.group(3)).group(1)
t3=re.match('([^\d]*)(\d*℃)(\d*℃)(.*)',b4)
b4=b2.group(2)+": "+t3.group(1)+','+t3.group(3)+'-'+t3.group(2)+','+t3.group(4)
message=city+'未来三天的天气:\n'
message+=b1+'\n'+b3+'\n'+b4+'\n'
f.close()
print message
return message
def send_mail(send_from, send_to, subject, text,auth=(), send_server='localhost'):
msg = email.MIMEMultipart.MIMEMultipart()
msg['From'] = send_from
msg['To'] = email.Utils.COMMASPACE.join(send_to)
msg['Date'] = email.Utils.formatdate(localtime=True)
#utf8 encoding to avoid irrecognizable characters to mailclient with gbk encoding(such as sina,sohu etc)
#msg['Subject'] = subject
msg['Subject'] = email.Header.Header(subject,'utf-8')
msg.attach(email.MIMEText.MIMEText(text,_subtype='plain',_charset='utf-8'))
while 1:
try:
smtp = smtplib.SMTP(send_server)
except socket.gaierror, detail:
print detail
continue
else:
break
#the two lines below are neccessary to
smtp.docmd("EHLO server" )
smtp.starttls()
smtp.login(*auth)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
if __name__=="__main__" :
weather_msg=''
weather=Weather()
for city in cities.keys():
#rm_pages(city)
weather_msg+=weather.get_message(city)
send_mail(
'gx2008758@gmail.com',
['bestfrineds@gmail.com','sisters@sina.com','gx2008758@gmail.com'],
"^_^天气预报^_^",
weather_msg,
('gx2008758@gmail.com', 'uguessit'),
#'smtp.gmail.com' )
'209.85.143.111' )
print 'Done.'
|