Skip to content

Commit

Permalink
Fix [BUG] KeyError: 'profile_def' on search #29 - Added checks when p…
Browse files Browse the repository at this point in the history
…arsing JSON - Updated to 1.0.8
  • Loading branch information
ScrappyCocco committed Sep 7, 2024
1 parent 96e1981 commit 305753e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions howlongtobeatpy/howlongtobeatpy/HowLongToBeatEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class HowLongToBeatEntry:
"""
A simple class to collect all game data that are being read from the JSON response
It contains just the main data and values for the game, the rest can be read manually from the JSON
Consider that some values could be None, such as profile_dev, since HLTB sometimes remove/add values
The full content for the entry is available in json_content
"""

Expand Down
33 changes: 20 additions & 13 deletions howlongtobeatpy/howlongtobeatpy/JSONResultParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,30 @@ def parse_json_result(self, input_json_result):
def parse_json_element(self, input_game_element):
current_entry = HowLongToBeatEntry()
# Compute base fields
current_entry.game_id = input_game_element["game_id"]
current_entry.game_name = input_game_element["game_name"]
current_entry.game_alias = input_game_element["game_alias"]
current_entry.game_type = input_game_element["game_type"]
current_entry.game_image_url = self.IMAGE_URL_PREFIX + input_game_element["game_image"]
current_entry.game_id = input_game_element.get("game_id")
current_entry.game_name = input_game_element.get("game_name")
current_entry.game_alias = input_game_element.get("game_alias")
current_entry.game_type = input_game_element.get("game_type")
if "game_image" in input_game_element:
current_entry.game_image_url = self.IMAGE_URL_PREFIX + input_game_element.get("game_image")
current_entry.game_web_link = self.GAME_URL_PREFIX + str(current_entry.game_id)
current_entry.review_score = input_game_element["review_score"]
current_entry.profile_dev = input_game_element["profile_dev"]
current_entry.profile_platforms = input_game_element["profile_platform"].split(", ")
current_entry.release_world = input_game_element["release_world"]
current_entry.review_score = input_game_element.get("review_score")
current_entry.profile_dev = input_game_element.get("profile_dev")
if "profile_platform" in input_game_element:
current_entry.profile_platforms = input_game_element.get("profile_platform").split(", ")
current_entry.release_world = input_game_element.get("release_world")
# Add full JSON content to the entry
current_entry.json_content = input_game_element
# Add a few times elements as help for the user
current_entry.main_story = round(input_game_element["comp_main"] / 3600, 2)
current_entry.main_extra = round(input_game_element["comp_plus"] / 3600, 2)
current_entry.completionist = round(input_game_element["comp_100"] / 3600, 2)
current_entry.all_styles = round(input_game_element["comp_all"] / 3600, 2)
# Calculate only if value is not None
if "comp_main" in input_game_element:
current_entry.main_story = round(input_game_element.get("comp_main") / 3600, 2)
if "comp_plus" in input_game_element:
current_entry.main_extra = round(input_game_element.get("comp_plus") / 3600, 2)
if "comp_100" in input_game_element:
current_entry.completionist = round(input_game_element.get("comp_100") / 3600, 2)
if "comp_all" in input_game_element:
current_entry.all_styles = round(input_game_element.get("comp_all") / 3600, 2)
# Compute Similarity
game_name_similarity = self.similar(self.game_name, current_entry.game_name,
self.game_name_numbers, self.similarity_case_sensitive)
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='1.0.7',
version='1.0.8',
packages=find_packages(exclude=['tests']),
description='A Python API for How Long to Beat',
long_description=long_description,
Expand Down
3 changes: 2 additions & 1 deletion howlongtobeatpy/tests/test_async_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ async def test_game_name_and_dev(self):
self.assertNotEqual(None, results, "Search Results are None")
best_result = TestNormalRequest.getMaxSimilarityElement(results)
self.assertEqual("Crysis Warhead", best_result.game_name)
self.assertEqual("Crytek Budapest", best_result.profile_dev)
if best_result.profile_dev is not None:
self.assertEqual("Crytek Budapest", best_result.profile_dev)
self.assertEqual("2008", str(best_result.release_world))
self.assertAlmostEqual(7, TestNormalRequest.getSimpleNumber(best_result.completionist), delta=3)

Expand Down
3 changes: 2 additions & 1 deletion howlongtobeatpy/tests/test_async_request_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ async def test_game_name_and_dev(self):
result = await HowLongToBeat().async_search_from_id(2071)
self.assertNotEqual(None, result, "Search Result is None")
self.assertEqual("Crysis Warhead", result.game_name)
self.assertEqual("Crytek Budapest", result.profile_dev)
if result.profile_dev is not None:
self.assertEqual("Crytek Budapest", result.profile_dev)
self.assertEqual("2008", str(result.release_world))
self.assertAlmostEqual(7, TestNormalRequest.getSimpleNumber(result.completionist), delta=3)

Expand Down
3 changes: 2 additions & 1 deletion howlongtobeatpy/tests/test_normal_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_game_name_and_dev(self):
self.assertNotEqual(None, results, "Search Results are None")
best_result = self.getMaxSimilarityElement(results)
self.assertEqual("Crysis Warhead", best_result.game_name)
self.assertEqual("Crytek Budapest", best_result.profile_dev)
if best_result.profile_dev is not None:
self.assertEqual("Crytek Budapest", best_result.profile_dev)
self.assertEqual("2008", str(best_result.release_world))
self.assertAlmostEqual(7, self.getSimpleNumber(best_result.completionist), delta=3)

Expand Down
3 changes: 2 additions & 1 deletion howlongtobeatpy/tests/test_normal_request_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_game_name_and_dev(self):
result = HowLongToBeat().search_from_id(2071)
self.assertNotEqual(None, result, "Search Result is None")
self.assertEqual("Crysis Warhead", result.game_name)
self.assertEqual("Crytek Budapest", result.profile_dev)
if result.profile_dev is not None:
self.assertEqual("Crytek Budapest", result.profile_dev)
self.assertEqual("2008", str(result.release_world))
self.assertAlmostEqual(7, TestNormalRequest.getSimpleNumber(result.completionist), delta=3)

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.7
sonar.projectVersion=1.0.8
sonar.python.version=3.9

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

0 comments on commit 305753e

Please sign in to comment.