Guest User

ogs game downloader

a guest
Feb 16th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import requests
  3. import json
  4. import os
  5.  
  6. sess=requests.Session()
  7.  
  8. def raw_get(url, filepath, skip_read=False):
  9.     if os.path.exists(filepath) and os.path.getsize(filepath)>0:
  10.         if skip_read:
  11.             print("skip", filepath)
  12.             content=""
  13.         else:
  14.             with open(filepath,"rb") as f:
  15.                 content=f.read()
  16.             print("read", filepath)
  17.     else:
  18.         print("GET", url)
  19.         xx=sess.get(url)
  20.         content=xx.text
  21.         with open(filepath,"wb") as f:
  22.             f.write(content.encode("utf8"))
  23.         print("saved", filepath)
  24.     return content
  25.  
  26. def json_get(url, filepath):
  27.     return json.loads(raw_get(url, "./json/"+filepath))
  28.  
  29. def get_player_id(username):
  30.    url="http://online-go.com/api/v1/players?username=" + username
  31.    ds=json_get(url, "player-{}.json".format(username));
  32.    return ds["results"][0]["id"]
  33.    # Player not found ?
  34.  
  35. def is_bot_game(g):
  36.     return g["players"]["white"]["ui_class"] == "bot" \
  37.     or g["players"]["black"]["ui_class"] == "bot"
  38.  
  39. def get_games(pid, count):
  40.     page=1
  41.     gameCount=0
  42.     while gameCount < count:
  43.         ds = json_get("http://online-go.com/api/v1/players/{}/games?ordering=-id&page={}".format(pid, page), "gamelist-{}-{}.json".format(pid,page))
  44.         games = ds["results"]
  45.         for g in games:
  46.             if not is_bot_game(g):
  47.                 raw_get(
  48.                 "http://online-go.com/api/v1/games/{}/sgf".format(g["id"]),
  49.                     "./sgf/{}-{}-{}.sgf".format(
  50.                         g["id"],
  51.                         g["players"]["black"]["username"],
  52.                         g["players"]["white"]["username"]),
  53.                     skip_read=True)
  54.                 gameCount += 1
  55.                 if gameCount == count:
  56.                     break
  57.         page += 1
  58.         print("games: %d/%d" % (gameCount,count))
  59.  
  60. def make_dir(d):
  61.     if not os.path.exists(d):
  62.         os.mkdir(d)
  63.         print("created directory", d)
  64.  
  65. def main():
  66.     make_dir("json")
  67.     make_dir("sgf")
  68.     username=input("Input your username(case sensitive): ")
  69.     try:
  70.         userid=get_player_id(username)
  71.     except:
  72.         print("failed to get user id")
  73.         return
  74.     count=40
  75.     print("player id:", userid)
  76.     print("fetching game list")
  77.     get_games(userid, count)
  78.  
  79. main()
Add Comment
Please, Sign In to add comment