-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtext2doi
executable file
·42 lines (32 loc) · 881 Bytes
/
text2doi
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# text2doi
#
# purpose: get DOI from references in text format (one per line)
# license: MIT License
# author: François-Xavier Coudert
# e-mail: [email protected]
#
import codecs
import locale
import requests
import sys
def main():
# Set our output to the right encoding if none was chosen
if sys.stdout.encoding is None:
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
for s in sys.stdin.readlines():
s = s.strip()
if len(s) == 0:
continue
try:
print(getDOI(s))
except Exception:
print("Reference not found: " + s)
def getDOI(s):
p = {"q": s, "sort": "score"}
r = requests.get("https://search.crossref.org/dois", params=p)
return r.json()[0]["doi"]
if __name__ == '__main__':
main()