-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbibtex.py
170 lines (135 loc) · 5.06 KB
/
bibtex.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import bibtexparser
from bibtexparser.bibdatabase import BibDatabase
entry_type_by_crossref_type = {
'book-section': 'inbook',
'monograph': 'article',
'report': 'article',
'peer-review': 'misc',
'book-track': 'inbook',
'journal-article': 'article',
'book-part': 'inbook',
'other': 'misc',
'book': 'book',
'journal-volume': 'misc',
'book-set': 'misc',
'reference-entry': 'misc',
'proceedings-article': 'inproceedings',
'journal': 'misc',
'component': 'misc',
'book-chapter': 'inbook',
'proceedings-series': 'misc',
'report-series': 'misc',
'proceedings': 'proceedings',
'standard': 'misc',
'reference-book': 'book',
'posted-content': 'unpublished',
'journal-issue': 'misc',
'dissertation': 'phdthesis',
'grant': 'misc',
'dataset': 'misc',
'book-series': 'misc',
'edited-book': 'book',
'standard-series': 'misc',
}
def dump_bibtex(work):
entry_type = entry_type_by_crossref_type.get(work.get('type'))
work_id = work.get('id')
if not (entry_type and work_id):
return None
entry = {
'ENTRYTYPE': entry_type,
'ID': work_id,
'biburl': f'{work_id}.bib'.replace('openalex.org', 'api.openalex.org/works')
}
if doi := work.get('doi'):
entry['doi'] = doi.replace('https://doi.org/', '')
bib_db = BibDatabase()
_populate_entry(entry, work)
bib_db.entries.append(entry)
return bibtexparser.dumps(bib_db)
def _populate_entry(entry, work):
entry_type = entry['ENTRYTYPE']
if entry_type == 'article':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'journal', _journal_name(work))
_set_if(entry, 'year', _year(work))
_set_if(entry, 'volume', _volume(work))
_set_if(entry, 'number', _issue(work))
_set_if(entry, 'pages', _pages(work))
elif entry_type == 'book':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'publisher', _publisher(work))
_set_if(entry, 'year', _year(work))
elif entry_type == 'inbook':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'publisher', _publisher(work))
_set_if(entry, 'booktitle', _book_title(work))
_set_if(entry, 'year', _year(work))
_set_if(entry, 'pages', _pages(work))
elif entry_type == 'inproceedings':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'booktitle', _publisher(work))
_set_if(entry, 'year', _year(work))
_set_if(entry, 'pages', _pages(work))
elif entry_type == 'misc':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'howpublished', (work.get('host_venue') or {}).get('url'))
_set_if(entry, 'year', _year(work))
elif entry_type == 'phdthesis':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'school', _school(work))
_set_if(entry, 'year', _year(work))
elif entry_type == 'proceedings':
_set_if(entry, 'editor', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'series', _host_venue_display_name(work))
_set_if(entry, 'volume', _volume(work))
_set_if(entry, 'publisher', _publisher(work))
_set_if(entry, 'year', _year(work))
elif entry_type == 'unpublished':
_set_if(entry, 'author', _author(work))
_set_if(entry, 'title', work.get('title'))
_set_if(entry, 'year', _year(work))
_set_if(entry, 'howpublished', (work.get('host_venue') or {}).get('url'))
def _set_if(entry, key, value):
if value is not None:
entry[key] = value
def _year(work):
return work.get('publication_year') and str(work.get('publication_year'))
def _book_title(work):
return (work.get('host_venue') or {}).get('display_name')
def _school(work):
return ((work.get('authorships') or [{}])[0].get('institutions') or [{}])[0].get('display_name')
def _author(work):
return ' and '.join([
dn for dn in
[
(a.get('author') or {}).get('display_name')
for a in (work.get('authorships') or [])
]
if dn
])
def _journal_name(work):
return (work.get('host_venue') or {}).get('publisher')
def _publisher(work):
return (work.get('host_venue') or {}).get('publisher')
def _host_venue_display_name(work):
return (work.get('host_venue') or {}).get('display_name')
def _volume(work):
return (work.get('biblio') or {}).get('volume')
def _issue(work):
return (work.get('biblio') or {}).get('issue')
def _pages(work):
first_page = (work.get('biblio') or {}).get('first_page')
last_page = (work.get('biblio') or {}).get('last_page')
if first_page and last_page:
return f'{first_page}--{last_page}'
elif first_page:
return first_page
return None