/
core.py
1071 lines (999 loc) · 34.6 KB
/
core.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint:disable-msg=E0611,I1101
"""Module bundling all functions needed to determine the date of HTML strings
or LXML trees.
"""
## This file is available from https://github.com/adbar/htmldate
## under GNU GPL v3 license
# standard
import logging
import re
from collections import Counter
from copy import deepcopy
from datetime import datetime
from functools import lru_cache, partial
from typing import Match, Optional, Pattern, Tuple, Counter as Counter_Type
from lxml.html import HtmlElement, tostring # type: ignore
# own
from .extractors import (
discard_unwanted,
extract_url_date,
extract_partial_url_date,
idiosyncrasies_search,
img_search,
json_search,
timestamp_search,
try_date_expr,
DATE_EXPRESSIONS,
FAST_PREPEND,
SLOW_PREPEND,
FREE_TEXT_EXPRESSIONS,
MAX_TEXT_SIZE,
YEAR_PATTERN,
YMD_PATTERN,
COPYRIGHT_PATTERN,
THREE_PATTERN,
THREE_CATCH,
THREE_LOOSE_PATTERN,
THREE_LOOSE_CATCH,
SELECT_YMD_PATTERN,
SELECT_YMD_YEAR,
YMD_YEAR,
DATESTRINGS_PATTERN,
DATESTRINGS_CATCH,
SLASHES_PATTERN,
SLASHES_YEAR,
YYYYMM_PATTERN,
YYYYMM_CATCH,
MMYYYY_PATTERN,
MMYYYY_YEAR,
SIMPLE_PATTERN,
THREE_COMP_REGEX_A,
THREE_COMP_REGEX_B,
TWO_COMP_REGEX,
)
from .settings import CACHE_SIZE, HTML_CLEANER, MAX_POSSIBLE_CANDIDATES
from .utils import load_html
from .validators import (
check_extracted_reference,
compare_values,
convert_date,
date_validator,
filter_ymd_candidate,
get_min_date,
get_max_date,
output_format_validator,
plausible_year_filter,
)
LOGGER = logging.getLogger(__name__)
def logstring(element: HtmlElement) -> str:
"""Format the element to be logged to a string."""
return tostring(element, pretty_print=False, encoding="unicode").strip() # type: ignore
DATE_ATTRIBUTES = {
"article.created",
"article_date_original",
"article.published",
"article:published_time",
"article:publicationdate",
"bt:pubdate",
"citation_date",
"citation_publication_date",
"created",
"cxenseparse:recs:publishtime",
"date",
"date_published",
"datecreated",
"dateposted",
"datepublished",
# Dublin Core: https://wiki.whatwg.org/wiki/MetaExtensions
"dc.date",
"dc.created",
"dc.date.created",
"dc.date.issued",
"dc.date.publication",
"dcterms.created",
"dcterms.date",
"dcterms.issued",
"dc:created",
"dc:date",
"gentime",
# Open Graph: https://opengraphprotocol.org/
"og:published_time",
"og:article:published_time",
"originalpublicationdate",
"parsely-pub-date",
"pubdate",
"publishdate",
"publish_date",
"published-date",
"publication_date",
"rnews:datepublished",
"sailthru.date",
"shareaholic:article_published_time",
"timestamp",
}
PROPERTY_MODIFIED = {
"article:modified_time",
"datemodified",
"dc.modified",
"dcterms.modified",
"modified_time",
"og:article:modified_time",
"og:updated_time",
"release_date",
"updated_time",
}
NON_DIGITS_REGEX = re.compile(r"\D+$")
GER_STRIP_REGEX = re.compile(r"^am ")
LAST_MODIFIED = {"lastmodified", "last-modified"}
ITEMPROP_ATTRS_ORIGINAL = {"datecreated", "datepublished", "pubyear"}
ITEMPROP_ATTRS_MODIFIED = {"datemodified", "dateupdate"}
ITEMPROP_ATTRS = ITEMPROP_ATTRS_ORIGINAL.union(ITEMPROP_ATTRS_MODIFIED)
CLASS_ATTRS = {"date-published", "published", "time published"}
def examine_date_elements(
tree: HtmlElement,
expression: str,
outputformat: str,
extensive_search: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[str]:
"""Check HTML elements one by one for date expressions"""
elements = tree.xpath(expression)
if not elements or len(elements) > MAX_POSSIBLE_CANDIDATES:
return None
# loop through the elements to analyze
attempt = None
for elem in elements:
# trim
text = " ".join(elem.text_content().split()).strip()
# simple length heuristic
if len(text) > 6: # could be 8 or 9
# shorten and try the beginning of the string
# trim non-digits at the end of the string
toexamine = NON_DIGITS_REGEX.sub("", text[:MAX_TEXT_SIZE])
LOGGER.debug(
"analyzing (HTML): %s",
" ".join(
tostring(elem, pretty_print=False, encoding="unicode").split()
).strip()[:100],
)
attempt = try_date_expr(
toexamine, outputformat, extensive_search, min_date, max_date
)
if attempt is not None:
break
# try link title (Blogspot)
title_attr = elem.get("title")
if title_attr is not None and len(title_attr) > 0:
toexamine = NON_DIGITS_REGEX.sub("", title_attr[:MAX_TEXT_SIZE])
attempt = try_date_expr(
toexamine, outputformat, extensive_search, min_date, max_date
)
if attempt is not None:
break
# catchall
return attempt
def examine_header(
tree: HtmlElement,
outputformat: str,
extensive_search: bool,
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[str]:
"""
Parse header elements to find date cues
:param tree:
LXML parsed tree object
:type htmlstring: LXML tree
:param outputformat:
Provide a valid datetime format for the returned string
(see datetime.strftime())
:type outputformat: string
:param extensive_search:
Activate pattern-based opportunistic text search
:type extensive_search: boolean
:param original_date:
Look for original date (e.g. publication date) instead of most recent
one (e.g. last modified, updated time)
:type original_date: boolean
:param min_date:
Set the earliest acceptable date manually (YYYY-MM-DD format)
:type min_date: string
:param max_date:
Set the latest acceptable date manually (YYYY-MM-DD format)
:type max_date: string
:return: Returns a valid date expression as a string, or None
"""
headerdate, reserve = None, None
tryfunc = partial(
try_date_expr,
outputformat=outputformat,
extensive_search=extensive_search,
min_date=min_date,
max_date=max_date,
)
# loop through all meta elements
for elem in tree.iterfind(".//meta"):
# safeguard
if not elem.attrib: # len(elem.attrib) < 1:
continue
# property attribute
if "property" in elem.attrib and "content" in elem.attrib:
attribute = elem.get("property").lower()
# original date or modified date: override published_time
if (
original_date
and attribute in DATE_ATTRIBUTES
or original_date is not True
and (attribute in PROPERTY_MODIFIED or attribute in DATE_ATTRIBUTES)
):
LOGGER.debug("examining meta property: %s", logstring(elem))
headerdate = tryfunc(elem.get("content"))
elif "name" in elem.attrib and "content" in elem.attrib:
# url
if elem.get("name").lower() == "og:url":
headerdate = extract_url_date(
elem.get("content"), outputformat, min_date, max_date
)
# date
elif elem.get("name").lower() in DATE_ATTRIBUTES:
LOGGER.debug("examining meta name: %s", logstring(elem))
headerdate = tryfunc(elem.get("content"))
# modified
elif elem.get("name").lower() in LAST_MODIFIED:
LOGGER.debug("examining meta name: %s", logstring(elem))
if original_date is False:
headerdate = tryfunc(elem.get("content"))
else:
reserve = tryfunc(elem.get("content"))
elif "pubdate" in elem.attrib:
if elem.get("pubdate").lower() == "pubdate":
LOGGER.debug("examining meta pubdate: %s", logstring(elem))
headerdate = tryfunc(elem.get("content"))
elif "itemprop" in elem.attrib:
attribute = elem.get("itemprop").lower()
# original: store / updated: override date
if attribute in ITEMPROP_ATTRS:
attempt = None
LOGGER.debug("examining meta itemprop: %s", logstring(elem))
if "datetime" in elem.attrib:
attempt = tryfunc(elem.get("datetime"))
elif "content" in elem.attrib:
attempt = tryfunc(elem.get("content"))
# store value
if attempt is not None:
if (attribute in ITEMPROP_ATTRS_ORIGINAL and original_date) or (
attribute in ITEMPROP_ATTRS_MODIFIED and not original_date
):
headerdate = attempt
# put on hold: hurts precision
# else:
# reserve = attempt
# reserve with copyrightyear
elif attribute == "copyrightyear":
LOGGER.debug("examining meta itemprop: %s", logstring(elem))
if "content" in elem.attrib:
attempt = "-".join([elem.get("content"), "01", "01"])
if (
date_validator(
attempt, "%Y-%m-%d", earliest=min_date, latest=max_date
)
is True
):
reserve = attempt
# http-equiv, rare
elif "http-equiv" in elem.attrib:
attribute = elem.get("http-equiv").lower()
if attribute == "date":
LOGGER.debug("examining meta http-equiv: %s", logstring(elem))
if original_date:
headerdate = tryfunc(elem.get("content"))
else:
reserve = tryfunc(elem.get("content"))
elif attribute == "last-modified":
LOGGER.debug("examining meta http-equiv: %s", logstring(elem))
if original_date is False:
headerdate = tryfunc(elem.get("content"))
else:
reserve = tryfunc(elem.get("content"))
# exit loop
if headerdate is not None:
break
# if nothing was found, look for lower granularity (so far: "copyright year")
if headerdate is None and reserve is not None:
LOGGER.debug("opting for reserve date with less granularity")
headerdate = reserve
# return value
return headerdate
def select_candidate(
occurrences: Counter_Type[str],
catch: Pattern[str],
yearpat: Pattern[str],
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[Match[str]]:
"""Select a candidate among the most frequent matches"""
match = None
# LOGGER.debug('occurrences: %s', occurrences)
if len(occurrences) == 0 or len(occurrences) > MAX_POSSIBLE_CANDIDATES:
return None
if len(occurrences) == 1:
match = catch.search(list(occurrences.keys())[0])
if match:
return match
# select among most frequent
firstselect = occurrences.most_common(10)
LOGGER.debug("firstselect: %s", firstselect)
# sort and find probable candidates
if original_date:
bestones = sorted(firstselect)[:2]
else:
bestones = sorted(firstselect, reverse=True)[:2]
first_pattern, first_count = bestones[0][0], bestones[0][1]
second_pattern, second_count = bestones[1][0], bestones[1][1]
LOGGER.debug("bestones: %s", bestones)
# plausibility heuristics
validation1, validation2 = False, False
match1 = yearpat.search(first_pattern)
if match1 is not None:
year1 = match1[1]
validation1 = date_validator(year1, "%Y", earliest=min_date, latest=max_date)
match2 = yearpat.search(second_pattern)
if match2 is not None:
year2 = match2[1]
validation2 = date_validator(year2, "%Y", earliest=min_date, latest=max_date)
# safety net: plausibility
if validation1 is True and validation2 is True:
# same number of occurrences: always take top of the pile?
if first_count == second_count:
match = catch.search(first_pattern)
# safety net: newer date but up to 50% less frequent
elif year2 != year1 and second_count / first_count > 0.5:
match = catch.search(second_pattern)
# not newer or hopefully not significant
else:
match = catch.search(first_pattern)
elif validation1 is False and validation2 is True:
match = catch.search(second_pattern)
elif validation1 is True and validation2 is False:
match = catch.search(first_pattern)
else:
LOGGER.debug("no suitable candidate: %s %s", year1, year2)
return None
return match
def search_pattern(
htmlstring: str,
pattern: Pattern[str],
catch: Pattern[str],
yearpat: Pattern[str],
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[Match[str]]:
"""Chained candidate filtering and selection"""
candidates = plausible_year_filter(
htmlstring, pattern=pattern, yearpat=yearpat, earliest=min_date, latest=max_date
)
return select_candidate(
candidates, catch, yearpat, original_date, min_date, max_date
)
@lru_cache(maxsize=CACHE_SIZE)
def compare_reference(
reference: int,
expression: str,
outputformat: str,
extensive_search: bool,
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> int:
"""Compare candidate to current date reference (includes date validation and older/newer test)"""
attempt = try_date_expr(
expression, outputformat, extensive_search, min_date, max_date
)
if attempt is not None:
return compare_values(reference, attempt, outputformat, original_date)
return reference
def examine_abbr_elements(
tree: HtmlElement,
outputformat: str,
extensive_search: bool,
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[str]:
"""Scan the page for abbr elements and check if their content contains an eligible date"""
elements = tree.findall(".//abbr")
if elements is not None and len(elements) < MAX_POSSIBLE_CANDIDATES:
reference = 0
for elem in elements:
# data-utime (mostly Facebook)
if "data-utime" in elem.attrib:
try:
candidate = int(elem.get("data-utime"))
except ValueError:
continue
LOGGER.debug("data-utime found: %s", candidate)
# look for original date
if original_date and (reference == 0 or candidate < reference):
reference = candidate
# look for newest (i.e. largest time delta)
elif not original_date and candidate > reference:
reference = candidate
# class
if "class" in elem.attrib and elem.get("class") in CLASS_ATTRS:
# other attributes
if "title" in elem.attrib:
trytext = elem.get("title")
LOGGER.debug("abbr published-title found: %s", trytext)
# shortcut
if original_date:
attempt = try_date_expr(
trytext, outputformat, extensive_search, min_date, max_date
)
if attempt is not None:
return attempt
else:
reference = compare_reference(
reference,
trytext,
outputformat,
extensive_search,
original_date,
min_date,
max_date,
)
# faster execution
if reference > 0:
break
# dates, not times of the day
if elem.text and len(elem.text) > 10:
trytext = GER_STRIP_REGEX.sub("", elem.text)
LOGGER.debug("abbr published found: %s", trytext)
reference = compare_reference(
reference,
trytext,
outputformat,
extensive_search,
original_date,
min_date,
max_date,
)
# convert and return
converted = check_extracted_reference(
reference, outputformat, min_date, max_date
)
if converted is not None:
return converted
# try rescue in abbr content
dateresult = examine_date_elements(
tree, ".//abbr", outputformat, extensive_search, min_date, max_date
)
if dateresult is not None:
return dateresult
return None
def examine_time_elements(
tree: HtmlElement,
outputformat: str,
extensive_search: bool,
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[str]:
"""Scan the page for time elements and check if their content contains an eligible date"""
elements = tree.findall(".//time")
if elements is not None and len(elements) < MAX_POSSIBLE_CANDIDATES:
# scan all the tags and look for the newest one
reference = 0
for elem in elements:
shortcut_flag = False
# go for datetime
if "datetime" in elem.attrib and len(elem.get("datetime")) > 6:
# shortcut: time pubdate
if "pubdate" in elem.attrib and elem.get("pubdate") == "pubdate":
if original_date:
shortcut_flag = True
LOGGER.debug("time pubdate found: %s", elem.get("datetime"))
# first choice: entry-date + datetime attribute
elif "class" in elem.attrib:
if elem.get("class").startswith("entry-date") or elem.get(
"class"
).startswith("entry-time"):
# shortcut
if original_date:
shortcut_flag = True
LOGGER.debug("time/datetime found: %s", elem.get("datetime"))
# updated time
elif elem.get("class") == "updated" and not original_date:
LOGGER.debug(
"updated time/datetime found: %s", elem.get("datetime")
)
# datetime attribute
else:
LOGGER.debug("time/datetime found: %s", elem.get("datetime"))
# analyze attribute
if shortcut_flag:
attempt = try_date_expr(
elem.get("datetime"),
outputformat,
extensive_search,
min_date,
max_date,
)
if attempt is not None:
return attempt
else:
reference = compare_reference(
reference,
elem.get("datetime"),
outputformat,
extensive_search,
original_date,
min_date,
max_date,
)
if reference > 0:
break
# bare text in element
elif elem.text is not None and len(elem.text) > 6:
LOGGER.debug("time/datetime found: %s", elem.text)
reference = compare_reference(
reference,
elem.text,
outputformat,
extensive_search,
original_date,
min_date,
max_date,
)
# else...?
# return
converted = check_extracted_reference(
reference, outputformat, min_date, max_date
)
if converted is not None:
return converted
return None
def normalize_match(match: Optional[Match[str]]) -> Tuple[str, str]:
"""Normalize string output by adding "0" if necessary."""
day = match[1] # type: ignore[index]
if len(day) == 1:
day = "0" + day
month = match[2] # type: ignore[index]
if len(month) == 1:
month = "0" + month
return day, month
def search_page(
htmlstring: str,
outputformat: str,
original_date: bool,
min_date: datetime,
max_date: datetime,
) -> Optional[str]:
"""
Opportunistically search the HTML text for common text patterns
:param htmlstring:
The HTML document in string format, potentially cleaned and stripped to
the core (much faster)
:type htmlstring: string
:param outputformat:
Provide a valid datetime format for the returned string
(see datetime.strftime())
:type outputformat: string
:param original_date:
Look for original date (e.g. publication date) instead of most recent
one (e.g. last modified, updated time)
:type original_date: boolean
:return: Returns a valid date expression as a string, or None
"""
# copyright symbol
LOGGER.debug("looking for copyright/footer information")
copyear = 0
bestmatch = search_pattern(
htmlstring,
COPYRIGHT_PATTERN,
YEAR_PATTERN,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
if bestmatch is not None:
LOGGER.debug("Copyright detected: %s", bestmatch[0])
if (
date_validator(bestmatch[0], "%Y", earliest=min_date, latest=max_date)
is True
):
LOGGER.debug("copyright year/footer pattern found: %s", bestmatch[0])
copyear = int(bestmatch[0])
# 3 components
LOGGER.debug("3 components")
# target URL characteristics
bestmatch = search_pattern(
htmlstring,
THREE_PATTERN,
THREE_CATCH,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
result = filter_ymd_candidate(
bestmatch,
THREE_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# more loosely structured data
bestmatch = search_pattern(
htmlstring,
THREE_LOOSE_PATTERN,
THREE_LOOSE_CATCH,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
result = filter_ymd_candidate(
bestmatch,
THREE_LOOSE_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# YYYY-MM-DD/DD-MM-YYYY
candidates = plausible_year_filter(
htmlstring,
pattern=SELECT_YMD_PATTERN,
yearpat=SELECT_YMD_YEAR,
earliest=min_date,
latest=max_date,
)
# revert DD-MM-YYYY patterns before sorting
replacement = {}
for item in candidates:
match = THREE_COMP_REGEX_A.match(item)
day, month = normalize_match(match)
candidate = "-".join([match[3], month, day]) # type: ignore[index]
replacement[candidate] = candidates[item]
candidates = Counter(replacement)
# select
bestmatch = select_candidate(
candidates, YMD_PATTERN, YMD_YEAR, original_date, min_date, max_date
)
result = filter_ymd_candidate(
bestmatch,
SELECT_YMD_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# valid dates strings
bestmatch = search_pattern(
htmlstring,
DATESTRINGS_PATTERN,
DATESTRINGS_CATCH,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
result = filter_ymd_candidate(
bestmatch,
DATESTRINGS_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# DD?/MM?/YY
candidates = plausible_year_filter(
htmlstring,
pattern=SLASHES_PATTERN,
yearpat=SLASHES_YEAR,
earliest=min_date,
latest=max_date,
tocomplete=True,
)
# revert DD-MM-YYYY patterns before sorting
replacement = {}
for item in candidates:
match = THREE_COMP_REGEX_B.match(item)
day, month = normalize_match(match)
if match[3][0] == "9": # type: ignore[index]
year = "19" + match[3] # type: ignore[index]
else:
year = "20" + match[3] # type: ignore[index]
candidate = "-".join([year, month, day])
replacement[candidate] = candidates[item]
candidates = Counter(replacement)
bestmatch = select_candidate(
candidates, YMD_PATTERN, YMD_YEAR, original_date, min_date, max_date
)
result = filter_ymd_candidate(
bestmatch,
SLASHES_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# 2 components
LOGGER.debug("switching to two components")
# first option
bestmatch = search_pattern(
htmlstring,
YYYYMM_PATTERN,
YYYYMM_CATCH,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
if bestmatch is not None:
pagedate = "-".join([bestmatch[1], bestmatch[2], "01"])
if date_validator(
pagedate, "%Y-%m-%d", earliest=min_date, latest=max_date
) is True and (copyear == 0 or int(bestmatch[1]) >= copyear):
LOGGER.debug('date found for pattern "%s": %s', YYYYMM_PATTERN, pagedate)
return convert_date(pagedate, "%Y-%m-%d", outputformat)
# 2 components, second option
candidates = plausible_year_filter(
htmlstring,
pattern=MMYYYY_PATTERN,
yearpat=MMYYYY_YEAR,
earliest=min_date,
latest=max_date,
tocomplete=original_date,
)
# revert DD-MM-YYYY patterns before sorting
replacement = {}
for item in candidates:
match = TWO_COMP_REGEX.match(item)
month = match[1] # type: ignore[index]
if len(month) == 1:
month = "0" + month
candidate = "-".join([match[2], month, "01"]) # type: ignore[index]
replacement[candidate] = candidates[item]
candidates = Counter(replacement)
# select
bestmatch = select_candidate(
candidates, YMD_PATTERN, YMD_YEAR, original_date, min_date, max_date
)
result = filter_ymd_candidate(
bestmatch,
MMYYYY_PATTERN,
original_date,
copyear,
outputformat,
min_date,
max_date,
)
if result is not None:
return result
# catchall
if copyear != 0:
LOGGER.debug("using copyright year as default")
return convert_date(
"-".join([str(copyear), "01", "01"]), "%Y-%m-%d", outputformat
)
# 1 component, last try
LOGGER.debug("switching to one component")
bestmatch = search_pattern(
htmlstring,
SIMPLE_PATTERN,
YEAR_PATTERN,
YEAR_PATTERN,
original_date,
min_date,
max_date,
)
if bestmatch is not None:
pagedate = "-".join([bestmatch[0], "01", "01"])
if (
date_validator(pagedate, "%Y-%m-%d", earliest=min_date, latest=max_date)
is True
and int(bestmatch[0]) >= copyear
):
LOGGER.debug('date found for pattern "%s": %s', SIMPLE_PATTERN, pagedate)
return convert_date(pagedate, "%Y-%m-%d", outputformat)
return None
def find_date(
htmlobject: HtmlElement,
extensive_search: bool = True,
original_date: bool = False,
outputformat: str = "%Y-%m-%d",
url: Optional[str] = None,
verbose: bool = False,
min_date: Optional[datetime] = None,
max_date: Optional[datetime] = None,
) -> Optional[str]:
"""
Extract dates from HTML documents using markup analysis and text patterns
:param htmlobject:
Two possibilities: 1. HTML document (e.g. body of HTTP request or .html-file) in text string
form or LXML parsed tree or 2. URL string (gets detected automatically)
:type htmlobject: string or lxml tree
:param extensive_search:
Activate pattern-based opportunistic text search
:type extensive_search: boolean
:param original_date:
Look for original date (e.g. publication date) instead of most recent
one (e.g. last modified, updated time)
:type original_date: boolean
:param outputformat:
Provide a valid datetime format for the returned string
(see datetime.strftime())
:type outputformat: string
:param url:
Provide an URL manually for pattern-searching in URL
(in some cases much faster)
:type url: string
:param verbose:
Set verbosity level for debugging
:type verbose: boolean
:param min_date:
Set the earliest acceptable date manually (YYYY-MM-DD format)
:type min_date: string
:param max_date:
Set the latest acceptable date manually (YYYY-MM-DD format)
:type max_date: string
:return: Returns a valid date expression as a string, or None
"""
# init
if verbose:
logging.basicConfig(level=logging.DEBUG)
tree = load_html(htmlobject)
# unclear what this line is for and it impedes type checking:
# find_date.extensive_search = extensive_search
min_date, max_date = get_min_date(min_date), get_max_date(max_date)
# safety
if tree is None:
return None
if outputformat != "%Y-%m-%d" and not output_format_validator(outputformat):
return None
# URL
if url is None:
# link canonical
for elem in tree.iterfind('.//link[@rel="canonical"]'):
url = elem.get("href")
if url is not None:
break
if url is not None:
dateresult = extract_url_date(url, outputformat, min_date, max_date)
if dateresult is not None:
return dateresult
# first, try header
header_result = examine_header(
tree, outputformat, extensive_search, original_date, min_date, max_date
)
if header_result is not None:
return header_result
# try to use JSON data
json_result = json_search(tree, outputformat, original_date, min_date, max_date)
if json_result is not None:
return json_result
# try abbr elements
abbr_result = examine_abbr_elements(
tree, outputformat, extensive_search, original_date, min_date, max_date
)
if abbr_result is not None:
return abbr_result
# expressions + text_content
if extensive_search:
date_expr = SLOW_PREPEND + DATE_EXPRESSIONS
else:
date_expr = FAST_PREPEND + DATE_EXPRESSIONS
# first try in pruned tree
search_tree, discarded = discard_unwanted(deepcopy(tree))
dateresult = examine_date_elements(
search_tree, date_expr, outputformat, extensive_search, min_date, max_date
)
if dateresult is not None:
return dateresult
# TODO: decide on this
# search in discarded parts (e.g. archive.org-banner)
# for subtree in discarded:
# dateresult = examine_date_elements(subtree, DATE_EXPRESSIONS,
# outputformat, extensive_search, min_date, max_date)
# if dateresult is not None:
# return dateresult
# try time elements
time_result = examine_time_elements(
search_tree, outputformat, extensive_search, original_date, min_date, max_date
)
if time_result is not None:
return time_result
# clean before string search
try:
cleaned_html = HTML_CLEANER.clean_html(tree)
# rare LXML error: no NULL bytes or control characters
except ValueError:
cleaned_html = tree
LOGGER.error("lxml cleaner error")
# robust conversion to string