-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_checker.py
executable file
·102 lines (78 loc) · 2.73 KB
/
update_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import urllib.request
import os.path
import codecs
import difflib
from escape_sequence import *
class WebPage:
def __init__(self,name,url):
self.name = name
self.url = url
def get_html(self):
try:
response = urllib.request.urlopen(self.url)
except:
return None
charset = 'utf-8' #TODO : detection
byte_html = response.read()
try:
#codecs.lookup(charset)
html = byte_html.decode(charset,'replace')
except:
print(em('Failed to Detect Encoding at ') + self.url)
html = None
return html
def parse(self,html):
raise NotImplementedError
def beautify(self,parsed):
raise NotImplementedError
class Logger:
def __init__(self,name,log_dir = './log'):
self.name = name
self.log_dir = log_dir
def __load_log(self):
path = self.log_dir + '/' + self.name
return open(path,'r').read() if os.path.exists(path) else ""
def write_log(self,new_data):
if not os.path.exists(self.log_dir):
os.mkdir(self.log_dir)
print(em('Log Directory : '+ self.log_dir +' not found.Created directory.'))
path = self.log_dir + '/' + self.name
open(path,'w').write(new_data)
def get_updated(self,new_data):
old_data = self.__load_log()
ret =''
if old_data != new_data :
d = difflib.Differ()
diff = list(d.compare(old_data.splitlines(),new_data.splitlines()))
new_diff = []
for line in diff:
if line.startswith('+ '):
new_diff.append(line[2:])
ret = '\n'.join(new_diff)
if ret == None:
ret = ''
is_updated = True if len(ret)>0 else False
return is_updated , ret
class UpdateChecker(WebPage,Logger):
def __init__(self,name,url):
WebPage.__init__(self,name,url)
Logger.__init__(self,name)
self.name = name
self.url = url
def check_update(self,save_log = True):
html = self.get_html()
if html == None:
print(em("Failed to get html")+" at "+self.url)
return False , ''
parsed = self.parse(html)
is_updated,updates = self.get_updated(parsed)
ret =''
if is_updated :
if save_log: self.write_log(parsed)
ret = 'サイト:\r\n %s \r\n' % self.name
ret += 'URL :\r\n %s \r\n' % self.url
ret += '更新情報 :\r\n'
ret += self.beautify(updates)
return is_updated , ret