-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcomichelper.py
721 lines (452 loc) · 23.2 KB
/
webcomichelper.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
"""
Copyright 2011 Stonepaw & Helmic
This file is part of Webcomic Helper.
Webcomic Helper is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Webcomic Helper is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Webcomic Helper. If not, see <http://www.gnu.org/licenses/>.
"""
import clr
import System
import regexloader
from System import Func, ArgumentNullException, UriFormatException, Uri
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import DialogResult
clr.AddReference("System.Net")
from System.Net import HttpWebRequest, WebException, ProtocolViolationException
from System.IO import StreamReader
from System.Text.RegularExpressions import RegexOptions, Regex
from common import LinkRegex, ImageRegex, WebComicHelperResult, WebComicHelperResultEnum, NextPageLinkFormResult, REGEX_FILE
debug = True
print_source = False
debug_backup_regex = False
import wizard
#@Name Webcomic Helper
#@Hook Books, Library
#@Image webcomichelper.png
def WebComicHelper(books):
wizard.ComicRack = ComicRack
f = wizard.WebComicHelperWizard()
r = f.ShowDialog()
opencomic = f._open_webcomic.Checked
if r != DialogResult.Cancel:
if opencomic:
ComicRack.App.OpenBooks.Open(f.book, 0, 0)
f.Dispose()
def webcomic_helper_do_work(worker, e):
form = e.Argument["Form"]
if debug: print "Loading regex from file"
worker.ReportProgress(0, "Loading regex from file")
images_regex, links_regex, sites = regexloader.load_regex_from_file(REGEX_FILE)
if debug_backup_regex:
images_regex = []
links_regex = []
#Load the variables passed from the form. It should be in the form of a dict
first_page_url = e.Argument["FirstPage"]
image_url = e.Argument["Image"]
second_page_url = e.Argument["SecondPage"]
if worker.CancellationPending:
e.Cancel = True
return
try:
global first_page_uri
global second_page_uri
first_page_uri = System.Uri(first_page_url)
second_page_uri = System.Uri(second_page_url)
global escaped_first_page_uri
global escaped_second_page_uri
escaped_first_page_uri = System.Uri(escape_uri_string(first_page_uri))
escaped_second_page_uri = System.Uri(escape_uri_string(second_page_uri))
escaped_image_url = escape_uri_string(image_url)
if debug:
print first_page_uri.AbsoluteUri
print second_page_uri.AbsoluteUri
print escaped_first_page_uri.AbsoluteUri
print escaped_second_page_uri.AbsoluteUri
print image_url
print escaped_image_url
except UriFormatException, ex:
#Errored occured. Raise an error to exit the background thread
raise UriFormatException(ex.Message, ex)
except ArgumentNullException, ex:
raise ArgumentNullException(ex.Message, ex)
if debug: print "Checking if the input url is a defined site"
#Check if the site domain is in the list of sites
for site in sites:
if site._domain == first_page_uri.Host:
if debug: print first_page_uri.Host + " is a defined site"
e.Result = WebComicHelperResult(WebComicHelperResultEnum.Site, site._image_regex, site._link_regex)
return
#Get the page sources
#Get the source from the first page
if debug: print "Getting first page source"
worker.ReportProgress(1, "Retrieving first page source")
if worker.CancellationPending:
e.Cancel = True
return
global first_page_source
first_page_source = PageSource()
result = first_page_source.get_source(first_page_uri)
if result == False:
raise System.Exception(first_page_source._exception.Message, first_page_source._exception)
if print_source: print first_page_source._source
#Get the source from the second page
worker.ReportProgress(2, "Retrieving second page source")
if debug: print "Getting second page source"
if worker.CancellationPending:
e.Cancel = True
return
global second_page_source
second_page_source = PageSource()
result = second_page_source.get_source(second_page_uri)
if result == False:
raise System.Exception(second_page_source._exception.Message, second_page_source._exception)
if print_source: print second_page_source._source
#Try and find a image regex that works on both pages.
#Move this stuff to another method since it is pretty much the same for both link and image
worker.ReportProgress(3, "Searching for matching regex")
valid_image_regex = []
if debug: print "\n\nStarting to search for image regex"
for imageregex in images_regex:
if worker.CancellationPending:
e.Cancel = True
return
result = check_regex_against_source(imageregex, image_url, escaped_image_url)
if result:
valid_image_regex.append(result)
#If no valid image regex
if len(valid_image_regex) == 0:
if debug: print "No Valid image regex found"
#Try and get the url of the image on the second page from the user
second_image_url = form.Invoke(Func[str](form.get_second_image_url))
#User canceled the inputing the second image url
if second_image_url == "":
e.Result = WebComicHelperResult(WebComicHelperResultEnum.NoImages)
return
#Try and create a image regex
image_regex = create_image_regex(image_url, second_image_url)
#couldn't create a image regex
if image_regex is None:
e.Result = WebComicHelperResult(WebComicHelperResultEnum.NoImages)
return
valid_image_regex.append(image_regex)
#Now try and find a link regex that works on both pages
if debug: print "\n\n\nFinding a next page link regular expression"
valid_link_regex = []
for linkregex in links_regex:
if worker.CancellationPending:
e.Cancel = True
return
result = check_regex_against_source(linkregex, second_page_uri.AbsoluteUri, escaped_second_page_uri.AbsoluteUri)
if result:
valid_link_regex.append(result)
if len(valid_link_regex) == 0:
if debug: print "No valid link regex could be found"
next_page_link_result = form.Invoke(Func[NextPageLinkFormResult](form.get_next_page_link))
if next_page_link_result.text == "":
e.Result = WebComicHelperResult(WebComicHelperResultEnum.NoLinks)
return
if next_page_link_result.is_image:
link_regex = create_next_link_regx_with_image(next_page_link_result.text)
else:
link_regex = create_next_link_regx_with_text(next_page_link_result.text)
if link_regex is None:
e.Result = WebComicHelperResult(WebComicHelperResultEnum.NoLinks)
return
valid_link_regex.append(link_regex)
#If the script gets this far there should be at least one valid image regex and one valid link regex
return_image_regex = get_best_regex(valid_image_regex, "Image")
return_link_regex = get_best_regex(valid_link_regex, "Link")
worker.ReportProgress(4, "Done")
e.Result = WebComicHelperResult(WebComicHelperResultEnum.Success, return_image_regex, return_link_regex)
if worker.CancellationPending:
e.Cancel = True
return
def check_regex_against_source(regex, check_value, check_value2):
matches = Regex.Matches(first_page_source._source, regex._regex, RegexOptions.IgnoreCase)
if matches.Count != 0:
if debug:
print "\n\nFound " + str(matches.Count) + " match(es) on the first page with regex: " + regex._regex
print "Captured: " + matches[0].Value
print "link group: " + matches[0].Groups["link"].Value
#We don't care if there is more than one result. As long as the first result is the correct image
result, result_uri = Uri.TryCreate(first_page_uri, matches[0].Groups["link"].Value)
if result and result_uri.AbsoluteUri in (check_value, check_value2):
#Valid url and matches against the check_value
if debug: print "Valid uri"
else:
return False
matches_second = Regex.Matches(second_page_source._source, regex._regex, RegexOptions.IgnoreCase)
if matches_second.Count == 0:
if debug: print "No matches found on the second page"
return False
#Regex match on the second page. Same deal as above. We don't care if there is more than one result
if debug:
print "\nFound " + str(matches_second.Count) + " match(es) on the second page"
print "Captured: " + matches_second[0].Value
print "link group: " + matches_second[0].Groups["link"].Value
result, result_uri = Uri.TryCreate(second_page_uri, matches_second[0].Groups["link"].Value)
if result:
regex._matches = matches.Count
if debug: print "Added to valid regex"
return regex
else:
if debug: print "Invalid uri"
return False
return False
def get_best_regex(regex_list, name = ""):
return_regex = ""
if len(regex_list) > 1:
#More than one valid regex so try and choose the best one.
number = 0
#try and get the one with the least amount of matches
for regex in regex_list:
if number == 0 or regex._matches < number:
number = regex._matches
return_regex = regex._regex
if debug: print "\n\n" + name + " regex being used is : " + return_regex + "\nand has " + str(number) + " matches per page"
else:
return_regex = regex_list[0]._regex
if debug: print "\n\n" + name + " regex being used is : " + return_regex + "\nand has " + str(regex_list[0]._matches) + " matches per page"
return return_regex
def create_image_regex(first_image_url, second_image_url):
if debug: print "Starting to create an image regex with urls:\n" + first_image_url + "\n" + second_image_url
base = get_string_intersect(first_image_url, second_image_url)
baseuri = Uri(base)
relativeuri = first_page_uri.MakeRelativeUri(baseuri)
if relativeuri.IsAbsoluteUri:
domain = relativeuri.Scheme + "://" + relativeuri.Host
relative = relativeuri.AbsolutePath
else:
domain = baseuri.Scheme + "://" + baseuri.Host
relative = relativeuri.OriginalString
domain = escape_regex_characters(domain)
relative = relative.lstrip("/")
relative = escape_regex_characters(relative)
relative = replace_date_values(relative)
domain = Regex.Replace(domain, "www\\\\.", "(?:www\\.)?", RegexOptions.IgnoreCase)
baseregex = "(?:" + domain + ")?\\.*/?" + relative
if debug: print "baseregex is: " + baseregex
regex = "src\\s*=\\s*(?([\"'])[\"'](?<link>" + baseregex + "[^\"']+)[\"']|(?<link>" + baseregex + "[^\\s<>]+))"
result, matches = check_created_image_regex(first_image_url, second_image_url, regex)
if not result:
regex = "(?([\"'])[\"'](?<link>" + baseregex + "[^\"']+)[\"']|(?<link>" + baseregex + "[^\\s<>]+))"
result, matches = check_created_image_regex(first_image_url, second_image_url, regex)
if not result:
#Sometimes the relative link creator makes a bad split, in that case try one other way:
#Check if we made the split before:
if baseuri.Host == first_page_uri.Host:
domain = baseuri.Scheme + "://" + baseuri.Host
relative = baseuri.AbsolutePath
relative = relative.lstrip("/")
domain = escape_regex_characters(domain)
relative = escape_regex_characters(relative)
relative = replace_date_values(relative)
domain = Regex.Replace(domain, "www\\\\.", "(?:www\\.)?", RegexOptions.IgnoreCase)
baseregex = "(?:" + domain + ")?\\.*/?" + relative
if debug: print "baseregex is: " + baseregex
regex = "src\\s*=\\s*(?([\"'])[\"'](?<link>" + baseregex + "[^\"']+)[\"']|(?<link>" + baseregex + "[^\\s<>]+))"
result, matches = check_created_image_regex(first_image_url, second_image_url, regex)
if not result:
regex = "(?([\"'])[\"'](?<link>" + baseregex + "[^\"']+)[\"']|(?<link>" + baseregex + "[^\\s<>]+))"
result, matches = check_created_image_regex(first_image_url, second_image_url, regex)
if not result:
if debug: print "Unable create a working image regex"
return None
if debug: print "Found a working image regex: " + regex
imgregex = ImageRegex(regex)
imgregex._matches = matches
return imgregex
def replace_date_values(string):
string = Regex.Replace(string, r"(?<=/)\d+(?=/)", r"(?<=/)\d+(?=/)", RegexOptions.IgnoreCase)
string = Regex.Replace(string, r"(?<=/)(?:jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|june?|july?|aug(?:ust)?|sep(?:tember)?|oct(?:ober)?|nov(?:ember)?|dec(?:ember)?)(?=/)",
r"(?<=/)(?:jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|june?|july?|aug(?:ust)?|sep(?:tember)?|oct(?:ober)?|nov(?:ember)?|dec(?:ember)?)(?=/)", RegexOptions.IgnoreCase)
return string
def escape_uri_string(uri):
if type(uri) != System.Uri:
uri = Uri(uri)
string = uri.AbsolutePath
for character in ["%", "!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "?", "#", "[", "]", " "]:
string = string.replace(character, Uri.HexEscape(character))
return uri.Scheme + "://" + uri.Host + string
def check_created_image_regex(first_image_url, second_image_url, regex):
matches = Regex.Matches(first_page_source._source, regex, RegexOptions.IgnoreCase)
escaped_first_image_url = escape_uri_string(first_image_url)
escaped_second_image_url = escape_uri_string(second_image_url)
if matches.Count == 0:
return False, 0
else:
if debug:
print "\nFound " + str(matches.Count) + " match(es) on the first page with regex: " + regex
print "Captured: " + matches[0].Value
print "link group: " + matches[0].Groups["link"].Value
#We don't care if there is more than one result. As long as the first result is the correct image
result, image_uri = Uri.TryCreate(first_page_uri, matches[0].Groups["link"].Value)
#Valid url and matches the input image url
if result and image_uri.AbsoluteUri in (first_image_url, escaped_first_image_url):
if debug: print "Valid uri and matches image url"
else:
if debug: print "Not a valid uri or doesn't match image url"
return False, 0
matches_second = Regex.Matches(second_page_source._source, regex, RegexOptions.IgnoreCase)
if matches_second.Count == 0:
if debug: print "No matches on the second page"
return False, 0
#Regex match on the second page. Same deal as above. We don't care if there is more than one result
if debug:
print "\nFound " + str(matches_second.Count) + " match(es) on the second page"
print "Captured: " + matches_second[0].Value
print "link group: " + matches_second[0].Groups["link"].Value
result, image_uri = Uri.TryCreate(second_page_uri, matches_second[0].Groups["link"].Value)
if result and image_uri.AbsoluteUri in (second_image_url, escaped_second_image_url):
if debug: print "Regex works on both pages and returns the correct image"
return True, matches.Count
else:
if debug: print "Invalid Uri or doesn't match the second image url"
return False, 0
return False, matches.Count
def escape_regex_characters(string):
string = string.replace("\\", "\\\\")
string = string.replace("^", "\\^")
string = string.replace("$", "\\$")
string = string.replace(".", "\\.")
string = string.replace("|", "\\|")
string = string.replace("{", "\\{")
string = string.replace("}", "\\}")
string = string.replace("[", "\\[")
string = string.replace("]", "\\]")
string = string.replace("(", "\\(")
string = string.replace(")", "\\)")
string = string.replace("*", "\\*")
string = string.replace("+", "\\+")
string = string.replace("?", "\\?")
return string
def create_next_link_regx_with_image(next_link_image_url):
"""Creates a link regex using the image behind the next link"""
if debug: print "Trying to create a next page link regex with image url:\n" + next_link_image_url
next_link_url_escaped = escape_regex_characters(next_link_image_url)
regex = "<a\\s[^<>]*href\\s*=\\s*(?([\"'])[\"'](?<link>[^\"']+)[\"']|(?<link>[^\\s<>]+))[^<>]*>[\\s\\n\\r\\t]*<img\\s[^<>]*src\\s*=\\s*[\"']?" + next_link_url_escaped + "[\"']?"
result, matches = check_created_link_regex(regex)
if not result:
#Try with a relative link:
relative_next_link_url = first_page_uri.MakeRelativeUri(Uri(next_link_image_url)).OriginalString
if debug: print "Trying with relative image url: " + relative_next_link_url
relative_next_link_url = relative_next_link_url.lstrip("/")
relative_next_link_url = escape_regex_characters(relative_next_link_url)
regex = "<a\\s[^<>]*href\\s*=\\s*(?([\"'])[\"'](?<link>[^\"']+)[\"']|(?<link>[^\\s<>]+))[^<>]*>[\\s\\n\\r\\t]*<img\\s[^<>]*src\\s*=\\s*[\"']?\\.*/?" + relative_next_link_url + "[\"']?"
result, matches = check_created_link_regex(regex)
if not result:
#Try one other way
relative_next_link_url = Uri(next_link_image_url).AbsolutePath
relative_next_link_url = relative_next_link_url.lstrip("/")
relative_next_link_url = escape_regex_characters(relative_next_link_url)
if debug: print "Trying with relative image url: " + relative_next_link_url
regex = "<a\\s[^<>]*href\\s*=\\s*(?([\"'])[\"'](?<link>[^\"']+)[\"']|(?<link>[^\\s<>]+))[^<>]*>[\\s\\n\\r\\t]*<img\\s[^<>]*src\\s*=\\s*[\"']?\\.*/?" + relative_next_link_url + "[\"']?"
result, matches = check_created_link_regex(regex)
if not result:
if debug: print "Couldn't create a next page regex with the image url"
return None
if debug: print "Found a next page regex with the image url!"
link = LinkRegex(regex)
link._matches = matches
return link
def create_next_link_regx_with_text(text):
"""Creates a link regex using the image behind the next link"""
if debug: print "Trying to create a next page link regex with text:\n" + text
text = escape_regex_characters(text)
regex = "<a\\s[^<>]*?href\\s*=\\s*(?(['\"])[\"'](?<link>[^\"']+)[\"']|(?<link>[^\\s<>]+))[^<>]*?>[\\s\\n\\r\\t]*" + text + "[\\s\\n\\r\\t]*</a"
result, matches = check_created_link_regex(regex)
if not result:
if debug: print "Couldn't create a next page regex with the text"
return None
if debug: print "Found a next page regex with the text!"
link = LinkRegex(regex)
link._matches = matches
return link
def check_created_link_regex(regex):
matches = Regex.Matches(first_page_source._source, regex, RegexOptions.IgnoreCase)
if matches.Count == 0:
return False, 0
else:
if debug:
print "\nFound " + str(matches.Count) + " match(es) on the first page with regex: " + regex
print "Captured: " + matches[0].Value
print "link group: " + matches[0].Groups["link"].Value
#We don't care if there is more than one result. As long as the first result is the correct image
result, link_uri = Uri.TryCreate(first_page_uri, matches[0].Groups["link"].Value)
#Valid url and matches the input image url
if result and link_uri.AbsoluteUri in (second_page_uri.AbsoluteUri, escaped_second_page_uri.AbsoluteUri):
if debug: print "Valid uri and matches the second page url"
else:
if debug: print "Invalid uri or doesn't match the second page url"
return False, 0
matches_second = Regex.Matches(second_page_source._source, regex, RegexOptions.IgnoreCase)
if matches_second.Count == 0:
if debug: print "\nNo matches on the second page"
return False, 0
#Regex match on the second page. Same deal as above. We don't care if there is more than one result
if debug:
print "\nFound " + str(matches_second.Count) + " match(es) on the second page"
print "Captured: " + matches_second[0].Value
print "link group: " + matches_second[0].Groups["link"].Value
result, link_uri = Uri.TryCreate(second_page_uri, matches_second[0].Groups["link"].Value)
if result:
if debug: print "Regex works on both pages"
return True, matches.Count
else:
if debug: print "Invalid uri"
return False, matches.Count
def get_string_intersect(string1, string2):
#There is probably an easier way to do this but I can't find one at 2 in the morning :p
s1 = list(string1)
s2 = list(string2)
r = []
for i in range(0, len(s1)):
if s1[i] == s2[i]:
r.append(s1[i])
else:
break
#Because the base needs to end in /
r.reverse()
for i in r[:]:
if i != "/":
r.remove(i)
else:
break
r.reverse()
return "".join(r)
class PageSource(object):
"""PageSource is a class containing methods to get the page source from a website"""
def __init__(self):
self._source = ""
self._exception = None
def get_source(self, URI):
"""URI should be a valid URI instance"""
success = True
try:
request = HttpWebRequest.Create(URI)
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
except (ArgumentNullException, System.Security.SecurityException), ex:
self._exception = ex
return False
try:
responsestream = request.GetResponse()
except (System.Security.SecurityException, WebException), ex:
self._exception = ex
return False
try:
streamreader = StreamReader(responsestream.GetResponseStream())
self._source = streamreader.ReadToEnd()
except (ArgumentException, ProtocolViolationException), ex:
self._exception = ex
success = False
finally:
responsestream.Close()
streamreader.Close()
return success