aboutsummaryrefslogtreecommitdiffstats
path: root/python/sandcrawler/html_ingest.py
blob: 0e1439c0570fb3621872e65a8528d210a4a22fd7 (plain)
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

import sys
import json
import datetime
import argparse
from typing import List, Optional, Any

import trafilatura
import pydantic
from selectolax.parser import HTMLParser

from sandcrawler.ia import WaybackClient, CdxApiClient, ResourceResult
from sandcrawler.misc import gen_file_metadata
from sandcrawler.html_metadata import BiblioMetadata, html_extract_resources, html_extract_biblio, load_adblock_rules


def html_extract_fulltext_teixml(doc: bytes) -> dict:
    tei_xml = trafilatura.extract(doc,
        tei_output=True,
        include_comments=False,
        include_formatting=True,
    )
    if tei_xml:
        return dict(status="success", tei_xml=tei_xml)
    else:
        return dict(status="empty-xml")

class WebResource(pydantic.BaseModel):
    surt: str
    timestamp: datetime.datetime
    url: str
    sha1hex: str
    mimetype: str
    status_code: int
    size: Optional[int]
    sha256hex: Optional[str]
    resource_type: Optional[str]


def quick_fetch_html_resources(resources: List[dict], cdx_client: CdxApiClient, when: Optional[datetime.datetime]) -> List[WebResource]:
    """
    This is the lazy version that just does a CDX lookup for each resource.

    Takes a list instead of single record because we may want to circuit break
    on failure, and may introduce concurrency internal to this function.
    """

    full = []
    for resource in resources:
        cdx_row = cdx_client.lookup_best(resource['url'])
        if not cdx_row:
            raise Exception("CDX lookup failed")
        if cdx_row.url != resource['url']:
            pass
            #raise Exception(
            #    f"CDX lookup URL mismatch: {cdx_row.url} != {resource['url']}")
        full.append(WebResource(
            surt=cdx_row.surt,
            timestamp=cdx_row.datetime,
            url=cdx_row.url,
            sha1hex=cdx_row.sha1hex,
            mimetype=cdx_row.mimetype,
            status_code=cdx_row.status_code,
            size=None,
            sha256hex=None,
            resource_type=resource['type'],
        ))

    return full


def fetch_html_resources(resources: List[dict], wayback_client: WaybackClient, when: Optional[datetime.datetime]) -> List[WebResource]:
    """
    This is the full version which fetches each resource from wayback/petabox
    and calculates additional hashes.

    Could make this concurrent in the future, eg: https://realpython.com/python-concurrency/#threading-version
    """

    full = []
    for resource in resources:
        wayback_resp = wayback_client.lookup_resource(resource['url'])
        if not wayback_resp:
            raise Exception("wayback lookup failed")
        assert wayback_resp.status == 'success'
        if wayback_resp.cdx.url != resource['url']:
            pass
            #raise Exception(
            #    f"CDX lookup URL mismatch: {cdx_row.url} != {resource['url']}")
        file_meta = gen_file_metadata(wayback_resp.body)
        assert file_meta['sha1hex'] == wayback_resp.cdx.sha1hex
        full.append(WebResource(
            surt=wayback_resp.cdx.surt,
            timestamp=wayback_resp.cdx.datetime,
            url=wayback_resp.cdx.url,
            sha1hex=file_meta['sha1hex'],
            mimetype=file_meta['mimetype'],
            status_code=wayback_resp.cdx.status_code,
            size=file_meta['size_bytes'],
            sha256hex=file_meta['sha256hex'],
            resource_type=resource['type'],
        ))

    return full


def run_single(url: str, timestamp: Optional[str] = None, quick_mode: bool = False) -> None:

    adblock = load_adblock_rules()
    wayback_client = WaybackClient()

    html_resource = wayback_client.lookup_resource(url, "text/html")
    if html_resource.status != "success":
        print(json.dumps(html_resource, indent=2))
        return

    file_meta = gen_file_metadata(html_resource.body)
    # XXX:
    assert file_meta['mimetype'] == "text/html"

    html_doc = HTMLParser(html_resource.body)
    html_meta = html_extract_biblio(html_doc)
    html_fulltext = html_extract_fulltext_teixml(html_resource.body)
    raw_resources = html_extract_resources(html_resource.terminal_url, html_doc, adblock)

    # XXX:
    when = None

    full_resources: List[WebResource] = []
    if quick_mode:
        full_resources = quick_fetch_html_resources(raw_resources, wayback_client.cdx_client, when)
    else:
        full_resources = fetch_html_resources(raw_resources, wayback_client, when)

    output = dict(
        status="success",
        #html_resource=html_resource,
        file_meta=file_meta,
        html_fulltext=html_fulltext,
        # XXX:
        html_meta=html_meta and html_meta.dict(exclude_none=True, exclude={'release_date'}),
        resources=[r.dict(exclude_none=True, exclude={'timestamp'}) for r in full_resources],
    )

    print(json.dumps(output, indent=2))


def main() -> None:
    """
    Run this command like:

        python -m sandcrawler.html_ingest
    """

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    subparsers = parser.add_subparsers()

    sub = subparsers.add_parser(
        "single", help="tries to ingest a single URL, dumps result to stdout"
    )
    sub.set_defaults(func="run_single")
    sub.add_argument(
        "url",
        help="URL to fetch",
        type=str,
    )
    sub.add_argument(
        "--timestamp",
        help="timestamp for which to fetch document from wayback",
        type=str,
    )
    sub.add_argument(
        "--quick-mode",
        help="don't fetch resources, only do CDX lookup",
        action="store_true",
    )

    args = parser.parse_args()
    if not args.__dict__.get("func"):
        print("tell me what to do! (try --help)")
        sys.exit(-1)

    if args.func == "run_single":
        run_single(args.url, args.timestamp, args.quick_mode)
    else:
        #func = getattr(wp, args.func)
        #func()
        raise NotImplementedError()

if __name__ == "__main__":
    main()