2
2
# IMPORTS
3
3
4
4
import re
5
- import html
6
5
import json
7
6
from enum import Enum
8
7
from bs4 import BeautifulSoup
@@ -43,7 +42,7 @@ def get_search_request_headers():
43
42
return headers
44
43
45
44
@staticmethod
46
- def get_search_request_data (game_name : str , search_modifiers : SearchModifiers , page : int ):
45
+ def get_search_request_data (game_name : str , search_modifiers : SearchModifiers , page : int , api_key : str ):
47
46
"""
48
47
Generate the data payload for the search request
49
48
@param game_name: The name of the game to search
@@ -74,6 +73,7 @@ def get_search_request_data(game_name: str, search_modifiers: SearchModifiers, p
74
73
'modifier' : search_modifiers .value ,
75
74
},
76
75
'users' : {
76
+ 'id' : api_key ,
77
77
'sortCategory' : "postcount"
78
78
},
79
79
'filter' : "" ,
@@ -94,12 +94,12 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
94
94
@return: The HTML code of the research if the request returned 200(OK), None otherwise
95
95
"""
96
96
headers = HTMLRequests .get_search_request_headers ()
97
- payload = HTMLRequests .get_search_request_data (game_name , search_modifiers , page )
98
97
api_key_result = HTMLRequests .send_website_request_getcode (False )
99
98
if api_key_result is None :
100
99
api_key_result = HTMLRequests .send_website_request_getcode (True )
100
+ payload = HTMLRequests .get_search_request_data (game_name , search_modifiers , page , api_key_result )
101
101
# Make the post request and return the result if is valid
102
- search_url_with_key = HTMLRequests .SEARCH_URL + "/" + api_key_result
102
+ search_url_with_key = HTMLRequests .SEARCH_URL
103
103
resp = requests .post (search_url_with_key , headers = headers , data = payload , timeout = 60 )
104
104
if resp .status_code == 200 :
105
105
return resp .text
@@ -116,10 +116,10 @@ async def send_async_web_request(game_name: str, search_modifiers: SearchModifie
116
116
@return: The HTML code of the research if the request returned 200(OK), None otherwise
117
117
"""
118
118
headers = HTMLRequests .get_search_request_headers ()
119
- payload = HTMLRequests .get_search_request_data (game_name , search_modifiers , page )
120
119
api_key_result = await HTMLRequests .async_send_website_request_getcode (False )
121
120
if api_key_result is None :
122
121
api_key_result = await HTMLRequests .async_send_website_request_getcode (True )
122
+ payload = HTMLRequests .get_search_request_data (game_name , search_modifiers , page , api_key_result )
123
123
# Make the post request and return the result if is valid
124
124
search_url_with_key = HTMLRequests .SEARCH_URL + "/" + api_key_result
125
125
async with aiohttp .ClientSession () as session :
@@ -207,7 +207,7 @@ async def async_get_game_title(game_id: int):
207
207
text = await resp .text ()
208
208
return HTMLRequests .__cut_game_title (text )
209
209
return None
210
-
210
+
211
211
@staticmethod
212
212
def send_website_request_getcode (parse_all_scripts : bool ):
213
213
"""
@@ -218,24 +218,24 @@ def send_website_request_getcode(parse_all_scripts: bool):
218
218
headers = HTMLRequests .get_title_request_headers ()
219
219
resp = requests .get (HTMLRequests .BASE_URL , headers = headers , timeout = 60 )
220
220
if resp .status_code == 200 and resp .text is not None :
221
- # Parse the HTML content using BeautifulSoup
222
- soup = BeautifulSoup (resp .text , 'html.parser' )
223
- # Find all <script> tags with a src attribute containing the substring
224
- scripts = soup .find_all ('script' , src = True )
225
- if parse_all_scripts :
226
- matching_scripts = [script ['src' ] for script in scripts ]
227
- else :
228
- matching_scripts = [script ['src' ] for script in scripts if '_app-' in script ['src' ]]
229
- for script_url in matching_scripts :
230
- script_url = HTMLRequests .BASE_URL + script_url
231
- script_resp = requests .get (script_url , headers = headers , timeout = 60 )
232
- if script_resp .status_code == 200 and script_resp .text is not None :
233
- pattern = r'"/api/search/".concat\( "([a-zA-Z0-9 ]+)"\) '
234
- matches = re .findall (pattern , script_resp .text )
235
- for match in matches :
236
- return match
221
+ # Parse the HTML content using BeautifulSoup
222
+ soup = BeautifulSoup (resp .text , 'html.parser' )
223
+ # Find all <script> tags with a src attribute containing the substring
224
+ scripts = soup .find_all ('script' , src = True )
225
+ if parse_all_scripts :
226
+ matching_scripts = [script ['src' ] for script in scripts ]
227
+ else :
228
+ matching_scripts = [script ['src' ] for script in scripts if '_app-' in script ['src' ]]
229
+ for script_url in matching_scripts :
230
+ script_url = HTMLRequests .BASE_URL + script_url
231
+ script_resp = requests .get (script_url , headers = headers , timeout = 60 )
232
+ if script_resp .status_code == 200 and script_resp .text is not None :
233
+ pattern = r'users\s*:\s*{\s*id\s*:\s* "([^" ]+)"'
234
+ matches = re .findall (pattern , script_resp .text )
235
+ for match in matches :
236
+ return match
237
237
return None
238
-
238
+
239
239
@staticmethod
240
240
async def async_send_website_request_getcode (parse_all_scripts : bool ):
241
241
"""
@@ -262,11 +262,11 @@ async def async_send_website_request_getcode(parse_all_scripts: bool):
262
262
async with session .get (script_url , headers = headers ) as script_resp :
263
263
if script_resp is not None and str (resp .status ) == "200" :
264
264
script_resp_text = await script_resp .text ()
265
- pattern = r'"/api/search/".concat\( "([a-zA-Z0-9 ]+)"\) '
265
+ pattern = r'users\s*:\s*{\s*id\s*:\s* "([^" ]+)"'
266
266
matches = re .findall (pattern , script_resp_text )
267
267
for match in matches :
268
268
return match
269
269
else :
270
- return None
270
+ return None
271
271
else :
272
- return None
272
+ return None
0 commit comments