From 5709d5e4660acd09081f212405cba5076cd49792 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Thu, 4 Apr 2019 22:30:54 +0100 Subject: [PATCH] codeforces provider --- coding/codeforces.py | 108 +++++++++++++++++++++++++++++++++++++++++++ coding/topcoder.py | 11 +++-- 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 coding/codeforces.py diff --git a/coding/codeforces.py b/coding/codeforces.py new file mode 100644 index 0000000..7ab96f0 --- /dev/null +++ b/coding/codeforces.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +from datetime import datetime +from typing import NamedTuple +from pathlib import Path +import json +from typing import Dict, Iterator, Any + +from kython import cproperty, fget +from kython.konsume import dell, zoom, keq, akeq +from kython.kerror import Res, ytry, unwrap +from kython.kdatetime import as_utc + + +_BDIR = Path('/L/Dropbox/data/codeforces') + + +Cid = int + +class Contest(NamedTuple): + cid: Cid + when: datetime + + @classmethod + def make(cls, j) -> 'Contest': + return cls( + cid=j['id'], + when=as_utc(j['startTimeSeconds']), + ) + +Cmap = Dict[Cid, Contest] + +def get_contests() -> Cmap: + last = max(_BDIR.glob('allcontests*.json')) + j = json.loads(last.read_text()) + d = {} + for c in j['result']: + cc = Contest.make(c) + d[cc.cid] = cc + return d + + +def get_latest(): + last = max(_BDIR.glob('codeforces*.json')) + return json.loads(last.read_text()) + + +class Competition(NamedTuple): + json: Dict[str, Any] + cmap: Cmap + + @cproperty + def uid(self) -> str: + return self.contest_id + + @property + def contest_id(self): + return self.json['contestId'] + + def __hash__(self): + return hash(self.contest_id) + + @cproperty + def when(self) -> datetime: + return self.cmap[self.uid].when + + @cproperty + def contest(self) -> str: + return self.json['contestName'] + + @cproperty + def summary(self) -> str: + return f'participated in {self.contest}' # TODO + + @classmethod + def make(cls, cmap, json) -> Iterator[Res['Competition']]: + yield cls(cmap=cmap, json=json) + yield from ytry(lambda: akeq(json, 'contestId', 'contestName', 'rank', 'oldRating', 'newRating')) + + +def iter_data() -> Iterator[Res[Competition]]: + cmap = get_contests() + + j = get_latest() + dell(j, 'status') + + j = zoom(j, 'result') + + for c in j: + dell(c, 'handle', 'ratingUpdateTimeSeconds') + yield from Competition.make(cmap=cmap, json=c) + + +def get_data(): + return list(sorted(iter_data(), key=fget(Competition.when))) + +def main(): + for d in iter_data(): + try: + d = unwrap(d) + except Exception as e: + print(f'ERROR! {d}') + else: + print(f'{d.when}: {d.summary}') + + + +if __name__ == '__main__': + main() diff --git a/coding/topcoder.py b/coding/topcoder.py index 8bea8b5..efe16b2 100644 --- a/coding/topcoder.py +++ b/coding/topcoder.py @@ -20,10 +20,14 @@ class Competition(NamedTuple): @cproperty def uid(self) -> str: - return self.contest + return self.contest_id + + @property + def contest_id(self) -> str: + return self.json['challengeId'] def __hash__(self): - return hash(self.json['challengeId']) + return hash(self.contest_id) @cproperty def contest(self) -> str: @@ -80,9 +84,10 @@ def main(): for d in iter_data(): try: d = unwrap(d) - print(d.summary) except Exception as e: print(f'ERROR! {d}') + else: + print(d.summary) if __name__ == '__main__':