proof-citations 1.33.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- proof_citations/__init__.py +10 -0
- proof_citations/cli.py +67 -0
- proof_citations/data/academic_domains.json +97 -0
- proof_citations/data/government_tlds.json +114 -0
- proof_citations/data/major_news.json +69 -0
- proof_citations/data/reference_domains.json +44 -0
- proof_citations/data/unreliable_domains.json +54 -0
- proof_citations/fetch.py +214 -0
- proof_citations/latex_text.py +85 -0
- proof_citations/normalize.py +174 -0
- proof_citations/oa_lookup.py +81 -0
- proof_citations/source_credibility.py +389 -0
- proof_citations/verify.py +1072 -0
- proof_citations-1.33.0.dist-info/METADATA +76 -0
- proof_citations-1.33.0.dist-info/RECORD +19 -0
- proof_citations-1.33.0.dist-info/WHEEL +5 -0
- proof_citations-1.33.0.dist-info/entry_points.txt +2 -0
- proof_citations-1.33.0.dist-info/licenses/LICENSE +21 -0
- proof_citations-1.33.0.dist-info/top_level.txt +1 -0
proof_citations/cli.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Command-line entry point for proof-citations.
|
|
2
|
+
|
|
3
|
+
proof-citations verify --url URL --quote "QUOTE" --fact-id B1
|
|
4
|
+
proof-citations verify --facts facts.json
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import json
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
from proof_citations import verify_citation, verify_all_citations
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _cmd_verify(args) -> int:
|
|
15
|
+
if args.facts:
|
|
16
|
+
with open(args.facts) as f:
|
|
17
|
+
facts = json.load(f)
|
|
18
|
+
results = verify_all_citations(facts)
|
|
19
|
+
# verify_all_citations returns a dict (per verify_citations.py).
|
|
20
|
+
# Emit as-is, and compute exit from statuses.
|
|
21
|
+
sys.stdout.write(
|
|
22
|
+
json.dumps(results, indent=2 if args.pretty else None) + "\n"
|
|
23
|
+
)
|
|
24
|
+
statuses = [r.get("status") for r in results.values()] \
|
|
25
|
+
if isinstance(results, dict) else []
|
|
26
|
+
return 0 if statuses and all(s == "verified" for s in statuses) else 1
|
|
27
|
+
|
|
28
|
+
if not args.url or not args.quote:
|
|
29
|
+
sys.stderr.write("error: --url and --quote are required (or --facts)\n")
|
|
30
|
+
return 2
|
|
31
|
+
|
|
32
|
+
result = verify_citation(args.url, args.quote, args.fact_id)
|
|
33
|
+
if args.json:
|
|
34
|
+
sys.stdout.write(
|
|
35
|
+
json.dumps(result, indent=2 if args.pretty else None, default=str)
|
|
36
|
+
+ "\n"
|
|
37
|
+
)
|
|
38
|
+
else:
|
|
39
|
+
sys.stdout.write(f"{result.get('status')}: {args.url}\n")
|
|
40
|
+
return 0 if result.get("status") == "verified" else 1
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
44
|
+
parser = argparse.ArgumentParser(prog="proof-citations")
|
|
45
|
+
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
46
|
+
|
|
47
|
+
verify = sub.add_parser("verify", help="Verify a quote appears at a URL.")
|
|
48
|
+
verify.add_argument("--url")
|
|
49
|
+
verify.add_argument("--quote")
|
|
50
|
+
verify.add_argument("--fact-id", default="B1",
|
|
51
|
+
help="Identifier used in messages (default: B1).")
|
|
52
|
+
verify.add_argument("--facts", help="Path to JSON file of facts to verify.")
|
|
53
|
+
verify.add_argument("--json", action="store_true", help="Emit JSON.")
|
|
54
|
+
verify.add_argument("--pretty", action="store_true", help="Pretty-print JSON.")
|
|
55
|
+
verify.set_defaults(func=_cmd_verify)
|
|
56
|
+
|
|
57
|
+
return parser
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def main(argv=None) -> int:
|
|
61
|
+
parser = build_parser()
|
|
62
|
+
args = parser.parse_args(argv)
|
|
63
|
+
return args.func(args)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Academic/research TLDs and known scholarly publisher domains. Tier 4 (peer-reviewed/institutional).",
|
|
3
|
+
"tld_suffixes": [
|
|
4
|
+
".edu",
|
|
5
|
+
".edu.au",
|
|
6
|
+
".edu.br",
|
|
7
|
+
".edu.cn",
|
|
8
|
+
".edu.hk",
|
|
9
|
+
".edu.sg",
|
|
10
|
+
".edu.tw",
|
|
11
|
+
".edu.mx",
|
|
12
|
+
".edu.ar",
|
|
13
|
+
".edu.co",
|
|
14
|
+
".edu.pe",
|
|
15
|
+
".ac.uk",
|
|
16
|
+
".ac.jp",
|
|
17
|
+
".ac.kr",
|
|
18
|
+
".ac.il",
|
|
19
|
+
".ac.in",
|
|
20
|
+
".ac.nz",
|
|
21
|
+
".ac.za",
|
|
22
|
+
".ac.id",
|
|
23
|
+
".ac.th",
|
|
24
|
+
".uni.edu",
|
|
25
|
+
".edu.pl",
|
|
26
|
+
".edu.tr"
|
|
27
|
+
],
|
|
28
|
+
"known_domains": [
|
|
29
|
+
"nature.com",
|
|
30
|
+
"science.org",
|
|
31
|
+
"sciencemag.org",
|
|
32
|
+
"thelancet.com",
|
|
33
|
+
"bmj.com",
|
|
34
|
+
"nejm.org",
|
|
35
|
+
"cell.com",
|
|
36
|
+
"pnas.org",
|
|
37
|
+
"plos.org",
|
|
38
|
+
"plosone.org",
|
|
39
|
+
"plosmedicine.org",
|
|
40
|
+
"springer.com",
|
|
41
|
+
"link.springer.com",
|
|
42
|
+
"wiley.com",
|
|
43
|
+
"onlinelibrary.wiley.com",
|
|
44
|
+
"sciencedirect.com",
|
|
45
|
+
"elsevier.com",
|
|
46
|
+
"tandfonline.com",
|
|
47
|
+
"taylorandfrancis.com",
|
|
48
|
+
"oxfordacademic.com",
|
|
49
|
+
"academic.oup.com",
|
|
50
|
+
"cambridge.org",
|
|
51
|
+
"journals.sagepub.com",
|
|
52
|
+
"sagepub.com",
|
|
53
|
+
"jstor.org",
|
|
54
|
+
"arxiv.org",
|
|
55
|
+
"biorxiv.org",
|
|
56
|
+
"medrxiv.org",
|
|
57
|
+
"ssrn.com",
|
|
58
|
+
"pubmed.ncbi.nlm.nih.gov",
|
|
59
|
+
"ncbi.nlm.nih.gov",
|
|
60
|
+
"scholar.google.com",
|
|
61
|
+
"semanticscholar.org",
|
|
62
|
+
"researchgate.net",
|
|
63
|
+
"doi.org",
|
|
64
|
+
"crossref.org",
|
|
65
|
+
"cochranelibrary.com",
|
|
66
|
+
"annualreviews.org",
|
|
67
|
+
"frontiersin.org",
|
|
68
|
+
"mdpi.com",
|
|
69
|
+
"ieee.org",
|
|
70
|
+
"ieeexplore.ieee.org",
|
|
71
|
+
"acm.org",
|
|
72
|
+
"dl.acm.org",
|
|
73
|
+
"aps.org",
|
|
74
|
+
"aaas.org",
|
|
75
|
+
"royalsocietypublishing.org",
|
|
76
|
+
"worldscientific.com",
|
|
77
|
+
"degruyter.com",
|
|
78
|
+
"karger.com",
|
|
79
|
+
"biomedcentral.com",
|
|
80
|
+
"iopscience.iop.org",
|
|
81
|
+
"aanda.org",
|
|
82
|
+
"journals.aps.org",
|
|
83
|
+
"physrev.org",
|
|
84
|
+
"aip.org",
|
|
85
|
+
"scitation.org",
|
|
86
|
+
"projecteuclid.org",
|
|
87
|
+
"msp.org",
|
|
88
|
+
"ams.org",
|
|
89
|
+
"zbmath.org"
|
|
90
|
+
],
|
|
91
|
+
"doi_prefixes": [
|
|
92
|
+
"https://doi.org/",
|
|
93
|
+
"http://doi.org/",
|
|
94
|
+
"https://dx.doi.org/",
|
|
95
|
+
"http://dx.doi.org/"
|
|
96
|
+
]
|
|
97
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Government and intergovernmental TLDs and domain suffixes. Tier 5 (primary official source).",
|
|
3
|
+
"_comment_un_agencies": "UN subsidiary organs and specialized agencies. un.int excluded — subdomain matching would promote all *.un.int hosts (including member-state mission sites) to tier 5.",
|
|
4
|
+
"tld_suffixes": [
|
|
5
|
+
".gov",
|
|
6
|
+
".gov.uk",
|
|
7
|
+
".gov.au",
|
|
8
|
+
".gov.ca",
|
|
9
|
+
".gov.nz",
|
|
10
|
+
".gov.za",
|
|
11
|
+
".gov.in",
|
|
12
|
+
".gov.sg",
|
|
13
|
+
".gov.ie",
|
|
14
|
+
".gov.il",
|
|
15
|
+
".gc.ca",
|
|
16
|
+
".gouv.fr",
|
|
17
|
+
".gouv.qc.ca",
|
|
18
|
+
".gob.mx",
|
|
19
|
+
".gob.es",
|
|
20
|
+
".gob.ar",
|
|
21
|
+
".gob.cl",
|
|
22
|
+
".gob.pe",
|
|
23
|
+
".gobierno.ar",
|
|
24
|
+
".go.jp",
|
|
25
|
+
".go.kr",
|
|
26
|
+
".go.id",
|
|
27
|
+
".go.th",
|
|
28
|
+
".gov.br",
|
|
29
|
+
".gov.cn",
|
|
30
|
+
".gov.tw",
|
|
31
|
+
".gov.ph",
|
|
32
|
+
".gov.my",
|
|
33
|
+
".gov.eg",
|
|
34
|
+
".gov.ng",
|
|
35
|
+
".gov.gh",
|
|
36
|
+
".gov.ke",
|
|
37
|
+
".govt.nz",
|
|
38
|
+
".bund.de",
|
|
39
|
+
".admin.ch",
|
|
40
|
+
".overheid.nl",
|
|
41
|
+
".regeringen.se",
|
|
42
|
+
".mil",
|
|
43
|
+
".mil.br"
|
|
44
|
+
],
|
|
45
|
+
"known_domains": [
|
|
46
|
+
"europa.eu",
|
|
47
|
+
"un.org",
|
|
48
|
+
"who.int",
|
|
49
|
+
"worldbank.org",
|
|
50
|
+
"imf.org",
|
|
51
|
+
"oecd.org",
|
|
52
|
+
"nato.int",
|
|
53
|
+
"wto.org",
|
|
54
|
+
"iaea.org",
|
|
55
|
+
"unicef.org",
|
|
56
|
+
"unhcr.org",
|
|
57
|
+
"icj-cij.org",
|
|
58
|
+
"icc-cpi.int",
|
|
59
|
+
"ilo.org",
|
|
60
|
+
"fao.org",
|
|
61
|
+
"wipo.int",
|
|
62
|
+
"itu.int",
|
|
63
|
+
"unep.org",
|
|
64
|
+
"undp.org",
|
|
65
|
+
"unfccc.int",
|
|
66
|
+
"ipcc.ch",
|
|
67
|
+
"data.worldbank.org",
|
|
68
|
+
"data.un.org",
|
|
69
|
+
"stats.oecd.org",
|
|
70
|
+
"federalreserve.gov",
|
|
71
|
+
"bls.gov",
|
|
72
|
+
"census.gov",
|
|
73
|
+
"cdc.gov",
|
|
74
|
+
"nih.gov",
|
|
75
|
+
"nasa.gov",
|
|
76
|
+
"noaa.gov",
|
|
77
|
+
"epa.gov",
|
|
78
|
+
"fda.gov",
|
|
79
|
+
"sec.gov",
|
|
80
|
+
"treasury.gov",
|
|
81
|
+
"state.gov",
|
|
82
|
+
"justice.gov",
|
|
83
|
+
"uscourts.gov",
|
|
84
|
+
"congress.gov",
|
|
85
|
+
"whitehouse.gov",
|
|
86
|
+
"ons.gov.uk",
|
|
87
|
+
"bankofengland.co.uk",
|
|
88
|
+
"abs.gov.au",
|
|
89
|
+
"rba.gov.au",
|
|
90
|
+
"statcan.gc.ca",
|
|
91
|
+
"bankofcanada.ca",
|
|
92
|
+
"destatis.de",
|
|
93
|
+
"bundesbank.de",
|
|
94
|
+
"insee.fr",
|
|
95
|
+
"banque-france.fr",
|
|
96
|
+
"istat.it",
|
|
97
|
+
"stats.govt.nz",
|
|
98
|
+
"ecb.europa.eu",
|
|
99
|
+
"eurostat.ec.europa.eu",
|
|
100
|
+
"unrwa.org",
|
|
101
|
+
"ungeneva.org",
|
|
102
|
+
"wfp.org",
|
|
103
|
+
"unesco.org",
|
|
104
|
+
"unodc.org",
|
|
105
|
+
"unhabitat.org",
|
|
106
|
+
"unwomen.org",
|
|
107
|
+
"unaids.org",
|
|
108
|
+
"unido.org",
|
|
109
|
+
"unctad.org",
|
|
110
|
+
"unops.org",
|
|
111
|
+
"reliefweb.int",
|
|
112
|
+
"ohchr.org"
|
|
113
|
+
]
|
|
114
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Major news organizations with established editorial standards. Tier 3 (major news). Based on Wikipedia's Perennial Sources list (generally reliable).",
|
|
3
|
+
"_comment_newer_outlets": "Established newer outlets with editorial standards, added 2026-04.",
|
|
4
|
+
"known_domains": [
|
|
5
|
+
"apnews.com",
|
|
6
|
+
"reuters.com",
|
|
7
|
+
"bbc.com",
|
|
8
|
+
"bbc.co.uk",
|
|
9
|
+
"nytimes.com",
|
|
10
|
+
"washingtonpost.com",
|
|
11
|
+
"wsj.com",
|
|
12
|
+
"economist.com",
|
|
13
|
+
"ft.com",
|
|
14
|
+
"theguardian.com",
|
|
15
|
+
"aljazeera.com",
|
|
16
|
+
"npr.org",
|
|
17
|
+
"pbs.org",
|
|
18
|
+
"abcnews.go.com",
|
|
19
|
+
"cbsnews.com",
|
|
20
|
+
"nbcnews.com",
|
|
21
|
+
"cnn.com",
|
|
22
|
+
"usatoday.com",
|
|
23
|
+
"latimes.com",
|
|
24
|
+
"chicagotribune.com",
|
|
25
|
+
"bostonglobe.com",
|
|
26
|
+
"politico.com",
|
|
27
|
+
"thehill.com",
|
|
28
|
+
"bloomberg.com",
|
|
29
|
+
"cnbc.com",
|
|
30
|
+
"time.com",
|
|
31
|
+
"newsweek.com",
|
|
32
|
+
"theatlantic.com",
|
|
33
|
+
"newyorker.com",
|
|
34
|
+
"propublica.org",
|
|
35
|
+
"theintercept.com",
|
|
36
|
+
"foreignaffairs.com",
|
|
37
|
+
"foreignpolicy.com",
|
|
38
|
+
"deseret.com",
|
|
39
|
+
"dw.com",
|
|
40
|
+
"france24.com",
|
|
41
|
+
"scmp.com",
|
|
42
|
+
"abc.net.au",
|
|
43
|
+
"cbc.ca",
|
|
44
|
+
"rte.ie",
|
|
45
|
+
"irishtimes.com",
|
|
46
|
+
"smh.com.au",
|
|
47
|
+
"nzherald.co.nz",
|
|
48
|
+
"japantimes.co.jp",
|
|
49
|
+
"haaretz.com",
|
|
50
|
+
"jpost.com",
|
|
51
|
+
"timesofisrael.com",
|
|
52
|
+
"hindustantimes.com",
|
|
53
|
+
"thehindu.com",
|
|
54
|
+
"scientificamerican.com",
|
|
55
|
+
"newscientist.com",
|
|
56
|
+
"nationalgeographic.com",
|
|
57
|
+
"smithsonianmag.com",
|
|
58
|
+
"popsci.com",
|
|
59
|
+
"livescience.com",
|
|
60
|
+
"sciencenews.org",
|
|
61
|
+
"theconversation.com",
|
|
62
|
+
"semafor.com",
|
|
63
|
+
"axios.com",
|
|
64
|
+
"themarkup.org",
|
|
65
|
+
"restofworld.org",
|
|
66
|
+
"defector.com",
|
|
67
|
+
"theinformation.com"
|
|
68
|
+
]
|
|
69
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Established reference sources, encyclopedias, major data aggregators, and known-good government data republishers. Tier 3 (established reference).",
|
|
3
|
+
"known_domains": [
|
|
4
|
+
"en.wikipedia.org",
|
|
5
|
+
"wikipedia.org",
|
|
6
|
+
"britannica.com",
|
|
7
|
+
"merriam-webster.com",
|
|
8
|
+
"dictionary.com",
|
|
9
|
+
"encyclopedia.com",
|
|
10
|
+
"plato.stanford.edu",
|
|
11
|
+
"investopedia.com",
|
|
12
|
+
"webmd.com",
|
|
13
|
+
"mayoclinic.org",
|
|
14
|
+
"clevelandclinic.org",
|
|
15
|
+
"statista.com",
|
|
16
|
+
"tradingeconomics.com",
|
|
17
|
+
"macrotrends.net",
|
|
18
|
+
"ourworldindata.org",
|
|
19
|
+
"gapminder.org",
|
|
20
|
+
"wolframalpha.com",
|
|
21
|
+
"mathworld.wolfram.com",
|
|
22
|
+
"khanacademy.org",
|
|
23
|
+
"coursera.org",
|
|
24
|
+
"rateinflation.com",
|
|
25
|
+
"inflationdata.com",
|
|
26
|
+
"measuringworth.com",
|
|
27
|
+
"usinflationcalculator.com",
|
|
28
|
+
"multpl.com",
|
|
29
|
+
"fred.stlouisfed.org",
|
|
30
|
+
"minneapolisfed.org",
|
|
31
|
+
"bea.gov",
|
|
32
|
+
"numbeo.com",
|
|
33
|
+
"countryeconomy.com",
|
|
34
|
+
"officialdata.org",
|
|
35
|
+
"brainfacts.org",
|
|
36
|
+
"simplypsychology.org",
|
|
37
|
+
"physicsworld.com",
|
|
38
|
+
"sciencebasedmedicine.org",
|
|
39
|
+
"amnh.org",
|
|
40
|
+
"burkemuseum.org",
|
|
41
|
+
"snopes.com",
|
|
42
|
+
"factcheck.org"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Known unreliable, fake news, conspiracy, or junk science domains. Tier 1 (flagged unreliable). Sourced from OpenSources, Iffy.news, and Wikipedia Perennial Sources (deprecated/generally unreliable). This is intentionally a conservative list — only domains with broad consensus on unreliability.",
|
|
3
|
+
"known_domains": [
|
|
4
|
+
"infowars.com",
|
|
5
|
+
"naturalnews.com",
|
|
6
|
+
"beforeitsnews.com",
|
|
7
|
+
"globalresearch.ca",
|
|
8
|
+
"zerohedge.com",
|
|
9
|
+
"breitbart.com",
|
|
10
|
+
"dailycaller.com",
|
|
11
|
+
"thegatewaypundit.com",
|
|
12
|
+
"oann.com",
|
|
13
|
+
"newsmax.com",
|
|
14
|
+
"rt.com",
|
|
15
|
+
"sputniknews.com",
|
|
16
|
+
"tass.com",
|
|
17
|
+
"presstv.ir",
|
|
18
|
+
"globalresearch.ca",
|
|
19
|
+
"collective-evolution.com",
|
|
20
|
+
"davidicke.com",
|
|
21
|
+
"mercola.com",
|
|
22
|
+
"greenmedinfo.com",
|
|
23
|
+
"activistpost.com",
|
|
24
|
+
"lewrockwell.com",
|
|
25
|
+
"worldtruth.tv",
|
|
26
|
+
"yournewswire.com",
|
|
27
|
+
"neonnettle.com",
|
|
28
|
+
"newspunch.com",
|
|
29
|
+
"theepochtimes.com",
|
|
30
|
+
"dailywire.com",
|
|
31
|
+
"occupydemocrats.com",
|
|
32
|
+
"bipartisanreport.com",
|
|
33
|
+
"palmerreport.com",
|
|
34
|
+
"addictinginfo.com",
|
|
35
|
+
"conservativetribune.com",
|
|
36
|
+
"freedomdaily.com",
|
|
37
|
+
"worldnewsdailyreport.com",
|
|
38
|
+
"empirenews.net",
|
|
39
|
+
"nationalreport.net",
|
|
40
|
+
"huzlers.com",
|
|
41
|
+
"theonion.com",
|
|
42
|
+
"babylonbee.com",
|
|
43
|
+
"clickhole.com",
|
|
44
|
+
"borowitz.com",
|
|
45
|
+
"waterfordwhispersnews.com"
|
|
46
|
+
],
|
|
47
|
+
"satire_domains": [
|
|
48
|
+
"theonion.com",
|
|
49
|
+
"babylonbee.com",
|
|
50
|
+
"clickhole.com",
|
|
51
|
+
"borowitz.com",
|
|
52
|
+
"waterfordwhispersnews.com"
|
|
53
|
+
]
|
|
54
|
+
}
|
proof_citations/fetch.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""
|
|
2
|
+
fetch.py — HTTP fetching with fallback chain for proof-engine.
|
|
3
|
+
|
|
4
|
+
Handles: live fetch -> snapshot -> Wayback Machine fallback.
|
|
5
|
+
Also handles PDF text extraction and GitHub raw README fallback.
|
|
6
|
+
|
|
7
|
+
Extracted from verify_citations.py to separate transport from matching logic.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
import requests
|
|
15
|
+
except ImportError:
|
|
16
|
+
requests = None
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# ---------------------------------------------------------------------------
|
|
20
|
+
# PDF text extraction
|
|
21
|
+
# ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
def extract_pdf_text(content: bytes) -> str | None:
|
|
24
|
+
"""Extract text from PDF bytes. Tries pdfplumber first, then PyPDF2.
|
|
25
|
+
|
|
26
|
+
Returns None if no PDF library is available or if extraction fails.
|
|
27
|
+
"""
|
|
28
|
+
try:
|
|
29
|
+
import pdfplumber
|
|
30
|
+
import io
|
|
31
|
+
with pdfplumber.open(io.BytesIO(content)) as pdf:
|
|
32
|
+
return "\n".join(page.extract_text() or "" for page in pdf.pages)
|
|
33
|
+
except ImportError:
|
|
34
|
+
pass
|
|
35
|
+
except Exception:
|
|
36
|
+
pass
|
|
37
|
+
try:
|
|
38
|
+
import PyPDF2
|
|
39
|
+
import io
|
|
40
|
+
reader = PyPDF2.PdfReader(io.BytesIO(content))
|
|
41
|
+
return "\n".join(page.extract_text() or "" for page in reader.pages)
|
|
42
|
+
except ImportError:
|
|
43
|
+
pass
|
|
44
|
+
except Exception:
|
|
45
|
+
pass
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# ---------------------------------------------------------------------------
|
|
50
|
+
# Wayback Machine fallback
|
|
51
|
+
# ---------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
def try_wayback(url: str, timeout: int = 15) -> str | None:
|
|
54
|
+
"""Try fetching a URL from the Wayback Machine. Returns page text or None."""
|
|
55
|
+
if requests is None:
|
|
56
|
+
return None
|
|
57
|
+
wayback_url = f"https://web.archive.org/web/{url}"
|
|
58
|
+
try:
|
|
59
|
+
resp = requests.get(wayback_url, timeout=timeout,
|
|
60
|
+
headers={"User-Agent": "proof-engine/1.0"},
|
|
61
|
+
allow_redirects=True)
|
|
62
|
+
resp.raise_for_status()
|
|
63
|
+
return resp.text
|
|
64
|
+
except requests.exceptions.RequestException:
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# ---------------------------------------------------------------------------
|
|
69
|
+
# GitHub raw README fallback
|
|
70
|
+
# ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
_GITHUB_REPO_RE = re.compile(r'^https?://github\.com/([^/]+)/([^/]+)/?$')
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
_README_CANDIDATES = ["README.md", "README.rst", "README.txt", "README", "readme.md"]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def try_github_raw(url: str, timeout: int = 15) -> str | None:
|
|
79
|
+
"""Try fetching a GitHub repo's raw README. Returns text or None.
|
|
80
|
+
|
|
81
|
+
Only applies to bare repo URLs (github.com/owner/repo). URLs with
|
|
82
|
+
file paths are not rewritten. GitHub renders repo pages via JavaScript,
|
|
83
|
+
so requests.get() gets a React shell instead of the README content.
|
|
84
|
+
|
|
85
|
+
Tries multiple README filenames (README.md, README.rst, README.txt,
|
|
86
|
+
README, readme.md) since repos vary in naming convention.
|
|
87
|
+
"""
|
|
88
|
+
if requests is None:
|
|
89
|
+
return None
|
|
90
|
+
m = _GITHUB_REPO_RE.match(url)
|
|
91
|
+
if not m:
|
|
92
|
+
return None
|
|
93
|
+
owner, repo = m.group(1), m.group(2)
|
|
94
|
+
for readme_name in _README_CANDIDATES:
|
|
95
|
+
raw_url = f"https://raw.githubusercontent.com/{owner}/{repo}/HEAD/{readme_name}"
|
|
96
|
+
try:
|
|
97
|
+
resp = requests.get(raw_url, timeout=timeout,
|
|
98
|
+
headers={"User-Agent": "proof-engine/1.0"},
|
|
99
|
+
allow_redirects=True)
|
|
100
|
+
resp.raise_for_status()
|
|
101
|
+
return resp.text
|
|
102
|
+
except requests.exceptions.RequestException:
|
|
103
|
+
continue
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# ---------------------------------------------------------------------------
|
|
108
|
+
# Page fetching with fallback chain
|
|
109
|
+
# ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
def fetch_page(url: str, timeout: int = 15, snapshot: str = None,
|
|
112
|
+
wayback_fallback: bool = False,
|
|
113
|
+
skip_live_fetch: bool = False,
|
|
114
|
+
snapshot_file: str = None) -> tuple[str | None, str, str | None]:
|
|
115
|
+
"""Fetch page text using the standard fallback chain.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
url: The URL to fetch.
|
|
119
|
+
timeout: Fetch timeout in seconds.
|
|
120
|
+
snapshot: Pre-fetched page text for offline verification (inline).
|
|
121
|
+
wayback_fallback: If True, try Wayback Machine as last resort.
|
|
122
|
+
skip_live_fetch: If True, skip live HTTP fetch (e.g., when requests
|
|
123
|
+
is unavailable in the calling module).
|
|
124
|
+
snapshot_file: Path to a local file containing pre-fetched page text.
|
|
125
|
+
Used for paywalled content that cannot be embedded inline. Inline
|
|
126
|
+
snapshot takes precedence over snapshot_file.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
(page_text, fetch_mode, error_message)
|
|
130
|
+
- page_text: The page text, or None if all methods failed
|
|
131
|
+
- fetch_mode: "live", "snapshot", "wayback", "github_raw", or "fetch_failed"
|
|
132
|
+
- error_message: Error description if failed, else None
|
|
133
|
+
"""
|
|
134
|
+
# --- 1. Try live fetch ---
|
|
135
|
+
fetch_error_msg = None
|
|
136
|
+
if requests is not None and not skip_live_fetch:
|
|
137
|
+
try:
|
|
138
|
+
resp = requests.get(
|
|
139
|
+
url,
|
|
140
|
+
timeout=timeout,
|
|
141
|
+
headers={"User-Agent": "proof-engine/1.0"},
|
|
142
|
+
allow_redirects=True,
|
|
143
|
+
)
|
|
144
|
+
resp.raise_for_status()
|
|
145
|
+
|
|
146
|
+
content_type = resp.headers.get("Content-Type", "")
|
|
147
|
+
is_pdf = "application/pdf" in content_type or url.lower().endswith(".pdf")
|
|
148
|
+
|
|
149
|
+
if is_pdf:
|
|
150
|
+
pdf_text = extract_pdf_text(resp.content)
|
|
151
|
+
if pdf_text is None:
|
|
152
|
+
fetch_error_msg = "PDF detected but no extraction library available (pip install pdfplumber)"
|
|
153
|
+
else:
|
|
154
|
+
return pdf_text, "live", None
|
|
155
|
+
else:
|
|
156
|
+
page_text = resp.text
|
|
157
|
+
# --- 1.5. GitHub raw README fallback ---
|
|
158
|
+
# Only try if live fetch returned an empty/JS-shell page (< 500 chars of
|
|
159
|
+
# visible text after tag stripping) for a bare GitHub repo URL.
|
|
160
|
+
# This preserves quotes about repo metadata that appears on the rendered
|
|
161
|
+
# github.com page, falling back to raw README only when the live page
|
|
162
|
+
# didn't contain useful content.
|
|
163
|
+
if len(re.sub(r'<[^>]+>', '', page_text).strip()) < 500:
|
|
164
|
+
github_text = try_github_raw(url, timeout)
|
|
165
|
+
if github_text is not None:
|
|
166
|
+
return github_text, "github_raw", None
|
|
167
|
+
return page_text, "live", None
|
|
168
|
+
|
|
169
|
+
except requests.exceptions.Timeout:
|
|
170
|
+
fetch_error_msg = f"Timeout after {timeout}s on {url}"
|
|
171
|
+
except requests.exceptions.ConnectionError as e:
|
|
172
|
+
fetch_error_msg = f"Connection error on {url}: {e}"
|
|
173
|
+
except requests.exceptions.HTTPError:
|
|
174
|
+
fetch_error_msg = f"HTTP {resp.status_code} on {url}"
|
|
175
|
+
except requests.exceptions.RequestException as e:
|
|
176
|
+
fetch_error_msg = f"{e}"
|
|
177
|
+
else:
|
|
178
|
+
fetch_error_msg = "requests package not installed — skipping live fetch"
|
|
179
|
+
|
|
180
|
+
# --- 1.5b. GitHub raw README fallback (when live fetch failed entirely) ---
|
|
181
|
+
if 'github.com' in url:
|
|
182
|
+
github_text = try_github_raw(url, timeout)
|
|
183
|
+
if github_text is not None:
|
|
184
|
+
return github_text, "github_raw", None
|
|
185
|
+
|
|
186
|
+
# --- 2. Try snapshot fallback ---
|
|
187
|
+
if snapshot:
|
|
188
|
+
return snapshot, "snapshot", None
|
|
189
|
+
|
|
190
|
+
# --- 2b. Try snapshot_file fallback ---
|
|
191
|
+
if snapshot_file:
|
|
192
|
+
if os.path.isfile(snapshot_file):
|
|
193
|
+
try:
|
|
194
|
+
with open(snapshot_file, "r", encoding="utf-8") as f:
|
|
195
|
+
return f.read(), "snapshot", None
|
|
196
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
197
|
+
read_err = f"snapshot_file '{snapshot_file}' could not be read: {exc}"
|
|
198
|
+
fetch_error_msg = f"{fetch_error_msg}; {read_err}" if fetch_error_msg else read_err
|
|
199
|
+
else:
|
|
200
|
+
# Record the missing file but continue to next fallback
|
|
201
|
+
snapshot_file_msg = f"snapshot_file '{snapshot_file}' not found (paywalled content stored locally only)"
|
|
202
|
+
if fetch_error_msg:
|
|
203
|
+
fetch_error_msg = f"{fetch_error_msg}; {snapshot_file_msg}"
|
|
204
|
+
else:
|
|
205
|
+
fetch_error_msg = snapshot_file_msg
|
|
206
|
+
|
|
207
|
+
# --- 3. Try Wayback Machine ---
|
|
208
|
+
if wayback_fallback:
|
|
209
|
+
wayback_text = try_wayback(url, timeout)
|
|
210
|
+
if wayback_text is not None:
|
|
211
|
+
return wayback_text, "wayback", None
|
|
212
|
+
|
|
213
|
+
# --- 4. All methods exhausted ---
|
|
214
|
+
return None, "fetch_failed", fetch_error_msg
|