Advertisement
here2share

# accuweather2sms.py

Sep 27th, 2017
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. # accuweather2sms.py  !!!
  3.  
  4. url = 'https://www.accuweather.com/en/ca/toronto/m5j/hourly-weather-forecast'
  5.  
  6. try: import messaging, appuifw
  7. except: pass
  8. # help(messaging)
  9.  
  10. print 'AccuWeather SMS'
  11.  
  12. import urllib2 # note: most recommended above urllib
  13. import HTMLParser
  14. hpx = HTMLParser.HTMLParser()
  15. import re
  16.  
  17. 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'}
  18.  
  19. allow='''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~''' #string.printable
  20.  
  21. def cleanup(s):
  22.     sss='' 
  23.     for z in s:
  24.         if z in allow:
  25.             sss+=z
  26.         sss.replace('&#176;','')
  27.     return sss
  28. def html2text(h):
  29.     h = cleanup(h)
  30.     str1 = hpx.unescape(h)
  31.     int2 = str1.lower().find("<body")
  32.     if int2>0:
  33.        str1 = str1[int2:]
  34.     int2 = str1.lower().find("</body>")
  35.     if int2>0:
  36.        str1 = str1[:int2]
  37.     list1 = ['<br>',  '<tr',  '<td', '</p>', 'span>', 'li>', '</h', 'div>' ]
  38.     list2 = [chr(13), chr(13), chr(9), chr(13), chr(13),  chr(13), chr(13), chr(13)]
  39.     bolFlag1 = True
  40.     bolFlag2 = True
  41.     strReturn = ""
  42.     for int1 in range(len(str1)):
  43.       str2 = str1[int1]
  44.       for int2 in range(len(list1)):
  45.         if str1[int1:int1+len(list1[int2])].lower() == list1[int2]:
  46.            strReturn = strReturn + list2[int2]
  47.       if str1[int1:int1+7].lower() == '<script' or str1[int1:int1+9].lower() == '<noscript':
  48.          bolFlag1 = False
  49.       if str1[int1:int1+6].lower() == '<style':
  50.          bolFlag1 = False
  51.       if str1[int1:int1+7].lower() == '</style':
  52.          bolFlag1 = True
  53.       if str1[int1:int1+9].lower() == '</script>' or str1[int1:int1+11].lower() == '</noscript>':
  54.          bolFlag1 = True
  55.       if str2 == '<':
  56.          bolFlag2 = False
  57.       if bolFlag1 and bolFlag2 and (ord(str2) != 10) :
  58.         strReturn = strReturn + str2
  59.       if str2 == '>':
  60.          bolFlag2 = True
  61.       if bolFlag1 and bolFlag2:
  62.         strReturn = strReturn.replace(chr(32)+chr(13), chr(13))
  63.         strReturn = strReturn.replace(chr(9)+chr(13), chr(13))
  64.         strReturn = strReturn.replace(chr(13)+chr(32), chr(13))
  65.         strReturn = strReturn.replace(chr(13)+chr(9), chr(13))
  66.         strReturn = strReturn.replace(chr(13)+chr(13), chr(13))
  67.     strReturn = strReturn.replace(chr(13), '\n')
  68.     return strReturn
  69.  
  70. zzz='''
  71. Temp (
  72. Wind (
  73. Forecast
  74. Rain
  75. Snow
  76. Ice
  77. Cloud
  78. Humidity
  79. Dew
  80. Visibility
  81. UV Index
  82. '''.strip().splitlines()
  83.  
  84. day='''
  85. Sunday
  86. Monday
  87. Tuesday
  88. Wednesday
  89. Thursday
  90. Friday
  91. Saturday
  92. Sunday
  93. '''.strip().splitlines()
  94.  
  95. allow='''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~''' #string.printable
  96.  
  97. def next8page(h):
  98.     return max(re.findall(r'''(?<=href=['"])%s\?.*?(?=['"])''' % (site),h))
  99.  
  100. def sort_data(h):
  101.     h=h.replace('\t','  ')
  102.     while '\n\n' in h:
  103.         h=h.replace('\n\n','\n')
  104.     h=h.replace('\n',' \n')
  105.     for z in day:
  106.         h=h.replace(z,'$$$'+z)
  107.     h=h.split('$$$')
  108.    
  109.     rrr=[]
  110.     for s in h:
  111.         row=[]
  112.         s=s.splitlines()
  113.         while s:
  114.             t=s.pop(0).strip()
  115.             if t in day:
  116.                 d=dd=t
  117.                 t='\n.\n'+t
  118.                 for _ in 'z'*8:
  119.                     m = s.pop(0)
  120.                     if ('12am' in m):
  121.                         d = day[day.index(dd)+1]
  122.                         t = t.replace(dd,d)
  123.                     row.append(t+': '+m)
  124.             else:
  125.                 p=''
  126.                 if t in zzz:
  127.                     if 'Temp (' in t:
  128.                         t='Temp (Celsius)'
  129.                         p='c'
  130.                     elif 'Dew ' in t:
  131.                         t='Dew Point'
  132.                         p='c'
  133.                     for z in range(8):
  134.                         m = s.pop(0).strip()
  135.                         if p: m=m[:-2]
  136.                         row[z] += '\n'+t+': '+m+p
  137.         rrr += row
  138.     s = '\n'.join(rrr)
  139.     while '   ' in s:
  140.         s=s.replace('   ','  ')
  141.     s=s.replace('\n\n','\n')
  142.     return s
  143. #
  144.  
  145. site = "https://www.accuweather.com/en/ca/toronto/m5j/hourly-weather-forecast/55488"
  146.  
  147. def req(urlx):
  148.     h=''
  149.     for z in '321':
  150.         print z+'...',
  151.         while 1:
  152.             try:
  153.                 req = urllib2.Request(url=urlx,headers=user_agent)
  154.                 html = urllib2.urlopen(req).read()
  155.                 break
  156.             except:
  157.                 print '...',
  158.         h2t = html2text(html).split('Next 8 hours')[1]
  159.         print
  160.         print h2t
  161.         print
  162.         h += sort_data(h2t)
  163.         if z != '1':
  164.             urlx = next8page(html)
  165.     return h
  166.  
  167. print 'might take a few minutes collecting data from the website'
  168. msg = req(site)
  169. # msg = sort_data(s) # for testing
  170.  
  171. # num=1235551234
  172. # num=appuifw.query(u"Enter Number: ", "number", u"")
  173.  
  174. if msg:
  175.     msg='24 Hour Intervals...\nAccuWeather SMS App via Kirk Lawrence\n\n'+msg
  176.     print msg
  177.     try:
  178.         messaging.sms_send(u'%d' % (num), msg)
  179.         print '\nMessage Sent!'
  180.     except:
  181.         print "\nSMS Unable To Send!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement