Datasets:
Download code/wasp26_lib/get_citations_refs.py from adsabs/AstroCLIMB: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/datasets/adsabs/AstroCLIMB/resolve/main/code/wasp26_lib/get_citations_refs.py
- Command line
-
hf download hf://datasets/adsabs/AstroCLIMB/code/wasp26_lib/get_citations_refs.py
-
curl -L -o get_citations_refs.py https://huggingface.co/datasets/adsabs/AstroCLIMB/resolve/main/code/wasp26_lib/get_citations_refs.py
2.27 kB
| import requests | |
| from urllib.parse import urlencode | |
| def get_citations_refs(doi, scix_token): | |
| """ | |
| Given a DOI and a valid SciX APOI token, returns lists of DOIs of papers referenced and citing the parameter DOI | |
| Also returns alternates DOIs | |
| """ | |
| ref_DOIs, ref_DOIs_alt, cit_DOIs, cit_DOIs_alt = None,None,None,None | |
| # query for refs | |
| encoded_query = urlencode({'q': f'references(doi:{doi})', | |
| 'fl': 'title, bibcode, doi', | |
| 'rows': 2000 | |
| }) | |
| try: | |
| req = requests.get(f'https://api.adsabs.harvard.edu/v1/search/query?{encoded_query}', \ | |
| headers={'Authorization': f'Bearer {scix_token}'}) | |
| req.raise_for_status() | |
| except: | |
| print(f'SciX references request failed. Status code {req.status_code}. URL {doi}') | |
| else: | |
| ref_DOIs = [] | |
| ref_DOIs_alt = [] | |
| for d in req.json()['response']['docs']: | |
| if 'doi' in d.keys(): | |
| ref_DOIs.append(d['doi'][0]) | |
| ref_DOIs_alt.append(d['doi'][1:]) | |
| # ref_DOIs = [d['doi'][0] for d in req.json()['response']['docs']] | |
| # ref_DOIs_alt = [d['doi'][1:] for d in req.json()['response']['docs']] | |
| # query for citations | |
| encoded_query = urlencode({'q': f'citations(doi:{doi})', | |
| 'fl': 'title, bibcode, doi', | |
| 'rows': 2000, | |
| }) | |
| try: | |
| req = requests.get(f'https://api.adsabs.harvard.edu/v1/search/query?{encoded_query}', \ | |
| headers={'Authorization': f'Bearer {scix_token}'}) | |
| req.raise_for_status() | |
| except: | |
| print(f'SciX citations request failed. Status code {req.status_code}. URL {doi}') | |
| else: | |
| cit_DOIs = [] | |
| cit_DOIs_alt = [] | |
| for d in req.json()['response']['docs']: | |
| if 'doi' in d.keys(): | |
| cit_DOIs.append(d['doi'][0]) | |
| cit_DOIs_alt.append(d['doi'][1:]) | |
| # cit_DOIs = [d['doi'][0] for d in req.json()['response']['docs']] | |
| # cit_DOIs_alt = [d['doi'][1:] for d in req.json()['response']['docs']] | |
| return(ref_DOIs, ref_DOIs_alt, cit_DOIs, cit_DOIs_alt) |