Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Created November 16, 2023 14:41
Show Gist options
  • Save mikkohei13/99d55bdc6b248b344a827dd5fa07b466 to your computer and use it in GitHub Desktop.
Save mikkohei13/99d55bdc6b248b344a827dd5fa07b466 to your computer and use it in GitHub Desktop.
Fetch article citation counts based on DOI's from opencitations.net API
# Made with ChatGPT/GPT-4
import requests
import pandas as pd
import time
import csv
def fetch_citation_count(doi):
"""
Function to fetch the citation count for a given DOI using the OpenCitations API.
"""
url = f"https://opencitations.net/index/coci/api/v1/citations/{doi}"
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return len(data) # The number of items in the response is the citation count
else:
return "Error: API request unsuccessful"
except Exception as e:
return f"Error: {e}"
# Load your DOI list
# File must have 1 doi per row
doi_list = pd.read_csv('doi.csv', header=None)
# Path to the output CSV file
output_file = 'citation_counts.csv'
# Write headers to the output file
with open(output_file, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['DOI', 'Citation Count'])
# Iterate over each DOI and append citation counts to the file
for doi in doi_list[0]:
citation_count = fetch_citation_count(doi)
with open(output_file, 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow([doi, citation_count])
# Pause to avoid hitting rate limits of the API
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment