-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdupes.py
More file actions
49 lines (41 loc) · 1.65 KB
/
Copy pathdupes.py
File metadata and controls
49 lines (41 loc) · 1.65 KB
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
from pathlib import Path
import click
from pymarc import Field, MARCReader, Record
@click.command()
@click.help_option("-h", "--help")
@click.argument(
"file",
metavar="<input.mrc>",
type=click.Path(exists=True, readable=True),
)
def print_duplicates(file: Path):
"""Print records with duplicate 001s from a MARC file."""
count: int = 0
records: dict[str, list[Record]] = {}
with open(file, "rb") as fh:
reader = MARCReader(fh)
click.echo(
f"Scanning {file} for duplicates, this takes time depending on the file size."
)
for record in reader:
if record:
id_field: list[Field] = record.get_fields("001")
if id_field and len(id_field) == 1:
record_id: str | None = id_field[0].value()
if record_id:
records.setdefault(record_id, []).append(record)
for recs in records.values():
if len(recs) > 1:
count += 1
for rec in recs:
link: str = ""
sysctl_field: Field | None = rec.get("999")
if sysctl_field:
biblionumber: str | None = sysctl_field.get("c")
if biblionumber:
link: str = f"https://library-staff.cca.edu/cgi-bin/koha/catalogue/detail.pl?biblionumber={biblionumber}"
title: str = rec.title if rec.title else "[no title]"
click.echo(f"{title} {link}")
click.echo(f"{count} duplicates out of {len(records)} unique 001s")
if __name__ == "__main__":
print_duplicates()