Skip to content

Commit ef2dcbc

Browse files
committed
Update to 0.1.17
Added User Agent to requests header to avoid error 403
1 parent 7dde185 commit ef2dcbc

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

howlongtobeatpy/howlongtobeatpy/HTMLRequests.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import html
88
from enum import Enum
9+
from fake_useragent import UserAgent
910

1011

1112
# ---------------------------------------------------------------------
@@ -33,9 +34,11 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
3334
@param search_modifiers: The "Modifiers" list in "Search Options", allow to show/isolate/hide DLCs
3435
@return: The HTML code of the research if the request returned 200(OK), None otherwise
3536
"""
37+
ua = UserAgent()
3638
headers = {
3739
'content-type': 'application/x-www-form-urlencoded',
38-
'accept': '*/*'
40+
'accept': '*/*',
41+
'User-Agent': ua.random
3942
}
4043
payload = {
4144
'queryString': game_name,
@@ -63,9 +66,11 @@ async def send_async_web_request(game_name: str, search_modifiers: SearchModifie
6366
@param search_modifiers: The "Modifiers" list in "Search Options", allow to show/isolate/hide DLCs
6467
@return: The HTML code of the research if the request returned 200(OK), None otherwise
6568
"""
69+
ua = UserAgent()
6670
headers = {
6771
'content-type': 'application/x-www-form-urlencoded',
68-
'accept': '*/*'
72+
'accept': '*/*',
73+
'User-Agent': ua.random
6974
}
7075
payload = {
7176
'queryString': game_name,
@@ -112,10 +117,15 @@ def get_game_title(game_id: int):
112117
@return: The game title from the given id
113118
"""
114119

120+
ua = UserAgent()
121+
headers = {
122+
'User-Agent': ua.random
123+
}
124+
115125
url_get = HTMLRequests.GAME_URL + str(game_id)
116126

117127
# Request and extract title
118-
contents = requests.get(url_get)
128+
contents = requests.get(url_get, headers=headers)
119129
return HTMLRequests.__cut_game_title(contents.text)
120130

121131
@staticmethod
@@ -126,11 +136,16 @@ async def async_get_game_title(game_id: int):
126136
@return: The game title from the given id
127137
"""
128138

139+
ua = UserAgent()
140+
headers = {
141+
'User-Agent': ua.random
142+
}
143+
129144
url_get = HTMLRequests.GAME_URL + str(game_id)
130145

131146
# Request and extract title
132147
async with aiohttp.ClientSession() as session:
133-
async with session.post(url_get) as resp:
148+
async with session.post(url_get, headers=headers) as resp:
134149
if resp is not None and str(resp.status) == "200":
135150
text = await resp.text()
136151
return HTMLRequests.__cut_game_title(text)

howlongtobeatpy/setup.py

Lines changed: 5 additions & 4 deletions
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.16',
7+
version='0.1.17',
88
packages=find_packages(exclude=['tests']),
99
description='A Python API for How Long to Beat',
1010
long_description=long_description,
@@ -14,8 +14,9 @@
1414
license='MIT',
1515
keywords='howlongtobeat gaming steam uplay origin time length how long to beat',
1616
install_requires=[
17-
'aiohttp>=3.7.1',
18-
'requests>=2.24.0',
19-
'aiounittest>=1.4.0'
17+
'aiohttp>=3.7.3',
18+
'requests>=2.25.1',
19+
'aiounittest>=1.4.0',
20+
'fake_useragent==0.1.11'
2021
],
2122
zip_safe=False)

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.16
5+
sonar.projectVersion=0.1.17
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)