litdb 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
litdb/__init__.py ADDED
@@ -0,0 +1 @@
1
+ import litdb.lab # noqa: F401
litdb/bibtex.py ADDED
@@ -0,0 +1,182 @@
1
+ # From https://raw.githubusercontent.com/ourresearch/openalex-formatter/refs/heads/main/bibtex.py
2
+ import bibtexparser
3
+ from bibtexparser.bibdatabase import BibDatabase
4
+
5
+ entry_type_by_crossref_type = {
6
+ "book-section": "inbook",
7
+ "monograph": "article",
8
+ "report": "article",
9
+ "peer-review": "misc",
10
+ "book-track": "inbook",
11
+ "review": "article", # new line added
12
+ "preprint": "article", # new line added
13
+ "article": "article", # new line added
14
+ "journal-article": "article",
15
+ "book-part": "inbook",
16
+ "other": "misc",
17
+ "paratext": "misc", # new line added
18
+ "book": "book",
19
+ "journal-volume": "misc",
20
+ "book-set": "misc",
21
+ "reference-entry": "misc",
22
+ "proceedings-article": "inproceedings",
23
+ "journal": "misc",
24
+ "component": "misc",
25
+ "book-chapter": "inbook",
26
+ "proceedings-series": "misc",
27
+ "report-series": "misc",
28
+ "proceedings": "proceedings",
29
+ "standard": "misc",
30
+ "reference-book": "book",
31
+ "posted-content": "unpublished",
32
+ "journal-issue": "misc",
33
+ "dissertation": "phdthesis",
34
+ "grant": "misc",
35
+ "dataset": "misc",
36
+ "book-series": "misc",
37
+ "edited-book": "book",
38
+ "standard-series": "misc",
39
+ }
40
+
41
+
42
+ def dump_bibtex(work):
43
+ # entry_type = entry_type_by_crossref_type.get(work.get('type'))
44
+ entry_type = entry_type_by_crossref_type.get(work.get("type_crossref"))
45
+
46
+ # work_id = work.get('id')
47
+ work_id = work.get("doi") or work.get("id")
48
+
49
+ if not (entry_type and work_id):
50
+ print(f"Unable to generate a bibtex entry for {work_id} ({entry_type})")
51
+ return None
52
+
53
+ entry = {
54
+ "ENTRYTYPE": entry_type,
55
+ "ID": work_id,
56
+ "biburl": f"{work_id}.bib".replace("openalex.org", "api.openalex.org/works"),
57
+ }
58
+
59
+ if doi := work.get("doi"):
60
+ entry["doi"] = doi.replace("https://doi.org/", "")
61
+
62
+ bib_db = BibDatabase()
63
+ _populate_entry(entry, work)
64
+
65
+ bib_db.entries.append(entry)
66
+ return bibtexparser.dumps(bib_db)
67
+
68
+
69
+ def _populate_entry(entry, work):
70
+ entry_type = entry["ENTRYTYPE"]
71
+
72
+ if entry_type == "article":
73
+ _set_if(entry, "author", _author(work))
74
+ _set_if(entry, "title", work.get("title"))
75
+ _set_if(entry, "journal", _journal_name(work))
76
+ _set_if(entry, "year", _year(work))
77
+ _set_if(entry, "volume", _volume(work))
78
+ _set_if(entry, "number", _issue(work))
79
+ _set_if(entry, "pages", _pages(work))
80
+ elif entry_type == "book":
81
+ _set_if(entry, "author", _author(work))
82
+ _set_if(entry, "title", work.get("title"))
83
+ _set_if(entry, "publisher", _publisher(work))
84
+ _set_if(entry, "year", _year(work))
85
+ elif entry_type == "inbook":
86
+ _set_if(entry, "author", _author(work))
87
+ _set_if(entry, "title", work.get("title"))
88
+ _set_if(entry, "publisher", _publisher(work))
89
+ _set_if(entry, "booktitle", _book_title(work))
90
+ _set_if(entry, "year", _year(work))
91
+ _set_if(entry, "pages", _pages(work))
92
+ elif entry_type == "inproceedings":
93
+ _set_if(entry, "author", _author(work))
94
+ _set_if(entry, "title", work.get("title"))
95
+ _set_if(entry, "booktitle", _publisher(work))
96
+ _set_if(entry, "year", _year(work))
97
+ _set_if(entry, "pages", _pages(work))
98
+ elif entry_type == "misc":
99
+ _set_if(entry, "author", _author(work))
100
+ _set_if(entry, "title", work.get("title"))
101
+ _set_if(entry, "howpublished", (work.get("host_venue") or {}).get("url"))
102
+ _set_if(entry, "year", _year(work))
103
+ elif entry_type == "phdthesis":
104
+ _set_if(entry, "author", _author(work))
105
+ _set_if(entry, "title", work.get("title"))
106
+ _set_if(entry, "school", _school(work))
107
+ _set_if(entry, "year", _year(work))
108
+ elif entry_type == "proceedings":
109
+ _set_if(entry, "editor", _author(work))
110
+ _set_if(entry, "title", work.get("title"))
111
+ _set_if(entry, "series", _host_venue_display_name(work))
112
+ _set_if(entry, "volume", _volume(work))
113
+ _set_if(entry, "publisher", _publisher(work))
114
+ _set_if(entry, "year", _year(work))
115
+ elif entry_type == "unpublished":
116
+ _set_if(entry, "author", _author(work))
117
+ _set_if(entry, "title", work.get("title"))
118
+ _set_if(entry, "year", _year(work))
119
+ _set_if(entry, "howpublished", (work.get("host_venue") or {}).get("url"))
120
+
121
+
122
+ def _set_if(entry, key, value):
123
+ if value is not None:
124
+ entry[key] = value
125
+
126
+
127
+ def _year(work):
128
+ return work.get("publication_year") and str(work.get("publication_year"))
129
+
130
+
131
+ def _book_title(work):
132
+ return (work.get("host_venue") or {}).get("display_name")
133
+
134
+
135
+ def _school(work):
136
+ return ((work.get("authorships") or [{}])[0].get("institutions") or [{}])[0].get(
137
+ "display_name"
138
+ )
139
+
140
+
141
+ def _author(work):
142
+ return " and ".join(
143
+ [
144
+ dn
145
+ for dn in [
146
+ (a.get("author") or {}).get("display_name")
147
+ for a in (work.get("authorships") or [])
148
+ ]
149
+ if dn
150
+ ]
151
+ )
152
+
153
+
154
+ def _journal_name(work):
155
+ return (work.get("host_venue") or {}).get("publisher")
156
+
157
+
158
+ def _publisher(work):
159
+ return (work.get("host_venue") or {}).get("publisher")
160
+
161
+
162
+ def _host_venue_display_name(work):
163
+ return (work.get("host_venue") or {}).get("display_name")
164
+
165
+
166
+ def _volume(work):
167
+ return (work.get("biblio") or {}).get("volume")
168
+
169
+
170
+ def _issue(work):
171
+ return (work.get("biblio") or {}).get("issue")
172
+
173
+
174
+ def _pages(work):
175
+ first_page = (work.get("biblio") or {}).get("first_page")
176
+ last_page = (work.get("biblio") or {}).get("last_page")
177
+
178
+ if first_page and last_page:
179
+ return f"{first_page}--{last_page}"
180
+ elif first_page:
181
+ return first_page
182
+ return None