-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcrossref.py
56 lines (38 loc) · 1.36 KB
/
crossref.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
#!/usr/bin/env python
# encoding: utf-8
"""
doi.py
Created by Andrew Ning on April 4, 2013
"""
import requests
import alfred
import sys
# get query from user
query = sys.argv[1]
if query.endswith('!!'):
doi = query.split('!!')[0].rstrip().lstrip()
results = [alfred.Item(title='Lookup by DOI: ' + doi,
subtitle='Import BibTeX',
attributes={'uid': doi, 'arg': doi},
icon='crossref.png')]
else:
# Use crossref metadata search (beta) to get the DOI
params = {'q': query, 'rows': '10'}
r = requests.get('http://search.labs.crossref.org/dois', params=params)
# write results in XML format for Alfred
results = []
for j in r.json():
doi = j['doi'].split('dx.doi.org/')[1]
info = j['fullCitation']
entries = info.split('\'')
subtitle = entries[0]
if len(entries) > 1:
subtitle += (''.join(entries[2:])[2:])
# strip out html tag for italic
subtitle = subtitle.replace('<i>', '')
subtitle = subtitle.replace('</i>', '')
results.append(alfred.Item(title=j['title'],
subtitle=subtitle,
attributes={'uid': doi, 'arg': doi},
icon='crossref.png'))
sys.stdout.write(alfred.xml(results))