Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # accuweather2sms.py !!!
- url = 'https://www.accuweather.com/en/ca/toronto/m5j/hourly-weather-forecast'
- try: import messaging, appuifw
- except: pass
- # help(messaging)
- print 'AccuWeather SMS'
- import urllib2 # note: most recommended above urllib
- import HTMLParser
- hpx = HTMLParser.HTMLParser()
- import re
- user_agent = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113 Firefox/2.0.0.11 Opera/9.25 Ubuntu/dapper-security'}
- allow='''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~''' #string.printable
- def cleanup(s):
- sss=''
- for z in s:
- if z in allow:
- sss+=z
- sss.replace('°','')
- return sss
- def html2text(h):
- h = cleanup(h)
- str1 = hpx.unescape(h)
- int2 = str1.lower().find("<body")
- if int2>0:
- str1 = str1[int2:]
- int2 = str1.lower().find("</body>")
- if int2>0:
- str1 = str1[:int2]
- list1 = ['<br>', '<tr', '<td', '</p>', 'span>', 'li>', '</h', 'div>' ]
- list2 = [chr(13), chr(13), chr(9), chr(13), chr(13), chr(13), chr(13), chr(13)]
- bolFlag1 = True
- bolFlag2 = True
- strReturn = ""
- for int1 in range(len(str1)):
- str2 = str1[int1]
- for int2 in range(len(list1)):
- if str1[int1:int1+len(list1[int2])].lower() == list1[int2]:
- strReturn = strReturn + list2[int2]
- if str1[int1:int1+7].lower() == '<script' or str1[int1:int1+9].lower() == '<noscript':
- bolFlag1 = False
- if str1[int1:int1+6].lower() == '<style':
- bolFlag1 = False
- if str1[int1:int1+7].lower() == '</style':
- bolFlag1 = True
- if str1[int1:int1+9].lower() == '</script>' or str1[int1:int1+11].lower() == '</noscript>':
- bolFlag1 = True
- if str2 == '<':
- bolFlag2 = False
- if bolFlag1 and bolFlag2 and (ord(str2) != 10) :
- strReturn = strReturn + str2
- if str2 == '>':
- bolFlag2 = True
- if bolFlag1 and bolFlag2:
- strReturn = strReturn.replace(chr(32)+chr(13), chr(13))
- strReturn = strReturn.replace(chr(9)+chr(13), chr(13))
- strReturn = strReturn.replace(chr(13)+chr(32), chr(13))
- strReturn = strReturn.replace(chr(13)+chr(9), chr(13))
- strReturn = strReturn.replace(chr(13)+chr(13), chr(13))
- strReturn = strReturn.replace(chr(13), '\n')
- return strReturn
- zzz='''
- Temp (
- Wind (
- Forecast
- Rain
- Snow
- Ice
- Cloud
- Humidity
- Dew
- Visibility
- UV Index
- '''.strip().splitlines()
- day='''
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
- '''.strip().splitlines()
- allow='''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~''' #string.printable
- def next8page(h):
- return max(re.findall(r'''(?<=href=['"])%s\?.*?(?=['"])''' % (site),h))
- def sort_data(h):
- h=h.replace('\t',' ')
- while '\n\n' in h:
- h=h.replace('\n\n','\n')
- h=h.replace('\n',' \n')
- for z in day:
- h=h.replace(z,'$$$'+z)
- h=h.split('$$$')
- rrr=[]
- for s in h:
- row=[]
- s=s.splitlines()
- while s:
- t=s.pop(0).strip()
- if t in day:
- d=dd=t
- t='\n.\n'+t
- for _ in 'z'*8:
- m = s.pop(0)
- if ('12am' in m):
- d = day[day.index(dd)+1]
- t = t.replace(dd,d)
- row.append(t+': '+m)
- else:
- p=''
- if t in zzz:
- if 'Temp (' in t:
- t='Temp (Celsius)'
- p='c'
- elif 'Dew ' in t:
- t='Dew Point'
- p='c'
- for z in range(8):
- m = s.pop(0).strip()
- if p: m=m[:-2]
- row[z] += '\n'+t+': '+m+p
- rrr += row
- s = '\n'.join(rrr)
- while ' ' in s:
- s=s.replace(' ',' ')
- s=s.replace('\n\n','\n')
- return s
- #
- site = "https://www.accuweather.com/en/ca/toronto/m5j/hourly-weather-forecast/55488"
- def req(urlx):
- h=''
- for z in '321':
- print z+'...',
- while 1:
- try:
- req = urllib2.Request(url=urlx,headers=user_agent)
- html = urllib2.urlopen(req).read()
- break
- except:
- print '...',
- h2t = html2text(html).split('Next 8 hours')[1]
- print
- print h2t
- print
- h += sort_data(h2t)
- if z != '1':
- urlx = next8page(html)
- return h
- print 'might take a few minutes collecting data from the website'
- msg = req(site)
- # msg = sort_data(s) # for testing
- # num=1235551234
- # num=appuifw.query(u"Enter Number: ", "number", u"")
- if msg:
- msg='24 Hour Intervals...\nAccuWeather SMS App via Kirk Lawrence\n\n'+msg
- print msg
- try:
- messaging.sms_send(u'%d' % (num), msg)
- print '\nMessage Sent!'
- except:
- print "\nSMS Unable To Send!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement