-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_coda_report.py
1598 lines (1508 loc) · 60.1 KB
/
run_coda_report.py
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, argparse, csv, json
from py_dataset import dataset
import random
import requests
from idutils import is_doi, is_arxiv, normalize_doi, normalize_arxiv
from progressbar import progressbar
from ames.harvesters import get_caltechfeed, get_records, get_orcid_works
from ames.harvesters import get_eprint_keys, get_eprint
from ames.harvesters import doi_in_authors, get_extended
from ames.utils import is_in_range
def keep_record(metadata, years, item_type=None, group=None):
keep = True
if years:
# Not implemented for CaltechDATA
if "date" in metadata:
year = metadata["date"].split("-")[0]
if is_in_range(years, year) == False:
keep = False
else:
keep = False
if item_type:
# CaltechDATA item
if "resourceTye" in metadata:
if metadata["resourceType"]["resourceTypeGeneral"] not in item_type:
keep = False
# Eprints item
elif "type" in metadata:
if "monograph_type" in metadata:
# There are records with monograph_type that arn't monographs
if metadata["type"] == "monograph":
if (
metadata["monograph_type"] not in item_type
and metadata["type"] not in item_type
):
keep = False
else:
if metadata["type"] not in item_type:
keep = False
else:
if metadata["type"] not in item_type:
keep = False
else:
print("Item type not found in record")
keep = False
if group:
# Not implemented for CaltechDATA
if "local_group" in metadata:
match = False
if isinstance(metadata["local_group"]["items"], list) == False:
# Deal with single item listings
metadata["local_group"]["items"] = [metadata["local_group"]["items"]]
for gname in metadata["local_group"]["items"]:
if gname in group:
match = True
if match == False:
keep = False
else:
keep = False
return keep
def break_up_group(metadata, field, val, row):
"""Break up a array in 'field' into a single element with number 'val'"""
if field in metadata:
if len(metadata[field]) > val:
row.append(metadata[field][val])
else:
# This many values are not present, add blank cell
row.append("")
else:
# Metadata not present, add blank cell
row.append("")
return row
def thesis_metadata(file_obj, keys, source, years=None):
"""Output a full metadata report of information about theses"""
file_obj.writerow(
[
"Author-Family",
"Author-Given",
"Title",
"Abstract",
"Type",
"Awards",
"Degree-Date",
"Institution",
"Division",
"Option-Major",
"Option-Minor",
"Keywords",
"Advisor1-Family",
"Advisor1-Given",
"Advisor1-Role",
"Advisor2-Family",
"Advisor2-Given",
"Advisor2-Role",
"Advisor3-Family",
"Advisor3-Given",
"Advisor3-Role",
"Advisor4-Family",
"Advisor4-Given",
"Advisor4-Role",
"Persistent URL",
"DOI",
]
)
all_metadata = []
dot_paths = [
".creators.items[0].name.family",
".creators.items[0].name.given",
".title",
".abstract",
".thesis_type",
".thesis_awards",
".date",
".institution",
".divisions.items[0]",
".option_major.items",
".option_minor.items",
".keywords",
".thesis_advisor.items[:].name.family",
".thesis_advisor.items[:].name.given",
".thesis_advisor.items[:].role",
".official_url",
".doi",
]
labels = [
"creator_family",
"creator_given",
"title",
"abstract",
"thesis_type",
"thesis_awards",
"date",
"institution",
"division",
"major",
"minor",
"keywords",
"advisor_family",
"advisor_given",
"advisor_role",
"official_url",
"doi",
]
if source.split(".")[-1] == "ds":
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
all_metadata.sort(key=lambda all_metadata: all_metadata["creator_family"])
for metadata in all_metadata:
# Determine if we want the record
if keep_record(metadata, years):
row = []
if "creator_given" in metadata:
row.append(metadata["creator_family"])
row.append(metadata["creator_given"])
else:
row.append(metadata["creator_family"])
row.append("")
row.append(metadata["title"])
row.append(metadata["abstract"])
row.append(metadata["thesis_type"])
if "thesis_awards" in metadata:
award = metadata["thesis_awards"]
else:
award = ""
row.append(award)
row.append(metadata["date"])
row.append(metadata["institution"])
row.append(metadata["division"])
major = ""
for opt in metadata["major"]:
if major == "":
major = opt
else:
major += ", " + opt
row.append(major)
minor = ""
if "minor" in metadata:
for opt in metadata["minor"]:
if minor == "":
minor = opt
else:
minor += ", " + opt
row.append(minor)
row.append(metadata["keywords"])
for i in range(4):
record = break_up_group(metadata, "advisor_family", i, row)
record = break_up_group(metadata, "advisor_given", i, row)
record = break_up_group(metadata, "advisor_role", i, row)
row.append(metadata["official_url"])
if "doi" in metadata:
doi = metadata["doi"]
else:
doi = ""
row.append(doi)
file_obj.writerow(row)
def thesis_search(file_obj, keys, source, search):
all_metadata = []
dot_paths = [".option_major.items", ".option_minor.items", "._Key", ".thesis_type"]
labels = ["major", "minor", "key", "thesis_type"]
if source.split(".")[-1] == "ds":
all_metadata = get_records(dot_paths, "recs", source, keys, labels)
for metadata in all_metadata:
# Determine if we want the record
update = False
for opt in metadata["major"]:
if opt in search:
update = True
if "minor" in metadata:
for opt in metadata["minor"]:
if opt in search:
update = True
if update:
file_obj.writerow([metadata["key"], metadata["thesis_type"]])
def thesis_doi(keys, source):
all_metadata = []
dot_paths = [".doi", "._Key", ".eprint_status"]
labels = ["doi", "key", "status"]
if source.split(".")[-1] == "ds":
all_metadata = get_records(dot_paths, "recs", source, keys, labels)
count = 0
for metadata in all_metadata:
if metadata["status"] == "archive":
if "doi" not in metadata:
count = count + 1
print(f"{count} archived theses without DOIs")
def thesis_list(file_obj, keys, source, search):
all_metadata = []
dot_paths = [
".full_text_status",
".eprint_status",
"._Key",
".doi",
".creators.items[0].name.family",
".creators.items[0].name.given",
".date",
".thesis_type",
]
labels = [
"full_text_status",
"status",
"key",
"doi",
"family",
"given",
"date",
"type",
]
if source.split(".")[-1] == "ds":
all_metadata = get_records(dot_paths, "data", source, keys, labels)
all_metadata.sort(key=lambda all_metadata: all_metadata["family"])
all_metadata.sort(key=lambda all_metadata: all_metadata["date"])
phd = []
eng = []
masters = []
for metadata in all_metadata:
# Determine if we want the record
if metadata["status"] == "archive":
if "full_text_status" in metadata:
if metadata["full_text_status"] == search:
if metadata["type"] == "masters":
write_thesis(masters, metadata)
if metadata["type"] == "phd":
write_thesis(phd, metadata)
if metadata["type"] == "engd":
write_thesis(eng, metadata)
file_obj.writerow(["PHD"])
for line in phd:
file_obj.writerow(line)
file_obj.writerow(["Engineers"])
for line in eng:
file_obj.writerow(line)
file_obj.writerow(["Masters"])
for line in masters:
file_obj.writerow(line)
def write_thesis(struct, metadata):
struct.append(
[
metadata["date"],
metadata["family"] + "," + metadata["given"],
metadata["key"],
]
)
def thesis_report(file_obj, keys, source, years=None):
"""Output a report of information about theses"""
file_obj.writerow(
[
"Year",
"Author",
"ORCID",
"Title",
"Advisor",
"Type",
"Option",
"Persistent URL",
"DOI",
]
)
all_metadata = []
dot_paths = [
".date",
".creators.items[0].name.family",
".creators.items[0].name.given",
".creators.items[0].orcid",
".title",
".thesis_advisor.items[0].name.family",
".thesis_advisor.items[0].name.given",
".thesis_type",
".option_major.items",
".official_url",
".doi",
]
labels = [
"date",
"creator_family",
"creator_given",
"creator_orcid",
"title",
"advisor_family",
"advisor_given",
"thesis_type",
"option",
"official_url",
"doi",
]
if source.split(".")[-1] == "ds":
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
all_metadata.sort(key=lambda all_metadata: all_metadata["creator_family"])
for metadata in all_metadata:
# Determine if we want the record
if keep_record(metadata, years):
if "creator_given" in metadata:
creator = metadata["creator_family"] + ", " + metadata["creator_given"]
else:
creator = metadata["creator_family"]
if "advisor_family" in metadata:
advisor = metadata["advisor_family"] + ", " + metadata["advisor_given"]
else:
advisor = ""
if "creator_orcid" in metadata:
orcid = metadata["creator_orcid"]
else:
orcid = ""
if "doi" in metadata:
doi = metadata["doi"]
else:
doi = ""
option = ""
for opt in metadata["option"]:
if option == "":
option = opt
else:
option += ", " + opt
file_obj.writerow(
[
metadata["date"],
creator,
orcid,
metadata["title"],
advisor,
metadata["thesis_type"],
option,
metadata["official_url"],
doi,
]
)
def missing_pub_date(file_obj, keys, source):
"""Output a report of records without a publication date"""
file_obj.writerow(
[
"Eprint ID",
"Resolver URL",
]
)
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = ["._Key", ".official_url", ".date"]
labels = ["eprint_id", "official_url", "date"]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
ep = metadata["eprint_id"]
url = metadata["official_url"]
if "date" not in metadata:
file_obj.writerow([ep, url])
print("Report finished!")
def duplicate_doi(file_obj, keys, source):
"""Output a report of duplicate DOIs"""
file_obj.writerow(
[
"DOI",
"Eprint ID 1",
"Resolver URL",
"Eprint ID 2",
]
)
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = ["._Key", ".official_url", ".doi"]
labels = ["eprint_id", "official_url", "doi"]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
dois = {}
for metadata in all_metadata:
ep = metadata["eprint_id"]
url = metadata["official_url"]
if "doi" in metadata:
doi = metadata["doi"]
if doi in dois:
file_obj.writerow([doi, ep, url, dois[doi]])
else:
dois[doi] = ep
print("Report finished!")
def doi_report(
file_obj, keys, source, years=None, all_records=True, item_type=None, group=None
):
"""Output a report of DOIs"""
file_obj.writerow(
[
"Eprint ID",
"Suggested DOI",
"Year",
"Author ID",
"Title",
"Resolver URL",
"Series Information",
"Files",
"Possible DOI",
]
)
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = [
"._Key",
".doi",
".official_url",
".date",
".related_url",
".creators",
".title",
".local_group",
".type",
".monograph_type",
".other_numbering_system",
".series",
".number",
".documents",
]
labels = [
"eprint_id",
"doi",
"official_url",
"date",
"related_url",
"creators",
"title",
"local_group",
"type",
"monograph_type",
"other_numbering_system",
"series",
"number",
"documents",
]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
doi = ""
# Determine if we want the record
if keep_record(metadata, years, item_type, group):
ep = metadata["eprint_id"]
suggested = ""
maybe = ""
dois = set()
article_dois = set()
arxiv = ""
if "doi" not in metadata:
if "related_url" in metadata and "items" in metadata["related_url"]:
items = metadata["related_url"]["items"]
for item in items:
if "url" in item:
url = item["url"].strip()
if "type" in item:
itype = item["type"].strip().lower()
if "description" in item:
description = item["description"].strip().lower()
if itype == "doi":
if is_doi(url):
normal = normalize_doi(url)
dois.add(normal)
else:
# We have a incorrectly formatted DOI
normal = False
maybe += url
if description == "article":
if normal:
article_dois.add(normal)
elif itype == "arxiv":
aidv = url.split("/")[-1]
arxiv = "10.48550/arXiv." + aidv
elif is_doi(url):
dois.add(normalize_doi(url))
if len(dois) == 1: # some records have duplicate DOIs
suggested = dois.pop()
elif len(article_dois) == 1: # The article DOI is probably correct
suggested = article_dois.pop()
elif arxiv != "":
suggested = arxiv
elif len(dois) > 0:
maybe += str(dois)
if "creators" in metadata:
if "id" in metadata["creators"]["items"][0]:
author = metadata["creators"]["items"][0]["id"]
else:
author = ""
if "title" not in metadata:
print(f"Record {ep} is missing Title")
exit()
title = metadata["title"]
url = metadata["official_url"]
if "date" in metadata:
year = metadata["date"].split("-")[0]
else:
year = ""
# Series info
series = ""
if "other_numbering_system" in metadata:
num = metadata["other_numbering_system"]
series = "other numbering:"
for item in num["items"]:
if "id" in item:
if "name" in item:
series += " " + item["name"] + " " + item["id"]
else:
series += " " + item["id"]
else:
series += " " + item["name"]
if "series" in metadata:
series += "series:"
if "number" in metadata:
series += " " + metadata["series"] + " " + metadata["number"]
else:
series += " " + metadata["series"]
files = ""
if "documents" in metadata:
file = False
for d in metadata["documents"]:
if "security" in d:
if d["security"] != "internal":
file = True
if file:
files = "files-present"
file_obj.writerow(
[ep, suggested, year, author, title, url, series, files, maybe]
)
print("Report finished!")
def status_report(file_obj, keys, source):
"""Output a report of items that have a status other than archive
or have metadata visability other than show.
Under normal circumstances this should return no records when run on feeds"""
file_obj.writerow(["Eprint ID", "Resolver URL", "Status", "Local Group"])
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = [
"._Key",
".eprint_status",
".official_url",
".metadata_visibility",
".local_group.items",
]
labels = [
"eprint_id",
"eprint_status",
"official_url",
"metadata_visibility",
"local_group",
]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
if metadata["eprint_status"] != "archive":
ep = metadata["eprint_id"]
status = metadata["eprint_status"]
url = metadata["official_url"]
print("Record matched: ", url)
file_obj.writerow([ep, url, status])
if metadata["metadata_visibility"] != "show":
print(metadata["metadata_visibility"])
ep = metadata["eprint_id"]
status = metadata["metadata_visibility"]
url = metadata["official_url"]
print("Record matched: ", url)
group = ""
if "local_group" in metadata:
for g in metadata["local_group"]:
group = group + g + " "
file_obj.writerow([ep, url, status, group])
print("Report finished!")
def license_report(file_obj, keys, source, item_type=None, rtype="summary"):
"""Write report with license types"""
if source.split(".")[0] != "CaltechDATA":
print(source.split(".")[0] + " is not supported for license report")
exit()
else:
all_metadata = []
dot_paths = [
"._Key",
".rightsList",
".resourceType",
".creators",
".fundingReferences",
]
labels = ["id", "rightsList", "resourceType", "creators", "fundingReferences"]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
licenses = {}
if rtype == "summary":
file_obj.writerow(["License Name", "Number of Records", "IDs"])
for metadata in all_metadata:
if item_type != None:
# Restrict to a specific item type
if metadata["resourceType"]["resourceTypeGeneral"] in item_type:
if "rightsList" in metadata:
lic = metadata["rightsList"][0]["rights"]
else:
lic = None
else:
lic = None
# Otherwise we always save license
else:
if "rightsList" in metadata:
lic = metadata["rightsList"][0]["rights"]
else:
lic = None
if lic != None:
if lic in licenses:
licenses[lic]["count"] += 1
licenses[lic]["ids"].append(metadata["id"])
else:
new = {}
new["count"] = 1
new["ids"] = [metadata["id"]]
licenses[lic] = new
for lic in licenses:
file_obj.writerow([lic, licenses[lic]["count"], licenses[lic]["ids"]])
else:
file_obj.writerow(["CaltechDATA ID", "Authors", "License", "Funders"])
for metadata in all_metadata:
write = False
if item_type != None:
# Restrict to a specific item type
if metadata["resourceType"]["resourceTypeGeneral"] in item_type:
write = True
# Otherwise we always save license
else:
write = True
if write == True:
creators = ""
for c in metadata["creators"]:
if creators != "":
creators += ";"
creators += c["creatorName"]
funders = ""
if "fundingReferences" in metadata:
for c in metadata["fundingReferences"]:
if funders != "":
funders += ";"
funders += c["funderName"]
if "rightsList" not in metadata:
print("record ", metadata["id"], " is missing license")
exit()
file_obj.writerow(
[
metadata["id"],
creators,
metadata["rightsList"][0]["rights"],
funders,
]
)
def file_report(file_obj, keys, source, years=None):
"""Write out a report of files with potential issues"""
file_obj.writerow(["Eprint ID", "Problem", "Impacted Files", "Resolver URL"])
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = ["._Key", ".documents", ".date", ".official_url"]
labels = ["eprint_id", "documents", "date", "official_url"]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
if "date" in metadata:
year = metadata["date"].split("-")[0]
if is_in_range(years, year):
if "documents" in metadata:
for d in metadata["documents"]:
if "files" in d:
for f in d["files"]:
filename = f["filename"]
extension = filename.split(".")[-1]
if extension == "html":
ep = metadata["eprint_id"]
url = metadata["official_url"]
print("HTML: ", url)
file_obj.writerow([ep, "HTML File", filename, url])
if extension == "htm":
ep = metadata["eprint_id"]
url = metadata["official_url"]
print("HTM: ", url)
file_obj.writerow([ep, "HTML File", filename, url])
if len(filename) > 200:
ep = metadata["eprint_id"]
url = metadata["official_url"]
print("Length: ", url)
file_obj.writerow(
[ep, "File Name Length", filename, url]
)
print("Report finished!")
def group_search(file_obj, keys, source, search, years=None):
"""Search for group name in Additional Information and Funders fields"""
file_obj.writerow(["Eprint ID", "Resolver URL", "Groups"])
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = [
"._Key",
".note",
".date",
".funders.items",
".official_url",
".local_group.items",
]
labels = ["eprint_id", "note", "date", "funders", "official_url", "groups"]
all_metadata = get_records(dot_paths, "group", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
if "date" in metadata:
year = metadata["date"].split("-")[0]
if is_in_range(years, year):
keep = False
if "note" in metadata:
if search in metadata["note"]:
keep = True
if "funders" in metadata:
for f in metadata["funders"]:
if "agency" in f:
if search in f["agency"]:
keep = True
if "groups" in metadata:
for f in metadata["groups"]:
if search in f:
keep = True
if keep:
ep = metadata["eprint_id"]
url = metadata["official_url"]
if "groups" in metadata:
groups = metadata["groups"]
else:
groups = ""
file_obj.writerow([ep, url, groups])
print("Report finished!")
def thesis_group_search(file_obj, keys, source, search, years=None):
"""Search for group name in Major field"""
file_obj.writerow(["Eprint ID", "Resolver URL", "Groups"])
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = [
"._Key",
".note",
".date",
".funders.items",
".official_url",
".local_group.items",
]
labels = ["eprint_id", "note", "date", "funders", "official_url", "groups"]
all_metadata = get_records(dot_paths, "group", source, keys, labels)
else:
for eprint_id in progressbar(keys, redirect_stdout=True):
all_metadata.append(get_eprint(source, eprint_id))
for metadata in all_metadata:
if "date" in metadata:
year = metadata["date"].split("-")[0]
if is_in_range(years, year):
keep = False
if "note" in metadata:
if search in metadata["note"]:
keep = True
if "funders" in metadata:
for f in metadata["funders"]:
if "agency" in f:
if search in f["agency"]:
keep = True
if "groups" in metadata:
for f in metadata["groups"]:
if search in f:
keep = True
if keep:
ep = metadata["eprint_id"]
url = metadata["official_url"]
if "groups" in metadata:
groups = metadata["groups"]
else:
groups = ""
file_obj.writerow([ep, url, groups])
print("Report finished!")
def people_search(file_obj, keys, source, names=None, division=None, years=None):
"""Search for people by division in CaltechPEOPLE"""
all_metadata = []
if source == "CaltechPEOPLE.ds":
dot_paths = [
"._Key",
".directory_info",
".orcid",
".sort_name",
".authors_id",
".CaltechAUTHORS",
]
labels = ["id", "directory_info", "orcid", "name", "authors_id", "authors"]
all_metadata = get_records(dot_paths, "p_list", source, keys, labels)
else:
print("The people_search report only works with the CaltechPEOPLE source")
exit()
all_metadata.sort(key=lambda all_metadata: all_metadata["id"])
ids = set()
for metadata in all_metadata:
if names:
if "authors_id" in metadata:
if metadata["authors_id"] in names:
if "authors" in metadata:
for key in metadata["authors"]:
ids.add(key)
elif "directory_info" in metadata:
directory = metadata["directory_info"]
keep = True
if "division" in directory:
if division:
if division != directory["division"]:
keep = False
if keep:
if "orcid" in metadata:
orcid = metadata["orcid"]
else:
orcid = ""
if "authors_id" in metadata:
authors = metadata["authors_id"]
else:
authors = ""
file_obj.writerow(
[
metadata["name"],
metadata["id"],
authors,
orcid,
directory["directory_person_type"],
directory["title"],
directory["division"],
directory["bio"],
]
)
if names:
in_range = []
dot_paths = ["._Key", ".date", ".local_group.items"]
labels = ["eprint_id", "date", "group"]
keys = dataset.keys("CaltechAUTHORS.ds")
all_metadata = get_records(dot_paths, "dois", "CaltechAUTHORS.ds", keys, labels)
for metadata in all_metadata:
to_do = True
if "group" in metadata:
for g in metadata["group"]:
if g == "Astronomy Department":
to_do = False
if years:
if "date" in metadata:
year = metadata["date"].split("-")[0]
if is_in_range(years, year) == False:
to_do = False
if to_do:
metadata_id = metadata["eprint_id"]
if metadata_id in ids:
file_obj.writerow([metadata_id])
print("Report finished!")
def record_number_report(file_obj, keys, source):
"""Write out a report of records where the record number doesn't match the
resolver URL"""
file_obj.writerow(["Eprint ID", "Record Number", "Resolver URL"])
all_metadata = []
if source.split(".")[-1] == "ds":
dot_paths = ["._Key", ".id_number", ".official_url"]
labels = ["eprint_id", "record_number", "official_url"]
all_metadata = get_records(dot_paths, "dois", source, keys, labels)
for metadata in all_metadata:
ep = metadata["eprint_id"]
resolver = metadata["official_url"]
number = metadata["record_number"]
resolver_tag = resolver.split("/")[-1]
if resolver_tag != number:
file_obj.writerow([ep, number, resolver])
print("Report finished!")
def alt_url_report(file_obj, keys, source):
print(f"Processing {len(keys)} eprint records for alt_url")
file_obj.writerow(["eprint_id", "alt_url", "related_url", "url_added", "type"])
if source.split(".")[-1] == "ds":
dot_paths = ["._Key", ".related_url.items", ".alt_url"]
labels = ["eprint_id", "items", "alt_url"]
all_metadata = get_records(dot_paths, "alt", source, keys, labels)
for metadata in progressbar(all_metadata, redirect_stdout=True):
key = metadata["eprint_id"]
if "alt_url" in metadata:
alt = metadata["alt_url"]
related = ""
type_v = "Other"
new_url = alt
if is_doi(new_url):
type_v = "DOI"
new_url = "https://doi.org/" + normalize_doi(new_url)
if is_arxiv(new_url):
type_v = "arXiv"
if "items" in metadata:
for i in metadata["items"]:
if i["url"] == new_url:
new_url = ""
if is_doi(i["url"]):
norm_rel = normalize_doi(i["url"])
norm = alt
# Make sure both are normalized
if type_v == "DOI":
norm = normalize_doi(norm)