-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_info.py
More file actions
52 lines (45 loc) · 1.72 KB
/
Copy pathupdate_info.py
File metadata and controls
52 lines (45 loc) · 1.72 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
import json, requests, sys
import os.path, os
from getcbd import site_url, transform_doi
try:
with open('list_cache.json') as fp:
a = json.load(fp)
except:
print('Reading the cache failed, please run getcbd with -r option!', file=sys.stderr)
sys.exit(1)
def get_product_info(prodname):
''' Returns the information on the product, given its name in full.
The returned tuple contains a URL and the maximum number of its images.
For example, ('https://127.0.0.1/files', 5, '10.1038/NCHEM.1072')
The spaces in the path will be replaced by %20.
Raises ValueError if the name was not found.
'''
path = None
for route in a:
if route['full'] == prodname:
path = route['path']
break
if path is None:
raise ValueError("The product was not found!")
resp = requests.get(site_url + 'infoJSON.php?dir=' + path).json()
return (site_url + resp['path'].replace(' ', '%20'), int(resp['max']), transform_doi(resp['doi']))
if os.path.isfile('info_cache.json'):
print('There is already a info_cache.json in this directory!')
if os.system('choice /m "Start from scratch?"') == 1:
lengths = dict()
else:
with open('info_cache.json') as fp:
lengths = json.load(fp)
else:
lengths = dict()
n = len(lengths)
try:
for (number, prod) in enumerate(a):
if number < n:
continue
print(f'Getting info of number {number}')
# lengths.append((getcbd.get_product_info(prod['full']), prod['full']))
lengths[prod['full']] = get_product_info(prod['full'])
except BaseException as ex:
print(ex.args)
json.dump(lengths, open('info_cache.json', 'w'))