Skip to content

Commit

Permalink
version 0.28
Browse files Browse the repository at this point in the history
  • Loading branch information
FriendsOfGalaxy committed Sep 13, 2019
1 parent 0b2b6b2 commit a45eb37
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion requirements/app.txt
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 7 additions & 3 deletions src/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ async def get_game_time(self, user_id, master_title_id, multiplayer_id):
</usage>
"""
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))
Expand Down
13 changes: 10 additions & 3 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.27"
__version__ = "0.28"
23 changes: 21 additions & 2 deletions tests/test_game_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
(10, 1451288960),
(120, 1551288960),
(120, 1551288960),
(0, 0)
(0, None)
]

NEW_BACKEND_GAME_USAGE_RESPONSES = [
Expand Down Expand Up @@ -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", [
("<total>30292</total>", "<lastSessionEndTimeStamp>0</lastSessionEndTimeStamp>", (505, None)),
("<total>59</total>", "", (1, None)),
("<total>128</total>", "<lastSessionEndTimeStamp>1497190184759</lastSessionEndTimeStamp>", (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('''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<usage>
{total}
{last_played_time}
</usage>
'''.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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit a45eb37

Please sign in to comment.