aboutsummaryrefslogtreecommitdiffstats
path: root/chocula/common.py
blob: 29b6e2631de73534f384678f8f4602a21b5e7c17 (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
import sys
import csv
import datetime
from typing import Iterable, Optional, Dict, Any, List
from collections import Counter
from dataclasses import dataclass

import ftfy

from chocula.util import clean_str, clean_issn, merge_spans
from chocula.config import ChoculaConfig
from chocula.database import DirectoryInfo, IssnDatabase, HomepageUrl


# Portico files have weirdly large field sizes
csv.field_size_limit(1310720)
THIS_YEAR = datetime.date.today().year


class DirectoryLoader:

    source_slug: str = "GENERIC"

    def __init__(self, config: ChoculaConfig):
        self.config = config

    def open_file(self) -> Iterable:
        raise NotImplementedError()

    def parse_record(self, record) -> Optional[DirectoryInfo]:
        raise NotImplementedError()

    def index_file(self, db) -> Counter:
        print(f"##### Loading {self.source_slug}...", file=sys.stderr)
        counts: Counter = Counter()
        cur = db.db.cursor()
        for record in self.open_file():
            counts["total"] += 1
            info = self.parse_record(record)
            if info:
                status = db.insert_directory(info, cur=cur)
                counts[status] += 1
        cur.close()
        db.db.commit()
        return counts


@dataclass
class KbartRecord:
    issnl: Optional[str]
    issne: Optional[str]
    issnp: Optional[str]
    title: Optional[str]
    publisher: Optional[str]
    start_year: Optional[int]
    end_year: Optional[int]
    start_volume: Optional[str]
    end_volume: Optional[str]
    url: Optional[HomepageUrl]
    embargo: Optional[str]
    year_spans: List[Any]


class KbartLoader:

    source_slug: str = "GENERIC"

    def __init__(self, config: ChoculaConfig):
        self.config = config

    def file_path(self) -> str:
        # return self.config.TEMPLATE.filepath)
        raise NotImplementedError()

    def open_file(self) -> Iterable:
        raw_file = open(self.file_path(), "rb").read().decode(errors="replace")
        fixed_file = ftfy.fix_text(raw_file)
        reader = csv.DictReader(fixed_file.split("\n"), delimiter="\t")
        return reader

    def parse_record(self, row: dict, issn_db: IssnDatabase) -> Optional[KbartRecord]:

        issne: Optional[str] = clean_issn(row["online_identifier"] or "")
        issnp: Optional[str] = clean_issn(row["print_identifier"] or "")
        issnl: Optional[str] = None
        if issne:
            issnl = issn_db.issn2issnl(issne)
        if issnp and not issnl:
            issnl = issn_db.issn2issnl(issnp)
        start_year: Optional[int] = None
        end_year: Optional[int] = None
        if row["date_first_issue_online"]:
            start_year = int(row["date_first_issue_online"][:4])
        if row["date_last_issue_online"]:
            end_year = int(row["date_last_issue_online"][:4])
        end_volume = row["num_last_vol_online"]
        # hack to handle open-ended preservation
        if end_year is None and end_volume and "(present)" in end_volume:
            end_year = THIS_YEAR
        record = KbartRecord(
            issnl=issnl,
            issnp=issnp,
            issne=issne,
            title=clean_str(row["publication_title"]),
            publisher=clean_str(row["publisher_name"]),
            url=HomepageUrl.from_url(row["title_url"]),
            embargo=clean_str(row["embargo_info"]),
            start_year=start_year,
            end_year=end_year,
            start_volume=clean_str(row["num_first_vol_online"]),
            end_volume=clean_str(row["num_last_vol_online"]),
            year_spans=[],
        )
        if record.start_volume == "null":
            record.start_volume = None
        if record.end_volume == "null":
            record.end_volume = None
        return record

    def index_file(self, db) -> Counter:
        """
        Transforms a KBART file into a dict of dicts; but basically a list of
        JSON objects, one per journal. KBART files can have multiple rows per
        journal (eg, different year spans), which is why this pass is needed.
        """
        print(f"##### Loading {self.source_slug} KBART...", file=sys.stderr)
        counts: Counter = Counter()
        kbart_dict: Dict[str, KbartRecord] = dict()
        for row in self.open_file():
            counts["total"] += 1

            record = self.parse_record(row, db.issn_db)
            if record is None:
                counts["skip-parse"] += 1
                continue
            elif not record.issnl:
                counts["skip-issnl"] += 1
                continue
            elif record.start_year is None or record.end_year is None:
                counts["partial-missing-years"] += 1
            counts["parsed"] += 1

            existing = kbart_dict.get(record.issnl, record)
            if record.start_year and record.end_year:
                old_spans = existing.year_spans or []
                if not record.start_year <= record.end_year:
                    new_spans = [[record.end_year, record.start_year]]
                else:
                    new_spans = [[record.start_year, record.end_year]]
                record.year_spans = merge_spans(old_spans, new_spans)
            elif record.year_spans:
                old_spans = existing.year_spans or []
                record.year_spans = merge_spans(old_spans, record.year_spans)
            kbart_dict[record.issnl] = record

        counts["unique-issnl"] = len(kbart_dict)
        cur = db.db.cursor()
        for issnl, record in kbart_dict.items():
            info = DirectoryInfo(
                directory_slug=self.source_slug,
                issnl=record.issnl,
                issne=record.issne,
                issnp=record.issnp,
                name=record.title,
                publisher=record.publisher,
                homepage_urls=[],
                extra=dict(year_spans=record.year_spans),
            )
            if record.url:
                info.homepage_urls.append(record.url)
            status = db.insert_directory(info, cur=cur)
            counts[status] += 1
        cur.close()
        db.db.commit()
        return counts


class OnixCsvLoader(KbartLoader):
    """
    Similar to the KBART loader class, but for ONIX CSV files instead of KBART
    formatted TSV.

    CSV columns:
    - ISSN
    - Title
    - Publisher
    - Url
    - Vol
    - No
    - Published
    - Deposited
    """

    def open_file(self) -> Iterable:
        f = open(self.file_path(), "r")
        # skip first line of PKP PLN Onix file, which is a "generated date" header
        if self.source_slug == "pkp_pln":
            next(f)
        return csv.DictReader(f)

    def parse_record(self, row: dict, issn_db: IssnDatabase) -> Optional[KbartRecord]:

        raw_issn = clean_issn(row["ISSN"])
        issnl = issn_db.issn2issnl(raw_issn or "")
        start_year = int(row["Published"][:4])
        start_volume = clean_str(row["Vol"])
        record = KbartRecord(
            issnl=issnl,
            issne=None,
            issnp=None,
            embargo=None,
            title=clean_str(row["Title"]),
            publisher=clean_str(row["Publisher"]),
            url=HomepageUrl.from_url(row["Url"]),
            start_year=start_year,
            end_year=start_year,
            start_volume=start_volume,
            end_volume=start_volume,
            year_spans=[],
        )
        return record


class CarinianaCsvLoader(KbartLoader):
    """
    Similar to the KBART loader class, but for custom CSV files instead of
    KBART formatted TSV.

    CSV columns:
      - Region
      - Knowledge Area
      - Publisher
      - Title
      - ISSN
      - eISSN
      - Preserved Volumes
      - Preserved Years
      - In Progress Volumes
      - In Progress Years

    TODO: volumes
    """

    def open_file(self) -> Iterable:
        return csv.DictReader(open(self.file_path(), "r"))

    def parse_record(self, row: dict, issn_db: IssnDatabase) -> Optional[KbartRecord]:

        raw_issn = clean_issn(row["ISSN"])
        issne = clean_issn(row["ISSN"])
        issnl = issn_db.issn2issnl(raw_issn or issne or "")
        # convert list of years to a set of year spans
        years = [int(y.strip()) for y in row["Preserved Years"].split(";") if y]
        year_spans = merge_spans([], [[y, y] for y in years])
        record = KbartRecord(
            issnl=issnl,
            issne=issne,
            issnp=None,
            embargo=None,
            title=clean_str(row["Title"]),
            publisher=clean_str(row["Publisher"]),
            url=None,
            start_year=None,
            end_year=None,
            start_volume=None,
            end_volume=None,
            year_spans=year_spans,
        )
        return record


class HathifilesLoader(KbartLoader):
    """
    Similar to the KBART loader class, but for Hathifiles bulk format.

    Relevant TSV columns ("one-indexed", not zero-indexed):

    - 2 access (allow=bright, deny=dark)
    - 5 description
    - 10 issn ("multiple values separated by comma")
    - 12 title (if translated, separated by equals or slash)
    - 13 imprint (publisher and year; often "publisher, year")
    - 17 rights_date_used (year; 9999=unknown)
    - 19 lang (MARC format)
    """

    def open_file(self) -> Iterable:
        return csv.DictReader(
            open(self.file_path(), "r"),
            delimiter="\t",
            fieldnames=[
                "htid",
                "access",
                "rights",
                "ht_bib_key",
                "description",
                "source",
                "source_bib_num",
                "oclc_num",
                "isbn",
                "issn",
                "lccn",
                "title",
                "imprint",
                "rights_reason_code",
                "rights_timestamp",
                "us_gov_doc_flag",
                "rights_date_used",
                "pub_place",
                "lang",
                "bib_fmt",
                "collection_code",
                "content_provider_code",
                "responsible_entity_code",
                "digitization_agent_code",
                "access_profile_code",
                "author",
            ],
        )

    def parse_record(self, row: dict, issn_db: IssnDatabase) -> Optional[KbartRecord]:

        # unpack fields
        # access = dict(allow="bright", deny="dark")[row['access']]
        raw_issn = clean_issn(row["issn"].split(",")[0])
        imprint = clean_str(row["imprint"])
        raw_date = row["rights_date_used"].strip()

        issnl = issn_db.issn2issnl(raw_issn or "")

        rights_date: Optional[int] = None
        if raw_date.isdigit():
            rights_date = int(raw_date)
        start_year: Optional[int] = rights_date
        if start_year == 9999:
            start_year = None

        publisher: Optional[str] = None
        if imprint:
            publisher = imprint.split(".")[0].split(",")[0].split("[")[0].strip()

        record = KbartRecord(
            issnl=issnl,
            issne=None,
            issnp=None,
            embargo=None,
            title=clean_str(row["title"]),
            publisher=publisher,
            url=None,
            start_year=start_year,
            end_year=start_year,
            start_volume=None,
            end_volume=None,
            year_spans=[],
        )
        return record