Skip to content

Commit 5749a20

Browse files
committed
Add auto filter code based on flags
1 parent 1279872 commit 5749a20

9 files changed

Lines changed: 68 additions & 6 deletions

howlongtobeatpy/howlongtobeatpy/HowLongToBeat.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ class HowLongToBeat:
2020
# Constructor with optional parameters
2121
# ------------------------------------------
2222

23-
def __init__(self, input_minimum_similarity: float = 0.4):
23+
def __init__(self, input_minimum_similarity: float = 0.4, input_auto_filter_times: bool = False):
2424
"""
2525
@param input_minimum_similarity: Minimum similarity to use to filter the results with the found name,
26+
@param input_auto_filter_times: If the json parser should automatically filter times based on the game types (online/sp)
2627
0 will return all the results; 1 means perfectly equal and should not be used; default is 0.4;
2728
"""
2829
self.minimum_similarity = input_minimum_similarity
30+
self.auto_filter_times = input_auto_filter_times
2931

3032
if platform.system() == 'Windows':
3133
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
@@ -126,11 +128,11 @@ def __parse_web_result(self, game_name: str, html_result, game_id=None,
126128
"""
127129
if game_id is None:
128130
parser = JSONResultParser(game_name, HTMLRequests.GAME_URL, self.minimum_similarity, game_id,
129-
similarity_case_sensitive)
131+
similarity_case_sensitive, self.auto_filter_times)
130132
else:
131133
# If the search is by id, ignore class minimum_similarity and set it to 0.0
132134
# The result is filtered by ID anyway, so the similarity shouldn't count too much
133135
# Also ignore similarity_case_sensitive and leave default value
134-
parser = JSONResultParser(game_name, HTMLRequests.GAME_URL, 0.0, game_id)
136+
parser = JSONResultParser(game_name, HTMLRequests.GAME_URL, 0.0, game_id, False, self.auto_filter_times)
135137
parser.parse_json_result(html_result)
136138
return parser.results

howlongtobeatpy/howlongtobeatpy/HowLongToBeatEntry.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ def __init__(self):
4343
self.completionist = None
4444
# All styles
4545
self.all_styles = None
46+
# invested_co value
47+
self.coop_time = None
48+
# invested_mp value
49+
self.mp_time = None
50+
# These are used to identify if the game has singpe, coop and/or multiplayer
51+
# So you can filter data based on those
52+
self.complexity_lvl_combine = False
53+
self.complexity_lvl_sp = False
54+
self.complexity_lvl_co = False
55+
self.complexity_lvl_mp = False

howlongtobeatpy/howlongtobeatpy/JSONResultParser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class JSONResultParser:
2020

2121
def __init__(self, input_game_name: str, input_game_url: str,
2222
input_minimum_similarity: float, input_game_id: int = None,
23-
input_similarity_case_sensitive: bool = True):
23+
input_similarity_case_sensitive: bool = True,
24+
input_auto_filter_times: bool = False):
2425
# Init instance variables
2526
self.results = []
2627
self.minimum_similarity = input_minimum_similarity
2728
self.similarity_case_sensitive = input_similarity_case_sensitive
29+
self.auto_filter_times = input_auto_filter_times
2830
self.game_id = input_game_id
2931
self.base_game_url = input_game_url
3032
# Init object
@@ -75,6 +77,21 @@ def parse_json_element(self, input_game_element):
7577
current_entry.completionist = round(input_game_element.get("comp_100") / 3600, 2)
7678
if "comp_all" in input_game_element:
7779
current_entry.all_styles = round(input_game_element.get("comp_all") / 3600, 2)
80+
if "invested_co" in input_game_element:
81+
current_entry.coop_time = round(input_game_element.get("invested_co") / 3600, 2)
82+
if "invested_mp" in input_game_element:
83+
current_entry.mp_time = round(input_game_element.get("invested_mp") / 3600, 2)
84+
# Add complexity booleans
85+
current_entry.complexity_lvl_combine = bool(input_game_element.get("comp_lvl_combine", 0))
86+
current_entry.complexity_lvl_sp = bool(input_game_element.get("comp_lvl_sp", 0))
87+
current_entry.complexity_lvl_co = bool(input_game_element.get("comp_lvl_co", 0))
88+
current_entry.complexity_lvl_mp = bool(input_game_element.get("comp_lvl_mp", 0))
89+
if self.auto_filter_times:
90+
# Auto-Nullify values based on the flags
91+
if current_entry.complexity_lvl_co is False:
92+
current_entry.coop_time = None
93+
if current_entry.complexity_lvl_mp is False:
94+
current_entry.mp_time = None
7895
# Compute Similarity
7996
game_name_similarity = self.similar(self.game_name, current_entry.game_name,
8097
self.game_name_numbers, self.similarity_case_sensitive)

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='1.0.17',
7+
version='1.0.18',
88
packages=find_packages(exclude=['tests']),
99
description='A Python API for How Long to Beat',
1010
long_description=long_description,

howlongtobeatpy/tests/test_async_request.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ async def test_game_name_with_numbers(self):
4242
self.assertEqual("The Witcher 3: Wild Hunt", best_result.game_name)
4343
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(best_result.main_story), delta=25)
4444

45+
@async_test
46+
async def test_game_with_auto_filter(self):
47+
results = await HowLongToBeat(input_auto_filter_times = True).async_search("The Witcher 3")
48+
self.assertNotEqual(None, results, "Search Results are None")
49+
best_result = TestNormalRequest.getMaxSimilarityElement(results)
50+
self.assertEqual("The Witcher 3: Wild Hunt", best_result.game_name)
51+
self.assertEqual(None, best_result.coop_time)
52+
self.assertEqual(None, best_result.mp_time)
53+
4554
@async_test
4655
async def test_game_with_values(self):
4756
results = await HowLongToBeat().async_search("Crysis 3")

howlongtobeatpy/tests/test_async_request_by_id.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ async def test_game_name_with_numbers(self):
3636
self.assertEqual("The Witcher 3: Wild Hunt", result.game_name)
3737
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(result.main_story), delta=25)
3838

39+
@async_test
40+
async def test_game_with_auto_filter(self):
41+
result = await HowLongToBeat(input_auto_filter_times = True).async_search_from_id(10270)
42+
self.assertNotEqual(None, result, "Search Result is None")
43+
self.assertEqual("The Witcher 3: Wild Hunt", result.game_name)
44+
self.assertEqual(None, result.coop_time)
45+
self.assertEqual(None, result.mp_time)
46+
3947
@async_test
4048
async def test_game_with_values(self):
4149
result = await HowLongToBeat().async_search_from_id(2070)

howlongtobeatpy/tests/test_normal_request.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ def test_game_name_with_numbers(self):
5858
self.assertEqual("The Witcher 3: Wild Hunt", best_result.game_name)
5959
self.assertAlmostEqual(50, self.getSimpleNumber(best_result.main_story), delta=5)
6060

61+
def test_game_with_auto_filter(self):
62+
results = HowLongToBeat(input_auto_filter_times = True).search("The Witcher 3")
63+
self.assertNotEqual(None, results, "Search Results are None")
64+
best_result = self.getMaxSimilarityElement(results)
65+
self.assertEqual("The Witcher 3: Wild Hunt", best_result.game_name)
66+
self.assertAlmostEqual(50, self.getSimpleNumber(best_result.main_story), delta=5)
67+
self.assertEqual(None, best_result.coop_time)
68+
self.assertEqual(None, best_result.mp_time)
69+
6170
def test_game_with_values(self):
6271
results = HowLongToBeat().search("Battlefield 2142")
6372
self.assertNotEqual(None, results, "Search Results are None")

howlongtobeatpy/tests/test_normal_request_by_id.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ def test_game_name_with_numbers(self):
3131
self.assertEqual("The Witcher 3: Wild Hunt", result.game_name)
3232
self.assertAlmostEqual(50, TestNormalRequest.getSimpleNumber(result.main_story), delta=5)
3333

34+
def test_game_with_auto_filter(self):
35+
result = HowLongToBeat(input_auto_filter_times = True).search_from_id(10270)
36+
self.assertNotEqual(None, result, "Search Result is None")
37+
self.assertEqual("The Witcher 3: Wild Hunt", result.game_name)
38+
self.assertEqual(None, result.coop_time)
39+
self.assertEqual(None, result.mp_time)
40+
3441
def test_game_with_values(self):
3542
result = HowLongToBeat().search_from_id(936)
3643
self.assertNotEqual(None, result, "Search Result is None")

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=1.0.17
5+
sonar.projectVersion=1.0.18
66
sonar.python.version=3.9
77

88
# Define separate root directories for sources and tests

0 commit comments

Comments
 (0)