Last active
January 31, 2023 01:34
-
-
Save hornc/03ebb32b870f078f8b354f44651743d9 to your computer and use it in GitHub Desktop.
Move OL editions to correct Work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from olclient.openlibrary import OpenLibrary | |
ol = OpenLibrary() | |
def move_editions(editions, master): | |
"""Sets a list of <editions> to a new master work ID using the Open Library Client. | |
Copies across extra author data from editions to master, if not already present. | |
Example: | |
move_editions(['OL8306567M','OL16682544M'], 'OL4005510W') | |
""" | |
output = [] | |
authors = [] | |
for e in editions: | |
ed = ol.get(e) | |
for a in ed.authors: | |
if a.type['key'] == '/type/delete': | |
ed.authors.remove(a) | |
ed.work_olid = master | |
#ed.validate() | |
authors += [a.olid for a in ed.authors if a.olid not in authors] | |
output.append(ed) | |
w = ol.get(master) | |
existing_authors = [a['author']['key'].replace('/authors/', '') for a in (w.authors if hasattr(w, 'authors') else [])] | |
authors = [a for a in authors if a not in existing_authors] | |
for a in authors: | |
if not hasattr(w, 'authors'): | |
w.authors = [] | |
w.add_author(ol.get(a)) | |
if authors: # only save if we have made changes | |
output.append(w) | |
return ol.save_many(output, "associate %i editions with work %s" % (len(editions), master)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment