Apr-30-2019, 07:08 AM
hi,
i have written a code that look for changes in website and produce it's html. but my concern is how can i look highlight the change that has happen.
below is my code:
i have written a code that look for changes in website and produce it's html. but my concern is how can i look highlight the change that has happen.
below is my code:
from urllib.request import urlopen
import EmailAlert
class Changes:
def __init__(self, url, archive='archived_page.html'):
self.url = url
self.new_html = []
self.old_html = []
self.change_list = []
self.archive = archive
def update(self):
with urlopen(self.url) as byt:
self.new_html = [x.decode('latin-1') for x in byt.readlines()]
open(self.archive, 'a').close()
with open(self.archive, 'rb+') as old:
self.old_html = [x.decode('latin-1') for x in old.readlines()]
old.seek(0)
old.truncate()
data = old.writelines(x.encode('latin-1') for x in self.new_html)
def diff(self):
self.change_list = []
for a, b in zip(self.old_html, self.new_html):
if a != b:
if not b.strip():
self.change_list.append(f'\nLine removed:\n{a}\n')
elif not a.strip():
self.change_list.append(f'\nLine added:\n{b}\n')
else:
self.change_list.append(f'\n{a}\nChanged to:\n{b}\n')
print(a, b)
return self.change_list
def email(self):
message = f'{len(self.change_list)} changes in {self.url}:\n'
message += ''.join(self.change_list)
EmailAlert.send(message)
if __name__ == '__main__':
link = 'file:///C:/Users/prince.bhatia/Desktop/projects/urlwatch-master/website-watcher-master/website-watcher-master/test/Maharashtra.htm'
#link = "http://www.rera.mp.gov.in/"
wiki_updater = Changes(link)
wiki_updater.update()
if wiki_updater.diff():
print("Website Updated")
wiki_updater.email()
