refactor code to download games into separate file
This commit is contained in:
parent
cae4a44703
commit
cee4796b34
2 changed files with 30 additions and 1 deletions
|
@ -80,7 +80,7 @@ def load(game_id: int) -> Optional[Game]:
|
||||||
def store(game: Game):
|
def store(game: Game):
|
||||||
stored = load(game.id)
|
stored = load(game.id)
|
||||||
if stored is None:
|
if stored is None:
|
||||||
print("inserting game with id {} into DB".format(game.id))
|
# print("inserting game with id {} into DB".format(game.id))
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO games"
|
"INSERT INTO games"
|
||||||
"(id, num_players, score, seed, variant_id)"
|
"(id, num_players, score, seed, variant_id)"
|
||||||
|
|
29
download_data.py
Normal file
29
download_data.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import json
|
||||||
|
from site_api import get, api, replay
|
||||||
|
from database import Game, store, load, commit
|
||||||
|
|
||||||
|
def download_games(variant_id):
|
||||||
|
url = "variants/{}".format(variant_id)
|
||||||
|
r = api(url)
|
||||||
|
if not r:
|
||||||
|
print("Not a valid variant: {}".format(variant_id))
|
||||||
|
return
|
||||||
|
num_entries = r['total_rows']
|
||||||
|
print("Downloading {} entries for variant {}".format(num_entries, variant_id))
|
||||||
|
num_pages = (num_entries + 99) // 100
|
||||||
|
for page in range(0, num_pages):
|
||||||
|
print("Downloading page {} of {}".format(page + 1, num_pages), end = '\r')
|
||||||
|
r = api(url + "?page={}".format(page))
|
||||||
|
for row in r['rows']:
|
||||||
|
row.pop('users')
|
||||||
|
row.pop('datetime')
|
||||||
|
g = Game(row)
|
||||||
|
g.variant_id = variant_id
|
||||||
|
store(g)
|
||||||
|
print()
|
||||||
|
print('Downloaded and stored {} entries for variant {}'.format(num_entries, variant_id))
|
||||||
|
commit()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
download_games(1)
|
Loading…
Reference in a new issue