forked from krzywon/saspubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_current_list.py
56 lines (49 loc) · 2.38 KB
/
update_current_list.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
import os
import json
import datetime
from config import GROUPS, DB_FILENAME_FMT, DB_PATH, VERSION_FILENAME_FMT,\
crossref_keys_to_update
from instrument_zotero_feed import append_from_crossref, extract_doi
from instrument_zotero_feed import main as zotero_feed_main
def find_older_duplicates(db, keys):
accessed = [db[key]['accessed']['date-parts'][0] if key in db.keys() and
'accessed' in db[key].keys() and 'date-parts' in
db[key]['accessed'].keys() else [1900, 1, 1] for key in keys]
dates = [datetime.date(acc[0], acc[1], acc[2]) for acc in accessed]
return keys.pop(dates.index(min(dates)))
def check_all_against_current(update_group, zotero_key=''):
csl_db_filename = DB_FILENAME_FMT.format(group=update_group)
csl_db_path = os.path.join(DB_PATH, csl_db_filename)
db = json.loads(open(csl_db_path, 'r').read())
doi_dict = dict([(key, extract_doi(item)) for key, item in db.items()])
duplicate_dict = {} # {doi: [key1, key2, ... , keyN]}
for key, doi in doi_dict.items():
duplicate_dict.setdefault(doi, []).append(key)
remove_list = [find_older_duplicates(db, key_list) for doi, key_list in
duplicate_dict.items() if len(key_list) > 1 and doi]
update_list = [item for item in db.values()]
changes = append_from_crossref(update_list,
keys_to_update=crossref_keys_to_update,
values_to_delete=remove_list,
group=update_group, api_key=zotero_key)
if changes:
version_filename = VERSION_FILENAME_FMT.format(group=update_group)
version_path = os.path.join(DB_PATH, version_filename)
md_path = "./static/{group}_publications.md".format(group=update_group)
# Remove files, if they exist, to be sure updates take effect
if os.path.isfile(csl_db_path):
os.remove(csl_db_path)
if os.path.isfile(version_path):
os.remove(version_path)
if os.path.isfile(md_path):
os.remove(md_path)
zotero_feed_main([update_group])
if __name__ == '__main__':
import sys
if len(sys.argv) > 2:
check_all_against_current(sys.argv[1], zotero_key=sys.argv[2])
elif len(sys.argv) > 1:
check_all_against_current(sys.argv[1])
else:
for group in GROUPS:
check_all_against_current(group)