aboutsummaryrefslogtreecommitdiffstats
path: root/python/sandcrawler/fileset_platforms.py
blob: 07d9844346b801cbdd1cb0586bfbde69cf716fb4 (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
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
import urllib.parse
from typing import Optional, Tuple

import internetarchive

from sandcrawler.fileset_types import (
    FilesetManifestFile,
    FilesetPlatformItem,
    IngestStrategy,
    PlatformRestrictedError,
    PlatformScopeError,
)
from sandcrawler.html_metadata import BiblioMetadata
from sandcrawler.ia import ResourceResult
from sandcrawler.misc import requests_retry_session


class FilesetPlatformHelper:
    def __init__(self):
        self.platform_name = "unknown"

    def match_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> bool:
        """
        Does this request look like it matches this platform?
        """
        raise NotImplementedError()

    def process_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> FilesetPlatformItem:
        """
        Fetch platform-specific metadata for this request (eg, via API calls)
        """
        raise NotImplementedError()

    def chose_strategy(self, item: FilesetPlatformItem) -> IngestStrategy:
        assert item.manifest
        total_size = sum([m.size for m in item.manifest]) or 0
        largest_size = max([m.size or 0 for m in item.manifest]) or 0
        if len(item.manifest) == 1:
            if total_size < 64 * 1024 * 1024:
                return IngestStrategy.WebFile
            else:
                return IngestStrategy.ArchiveorgFile
        else:
            if largest_size < 64 * 1024 * 1024 and total_size < 128 * 1024 * 1024 * 1024:
                return IngestStrategy.WebFileset
            else:
                return IngestStrategy.ArchiveorgFileset


class DataverseHelper(FilesetPlatformHelper):
    def __init__(self):
        super().__init__()
        self.platform_name = "dataverse"
        self.session = requests_retry_session()

    @staticmethod
    def parse_dataverse_persistentid(pid: str) -> dict:
        """
        Parses a persistentId into 5 sections:

        - type (doi or hdl)
        - authority (eg, DOI prefix)
        - shoulder (optional, eg 'DVN')
        - dataset_id (6-digit)
        - file_id

        The returned dict always has all components, which may be 'None' if optional.

        This is possible because the dataverse software only supports a handful
        of configurations and persistend identifier types.

        If there is an error parsing, raises a ValueError
        """
        id_type = None
        if pid.startswith("doi:10."):
            id_type = "doi"
            pid = pid[4:]
        elif pid.startswith("hdl:"):
            id_type = "hdl"
            pid = pid[4:]
        else:
            raise ValueError(f"unknown dataverse persistentId format: {pid}")

        comp = pid.split("/")
        if len(comp) < 2:
            raise ValueError(f"unknown dataverse persistentId format: {pid}")

        authority = comp[0]
        shoulder = None
        dataset_id = None
        file_id = None
        if len(comp[1]) != 6 and len(comp) == 3:
            shoulder = comp[1]
            dataset_id = comp[2]
        elif len(comp[1]) != 6 and len(comp) == 4:
            shoulder = comp[1]
            dataset_id = comp[2]
            file_id = comp[3]
        elif len(comp[1]) == 6 and len(comp) == 2:
            dataset_id = comp[1]
        elif len(comp[1]) == 6 and len(comp) == 3:
            dataset_id = comp[1]
            file_id = comp[2]
        else:
            raise ValueError(f"unknown dataverse persistentId format: {pid}")

        if len(dataset_id) != 6:
            raise ValueError(f"expected a 6-digit dataverse dataset id: {dataset_id}")
        if file_id and len(file_id) != 6:
            raise ValueError(f"expected a 6-digit dataverse file id: {file_id}")

        return {
            "type": id_type,
            "authority": authority,
            "shoulder": shoulder,
            "dataset_id": dataset_id,
            "file_id": file_id,
        }

    def match_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> bool:
        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        # TODO: could also do HTML platform detection or something?

        components = urllib.parse.urlparse(url)
        # platform_domain = components.netloc.split(':')[0].lower()
        params = urllib.parse.parse_qs(components.query)
        id_param = params.get("persistentId")
        if not id_param:
            return False
        platform_id = id_param[0]

        try:
            self.parse_dataverse_persistentid(platform_id)
        except ValueError:
            return False

        return True

    def process_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> FilesetPlatformItem:
        """
        Fetch platform-specific metadata for this request (eg, via API calls)


        HTTP GET https://demo.dataverse.org/api/datasets/export?exporter=dataverse_json&persistentId=doi:10.5072/FK2/J8SJZB
        """

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        # 1. extract domain, PID, and version from URL
        components = urllib.parse.urlparse(url)
        platform_domain = components.netloc.split(":")[0].lower()
        params = urllib.parse.parse_qs(components.query)
        id_param = params.get("persistentId")
        if not (id_param and id_param[0]):
            raise PlatformScopeError("Expected a Dataverse persistentId in URL")
        platform_id = id_param[0]
        version_param = params.get("version")
        dataset_version = None
        if version_param:
            dataset_version = version_param[0]

        try:
            parsed_id = self.parse_dataverse_persistentid(platform_id)
        except ValueError:
            raise PlatformScopeError("not actually in scope")

        if parsed_id["file_id"]:
            # TODO: maybe we could support this?
            raise PlatformScopeError(
                "only entire dataverse datasets can be archived with this tool"
            )

        # 1b. if we didn't get a version number from URL, fetch it from API
        if not dataset_version:
            resp = self.session.get(
                f"https://{platform_domain}/api/datasets/:persistentId/?persistentId={platform_id}",
                timeout=60.0,
            )
            resp.raise_for_status()
            obj = resp.json()
            if "latestVersion" not in obj["data"]:
                raise PlatformScopeError("could not find latest version for dataverse record")
            obj_latest = obj["data"]["latestVersion"]
            dataset_version = (
                f"{obj_latest['versionNumber']}.{obj_latest['versionMinorNumber']}"
            )

        # 2. API fetch
        resp = self.session.get(
            f"https://{platform_domain}/api/datasets/:persistentId/?persistentId={platform_id}&version={dataset_version}",
            timeout=60.0,
        )
        resp.raise_for_status()
        obj = resp.json()

        obj_latest = obj["data"]["latestVersion"]
        assert (
            dataset_version
            == f"{obj_latest['versionNumber']}.{obj_latest['versionMinorNumber']}"
        )
        assert platform_id == obj_latest["datasetPersistentId"]

        manifest = []
        for row in obj_latest["files"]:
            df = row["dataFile"]
            df_persistent_id = df["persistentId"]
            platform_url = f"https://{platform_domain}/api/access/datafile/:persistentId/?persistentId={df_persistent_id}"
            if df.get("originalFileName"):
                platform_url += "&format=original"

            extra = dict()
            # TODO: always save the version field?
            if row.get("version") != 1:
                extra["version"] = row["version"]
            if "description" in df:
                extra["description"] = df["description"]
            manifest.append(
                FilesetManifestFile(
                    path=df.get("originalFileName") or df["filename"],
                    size=df.get("originalFileSize") or df["filesize"],
                    md5=df["md5"],
                    # NOTE: don't get: sha1, sha256
                    mimetype=df["contentType"],
                    platform_url=platform_url,
                    extra=extra or None,
                )
            )

        platform_sub_id = platform_id.split("/")[-1]
        archiveorg_item_name = f"{platform_domain}-{platform_sub_id}-v{dataset_version}"
        archiveorg_item_meta = dict(
            # TODO: collection=platform_domain,
            collection="datasets",
            date=obj_latest["releaseTime"].split("T")[0],
            source=f"https://{platform_domain}/dataset.xhtml?persistentId={platform_id}&version={dataset_version}",
        )
        if platform_id.startswith("doi:10."):
            archiveorg_item_meta["doi"] = platform_id.replace("doi:", "")
        for block in obj_latest["metadataBlocks"]["citation"]["fields"]:
            if block["typeName"] == "title":
                archiveorg_item_meta["title"] = block["value"]
            elif block["typeName"] == "depositor":
                archiveorg_item_meta["creator"] = block["value"]
            elif block["typeName"] == "dsDescription":
                archiveorg_item_meta["description"] = block["value"][0]["dsDescriptionValue"][
                    "value"
                ]

        archiveorg_item_meta["description"] = archiveorg_item_meta.get("description", "")
        if obj_latest.get("termsOfUse"):
            archiveorg_item_meta["description"] += "\n<br>\n" + obj_latest["termsOfUse"]

        return FilesetPlatformItem(
            platform_name=self.platform_name,
            platform_status="success",
            manifest=manifest,
            platform_domain=platform_domain,
            platform_id=platform_id,
            archiveorg_item_name=archiveorg_item_name,
            archiveorg_item_meta=archiveorg_item_meta,
            web_bundle_url=f"https://{platform_domain}/api/access/dataset/:persistentId/?persistentId={platform_id}&format=original",
            # TODO: web_base_url= (for GWB downloading, in lieu of platform_url on individual files)
            extra=dict(version=dataset_version),
        )


def test_parse_dataverse_persistentid() -> None:

    valid = {
        "doi:10.25625/LL6WXZ": {
            "type": "doi",
            "authority": "10.25625",
            "shoulder": None,
            "dataset_id": "LL6WXZ",
            "file_id": None,
        },
        "doi:10.5072/FK2/J8SJZB": {
            "type": "doi",
            "authority": "10.5072",
            "shoulder": "FK2",
            "dataset_id": "J8SJZB",
            "file_id": None,
        },
        "doi:10.5072/FK2/J8SJZB/LL6WXZ": {
            "type": "doi",
            "authority": "10.5072",
            "shoulder": "FK2",
            "dataset_id": "J8SJZB",
            "file_id": "LL6WXZ",
        },
        "hdl:20.500.12690/RIN/IDDOAH/BTNH25": {
            "type": "hdl",
            "authority": "20.500.12690",
            "shoulder": "RIN",
            "dataset_id": "IDDOAH",
            "file_id": "BTNH25",
        },
        "doi:10.7910/DVN/6HPRIG": {
            "type": "doi",
            "authority": "10.7910",
            "shoulder": "DVN",
            "dataset_id": "6HPRIG",
            "file_id": None,
        },
    }

    invalid = [
        # "doi:10.5072/FK2/J8SJZB/LL6WXZ",
        "doi:10.25625/abcd",
        "other:10.25625/LL6WXZ",
        "10.25625/LL6WXZ",
        "doi:10.5072/FK2/J8SJZB/LL6WXZv123",
    ]

    for pid, val in valid.items():
        assert DataverseHelper.parse_dataverse_persistentid(pid) == val

    for pid in invalid:
        try:
            DataverseHelper.parse_dataverse_persistentid(pid)
            assert False, "should not get here"
        except ValueError:
            pass


class FigshareHelper(FilesetPlatformHelper):
    def __init__(self):
        super().__init__()
        self.platform_name = "figshare"
        self.session = requests_retry_session()

    @staticmethod
    def parse_figshare_url_path(path: str) -> Tuple[str, Optional[str]]:
        """
        Tries to parse a figshare URL into ID number and (optional) version number.

        Returns a two-element tuple; version number will be None if not found

        Raises a ValueError if not a figshare URL
        """
        # eg: /articles/Optimized_protocol_to_isolate_high_quality_genomic_DNA_from_different_tissues_of_a_palm_species/8987858/1
        #     /articles/dataset/STable_1_U-Pb_geochronologic_analyses_on_samples_xls/12127176/4

        comp = path.split("/")
        if len(comp) < 4 or comp[1] != "articles":
            raise ValueError(f"not a figshare URL: {path}")

        comp = comp[2:]
        if comp[0] in [
            "dataset",
        ]:
            comp = comp[1:]

        if len(comp) == 3 and comp[1].isdigit() and comp[2].isdigit():
            return (comp[1], comp[2])
        elif len(comp) == 2 and comp[1].isdigit():
            return (comp[1], None)
        else:
            raise ValueError(f"couldn't find figshare identiier: {path}")

    def match_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> bool:

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        components = urllib.parse.urlparse(url)
        platform_domain = components.netloc.split(":")[0].lower()

        # only work with full, versioned figshare.com URLs
        if "figshare.com" not in platform_domain:
            return False

        try:
            parsed = self.parse_figshare_url_path(components.path)
        except ValueError:
            return False

        # has file component
        if parsed[0] and parsed[1]:
            return True

        return False

    def process_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> FilesetPlatformItem:
        """
        Fetch platform-specific metadata for this request (eg, via API calls)
        """

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        # 1. extract domain, PID, and version from URL
        components = urllib.parse.urlparse(url)
        platform_domain = components.netloc.split(":")[0].lower()

        (platform_id, dataset_version) = self.parse_figshare_url_path(components.path)
        assert platform_id.isdigit(), f"expected numeric: {platform_id}"
        assert (
            dataset_version and dataset_version.isdigit()
        ), f"expected numeric: {dataset_version}"

        # 1b. if we didn't get a version number from URL, fetch it from API
        # TODO: implement this code path

        # 2. API fetch
        resp = self.session.get(
            f"https://api.figshare.com/v2/articles/{platform_id}/versions/{dataset_version}",
            timeout=60.0,
        )
        resp.raise_for_status()
        obj = resp.json()

        # figshare_type = obj['defined_type_name']

        if not obj["is_public"]:
            raise PlatformRestrictedError(f"record not public: {platform_id} {dataset_version}")
        if obj["is_embargoed"]:
            raise PlatformRestrictedError(
                f'record is embargoed: {obj.get("embargo_title")} ({platform_id} {dataset_version})'
            )

        manifest = []
        for row in obj["files"]:
            manifest.append(
                FilesetManifestFile(
                    path=row["name"],
                    size=row["size"],
                    md5=row["computed_md5"],
                    # NOTE: don't get: sha1, sha256, mimetype
                    platform_url=row["download_url"],
                    # extra=dict(),
                )
            )
            assert not row.get("is_link_only")

        authors = []
        for author in obj["authors"]:
            authors.append(author["full_name"])
        archiveorg_item_name = f"{platform_domain}-{platform_id}-v{dataset_version}"
        archiveorg_item_meta = dict(
            # TODO: collection=platform_domain,
            collection="datasets",
            creator=authors,
            doi=obj["doi"],
            title=obj["title"],
            date=obj["published_date"],
            source=obj["url_public_html"],
            description=obj["description"],
            license=obj["license"]["url"],
            version=obj["version"],
        )

        return FilesetPlatformItem(
            platform_name=self.platform_name,
            platform_status="success",
            manifest=manifest,
            platform_domain=platform_domain,
            platform_id=platform_id,
            archiveorg_item_name=archiveorg_item_name,
            archiveorg_item_meta=archiveorg_item_meta,
            web_bundle_url=f"https://ndownloader.figshare.com/articles/{platform_id}/versions/{dataset_version}",
            # TODO: web_base_url= (for GWB downloading, in lieu of platform_url on individual files)
            extra=dict(version=dataset_version),
        )


def test_parse_figshare_url_path() -> None:

    valid = {
        "/articles/Optimized_protocol_to_isolate_high_quality_genomic_DNA_from_different_tissues_of_a_palm_species/8987858/1": (
            "8987858",
            "1",
        ),
        "/articles/Optimized_protocol_to_isolate_high_quality_genomic_DNA_from_different_tissues_of_a_palm_species/8987858": (
            "8987858",
            None,
        ),
        "/articles/CIBERSORT_p-value_0_05/8217188/1": ("8217188", "1"),
        "/articles/dataset/STable_1_U-Pb_geochronologic_analyses_on_samples_xls/12127176/4": (
            "12127176",
            "4",
        ),
    }

    invalid = [
        "/articles/Optimized_protocol_to_isolate_high_quality_genomic_DNA_from_different_tissues_of_a_palm_species",
    ]

    for path, val in valid.items():
        assert FigshareHelper.parse_figshare_url_path(path) == val

    for path in invalid:
        try:
            FigshareHelper.parse_figshare_url_path(path)
            assert False, "should not get here"
        except ValueError:
            pass


class ZenodoHelper(FilesetPlatformHelper):
    def __init__(self):
        super().__init__()
        self.platform_name = "zenodo"
        self.session = requests_retry_session()

    def match_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> bool:

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        components = urllib.parse.urlparse(url)
        platform_domain = components.netloc.split(":")[0].lower()
        if platform_domain == "zenodo.org" and "/record/" in components.path:
            return True
        return False

    def process_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> FilesetPlatformItem:
        """
        Fetch platform-specific metadata for this request (eg, via API calls)
        """

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]

        # TODO: also look in base_url and resource-non-terminal for ident? to
        # check for work-level redirects

        # 1. extract identifier from URL
        # eg: https://zenodo.org/record/5230255
        components = urllib.parse.urlparse(url)
        platform_domain = components.netloc.split(":")[0].lower()
        if len(components.path.split("/")) < 2:
            raise PlatformScopeError("Expected a complete, versioned figshare URL")

        platform_id = components.path.split("/")[2]
        assert platform_id.isdigit(), f"expected numeric: {platform_id}"

        if "zenodo.org" not in platform_domain:
            raise PlatformScopeError(f"unexpected zenodo.org domain: {platform_domain}")

        # 2. API fetch
        resp = self.session.get(f"https://zenodo.org/api/records/{platform_id}", timeout=60.0)
        if resp.status_code == 410:
            raise PlatformRestrictedError("record deleted")
        resp.raise_for_status()
        obj = resp.json()

        assert obj["id"] == int(platform_id)
        work_id = obj["conceptrecid"]
        if work_id == obj["id"]:
            raise PlatformScopeError(
                "got a work-level zenodo record, not a versioned record: {work_id}"
            )

        # zenodo_type = obj['metadata']['resource_type']['type']

        if obj["metadata"]["access_right"] != "open":
            raise PlatformRestrictedError(
                "not publicly available ({obj['metadata']['access_right']}): {platform_domain} {platform_id}"
            )

        manifest = []
        for row in obj["files"]:
            mf = FilesetManifestFile(
                path=row["key"],
                size=row["size"],
                platform_url=row["links"]["self"],
                # extra=dict(),
            )
            checksum = row["checksum"]
            # eg: md5:35ffcab905f8224556dba76648cb7dad
            if checksum.startswith("md5:"):
                mf.md5 = checksum[4:]
            elif checksum.startswith("sha1:"):
                mf.sha1 = checksum[45]
            manifest.append(mf)

        authors = []
        for author in obj["metadata"]["creators"]:
            authors.append(author["name"])
        archiveorg_item_name = f"{platform_domain}-{platform_id}"
        archiveorg_item_meta = dict(
            # TODO: collection=platform_domain,
            collection="datasets",
            creator=authors,
            doi=obj["doi"],
            title=obj["metadata"]["title"],
            date=obj["metadata"]["publication_date"],
            source=obj["links"]["html"],
            description=obj["metadata"]["description"],
            license=obj["metadata"]["license"]["id"],
            version=obj["revision"],
            # obj['metadata']['version'] is, eg, git version tag
        )

        return FilesetPlatformItem(
            platform_name=self.platform_name,
            platform_status="success",
            manifest=manifest,
            platform_domain=platform_domain,
            platform_id=platform_id,
            archiveorg_item_name=archiveorg_item_name,
            archiveorg_item_meta=archiveorg_item_meta,
            # web_bundle_url=f"https://ndownloader.figshare.com/articles/{platform_id}/versions/{dataset_version}",
            # TODO: web_base_url= (for GWB downloading, in lieu of platform_url on individual files)
            extra=dict(version=obj["revision"]),
        )


class ArchiveOrgHelper(FilesetPlatformHelper):

    FORMAT_TO_MIMETYPE = {
        "BZIP": "application/x-bzip",
        "BZIP2": "application/x-bzip2",
        "ZIP": "application/zip",
        "GZIP": "application/gzip",
        "RAR": "application/vnd.rar",
        "TAR": "application/x-tar",
        "7z": "application/x-7z-compressed",
        "HTML": "text/html",
        "Text": "text/plain",
        "PDF": "application/pdf",
        "CSV": "text/csv",
        "XML": "application/xml",
        "JSON": "application/json",
        #'application/msword (.doc)', # .doc
        #'application/vnd.openxmlformats-officedocument.wordprocessingml.document', # .docx
        #'application/vnd.ms-excel', # .xls
        #'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', # .xlsx
        "MP3": "audio/mpeg",  # .mp3
        "MP4": "video/mp4",  # .mp4
        "MPEG": "video/mpeg",  # .mpeg
        "JPEG": "image/jpeg",
        "GIF": "image/gif",
        "PNG": "image/png",
        "TIFF": "image/tiff",
        "Unknown": None,
    }

    def __init__(self):
        super().__init__()
        self.platform_name = "archiveorg"
        self.session = internetarchive.get_session()

    @staticmethod
    def want_item_file(f: internetarchive.File, item_name: str) -> bool:
        """
        Filters IA API files
        """
        if f.source != "original":
            return False
        for suffix in [
            "_meta.sqlite",
            "_archive.torrent",
            "_itemimage.jpg",
            "_meta.xml",
            "_thumb.png",
            "_files.xml",
        ]:
            if f.name == item_name + suffix or f.name == item_name.lower() + suffix:
                return False
        if f.name.startswith("_"):
            return False
        if item_name.startswith("academictorrents_"):
            for suffix in [
                "_academictorrents.torrent",
                "_academictorrents_torrent.txt",
                ".bib",
            ]:
                if f.name == item_name + suffix:
                    return False
        return True

    def match_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> bool:

        if resource and resource.terminal_url:
            url = resource.terminal_url
        else:
            url = request["base_url"]
        patterns = [
            "://archive.org/details/",
            "://archive.org/download/",
        ]
        for p in patterns:
            if p in url:
                return True
        return False

    def process_request(
        self,
        request: dict,
        resource: Optional[ResourceResult],
        html_biblio: Optional[BiblioMetadata],
    ) -> FilesetPlatformItem:
        """
        Fetch platform-specific metadata for this request (eg, via API calls)
        """

        base_url_split = request["base_url"].split("/")
        # print(base_url_split, file=sys.stderr)
        assert len(base_url_split) in [5, 6]
        assert base_url_split[0] in ["http:", "https:"]
        assert base_url_split[2] == "archive.org"
        assert base_url_split[3] in ["details", "download"]
        item_name = base_url_split[4]
        if len(base_url_split) == 6 and base_url_split[5]:
            raise PlatformScopeError(
                "got an archive.org file path, not download/details page; individual files not handled yet"
            )

        # print(f"  archiveorg processing item={item_name}", file=sys.stderr)
        item = self.session.get_item(item_name)
        item_name = item.identifier
        item_collection = item.metadata["collection"]
        if type(item_collection) == list:
            item_collection = item_collection[0]
        assert item.metadata["mediatype"] not in ["collection", "web"]
        item_files = item.get_files(on_the_fly=False)
        item_files = [f for f in item_files if self.want_item_file(f, item_name)]
        manifest = []
        for f in item_files:
            assert f.name and f.sha1 and f.md5
            assert f.name is not None
            mf = FilesetManifestFile(
                path=f.name,
                size=int(f.size),
                sha1=f.sha1,
                md5=f.md5,
                mimetype=self.FORMAT_TO_MIMETYPE[f.format],
                platform_url=f"https://archive.org/download/{item_name}/{f.name}",
            )
            manifest.append(mf)

        return FilesetPlatformItem(
            platform_name=self.platform_name,
            platform_status="success",
            manifest=manifest,
            platform_domain="archive.org",
            platform_id=item_name,
            archiveorg_item_name=item_name,
            archiveorg_meta=dict(collection=item_collection),
        )

    def chose_strategy(self, item: FilesetPlatformItem) -> IngestStrategy:
        """
        Don't use default strategy picker; we are always doing an 'existing' in this case.
        """
        assert item.manifest is not None
        if len(item.manifest) == 1:
            # NOTE: code flow does not support ArchiveorgFilesetBundle for the
            # case of, eg, a single zipfile in an archive.org item
            return IngestStrategy.ArchiveorgFile
        elif len(item.manifest) >= 1:
            return IngestStrategy.ArchiveorgFileset
        else:
            raise NotImplementedError("empty dataset")