Skip to content

Commit

Permalink
Fix [BUG] Can't fetch the API key #30 : Moved the api key in the requ…
Browse files Browse the repository at this point in the history
…est body
  • Loading branch information
ScrappyCocco committed Nov 14, 2024
1 parent 85edb51 commit 76d3d36
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
52 changes: 26 additions & 26 deletions howlongtobeatpy/howlongtobeatpy/HTMLRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# IMPORTS

import re
import html
import json
from enum import Enum
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -43,7 +42,7 @@ def get_search_request_headers():
return headers

@staticmethod
def get_search_request_data(game_name: str, search_modifiers: SearchModifiers, page: int):
def get_search_request_data(game_name: str, search_modifiers: SearchModifiers, page: int, api_key: str):
"""
Generate the data payload for the search request
@param game_name: The name of the game to search
Expand Down Expand Up @@ -74,6 +73,7 @@ def get_search_request_data(game_name: str, search_modifiers: SearchModifiers, p
'modifier': search_modifiers.value,
},
'users': {
'id': api_key,
'sortCategory': "postcount"
},
'filter': "",
Expand All @@ -94,12 +94,12 @@ def send_web_request(game_name: str, search_modifiers: SearchModifiers = SearchM
@return: The HTML code of the research if the request returned 200(OK), None otherwise
"""
headers = HTMLRequests.get_search_request_headers()
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers, page)
api_key_result = HTMLRequests.send_website_request_getcode(False)
if api_key_result is None:
api_key_result = HTMLRequests.send_website_request_getcode(True)
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers, page, api_key_result)
# Make the post request and return the result if is valid
search_url_with_key = HTMLRequests.SEARCH_URL + "/" + api_key_result
search_url_with_key = HTMLRequests.SEARCH_URL
resp = requests.post(search_url_with_key, headers=headers, data=payload, timeout=60)
if resp.status_code == 200:
return resp.text
Expand All @@ -116,10 +116,10 @@ async def send_async_web_request(game_name: str, search_modifiers: SearchModifie
@return: The HTML code of the research if the request returned 200(OK), None otherwise
"""
headers = HTMLRequests.get_search_request_headers()
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers, page)
api_key_result = await HTMLRequests.async_send_website_request_getcode(False)
if api_key_result is None:
api_key_result = await HTMLRequests.async_send_website_request_getcode(True)
payload = HTMLRequests.get_search_request_data(game_name, search_modifiers, page, api_key_result)
# Make the post request and return the result if is valid
search_url_with_key = HTMLRequests.SEARCH_URL + "/" + api_key_result
async with aiohttp.ClientSession() as session:
Expand Down Expand Up @@ -207,7 +207,7 @@ async def async_get_game_title(game_id: int):
text = await resp.text()
return HTMLRequests.__cut_game_title(text)
return None

@staticmethod
def send_website_request_getcode(parse_all_scripts: bool):
"""
Expand All @@ -218,24 +218,24 @@ def send_website_request_getcode(parse_all_scripts: bool):
headers = HTMLRequests.get_title_request_headers()
resp = requests.get(HTMLRequests.BASE_URL, headers=headers, timeout=60)
if resp.status_code == 200 and resp.text is not None:
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(resp.text, 'html.parser')
# Find all <script> tags with a src attribute containing the substring
scripts = soup.find_all('script', src=True)
if parse_all_scripts:
matching_scripts = [script['src'] for script in scripts]
else:
matching_scripts = [script['src'] for script in scripts if '_app-' in script['src']]
for script_url in matching_scripts:
script_url = HTMLRequests.BASE_URL + script_url
script_resp = requests.get(script_url, headers=headers, timeout=60)
if script_resp.status_code == 200 and script_resp.text is not None:
pattern = r'"/api/search/".concat\("([a-zA-Z0-9]+)"\)'
matches = re.findall(pattern, script_resp.text)
for match in matches:
return match
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(resp.text, 'html.parser')
# Find all <script> tags with a src attribute containing the substring
scripts = soup.find_all('script', src=True)
if parse_all_scripts:
matching_scripts = [script['src'] for script in scripts]
else:
matching_scripts = [script['src'] for script in scripts if '_app-' in script['src']]
for script_url in matching_scripts:
script_url = HTMLRequests.BASE_URL + script_url
script_resp = requests.get(script_url, headers=headers, timeout=60)
if script_resp.status_code == 200 and script_resp.text is not None:
pattern = r'users\s*:\s*{\s*id\s*:\s*"([^"]+)"'
matches = re.findall(pattern, script_resp.text)
for match in matches:
return match
return None

@staticmethod
async def async_send_website_request_getcode(parse_all_scripts: bool):
"""
Expand All @@ -262,11 +262,11 @@ async def async_send_website_request_getcode(parse_all_scripts: bool):
async with session.get(script_url, headers=headers) as script_resp:
if script_resp is not None and str(resp.status) == "200":
script_resp_text = await script_resp.text()
pattern = r'"/api/search/".concat\("([a-zA-Z0-9]+)"\)'
pattern = r'users\s*:\s*{\s*id\s*:\s*"([^"]+)"'
matches = re.findall(pattern, script_resp_text)
for match in matches:
return match
else:
return None
return None
else:
return None
return None
4 changes: 2 additions & 2 deletions howlongtobeatpy/setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from setuptools import setup, find_packages

with open("README.md", "r") as fh:
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setup(name='howlongtobeatpy',
version='1.0.9',
version='1.0.10',
packages=find_packages(exclude=['tests']),
description='A Python API for How Long to Beat',
long_description=long_description,
Expand Down
12 changes: 6 additions & 6 deletions howlongtobeatpy/tests/test_async_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ async def test_game_name_with_numbers(self):
self.assertNotEqual(None, results, "Search Results are None")
best_result = TestNormalRequest.getMaxSimilarityElement(results)
self.assertEqual("The Witcher 3: Wild Hunt", best_result.game_name)
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(best_result.main_story), delta=5)
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(best_result.main_story), delta=25)

@async_test
async def test_game_with_values(self):
results = await HowLongToBeat().async_search("Battlefield 2142")
results = await HowLongToBeat().async_search("Crysis 3")
self.assertNotEqual(None, results, "Search Results are None")
best_result = TestNormalRequest.getMaxSimilarityElement(results)
self.assertEqual("Battlefield 2142", best_result.game_name)
self.assertAlmostEqual(14, TestNormalRequest.getSimpleNumber(best_result.main_story), delta=5)
self.assertAlmostEqual(17, TestNormalRequest.getSimpleNumber(best_result.main_extra), delta=5)
self.assertAlmostEqual(30, TestNormalRequest.getSimpleNumber(best_result.completionist), delta=5)
self.assertEqual("Crysis 3", best_result.game_name)
self.assertAlmostEqual(6, TestNormalRequest.getSimpleNumber(best_result.main_story), delta=20)
self.assertAlmostEqual(8, TestNormalRequest.getSimpleNumber(best_result.main_extra), delta=20)
self.assertAlmostEqual(13, TestNormalRequest.getSimpleNumber(best_result.completionist), delta=20)

@async_test
async def test_game_links(self):
Expand Down
14 changes: 7 additions & 7 deletions howlongtobeatpy/tests/test_async_request_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ async def test_game_name_with_numbers(self):
result = await HowLongToBeat().async_search_from_id(10270)
self.assertNotEqual(None, result, "Search Result is None")
self.assertEqual("The Witcher 3: Wild Hunt", result.game_name)
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(result.main_story), delta=5)
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(result.main_story), delta=25)

@async_test
async def test_game_with_values(self):
result = await HowLongToBeat().async_search_from_id(936)
self.assertNotEqual(None, result, "Search Result is None")
self.assertEqual("Battlefield 2142", result.game_name)
self.assertAlmostEqual(14, TestNormalRequest.getSimpleNumber(result.main_story), delta=5)
self.assertAlmostEqual(17, TestNormalRequest.getSimpleNumber(result.main_extra), delta=5)
self.assertAlmostEqual(30, TestNormalRequest.getSimpleNumber(result.completionist), delta=5)
result = await HowLongToBeat().async_search_from_id(2070)
self.assertNotEqual(None, result, "Search Results are None")
self.assertEqual("Crysis 3", result.game_name)
self.assertAlmostEqual(6, TestNormalRequest.getSimpleNumber(result.main_story), delta=20)
self.assertAlmostEqual(8, TestNormalRequest.getSimpleNumber(result.main_extra), delta=20)
self.assertAlmostEqual(13, TestNormalRequest.getSimpleNumber(result.completionist), delta=20)

@async_test
async def test_game_links(self):
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=1.0.9
sonar.projectVersion=1.0.10
sonar.python.version=3.9

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
Expand Down

0 comments on commit 76d3d36

Please sign in to comment.