Skip to content

Commit 4b9c240

Browse files
committed
Reduced code duplication by a lot to make sonarcloud happy
1 parent 053f91f commit 4b9c240

File tree

3 files changed

+70
-50
lines changed

3 files changed

+70
-50
lines changed

howlongtobeatpy/howlongtobeatpy/HTMLRequests.py

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,44 @@ class HTMLRequests:
2626
ORIGIN_HEADER = 'https://howlongtobeat.com'
2727
REFERER_HEADER = BASE_URL
2828
SEARCH_URL = BASE_URL + "search_results"
29-
GAME_URL = BASE_URL + "game?id="
29+
GAME_URL = BASE_URL + "game"
3030

3131
@staticmethod
32-
def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchModifiers.NONE, page: int = 1):
32+
def get_search_request_parameters(page: int):
3333
"""
34-
Function that search the game using a normal request
35-
@param game_name: The original game name received as input
36-
@param search_modifiers: The "Modifiers" list in "Search Options", allow to show/isolate/hide DLCs
37-
@param page: The page to explore of the research, unknown if this is actually used
38-
@return: The HTML code of the research if the request returned 200(OK), None otherwise
34+
Generate the parameters for the search request
35+
@param page: The page to search
36+
@return: The parameters object for the request
3937
"""
40-
ua = UserAgent()
4138
params = {
4239
'page': str(page)
4340
}
41+
return params
42+
43+
@staticmethod
44+
def get_search_request_headers():
45+
"""
46+
Generate the headers for the search request
47+
@return: The headers object for the request
48+
"""
49+
ua = UserAgent()
4450
headers = {
4551
'content-type': 'application/x-www-form-urlencoded',
4652
'accept': '*/*',
4753
'User-Agent': ua.random,
4854
'origin': HTMLRequests.ORIGIN_HEADER,
4955
'referer': HTMLRequests.REFERER_HEADER
5056
}
57+
return headers
58+
59+
@staticmethod
60+
def get_search_request_data(game_name: str, search_modifiers: SearchModifiers):
61+
"""
62+
Generate the data payload for the search request
63+
@param game_name: The name of the game to search
64+
@param search_modifiers: The search modifiers to use in the search
65+
@return: The request (data) payload object for the request
66+
"""
5167
payload = {
5268
'queryString': game_name,
5369
't': 'games',
@@ -63,6 +79,20 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
6379
'g': '',
6480
'randomize': '0'
6581
}
82+
return payload
83+
84+
@staticmethod
85+
def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchModifiers.NONE, page: int = 1):
86+
"""
87+
Function that search the game using a normal request
88+
@param game_name: The original game name received as input
89+
@param search_modifiers: The "Modifiers" list in "Search Options", allow to show/isolate/hide DLCs
90+
@param page: The page to explore of the research, unknown if this is actually used
91+
@return: The HTML code of the research if the request returned 200(OK), None otherwise
92+
"""
93+
params = HTMLRequests.get_search_request_parameters(page)
94+
headers = HTMLRequests.get_search_request_headers()
95+
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers)
6696
# Make the post request and return the result if is valid
6797
resp = requests.post(HTMLRequests.SEARCH_URL, params=params, headers=headers, data=payload)
6898
if resp.status_code == 200:
@@ -79,32 +109,9 @@ async def send_async_web_request(game_name: str, search_modifiers: SearchModifie
79109
@param page: The page to explore of the research, unknown if this is actually used
80110
@return: The HTML code of the research if the request returned 200(OK), None otherwise
81111
"""
82-
ua = UserAgent()
83-
params = {
84-
'page': str(page)
85-
}
86-
headers = {
87-
'content-type': 'application/x-www-form-urlencoded',
88-
'accept': '*/*',
89-
'User-Agent': ua.random,
90-
'origin': HTMLRequests.ORIGIN_HEADER,
91-
'referer': HTMLRequests.REFERER_HEADER
92-
}
93-
payload = {
94-
'queryString': game_name,
95-
't': 'games',
96-
'sorthead': 'popular',
97-
'sortd': 'Normal Order',
98-
'plat': '',
99-
'length_type': 'main',
100-
'length_min': '',
101-
'length_max': '',
102-
'detail': search_modifiers.value,
103-
'v': '',
104-
'f': '',
105-
'g': '',
106-
'randomize': '0'
107-
}
112+
params = HTMLRequests.get_search_request_parameters(page)
113+
headers = HTMLRequests.get_search_request_headers()
114+
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers)
108115
# Make the post request and return the result if is valid
109116
async with aiohttp.ClientSession() as session:
110117
async with session.post(HTMLRequests.SEARCH_URL, params=params, headers=headers, data=payload) as resp:
@@ -132,21 +139,40 @@ def __cut_game_title(game_title: str):
132139
return cut_title
133140

134141
@staticmethod
135-
def get_game_title(game_id: int):
142+
def get_title_request_parameters(game_id: int):
136143
"""
137-
Function that gets the title of a game from the game (howlongtobeat) id
138-
@param game_id: id of the game to get the title
139-
@return: The game title from the given id
144+
enerate the parameters for the search request
145+
@param game_id: The game id to search in HLTB
146+
@return: The parameters object for the request
140147
"""
141-
142-
ua = UserAgent()
143148
params = {
144149
'id': str(game_id)
145150
}
151+
return params
152+
153+
@staticmethod
154+
def get_title_request_headers():
155+
"""
156+
Generate the headers for the search request
157+
@return: The headers object for the request
158+
"""
159+
ua = UserAgent()
146160
headers = {
147161
'User-Agent': ua.random,
148162
'referer': HTMLRequests.REFERER_HEADER
149163
}
164+
return headers
165+
166+
@staticmethod
167+
def get_game_title(game_id: int):
168+
"""
169+
Function that gets the title of a game from the game (howlongtobeat) id
170+
@param game_id: id of the game to get the title
171+
@return: The game title from the given id
172+
"""
173+
174+
params = HTMLRequests.get_title_request_parameters(game_id)
175+
headers = HTMLRequests.get_title_request_headers()
150176

151177
# Request and extract title
152178
contents = requests.get(HTMLRequests.GAME_URL, params=params, headers=headers)
@@ -160,14 +186,8 @@ async def async_get_game_title(game_id: int):
160186
@return: The game title from the given id
161187
"""
162188

163-
ua = UserAgent()
164-
params = {
165-
'id': str(game_id)
166-
}
167-
headers = {
168-
'User-Agent': ua.random,
169-
'referer': HTMLRequests.REFERER_HEADER
170-
}
189+
params = HTMLRequests.get_title_request_parameters(game_id)
190+
headers = HTMLRequests.get_title_request_headers()
171191

172192
# Request and extract title
173193
async with aiohttp.ClientSession() as session:

howlongtobeatpy/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = fh.read()
55

66
setup(name='howlongtobeatpy',
7-
version='0.1.20',
7+
version='0.1.21',
88
packages=find_packages(exclude=['tests']),
99
description='A Python API for How Long to Beat',
1010
long_description=long_description,

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ sonar.organization=scrappycocco-github
22
sonar.projectKey=ScrappyCocco_HowLongToBeat-PythonAPI
33

44
sonar.projectName=HowLongToBeat-PythonAPI
5-
sonar.projectVersion=0.1.20
5+
sonar.projectVersion=0.1.21
66

77
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
88
# This property is optional if sonar.modules is set.

0 commit comments

Comments
 (0)