-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (50 loc) · 1.92 KB
/
utils.py
File metadata and controls
64 lines (50 loc) · 1.92 KB
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
from enum import Enum
from urllib.parse import quote
import requests
class SOURCE(Enum):
CROSS_REF = 1
OPEN_ALEX = 2
def fetch_doi_metadata(doi: str, source: SOURCE = SOURCE.CROSS_REF) -> dict:
"""Fetches metadata for a given DOI from the Crossref API."""
encoded_doi = quote(doi, safe='')
url = ""
if source == SOURCE.CROSS_REF:
url = f'https://api.crossref.org/works/{encoded_doi}'
else:
url = f'https://api.openalex.org/works/https://doi.org/{encoded_doi}'
headers = {'accept': 'application/json'}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching DOI {doi}: {e}")
return None
def build_reference_string(message: dict) -> str:
"""Build a human-readable reference string from work data."""
# Extract authors
authors = message.get('author', [])
author_str = ', '.join([f"{a.get('family', '')} {a.get('given', '')}"
for a in authors[:3]]) # First 3 authors
if len(authors) > 3:
author_str += " et al."
# Extract title
title = message.get('title', [''])[0] if message.get('title') else ''
# Extract year
issued = message.get('issued', {}).get('date-parts', [[]])[0]
year = issued[0] if issued else 'n.d.'
# Extract journal and volume/issue/pages
container_title = message.get('container-title', [''])[0] if message.get('container-title') else ''
volume = message.get('volume', '')
issue = message.get('issue', '')
page = message.get('page', '')
# Build reference
ref = f"{author_str} ({year}). {title}. {container_title}"
if volume:
ref += f", {volume}"
if issue:
ref += f"({issue})"
if page:
ref += f", {page}"
ref += "."
return ref