forked from internetarchive/openlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoclc_to_marc.py
47 lines (32 loc) · 1.09 KB
/
oclc_to_marc.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
"""Find marc record URL from oclc number.
Usage: python oclc_to_marc.py oclc_1 oclc_2
"""
import requests
import urllib
root = "https://openlibrary.org"
def wget(path):
return requests.get(root + path).json()
def find_marc_url(d):
if d.get('source_records'):
return d['source_records'][0]
# some times initial revision is 2 instead of 1. So taking first 3 revisions (in reverse order)
# and picking the machine comment from the last one
result = wget('%s.json?m=history&offset=%d' % (d['key'], d['revision'] - 3))
if result:
return result[-1]['machine_comment'] or ""
else:
return ""
def main(oclc):
query = urllib.parse.urlencode(
{'type': '/type/edition', 'oclc_numbers': oclc, '*': ''}
)
result = wget('/query.json?' + query)
for d in result:
print("\t".join([oclc, d['key'], find_marc_url(d)]))
if __name__ == "__main__":
import sys
if len(sys.argv) == 1 or "-h" in sys.argv or "--help" in sys.argv:
print(__doc__, file=sys.stderr)
else:
for oclc in sys.argv[1:]:
main(oclc)