-
Notifications
You must be signed in to change notification settings - Fork 0
/
fulltextURL.py
79 lines (68 loc) · 1.99 KB
/
fulltextURL.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
import requests
import json
import sys
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
ACCESS_TOKEN = config['thirdiron']['ACCESS_TOKEN']
LIBRARY = config['thirdiron']['LIBRARY']
def formatTitle(title):
y = ''
for x in title:
if x == ' ':
y = y + '+'
elif x.isalnum():
y = y + x
return y
def formatAuthor(author):
y = author.split(',')[0]
return y
def checkdoi(title,author):
z = title
searchQueryTitle = formatTitle(title)
searchQueryAuthor = formatAuthor(author)
url = 'https://api.crossref.org/works?query.bibliographic='+searchQueryTitle+'&query.author='+searchQueryAuthor
try:
x = requests.get(url,timeout=60)
except:
return -1
else:
x = json.loads(x.content)
resultCount = int(x['message']['items-per-page'])
for count in range(resultCount):
if(x['message']['items'][count]['title'][0].lower() == z.lower()):
return x['message']['items'][count]['title'][0], x['message']['items'][count]['DOI']
return -2
def getfullTextFile(doi):
url = 'https://public-api.thirdiron.com/public/v1/libraries/'+LIBRARY+'/articles/doi/'+doi+'?access_token='+ACCESS_TOKEN
x = requests.get(url,timeout=60)
try:
x = json.loads(x.content)
except:
return -3
else:
return x['data']['title'],x['data']['fullTextFile'],x['data']['authors']
def resolveURL(title,author):
title1,doi = checkdoi(title,author)
title2, url, authors = getfullTextFile(doi)
if title1 == title2:
data = {
'title':title,
'doi':doi,
'authors':authors,
'url':url
}
return data
else:
return -4
if __name__ == "__main__":
print(sys.argv[1])
print(sys.argv[2])
x = ''
try:
x = resolveURL(sys.argv[1],sys.argv[2])
x = json.dumps(x)
except:
print(x)
else:
print(x)