-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzotero-list.py
More file actions
executable file
·126 lines (88 loc) · 3.34 KB
/
zotero-list.py
File metadata and controls
executable file
·126 lines (88 loc) · 3.34 KB
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
#!/usr/bin/env python3
# Given a title and a Backlist post file, create a new collection
# on the Backlist Zotero group.
import os
import re
import sys
import bibtexparser
import json
from pyzotero import zotero
import backlist
library_id = os.getenv("BACKLIST_ZOT_LIBRARY_ID")
api_key = os.getenv("BACKLIST_ZOT_API_KEY")
def bibtex_data_from_file(f):
'''Given book file, strip YAML frontmatter and return only Bibtex data.'''
bibtex_result = ''
if f.readline() == '---\n':
yaml_active = True
for line in f.readlines():
if yaml_active:
if line == '---\n':
yaml_active = False
else:
bibtex_result += line
else:
for line in f.readlines():
bibtex_result += line
return bibtex_result
def create_collection(collection_name, entries):
test_collection = dict([
('name', collection_name)
])
response = zot.create_collection([test_collection])
collection_result = json.loads(response)
print("Creating Collection…")
collection_id = collection_result['success']['0']
print("Creating Items…")
created_items = zot.create_items(entries)
for key in created_items['successful'].keys():
print("Retrieve item…")
item = zot.item(created_items['successful'][key]['key'])
print("Add item to Collection…")
zot.addto_collection(collection_id, item)
def create_entry(entry):
entry_template = zot.item_template('book')
entry_template['creators'] = []
if 'author' in entry:
authors = creators_list(entry['author'], 'author')
entry_template['creators'] += authors
if 'editor' in entry:
editors = creators_list(entry['editor'], 'editor')
entry_template['creators'] += editors
if 'translator' in entry:
translators = creators_list(entry['translator'], 'translator')
entry_template['creators'] += translators
if 'series' in entry:
entry_template['series'] = entry['series']
entry_template['title'] = re.sub(r'[\{\}]',
'', entry['title'])
entry_template['publisher'] = entry['publisher']
entry_template['place'] = entry['address']
entry_template['date'] = entry['year']
return entry_template
def creators_list(creator_names, creator_type):
creators = []
for name in creator_names.split(' and '):
creator = {}
creator['creatorType'] = creator_type
creator['firstName'] = ' '.join(name.split(' ')[:-1])
creator['lastName'] = name.split(' ')[-1]
creators += [creator]
return creators
if __name__ == '__main__':
zot = zotero.Zotero(library_id, 'group', api_key)
collection_name = sys.argv[1]
project_path = sys.argv[2]
f = open(project_path)
yaml_frontmatter = backlist.grab_yaml_frontmatter(f)
books = backlist.book_ids_from_frontmatter(yaml_frontmatter)
book_data_paths = backlist.get_book_data_paths(project_path, books)
bibtex_string = ''
for path in book_data_paths:
book_file = open(path)
bibtex_string += bibtex_data_from_file(book_file)
bib_database = bibtexparser.loads(bibtex_string)
entries = []
for entry in bib_database.entries:
entries += [create_entry(entry)]
collection = create_collection(collection_name, entries)