Skip to content

Commit

Permalink
Small code rework - version 0.1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrappyCocco committed May 22, 2022
1 parent 1c87371 commit 053f91f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions howlongtobeatpy/howlongtobeatpy/HTMLRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SearchModifiers(Enum):

class HTMLRequests:
BASE_URL = 'https://howlongtobeat.com/'
ORIGIN_HEADER = 'https://howlongtobeat.com'
REFERER_HEADER = BASE_URL
SEARCH_URL = BASE_URL + "search_results"
GAME_URL = BASE_URL + "game?id="

Expand All @@ -43,8 +45,8 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
'content-type': 'application/x-www-form-urlencoded',
'accept': '*/*',
'User-Agent': ua.random,
'origin': 'https://howlongtobeat.com',
'referer': 'https://howlongtobeat.com/'
'origin': HTMLRequests.ORIGIN_HEADER,
'referer': HTMLRequests.REFERER_HEADER
}
payload = {
'queryString': game_name,
Expand All @@ -63,7 +65,7 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
}
# Make the post request and return the result if is valid
resp = requests.post(HTMLRequests.SEARCH_URL, params=params, headers=headers, data=payload)
if resp is not None and resp.status_code == 200:
if resp.status_code == 200:
return resp.text
else:
return None
Expand All @@ -85,8 +87,8 @@ async def send_async_web_request(game_name: str, search_modifiers: SearchModifie
'content-type': 'application/x-www-form-urlencoded',
'accept': '*/*',
'User-Agent': ua.random,
'origin': 'https://howlongtobeat.com',
'referer': 'https://howlongtobeat.com/'
'origin': HTMLRequests.ORIGIN_HEADER,
'referer': HTMLRequests.REFERER_HEADER
}
payload = {
'queryString': game_name,
Expand Down Expand Up @@ -124,7 +126,7 @@ def __cut_game_title(game_title: str):
if game_title is None or len(game_title) == 0:
return None

title = re.search("<title>([\w\W]*)<\/title>", game_title)
title = re.search("<title>(.*)<\/title>", game_title)
# The position of start and end of this method may change if the website change
cut_title = str(html.unescape(title.group(1)[12:-17]))
return cut_title
Expand All @@ -143,7 +145,7 @@ def get_game_title(game_id: int):
}
headers = {
'User-Agent': ua.random,
'referer': 'https://howlongtobeat.com/'
'referer': HTMLRequests.REFERER_HEADER
}

# Request and extract title
Expand All @@ -164,7 +166,7 @@ async def async_get_game_title(game_id: int):
}
headers = {
'User-Agent': ua.random,
'referer': 'https://howlongtobeat.com/'
'referer': HTMLRequests.REFERER_HEADER
}

# Request and extract title
Expand Down
2 changes: 1 addition & 1 deletion howlongtobeatpy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = fh.read()

setup(name='howlongtobeatpy',
version='0.1.19',
version='0.1.20',
packages=find_packages(exclude=['tests']),
description='A Python API for How Long to Beat',
long_description=long_description,
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sonar.organization=scrappycocco-github
sonar.projectKey=ScrappyCocco_HowLongToBeat-PythonAPI

sonar.projectName=HowLongToBeat-PythonAPI
sonar.projectVersion=0.1.19
sonar.projectVersion=0.1.20

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

0 comments on commit 053f91f

Please sign in to comment.