PYTHON   75
checkhours
Guest on 16th March 2023 12:14:35 AM


  1. #!/usr/bin/env python
  2.  
  3. import urllib2
  4. import hashlib
  5. import smtplib
  6. from email.mime.text import MIMEText
  7.  
  8. f = open('checkhours.txt')
  9. urllist = f.readlines()
  10.  
  11. buff = ""
  12. mailbuf = ""
  13.  
  14. send_email = False
  15.  
  16. for line in urllist:
  17.     #print line
  18.  
  19.     s = line.split('|')
  20.  
  21.     url = s[0]
  22.     locator_string = s[1]
  23.     chars_to_care_about = s[2]
  24.     hash = s[3]
  25.  
  26.     url = url.strip()
  27.     hash = hash.strip()
  28.     chars_to_care_about = int(chars_to_care_about.strip())
  29.     locator_string = locator_string.strip()
  30.  
  31.     response = urllib2.urlopen(url)
  32.     html = response.read()
  33.  
  34.     #print chars_to_care_about
  35.  
  36.     html = html.split(locator_string)[1]
  37.     html = html[0:chars_to_care_about]
  38.  
  39.  
  40.     m = hashlib.md5()
  41.     m.update(html)
  42.  
  43.     md5 =  m.hexdigest()
  44.  
  45.  
  46.     if (md5 != hash):
  47.         print "Different! ", url , "|", hash, "|", md5
  48.         mailbuf += "Different! %s\n" % (url)
  49.         send_email = True
  50.     else:
  51.         print "Same! ", url , "|", hash, "|", md5
  52.         mailbuf += "Same! %s\n" % (url)
  53.     buff += url + '|' + locator_string + '|' + str(chars_to_care_about) + '|' + md5 + '\n'
  54.  
  55.  
  56. f = open('checkhours.txt', 'w')
  57. f.write(buff)
  58.  
  59. #send_email = True
  60.  
  61. if (send_email):
  62.     msg = MIMEText(mailbuf)
  63.     msg['Subject'] = 'Difference detected in library hours by checkhours.py'
  64.     msg['From'] = 'noreply@example.org'
  65.  
  66.     msg['To'] = 'eli@example.org'
  67.  
  68.     s = smtplib.SMTP('64.26.72.134')
  69.     s.sendmail('noreply@example.org', 'eli@example.org', msg.as_string())
  70.  
  71.     s.quit()

Raw Paste

Login or Register to edit or fork this paste. It's free.