1111
1212import util
1313
14-
1514parser = argparse .ArgumentParser (description = "Webpage monitor." )
16- parser .add_argument (
17- '--roster_json' , type = str , default = './roster.json' , help = "path to the roster" )
18- parser .add_argument (
19- '--check_every' , type = int , default = 43200 , help = "check every N seconds" )
20- parser .add_argument (
21- '--exit_after' , type = int , default = None , help = "quit after N seconds" )
22- parser .add_argument (
23- '--tmp_dir' , type = str , default = '/tmp/webpage-monitor' ,
24- help = "directory to dump screenshots for comparison" )
25- parser .add_argument (
26- '--clear_cached' , action = 'store_true' ,
27- help = "whether to clear the screenshots on disk" )
15+ parser .add_argument ('--roster_json' ,
16+ type = str ,
17+ default = './roster.json' ,
18+ help = "path to the roster" )
19+ parser .add_argument ('--check_every' ,
20+ type = int ,
21+ default = 43200 ,
22+ help = "check every N seconds" )
23+ parser .add_argument ('--exit_after' ,
24+ type = int ,
25+ default = None ,
26+ help = "quit after N seconds" )
27+ parser .add_argument ('--tmp_dir' ,
28+ type = str ,
29+ default = '/tmp/webpage-monitor' ,
30+ help = "directory to dump screenshots for comparison" )
31+ parser .add_argument ('--clear_cached' ,
32+ action = 'store_true' ,
33+ help = "whether to clear the screenshots on disk" )
2834
2935
3036def main (args ):
@@ -46,8 +52,8 @@ def main(args):
4652 changed , deltas = [], []
4753
4854 for url , opt in tqdm (roster .items (), desc = "Checking URLs" ):
49- out_dir = join (
50- args . tmp_dir , replace_special_char (url )).rstrip ('/' )
55+ out_dir = join (args . tmp_dir ,
56+ replace_special_char (url )).rstrip ('/' )
5157
5258 # Take screenshots
5359 screenshot (url , out_dir , opt )
@@ -73,18 +79,22 @@ def main(args):
7379 if changed :
7480 msg = ''
7581 for url , delta in zip (changed , deltas ):
76- msg += "file://{delta}\n {url}\n \n " .format (
77- delta = delta , url = url )
82+ msg += "file://{delta}\n {url}\n \n " .format (delta = delta ,
83+ url = url )
7884 util .email_myself (msg , subject = "Webpage Monitor" )
7985 util .format_print ("Change detected; email sent" , 'header' )
8086
8187 if time () - start_t > exit_after :
8288 break
8389
8490
85- def diff_screenshots (
86- old_png , new_png , delta_png , pix_diff_thres = 0.1 , n_diff_thres = 16 ,
87- unchanged_alpha = 0.2 , diff_blur_sigma = 4 ):
91+ def diff_screenshots (old_png ,
92+ new_png ,
93+ delta_png ,
94+ pix_diff_thres = 0.1 ,
95+ n_diff_thres = 16 ,
96+ unchanged_alpha = 0.2 ,
97+ diff_blur_sigma = 4 ):
8898 old = util .imread_arr (old_png )
8999 new = util .imread_arr (new_png )
90100
@@ -94,7 +104,7 @@ def diff_screenshots(
94104 return delta_png
95105
96106 # Check content
97- pixel_is_diff = np .abs (old - new ) >= pix_diff_thres # (H, W, 3)
107+ pixel_is_diff = np .abs (old - new ) >= pix_diff_thres # (H, W, 3)
98108 pixel_is_diff = np .sum (pixel_is_diff , axis = 2 ) > 0
99109
100110 # Not enough different pixels for a change
@@ -114,10 +124,9 @@ def screenshot(url, out_dir, opt, width=512, delay=3):
114124 if not exists (out_dir ):
115125 makedirs (out_dir )
116126
117- cmd = (
118- 'webkit2png --fullsize --no-images --ignore-ssl-check --width={w} '
119- '--delay={delay} --dir={dir_} --filename={t} {url}' ).format (
120- w = width , delay = delay , dir_ = out_dir , t = time (), url = url )
127+ cmd = ('webkit2png --fullsize --no-images --ignore-ssl-check --width={w} '
128+ '--delay={delay} --dir={dir_} --filename={t} {url}' ).format (
129+ w = width , delay = delay , dir_ = out_dir , t = time (), url = url )
121130 util .call (cmd , silence_stdout = True )
122131
123132
@@ -128,8 +137,9 @@ def load_roster(roster_json):
128137
129138
130139def replace_special_char (url ):
131- return url .replace (
132- '/' , '_' ).replace ('?' , '_' ).replace ('&' , '_' ).replace (':' , '_' )
140+ return url .replace ('/' , '_' ).replace ('?' ,
141+ '_' ).replace ('&' ,
142+ '_' ).replace (':' , '_' )
133143
134144
135145if __name__ == '__main__' :
0 commit comments