-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
57 lines (45 loc) · 2.29 KB
/
backend.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
import json
import os
import sys
import urllib.parse, urllib.request
import config, user_config
class BackendClient:
def __init__(self):
self.paths = []
self.results = []
self.roms = []
# More potential here if GOG allows us to pull images etc.
def get_games_gb(self):
# Use Giant Bomb api to search for roms (first result is used)
# Only call for id and name, in json format, limited to 1 result
query_url = "https://www.giantbomb.com/api/search/?api_key={}&field_list=id,name&format=json&limit=1&query={}&resources=game"
self.get_rom_names()
# Retrieve the info for each wbfs found
for rom in self.roms:
url = query_url.format(config.api_key, urllib.parse.quote(rom)) # Add in params to the above url
response = urllib.request.urlopen(url)
search_results = json.loads(response.read())
self.results.append(
[search_results["results"][0]["id"], search_results["results"][0]["name"]] # Add games in the form of list with id and name
)
for x,y in zip(self.paths, self.results):
x.extend(y)
return self.paths
def get_rom_names(self):
# Search through directory for gcm, iso, gcz, ciso, and wbfs files (Wii roms)
for root, dirs, files in os.walk(user_config.roms_path):
for file in files:
if file.endswith((".gcm", ".iso", ".gcz", ".ciso", ".wbfs")):
self.paths.append([os.path.join(root, file)])
self.roms.append(os.path.splitext(os.path.basename(file))[0]) # Split name of file from it's path/extension
def get_state_changes(self, old_list, new_list):
old_dict = {x.game_id: x.local_game_state for x in old_list}
new_dict = {x.game_id: x.local_game_state for x in new_list}
result = []
# removed games
result.extend(LocalGame(id, LocalGameState.None_) for id in old_dict.keys() - new_dict.keys())
# added games
result.extend(local_game for local_game in new_list if local_game.game_id in new_dict.keys() - old_dict.keys())
# state changed
result.extend(LocalGame(id, new_dict[id]) for id in new_dict.keys() & old_dict.keys() if new_dict[id] != old_dict[id])
return result