aboutsummaryrefslogtreecommitdiff
path: root/doi2bib
blob: 1c26d21ab162e88de1d6cae640f856990d953d42 (plain)
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
#!/usr/bin/python3
# Adapted from https://scipython.com/blog/doi-to-bibtex/

import sys
import pycurl
from io import BytesIO
#import urllib.request
#from urllib.error import HTTPError

BASE_URL = 'http://dx.doi.org/'

try:
    doi = sys.argv[1]
except IndexError:
    print('Usage:\n{} <doi>'.format(sys.argv[0]))
    sys.exit(1)

url = BASE_URL + doi

# Urllib version
#req = urllib.request.Request(url)
#req.set_proxy('socks5://localhost:9050', 'socks')
#req.add_header('Accept', 'application/x-bibtex')

# PyCurl version with Tor support
# See http://pycurl.io/docs/latest/quickstart.html
buffer = BytesIO()
req    = pycurl.Curl()
req.setopt(req.URL, url)
req.setopt(req.HTTPHEADER, ('Accept: application/x-bibtex',))
req.setopt(req.FOLLOWLOCATION, True)
req.setopt(req.WRITEDATA, buffer)
req.setopt(req.PROXY, 'socks5://localhost:9050')

try:
    #with urllib.request.urlopen(req) as f:
    #    bibtex = f.read().decode()
    #print(bibtex)

    req.perform()
    req.close()
    body = buffer.getvalue()

    # Body is a byte string.
    # We have to know the encoding in order to print it to a text file
    # such as standard output.
    print(body.decode('iso-8859-1'))
except HTTPError as e:
    if e.code == 404:
        print('DOI not found.')
    else:
        print('Service unavailable.')
    sys.exit(1)