-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapeGames.py
45 lines (39 loc) · 1.86 KB
/
scrapeGames.py
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
import argparse
import sys
import requests
import json
import random
def pullMatch(matchID, apiKey):
url = f"https://open.faceit.com/data/v4/matches/{matchID}"
r = requests.get(url, headers={"accept": "application/json", "Authorization": f"Bearer {apiKey}"})
assert r.status_code == 200
return r.json()
def getPlayerMatch(playerId, apiKey):
url = f"https://open.faceit.com/data/v4/players/{playerId}/history?game=csgo&offset=0&limit=20"
r = requests.get(url, headers={"accept": "application/json", "Authorization": f"Bearer {apiKey}"})
assert r.status_code == 200
items = r.json()["items"]
index = random.randint(0, len(items)-1)
return items[index]["match_id"]
def getNextMatch(matchJson, apiKey):
faction = random.choice(["faction1", "faction2"])
roster = matchJson["teams"][faction]["roster"]
playerIndex = random.randint(0, len(roster)-1)
playerId = matchJson["teams"][faction]["roster"][playerIndex]["player_id"]
return getPlayerMatch(playerId, apiKey)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-k", "--key", dest="apiKey", required=True, help="faceit api key", type=str, default="")
parser.add_argument("-s", "--startGame", dest="initMatch", required=True, help="game to start pull sequence", type=str, default="")
parser.add_argument("-n", "--numMatches", dest="numMatches", required=False, help="Numbers of matches to try and pull", type=int, default=1000)
args = parser.parse_args()
cMatch = args.initMatch
apiKey = args.apiKey
if apiKey == "TODO" or cMatch == "TODO":
print("Please provide a valid api key and match id", file=sys.stderr)
sys.exit(1)
count = 0
while count < args.numMatches:
print(f"{cMatch}", flush=True, end="\n")
cMatch = getNextMatch(pullMatch(cMatch, apiKey), apiKey)
count += 1