-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogtools-game-id-retriever.py
174 lines (143 loc) · 7.52 KB
/
gogtools-game-id-retriever.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"""
gogtools - game id retriever
"""
# Python stuff
from json import loads as jsonloads, dumps as jsondump
from os.path import (
exists as osexists,
isfile as osisfile,
)
from time import sleep
from requests import get as requestget
# Our main, nice!
if __name__ == "__main__":
appName = "gogtools-game-id-retriever"
appVersion = "v0.0.3"
appGithub = "https://github.com/jrie/gogtools"
currentPage = 1
forwardOption = '+'
backwardOption = '-'
doSearch = True
versionRetrieverFile = 'gt-gid.json'
print(f"You are running {appName} {appVersion}\nFor details, visit Github @ {appGithub}")
print(f'All added information are added to the file: "{versionRetrieverFile}"')
while True:
if doSearch:
currentPage = 1
searchValue = input('\n[Exit by entering "x"] --- Search Gog.com ==> ')
searchValue = searchValue.lower().replace('_', ' ').strip()
if searchValue == "x":
break
if len(searchValue) < 3:
print(f'{appName} [ ERROR ] : Search value must be at least 3 characters long.')
continue
searchURL = f'https://catalog.gog.com/v1/catalog?limit=20&locale=en-US&page={currentPage}&order=desc:score&productType=in:game,pack,dlc&query=like:{searchValue}'
gogData = requestget(searchURL, timeout=30)
if gogData.status_code == 200:
jsonData = jsonloads(gogData.text)
if jsonData['productCount'] == 0:
print(f'{appName} [STATUS 200] : Nothing found for query.')
continue
else:
print(f'{appName} [STATUS 200] : Found the following items..\n')
gameItems = {}
for index, item in enumerate(jsonData['products']):
index += 1
if item["productType"] == "game":
itemType = "[ GAME ]"
elif item["productType"] == "dlc":
itemType = "[ DLC ]"
elif item["productType"] == "pack":
itemType = "[ PACK ]"
else:
print(f'{appName} [ ERROR ] : Type "{item["productType"]}" is not recognized, please report this at Github @ {appGithubUrl}.')
intIndex = int(index)
gameItems[intIndex] = {}
gameItems[intIndex]['gameId'] = item["id"]
gameItems[intIndex]['title'] = item["title"]
gameItems[intIndex]['slug'] = item["slug"]
gameItems[intIndex]['productType'] = item["productType"]
gameItems[intIndex]['url'] = item["storeLink"]
print(f'{index:<2}) {itemType:<9} "{item["title"]}"')
if jsonData['pages'] != 1:
print(f'\nYou are on page: {currentPage} of {jsonData["pages"]}')
print(f'{appName} use "{forwardOption}" to go forward, "{
backwardOption}" to go backward and "=" following a page number to jump to page')
inputSelection = input('\n[Continue search by entering "c" or exit using "x"] --- Gog.com selection: ')
inputSelection = inputSelection.lower().replace('_', ' ').strip()
if inputSelection.startswith('='):
try:
pageNumber = int(inputSelection[1:].strip())
if pageNumber >= 1 and pageNumber <= jsonData["pages"]:
currentPage = pageNumber
print(f'Jumping to page "{pageNumber}".\n')
else:
print(f'The provided page number "{pageNumber}" is out of range. There are {jsonData["pages"]} result pages.\n')
doSearch = False
continue
except ValueError:
print('Use "=1" to jump to page one, use "=6" to jump to page six.\n')
doSearch = False
continue
if inputSelection == "c":
doSearch = True
continue
if inputSelection == "x":
break
if jsonData['pages'] != 1:
pageInfo = f'{currentPage} of {jsonData["pages"]} total pages.'
if inputSelection == forwardOption:
if currentPage < jsonData['pages']:
currentPage += 1
doSearch = False
else:
print(f'{appName} : Cannot go forward. {pageInfo}')
continue
elif inputSelection == backwardOption:
if currentPage > 1:
currentPage -= 1
doSearch = False
else:
print(f'{appName} : Cannot go backward. {pageInfo}')
continue
inputSelection = int(inputSelection, 10)
if inputSelection in gameItems.keys():
currentItem = gameItems[inputSelection]
print(f'{appName} : Entry information.\n[TITLE ] {currentItem["title"]}\n[TYPE ] {
currentItem["productType"]}\n[GAME ID ] {currentItem["gameId"]}\n[URL ] {currentItem["url"]}\n[SLUG ] {currentItem["slug"]}')
print('')
inputSelection = input(f'[Exit by entering "x"] --- Add selection information to "{versionRetrieverFile}" (y/n): ')
inputSelection = inputSelection.lower().replace('_', ' ').strip()
if inputSelection == "x":
break
elif inputSelection == "y":
print(f'{appName} : Dumping data to "{versionRetrieverFile}"')
entryExists = False
if osexists(versionRetrieverFile) and osisfile(versionRetrieverFile):
with open(versionRetrieverFile, 'r') as inputFile:
dumpData = jsondump(currentItem)
for line in inputFile:
if line.startswith(dumpData):
entryExists = True
break
if entryExists:
print(f'{appName} : "{versionRetrieverFile}" already has: "{currentItem["title"]}" with game id "{currentItem["gameId"]}"\n')
sleep(3)
else:
with open(versionRetrieverFile, 'a') as outputFile:
outputFile.write(jsondump(currentItem) + '\n')
print(f'{appName} : "{versionRetrieverFile}" entry added.\n')
sleep(3)
elif inputSelection == "n":
print(f'{appName} : Not writing information!\n')
doSearch = False
continue
else:
print(f'{appName} : Input selection not found {inputSelection}')
doSearch = False
continue
elif gogData.status_code == 404:
print(f'{appName} [STATUS 404] : Gog.com catalog search not reachable.')
continue
print(f'\n{appName}: Finished run. Enjoy your day!')