This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerateCollectionLevelAbstract.py
86 lines (70 loc) · 2.92 KB
/
generateCollectionLevelAbstract.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
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
import json
import requests
import secrets
import csv
import argparse
import urllib3
secretsVersion = input('To edit production server, enter the name of the secrets file: ')
if secretsVersion != '':
try:
secrets = __import__(secretsVersion)
print('Editing Production')
except ImportError:
print('Editing Stage')
else:
print('Editing Stage')
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--fileNameCSV', help='the metadata CSV file. optional - if not provided, the script will ask for input')
parser.add_argument('-i', '--handle', help='handle of the collection. optional - if not provided, the script will ask for input')
args = parser.parse_args()
if args.fileNameCSV:
fileNameCSV =args.fileNameCSV
else:
fileNameCSV = input('Enter the metadata CSV file (including \'.csv\'): ')
if args.handle:
handle = args.handle
else:
handle = input('Enter collection handle: ')
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
baseURL = secrets.baseURL
email = secrets.email
password = secrets.password
filePath = secrets.filePath
verify = secrets.verify
skippedCollections = secrets.skippedCollections
data = {'email':email,'password':password}
header = {'content-type':'application/json','accept':'application/json'}
session = requests.post(baseURL+'/rest/login', headers=header, verify=verify, params=data).cookies['JSESSIONID']
cookies = {'JSESSIONID': session}
headerFileUpload = {'accept':'application/json'}
status = requests.get(baseURL+'/rest/status', headers=header, cookies=cookies, verify=verify).json()
userFullName = status['fullname']
print('authenticated')
endpoint = baseURL+'/rest/handle/'+handle
collection = requests.get(endpoint, headers=header, cookies=cookies, verify=verify).json()
collectionID = collection['uuid']
print(collection)
#Enter abstract text here
abstractText = ''
seriesTitles = []
with open(fileNameCSV) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
seriesTitle = row['Series title']
if seriesTitle not in seriesTitles:
seriesTitles.append(seriesTitle)
seriesLinks = ''
for seriesTitle in seriesTitles:
handleEdited = handle.replace('/', '%2F')
editedSeriesTitle = seriesTitle.replace(' ','+')
seriesLink = '<li><a href="https://jscholarship.library.jhu.edu/discover?scope='+handleEdited+'&query=%22'+editedSeriesTitle+'%22&sort_by=dc.title_sort&order=asc&submit=">'+seriesTitle+'</a></li>'
seriesLinks += seriesLink
abstractText = '<p>'+abstractText+'</p>'
seriesLinks = '<ul>'+seriesLinks+'</ul>'
introductoryText = abstractText + seriesLinks
collection['introductoryText'] = introductoryText
collection = json.dumps(collection)
print(collection)
post = requests.put(baseURL+'/rest/collections/'+collectionID, headers=header, cookies=cookies, verify=verify, data=collection)
print(post)
logout = requests.post(baseURL+'/rest/logout', headers=header, cookies=cookies, verify=verify)