-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
245 lines (213 loc) · 8.07 KB
/
fetch_data.py
File metadata and controls
245 lines (213 loc) · 8.07 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
"""Fetches Codeforces + interview data and writes JSON dumps under data/.
Run from GitHub Actions every 2h. The Anthropic routine reads these files
out of the cloned repo.
Outputs:
data/contests.json — upcoming contests
data/user.json — current rating, rank, handle
data/rating_history.json — past contests with rating changes
data/submissions_recent.json — last 30 days of submissions
data/solved.json — set of all solved problem keys
data/problemset.json — slim list of rated problems
data/adaptation.json — derived: rating boost from stretch hit-rate, per-tag solve rates
data/leetcode_daily.json — LeetCode's daily challenge (fresh each day)
"""
import json
from datetime import datetime, timedelta, timezone
from pathlib import Path
import requests
HANDLE = "Omar_Musayev"
DATA_DIR = Path(__file__).parent / "data"
TIMEOUT = 60
NOW = datetime.now(timezone.utc)
def cf_get(url):
r = requests.get(url, timeout=TIMEOUT)
r.raise_for_status()
data = r.json()
if data.get("status") != "OK":
raise RuntimeError(f"{url}: {data.get('status')}: {data.get('comment')}")
return data["result"]
def write_json(path: Path, payload: dict):
path.parent.mkdir(parents=True, exist_ok=True)
out = {"fetched_at": NOW.isoformat(), **payload}
path.write_text(json.dumps(out, indent=2, sort_keys=True) + "\n")
def fetch_contests():
contests = cf_get("https://codeforces.com/api/contest.list?gym=false")
upcoming = [c for c in contests if c.get("phase") == "BEFORE"]
write_json(DATA_DIR / "contests.json", {"upcoming": upcoming})
def fetch_user():
info = cf_get(f"https://codeforces.com/api/user.info?handles={HANDLE}")[0]
write_json(DATA_DIR / "user.json", {
"handle": HANDLE,
"rating": info.get("rating"),
"maxRating": info.get("maxRating"),
"rank": info.get("rank"),
"maxRank": info.get("maxRank"),
})
def fetch_rating_history():
hist = cf_get(f"https://codeforces.com/api/user.rating?handle={HANDLE}")
write_json(DATA_DIR / "rating_history.json", {"history": hist})
def fetch_submissions_and_solved():
subs = cf_get(f"https://codeforces.com/api/user.status?handle={HANDLE}&from=1&count=10000")
cutoff = (NOW - timedelta(days=30)).timestamp()
recent = []
solved = set()
for s in subs:
p = s.get("problem", {})
if s.get("verdict") == "OK" and "contestId" in p:
solved.add(f"{p['contestId']}-{p['index']}")
if s.get("creationTimeSeconds", 0) >= cutoff:
recent.append({
"id": s.get("id"),
"contestId": s.get("contestId"),
"creationTimeSeconds": s.get("creationTimeSeconds"),
"verdict": s.get("verdict"),
"problem": {
"contestId": p.get("contestId"),
"index": p.get("index"),
"name": p.get("name"),
"rating": p.get("rating"),
"tags": p.get("tags", []),
},
"participantType": s.get("author", {}).get("participantType"),
})
write_json(DATA_DIR / "solved.json", {"keys": sorted(solved)})
write_json(DATA_DIR / "submissions_recent.json", {"submissions": recent})
def fetch_problemset():
data = cf_get("https://codeforces.com/api/problemset.problems")
problems = [
{
"contestId": p.get("contestId"),
"index": p.get("index"),
"name": p.get("name"),
"rating": p.get("rating"),
"tags": p.get("tags", []),
}
for p in data["problems"]
if p.get("rating") is not None
]
write_json(DATA_DIR / "problemset.json", {"problems": problems})
LEETCODE_DAILY_QUERY = """
query questionOfToday {
activeDailyCodingChallengeQuestion {
date
link
question {
questionFrontendId
title
titleSlug
difficulty
topicTags { name }
}
}
}
"""
def fetch_leetcode_daily():
"""LeetCode's public daily challenge via their GraphQL. No auth."""
r = requests.post(
"https://leetcode.com/graphql/",
json={"query": LEETCODE_DAILY_QUERY, "operationName": "questionOfToday"},
headers={"Referer": "https://leetcode.com/"},
timeout=TIMEOUT,
)
r.raise_for_status()
d = r.json()["data"]["activeDailyCodingChallengeQuestion"]
q = d["question"]
write_json(DATA_DIR / "leetcode_daily.json", {
"date": d["date"],
"url": "https://leetcode.com" + d["link"],
"id": q["questionFrontendId"],
"title": q["title"],
"slug": q["titleSlug"],
"difficulty": q["difficulty"],
"tags": [t["name"] for t in q["topicTags"]],
})
def compute_adaptation():
"""Derive a rating boost (from stretch hit-rate) and per-tag solve rates from recent submissions.
The routine reads adaptation.json to:
- use `effective_rating` (= CF rating + boost) when picking band centers
- bias TOPIC_A/TOPIC_B picks toward tags with the lowest solve rates (user's weak spots)
"""
user = json.loads((DATA_DIR / "user.json").read_text())
subs = json.loads((DATA_DIR / "submissions_recent.json").read_text())["submissions"]
rating = user.get("rating") or 1200
# ----- Difficulty self-tuning: stretch hit-rate over last 14 days -----
stretch_floor = rating + 100
cutoff_14d = (NOW - timedelta(days=14)).timestamp()
stretch_attempts = {} # key -> solved?
for s in subs:
p = s.get("problem", {})
if not p.get("rating") or p["rating"] < stretch_floor:
continue
if (s.get("creationTimeSeconds") or 0) < cutoff_14d:
continue
key = f"{p.get('contestId')}-{p.get('index')}"
if key not in stretch_attempts:
stretch_attempts[key] = False
if s.get("verdict") == "OK":
stretch_attempts[key] = True
n_attempts = len(stretch_attempts)
n_solved = sum(stretch_attempts.values())
if n_attempts >= 5:
rate = n_solved / n_attempts
if rate > 0.5:
boost = 50
elif rate < 0.2:
boost = -50
else:
boost = 0
else:
boost = 0 # insufficient data
# ----- Per-tag solve rate over last 30 days -----
cutoff_30d = (NOW - timedelta(days=30)).timestamp()
tag_problems = {} # tag -> { "attempted": set, "solved": set }
for s in subs:
if (s.get("creationTimeSeconds") or 0) < cutoff_30d:
continue
p = s.get("problem", {})
tags = p.get("tags") or []
if not tags or p.get("contestId") is None:
continue
key = f"{p['contestId']}-{p.get('index')}"
ok = s.get("verdict") == "OK"
for tag in tags:
d = tag_problems.setdefault(tag, {"attempted": set(), "solved": set()})
d["attempted"].add(key)
if ok:
d["solved"].add(key)
tag_stats = {
tag: {
"attempted": len(v["attempted"]),
"solved": len(v["solved"]),
"rate": (len(v["solved"]) / len(v["attempted"])) if v["attempted"] else None,
}
for tag, v in tag_problems.items()
}
write_json(DATA_DIR / "adaptation.json", {
"base_rating": rating,
"rating_boost": boost,
"effective_rating": rating + boost,
"stretch_stats": {
"window_days": 14,
"attempts": n_attempts,
"solved": n_solved,
"rate": (n_solved / n_attempts) if n_attempts else None,
},
"tag_stats": tag_stats,
})
def main():
fetch_contests()
fetch_user()
fetch_rating_history()
fetch_submissions_and_solved()
fetch_problemset()
# User-derived: needs user.json + submissions_recent.json
compute_adaptation()
# External: don't let a LeetCode hiccup fail the whole run
try:
fetch_leetcode_daily()
except Exception as e:
print(f"leetcode_daily fetch failed (non-fatal): {e}")
print(f"Wrote data files at {NOW.isoformat()}")
if __name__ == "__main__":
main()