This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCHandler.py
109 lines (86 loc) · 3.5 KB
/
OCHandler.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
"""
This program implements OpenCritic API
https://app.swaggerhub.com/apis-docs/OpenCritic/OpenCritic-API/0.1.0
"""
import re
import urllib.request, urllib.parse, urllib.error
import json
from typing import List
def _safe_get(req: str) -> str:
"""
Safely make an request and returns the result in a json file
:param req: A parsed request encoded by urllib.parse.urlencode
:return: Requested object in json. Returns None if an error has occured
"""
try:
# Need to provide a valid user-agent header (otherwise we get 403 error)
request = urllib.request.Request(req, headers={'User-Agent': 'Mozilla/5.0'})
return urllib.request.urlopen(request).read()
except urllib.error.URLError as e:
if hasattr(e, "code"):
print("The server couldn't fulfill the request.")
print("Error code: ", e.code)
elif hasattr(e, 'reason'):
print("We failed to reach a server")
print("Reason: ", e.reason)
return None
def search(game: str) -> List[dict]:
"""
Search for matching entries in OpenCritic for a given game
:param game: Name of the game
:return: A list of dictionaries representing games that matches the queried term
"""
baseurl = "https://api.opencritic.com/api/game/search"
param = {'criteria': game}
# Switch alternate quote_via function to work with the API
return json.loads(_safe_get(baseurl + "?" + urllib.parse.urlencode(param, quote_via=urllib.parse.quote)))
def top_id(games: List[dict]) -> int:
"""
Returns the OpenCritic game ID for the top result in a given list of games
:param games: A list of games dictionary
:return: Integer game ID of the first game in the list
"""
return games[0]['id']
def check_validity(games: List[dict]) -> bool:
return games[0]['dist'] < 0.7
def get_review(id: int) -> dict:
"""
Gets the core game data for the game represented by a given game ID
:param id: Game ID of the game to search
:return: A dictionary object containing basic information about the game
"""
# Encode game ID in the url directly
url = "https://api.opencritic.com/api/game/%d"%id
# Switch alternate quote_via function to work with the API
return json.loads(_safe_get(url))
def suggestions(keyword: str) -> List[str]:
"""
Get suggestions from OpenCritic search. This function works, ran out of time to actually use it :(
:param keyword: Keyword entered by the user into the search box
:return: List of suggested game titles matching the user input
"""
res = search(keyword)
if len(res) == 0:
return []
return [x['name'] for x in res if x['dist'] <= 0.75]
def top_critic_score(game_data: dict) -> int:
"""
Gets the average top critic review score for a given game data object. This is the main metric
displayed on OpenCritic's website.
:param game_data: A game data object from OpenCritic
:return: Average top critic score
"""
return int(game_data['topCriticScore'])
def get_url(game_data: dict) -> str:
"""
Gets the URL to the OpenCritic page for a given game
:param game_data: A game data object from OpenCritic
:return: URL to the game on OpenCritic
"""
return "https://opencritic.com/game/%d/%s"%(game_data['id'], re.sub('\W+', '-', (game_data['name']).lower()))
if __name__ == '__main__':
val = search(input("Enter a game:\n"))
print(val)
game_id = top_id(val)
review = get_review(game_id)
print(top_critic_score(review))