From a45eb37a6252c05e7c5da4b4e6a01cdcaab3a9f9 Mon Sep 17 00:00:00 2001 From: FriendsOfGalaxy Date: Sat, 14 Sep 2019 00:06:10 +0200 Subject: [PATCH] version 0.28 --- requirements/app.txt | 2 +- src/backend.py | 10 +++++++--- src/plugin.py | 13 ++++++++++--- src/version.py | 2 +- tests/test_game_times.py | 23 +++++++++++++++++++++-- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/requirements/app.txt b/requirements/app.txt index 544151c..82994b9 100755 --- a/requirements/app.txt +++ b/requirements/app.txt @@ -1,3 +1,3 @@ -galaxy.plugin.api==0.46 +galaxy.plugin.api==0.48 pyobjc-framework-CoreServices==5.1.2; sys_platform == 'darwin' psutil==5.6.1; sys_platform == 'darwin' diff --git a/src/backend.py b/src/backend.py index b1ffc17..608d877 100755 --- a/src/backend.py +++ b/src/backend.py @@ -243,12 +243,16 @@ async def get_game_time(self, user_id, master_title_id, multiplayer_id): """ try: + def parse_last_played_time(lastplayed_timestamp) -> Optional[int]: + if lastplayed_timestamp is None: + return None + return round(int(lastplayed_timestamp.text) / 1000) or None # response is in miliseconds + content = await response.text() xml_response = ET.fromstring(content) total_play_time = round(int(xml_response.find("total").text) / 60) # response is in seconds - last_session_end_time = round( - int(xml_response.find("lastSessionEndTimeStamp").text) / 1000) # response is in miliseconds - return total_play_time, last_session_end_time + + return total_play_time, parse_last_played_time(xml_response.find("lastSessionEndTimeStamp")) except (ET.ParseError, AttributeError, ValueError) as e: logging.exception("Can not parse backend response: %s", await response.text()) raise UnknownBackendResponse(str(e)) diff --git a/src/plugin.py b/src/plugin.py index 76de473..28abc62 100755 --- a/src/plugin.py +++ b/src/plugin.py @@ -83,8 +83,8 @@ def _game_time_cache(self) -> Dict[OfferId, GameTime]: def _offer_id_cache(self): return self.persistent_cache.setdefault("offers", {}) - def shutdown(self): - asyncio.create_task(self._http_client.close()) + async def shutdown(self): + await self._http_client.close() def tick(self): self.handle_local_game_update_notifications() @@ -362,8 +362,15 @@ def _update_stored_cookies(self, morsels): def handshake_complete(self): def game_time_decoder(cache: Dict) -> Dict[OfferId, GameTime]: + def parse_last_played_time(entry): + # old cache might still contains 0 after plugin upgrade + lpt = entry.get("last_played_time") + if lpt == 0: + return None + return lpt + return { - offer_id: GameTime(entry["game_id"], entry["time_played"], entry["last_played_time"]) + offer_id: GameTime(entry["game_id"], entry["time_played"], parse_last_played_time(entry)) for offer_id, entry in cache.items() if entry and offer_id } diff --git a/src/version.py b/src/version.py index 0a9226d..ebdb722 100755 --- a/src/version.py +++ b/src/version.py @@ -1 +1 @@ -__version__ = "0.27" +__version__ = "0.28" diff --git a/tests/test_game_times.py b/tests/test_game_times.py index 7eadbed..a11a091 100644 --- a/tests/test_game_times.py +++ b/tests/test_game_times.py @@ -70,7 +70,7 @@ (10, 1451288960), (120, 1551288960), (120, 1551288960), - (0, 0) + (0, None) ] NEW_BACKEND_GAME_USAGE_RESPONSES = [ @@ -132,6 +132,25 @@ async def test_lastplayed_parsing(persona_id, http_client, create_xml_response): http_client.get.assert_called_once() +@pytest.mark.asyncio +@pytest.mark.parametrize("total, last_played_time, game_time", [ + ("30292", "0", (505, None)), + ("59", "", (1, None)), + ("128", "1497190184759", (2, 1497190185)), +]) +async def test_game_time_parsing(total, last_played_time, game_time, persona_id, http_client, create_xml_response): + http_client.get.return_value = create_xml_response(''' + + {total} + {last_played_time} + + '''.format(total=total, last_played_time=last_played_time)) + + assert game_time == await OriginBackendClient(http_client).get_game_time(persona_id, None, None) + + http_client.get.assert_called_once() + + @pytest.mark.asyncio async def test_prepare_game_times_context( authenticated_plugin, @@ -229,7 +248,7 @@ async def test_game_time_import_empty_cache( }''', { "DR:119971300": GameTime(game_id="DR:119971300", time_played=41, last_played_time=1551279330), - "OFB-EAST:109552409": GameTime(game_id="OFB-EAST:109552409", time_played=0, last_played_time=0), + "OFB-EAST:109552409": GameTime(game_id="OFB-EAST:109552409", time_played=0, last_played_time=None), "OFB-EAST:48217": GameTime(game_id="OFB-EAST:48217", time_played=116, last_played_time=1564660289), "Origin.OFR.0002694": GameTime(game_id="Origin.OFR.50.0002694", time_played=1, last_played_time=1555077603) }