forked from internetarchive/openlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoclc_to_marc.py
39 lines (31 loc) · 1.12 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
"""Find marc record URL from oclc number.
Usage: python oclc_to_marc.py oclc_1 oclc_2
"""
from __future__ import print_function
import urllib
import simplejson
root = "http://openlibrary.org"
def wget(path):
return simplejson.loads(urllib.urlopen(root + path).read())
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.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)