aboutsummaryrefslogtreecommitdiff
path: root/doi2bib
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2024-08-20 20:42:26 -0300
committerSilvio Rhatto <rhatto@riseup.net>2024-08-20 20:42:26 -0300
commitd1791e72e2e1442033cad2ad7ef1dd183562ddec (patch)
tree1a6f087ba2d77d6a7f69230da2ec8ba30c5b0f00 /doi2bib
downloadutils-doc-d1791e72e2e1442033cad2ad7ef1dd183562ddec.tar.gz
utils-doc-d1791e72e2e1442033cad2ad7ef1dd183562ddec.tar.bz2
Initial import
Diffstat (limited to 'doi2bib')
-rwxr-xr-xdoi2bib54
1 files changed, 54 insertions, 0 deletions
diff --git a/doi2bib b/doi2bib
new file mode 100755
index 0000000..b669737
--- /dev/null
+++ b/doi2bib
@@ -0,0 +1,54 @@
+#!/usr/bin/python3
+# Adapted from https://scipython.com/blog/doi-to-bibtex/
+
+import os
+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: {} <doi>'.format(os.path.basename(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)