Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import argparse
  3. import configparser
  4. import datetime
  5. import difflib
  6. import hashlib
  7. import html2text
  8. import logging
  9. import smtplib
  10. import time
  11. import urllib.request
  12.  
  13.  
  14. class UrlChange:
  15. """ URL is checked and a hash is made. Changes are regcognized and
  16. reported
  17. """
  18.  
  19. def __init__(self, url):
  20. self.url = url
  21. self.url_hash = self.create_hash()
  22. self.content = self.get_content()
  23. logger.info(("Start Monitoring... hash "
  24. "{url_hash}").format(url_hash=self.url_hash))
  25.  
  26. def get_content(self):
  27. """ The data is read from the url. """
  28. try:
  29. url_data = urllib.request.urlopen(self.url)
  30. url_data = url_data.read()
  31. url_data = url_data.decode("utf-8", "ignore")
  32. url_data = html2text.html2text(url_data)
  33. except Exception as e:
  34. logger.critical("Error: {}".format(e))
  35. raise
  36. return url_data
  37.  
  38. def create_hash(self):
  39. """ A md5 hash is created from the url_data. """
  40. url_data = self.get_content().encode("utf-8")
  41. md5_hash = hashlib.md5()
  42. md5_hash.update(url_data)
  43. return md5_hash.hexdigest()
  44.  
  45. def compare_hash(self):
  46. """ The hash is compared with the stored value. If there is a change
  47. a function is opend witch alerts the user.
  48. """
  49. if(self.create_hash() == self.url_hash):
  50. logger.info("Nothing has changed")
  51. return False
  52. else:
  53. logger.info("Something has changed")
  54. date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")
  55. if(not args.nomail):
  56. send_mail("Url has changed!",
  57. ("The Url {url} has changed at "
  58. "{date} .").format(url=self.url, date=date))
  59. if(not args.nodiff):
  60. diff = self.diff()
  61. logger.info("{diff}".format(**locals()))
  62. if(not args.nomail):
  63. diff.encode("ascii", "ignore")
  64. send_mail("Url difference!",
  65. ("The Url {url} has changed at {date} ."
  66. "nnNew contentn{diff}").format(url=self.url,
  67. date=date,
  68. diff=diff))
  69. return True
  70.  
  71. def diff(self):
  72. """ The function tries to extract the changed part of the url content.
  73. """
  74. result = ""
  75. new_content = self.get_content()
  76. s = difflib.SequenceMatcher(None, self.content, new_content)
  77. for tag, i1, i2, j1, j2 in s.get_opcodes():
  78. if(tag == "insert" or tag == "replaced"):
  79. result += new_content[j1:j2]
  80. return result
  81.  
  82.  
  83. def send_mail(subject, message):
  84. """ A connection to the smtp server is build. A mail is sent. """
  85. try:
  86. smtp_url = config["SMTP"]["url"]
  87. smtp_port = config["SMTP"]["port"]
  88. smtp_user = config["SMTP"]["user"]
  89. smtp_password = config["SMTP"]["password"]
  90. destination = config["SMTP"]["destination"]
  91.  
  92. server = smtplib.SMTP(smtp_url, smtp_port)
  93. server.set_debuglevel(0)
  94. server.ehlo()
  95. server.starttls()
  96. server.login(smtp_user, smtp_password)
  97. except Exception as e:
  98. logger.critical("Error: {}".format(e))
  99. raise
  100.  
  101. date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S")
  102. msg = ("From: {source}nSubject: {subject}nDate: {date}n"
  103. "n{message}").format(source=smtp_user, subject=subject, date=date,
  104. message=message)
  105.  
  106. server.sendmail(smtp_user, destination, msg)
  107. server.quit()
  108. logging.info("email was sent")
  109.  
  110. # Arguments from the console are parsed
  111. parser = argparse.ArgumentParser(
  112. description=("Monitor ifa website"
  113. "has changed.")
  114. )
  115. parser.add_argument("url", help="url that should be monitored")
  116. parser.add_argument("-t", "--time",
  117. help="seconds between checks (default: 600)",
  118. default=600, type=int)
  119. parser.add_argument("-nd", "--nodiff", help="show no difference",
  120. action="store_true")
  121. parser.add_argument("-n", "--nomail", help="no email is sent",
  122. action="store_true")
  123. args = parser.parse_args()
  124.  
  125. # A new logging object is created
  126. logging.basicConfig(format="%(asctime)s %(message)s",
  127. datefmt="%d.%m.%Y %H:%M:%S")
  128. logger = logging.getLogger()
  129. logger.setLevel(logging.INFO)
  130.  
  131. # Parse the configuration file
  132. config = configparser.ConfigParser()
  133. try:
  134. config.read("config.conf")
  135. if("SMTP" not in config):
  136. raise ValueError("Can't find smtp data in config.conf")
  137. except Exception as e:
  138. logger.critical("Error: {}".format(e))
  139. raise
  140.  
  141. # Main part
  142. url1 = UrlChange(args.url)
  143. time.sleep(args.time)
  144. while(True):
  145. if(url1.compare_hash()):
  146. break
  147. time.sleep(args.time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement