Skip to content

Commit 574ba9c

Browse files
authored
Update update_citations.py
1 parent 7e058b2 commit 574ba9c

File tree

1 file changed

+21
-53
lines changed

1 file changed

+21
-53
lines changed

scripts/update_citations.py

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
1-
#!/usr/bin/env python3
2-
import os
1+
from scholarly import scholarly
2+
import jsonpickle
33
import json
4-
import requests
5-
from bs4 import BeautifulSoup
6-
7-
# === CONFIG ===
8-
SCHOLAR_USER_ID = "8-IhrB0AAAAJ" # your Google Scholar user ID
9-
OUT_PATH = "assets/json/citations.json"
10-
11-
def fetch_total_citations():
12-
url = f"https://scholar.google.com/citations?user={SCHOLAR_USER_ID}&hl=en"
13-
headers = {
14-
"User-Agent": (
15-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
16-
"AppleWebKit/537.36 (KHTML, like Gecko) "
17-
"Chrome/124.0 Safari/537.36"
18-
)
19-
}
20-
r = requests.get(url, headers=headers, timeout=30)
21-
r.raise_for_status()
22-
23-
soup = BeautifulSoup(r.text, "html.parser")
24-
25-
# On Scholar, the total citations is in the first cell of this table
26-
table = soup.find("table", id="gsc_rsb_st")
27-
if not table:
28-
raise RuntimeError("Could not find citation stats table on Google Scholar page.")
29-
30-
cells = table.find_all("td", class_="gsc_rsb_std")
31-
if not cells:
32-
raise RuntimeError("Could not find citation value cell on Google Scholar page.")
33-
34-
total_citations = cells[0].get_text(strip=True)
35-
return total_citations
36-
37-
def main():
38-
citations = fetch_total_citations()
39-
print(f"Total citations: {citations}")
40-
41-
data = {
42-
"schemaVersion": 1,
43-
"label": "citations",
44-
"message": citations,
45-
"color": "9cf"
46-
}
47-
48-
os.makedirs(os.path.dirname(OUT_PATH), exist_ok=True)
49-
with open(OUT_PATH, "w", encoding="utf-8") as f:
50-
json.dump(data, f, ensure_ascii=False)
51-
52-
print(f"Wrote {OUT_PATH}")
4+
from datetime import datetime
5+
import os
536

54-
if __name__ == "__main__":
55-
main()
7+
author: dict = scholarly.search_author_id(os.environ['GOOGLE_SCHOLAR_ID'])
8+
scholarly.fill(author, sections=['basics', 'indices', 'counts', 'publications'])
9+
name = author['name']
10+
author['updated'] = str(datetime.now())
11+
author['publications'] = {v['author_pub_id']:v for v in author['publications']}
12+
print(json.dumps(author, indent=2))
13+
os.makedirs('results', exist_ok=True)
14+
with open(f'results/gs_data.json', 'w') as outfile:
15+
json.dump(author, outfile, ensure_ascii=False)
16+
17+
shieldio_data = {
18+
"schemaVersion": 1,
19+
"label": "citations",
20+
"message": f"{author['citedby']}",
21+
}
22+
with open(f'results/gs_data_shieldsio.json', 'w') as outfile:
23+
json.dump(shieldio_data, outfile, ensure_ascii=False)

0 commit comments

Comments
 (0)