/
vgr.py
95 lines (74 loc) · 2.7 KB
/
vgr.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
import requests
from key import API_KEY
class VGR():
def __init__(self):
self.games = []
self.headers = {"X-Mashape-Key": API_KEY, "Accept": "application/json"}
self.genres = []
self.selection = 0
self.search = []
self.recommendations = []
def user_search(self):
user_input = raw_input("Search for a game title (e.g. 'metroid'): ")
print "\n"
search_url_format = "https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name&limit=10&offset=0&search={}".format(user_input)
response = requests.get(search_url_format, headers=self.headers)
info = response.json()
count = 1
for game in info:
print str(count) + ". " + str(game["name"])
if game["id"] not in self.search:
self.search.append(game["id"])
count += 1
print "\n"
selection = raw_input("Use the numpad to select your game: ")
print "\n"
self.selection = self.search[int(selection)-1]
self.game_lookup(self.search[int(selection)-1])
def game_lookup(self, code):
game_url_format = "https://igdbcom-internet-game-database-v1.p.mashape.com/games/{}?fields=*".format(code)
response = requests.get(game_url_format, headers=self.headers)
game_info = response.json()
self.genres = game_info[0]['genres']
self.genre_scrape(self.genres)
def genre_scrape(self, genres):
genre = ','.join(map(str, genres))
genre_url_format = "https://igdbcom-internet-game-database-v1.p.mashape.com/genres/{}?fields=*".format(genre)
response = requests.get(genre_url_format, headers=self.headers)
genre_info = response.json()
self.temp = []
print "Your genres are: "
for x in range(len(self.genres)):
print genre_info[x]['name']
for games in genre_info[x]['games']:
if len(self.genres) == 1:
self.games.append(games)
else:
if games in self.temp:
self.games.append(games)
else:
self.temp.append(games)
print str(len(self.games)) + " games found\n"
print "Pulling data from servers... Please wait.\n"
self.recommend()
def recommend(self):
temp = ','.join(map(str,self.games))
game_url_format = "https://igdbcom-internet-game-database-v1.p.mashape.com/games/{}?fields=*".format(temp)
response = requests.get(game_url_format, headers=self.headers)
game_info = response.json()
for games in game_info:
try:
if games['id'] != self.selection:
self.recommendations.append([games['name'], games['rating']])
except KeyError:
pass
print "Analysis complete, your game recommendations are: "
self.recommendations.sort(key=lambda item: item[1], reverse=True)
if len(self.recommendations) == 0:
print "No games found"
else:
for games in range(5):
print self.recommendations[games][0]
if __name__ == '__main__':
test = VGR()
test.user_search()