This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steammatch.py
119 lines (99 loc) · 3.47 KB
/
steammatch.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import requests
# Get the Steam API key from steamapi.txt
f = open("steamapi.txt", "r")
key = f.read()
f.close()
del f
class SteamAPIError(Exception):
pass
class SteamRequestError(SteamAPIError):
pass
class InvalidVanityURLError(SteamAPIError):
def __init__(self, error, vanity):
self.error = error
self.vanity = vanity
class PrivateProfileError(SteamAPIError):
def __init__(self, steamid):
self.steamid = steamid
class SteamGame:
def __init__(self, d: dict):
self.appid = int(d["appid"])
self.name = d["name"]
self.icon = d["img_icon_url"]
self.grid = d["img_logo_url"]
def __hash__(self) -> int:
return self.appid
def __eq__(self, other) -> bool:
return self.appid == other.appid
def resolve_vanity(name: str) -> int:
r = requests.get("http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={key}&vanityurl={name}".format(key=key, name=name))
if r.status_code != 200:
raise SteamRequestError("Error during the API request: {}{}".format(r.status_code, r.content))
j = r.json()["response"]
if j["success"] == 42:
raise InvalidVanityURLError("Vanity url not found", name)
elif j["success"] != 1:
raise SteamAPIError("Unknown exception: {message}".format(message=j["message"]))
return j["steamid"]
def get_steam_games_owned(steamid: int, freetoplay=True) -> list:
# Request the list of games played
params = {
"steamid": steamid,
"key": key,
"format": "json",
"include_appinfo": 1
}
if freetoplay:
params["include_played_free_games"] = 1
r = requests.get("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/", params=params)
if r.status_code != 200:
raise SteamRequestError("Error during the API request.")
data = r.json()
if 'games' not in data["response"]:
raise PrivateProfileError(steamid)
owned = list()
for game in data["response"]["games"]:
owned.append(SteamGame(game))
return owned
def and_games(steamids: list) -> set:
for index, player in enumerate(steamids):
try:
steamids[index] = int(player)
except ValueError:
steamids[index] = int(resolve_vanity(player))
current = set(get_steam_games_owned(steamids.pop(0)))
for player in steamids:
current = current & set(get_steam_games_owned(player))
return current
def or_games(steamids: list) -> set:
for index, player in enumerate(steamids):
try:
steamids[index] = int(player)
except ValueError:
steamids[index] = int(resolve_vanity(player))
current = set(get_steam_games_owned(steamids.pop(0)))
for player in steamids:
current = current | set(get_steam_games_owned(player))
return current
def diff_games(first, second):
try:
first = int(first)
except ValueError:
first = int(resolve_vanity(first))
try:
second = int(second)
except ValueError:
second = int(resolve_vanity(second))
result = set(get_steam_games_owned(first)) - set(get_steam_games_owned(second))
return result
def xor_games(first, second):
try:
first = int(first)
except ValueError:
first = int(resolve_vanity(first))
try:
second = int(second)
except ValueError:
second = int(resolve_vanity(second))
result = set(get_steam_games_owned(first)) ^ set(get_steam_games_owned(second))
return result