From 7082b09f7804553487c469630b526d239e7adf06 Mon Sep 17 00:00:00 2001 From: schneefux Date: Mon, 23 Jan 2017 20:04:34 +0100 Subject: api: fix getting new matches --- api.py | 13 ++++++++----- crawler.py | 2 ++ database.py | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/api.py b/api.py index 1686204..f5e1380 100644 --- a/api.py +++ b/api.py @@ -11,13 +11,16 @@ async def main(): await db.connect("postgres://vgstats@localhost/vgstats") api = crawler.Crawler() try: - await db.meta("last_match_update") + last_match_update = await db.meta("last_match_update") except KeyError: - await db.meta("last_match_update", - datetime.datetime(1, 1, 1).isoformat()) - matches = await api.matches_since( - await db.meta("last_match_update")) + last_match_update = datetime.datetime(1, 1, 1).isoformat() + await db.meta("last_match_update", last_match_update) + print("getting new matches since " + last_match_update) + await db.meta("last_match_update", datetime.datetime.now().isoformat()) + matches = await api.matches_since(last_match_update) + print("inserting data into database") await db.upsert("na", matches, True) + print("calculating stats") print(await db.select("SELECT data->'attributes'->>'name' as name FROM player WHERE CAST(data->'attributes'->'stats'->>'winStreak' AS integer) > 3")) loop = asyncio.get_event_loop() diff --git a/crawler.py b/crawler.py index 96eb8ce..07ca22f 100644 --- a/crawler.py +++ b/crawler.py @@ -78,4 +78,6 @@ class Crawler(object): :return: Processed API response :rtype: list of dict """ + date = ".".join(date.split(".")[:-1]) # remove microseconds + date = date + "Z" return await self.matches(region, {"filter[createdAt-start]": date}) diff --git a/database.py b/database.py index 8fad408..5119899 100644 --- a/database.py +++ b/database.py @@ -105,7 +105,7 @@ class Database(object): "WHERE key='" + key + "'") if len(result) == 0: raise KeyError - return result[0]["value"] + return json.loads(result[0]["value"]) async def execute(self, query, args): """Runs an SQL statement. -- cgit v1.3.1