File size: 7,004 Bytes
50e90ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Google Scholar Citation API — backed by SerpAPI, with daily cache.

Deploy to HuggingFace Spaces (Docker SDK).
"""

import os
import time
import json
import hashlib
import logging
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

import httpx
from fastapi import FastAPI, Query, HTTPException
from fastapi.responses import JSONResponse

# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
SERPAPI_KEY = os.environ.get("SERPAPI_KEY", "")
CACHE_DIR = Path(os.environ.get("CACHE_DIR", "/tmp/scholar_cache"))
CACHE_DIR.mkdir(parents=True, exist_ok=True)

CACHE_TTL_SECONDS = 86400  # 24 hours — at most 1 SerpAPI call per author per day

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("scholar-api")

# ---------------------------------------------------------------------------
# FastAPI app
# ---------------------------------------------------------------------------
app = FastAPI(
    title="Google Scholar Citation API",
    description=(
        "Fetches Google Scholar author citation metrics via SerpAPI. "
        "Results are cached for 24 hours to minimize API usage."
    ),
    version="1.0.0",
)


# ---------------------------------------------------------------------------
# Cache helpers
# ---------------------------------------------------------------------------
def _cache_key(author_id: str) -> str:
    """Return a safe filename for the author_id."""
    h = hashlib.sha256(author_id.encode()).hexdigest()[:16]
    return f"{author_id}_{h}"


def _read_cache(author_id: str) -> Optional[dict]:
    """Return cached data if it exists and is fresh (< CACHE_TTL_SECONDS)."""
    cache_file = CACHE_DIR / f"{_cache_key(author_id)}.json"
    if not cache_file.exists():
        return None
    try:
        data = json.loads(cache_file.read_text(encoding="utf-8"))
        cached_ts = data.get("_cached_at", 0)
        if time.time() - cached_ts < CACHE_TTL_SECONDS:
            logger.info("Cache HIT for author_id=%s", author_id)
            return data
        logger.info("Cache EXPIRED for author_id=%s", author_id)
    except Exception:
        logger.warning("Cache read error for author_id=%s", author_id, exc_info=True)
    return None


def _write_cache(author_id: str, data: dict) -> None:
    """Persist data with a timestamp."""
    data["_cached_at"] = time.time()
    data["_cached_at_human"] = datetime.now(timezone.utc).isoformat()
    cache_file = CACHE_DIR / f"{_cache_key(author_id)}.json"
    cache_file.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
    logger.info("Cache WRITE for author_id=%s", author_id)


# ---------------------------------------------------------------------------
# SerpAPI caller
# ---------------------------------------------------------------------------
async def _fetch_from_serpapi(author_id: str) -> dict:
    """Call SerpAPI Google Scholar Author endpoint."""
    if not SERPAPI_KEY:
        raise HTTPException(
            status_code=500,
            detail="SERPAPI_KEY is not configured. Set it as an environment variable / HF Space secret.",
        )

    params = {
        "engine": "google_scholar_author",
        "author_id": author_id,
        "api_key": SERPAPI_KEY,
    }

    async with httpx.AsyncClient(timeout=30) as client:
        resp = await client.get("https://serpapi.com/search.json", params=params)

    if resp.status_code != 200:
        logger.error("SerpAPI error: %s %s", resp.status_code, resp.text[:500])
        raise HTTPException(
            status_code=502,
            detail=f"SerpAPI returned status {resp.status_code}: {resp.text[:300]}",
        )

    raw = resp.json()

    # Extract the fields we care about
    author_info = raw.get("author", {})
    cited_by = raw.get("cited_by", {})
    articles = raw.get("articles", [])

    result = {
        "author": {
            "name": author_info.get("name"),
            "affiliations": author_info.get("affiliations"),
            "thumbnail": author_info.get("thumbnail"),
            "interests": [
                {"title": i.get("title"), "link": i.get("link")}
                for i in author_info.get("interests", [])
            ],
        },
        "citation_stats": {
            "table": cited_by.get("table", []),
            "graph": cited_by.get("graph", []),
        },
        "articles": [
            {
                "title": a.get("title"),
                "link": a.get("link"),
                "authors": a.get("authors"),
                "publication": a.get("publication"),
                "cited_by_value": a.get("cited_by", {}).get("value"),
                "year": a.get("year"),
            }
            for a in articles
        ],
    }
    return result


# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.get("/")
async def root():
    return {
        "message": "Google Scholar Citation API",
        "usage": "GET /citations?author_id=<GOOGLE_SCHOLAR_AUTHOR_ID>",
        "docs": "/docs",
    }


@app.get("/citations")
async def get_citations(
    author_id: str = Query(
        ...,
        description="Google Scholar author ID (e.g. 'JicYPdAAAAAJ' for Elon Musk-style profiles).",
        min_length=4,
    ),
):
    """
    Return citation metrics for a Google Scholar author.

    - First checks cache (valid for 24 h).
    - Falls back to SerpAPI if no fresh cache exists.
    """
    # 1. Try cache
    cached = _read_cache(author_id)
    if cached is not None:
        cached["_source"] = "cache"
        return JSONResponse(content=cached)

    # 2. Fetch from SerpAPI
    logger.info("Cache MISS — calling SerpAPI for author_id=%s", author_id)
    data = await _fetch_from_serpapi(author_id)

    # 3. Write cache
    _write_cache(author_id, data)

    data["_source"] = "serpapi"
    return JSONResponse(content=data)


@app.get("/cache/status")
async def cache_status(
    author_id: str = Query(..., description="Author ID to check cache for"),
):
    """Check whether a cached entry exists and how old it is."""
    cache_file = CACHE_DIR / f"{_cache_key(author_id)}.json"
    if not cache_file.exists():
        return {"cached": False}
    try:
        data = json.loads(cache_file.read_text(encoding="utf-8"))
        cached_at = data.get("_cached_at", 0)
        age_seconds = time.time() - cached_at
        return {
            "cached": True,
            "cached_at": data.get("_cached_at_human"),
            "age_seconds": round(age_seconds, 1),
            "expires_in_seconds": max(0, round(CACHE_TTL_SECONDS - age_seconds, 1)),
        }
    except Exception:
        return {"cached": False, "error": "failed to read cache"}


@app.get("/health")
async def health():
    return {"status": "ok", "serpapi_key_set": bool(SERPAPI_KEY)}