Ejemplo n.º 1
0
    async def update_communication_id_cache(self, title_ids: List[TitleId]) \
            -> Dict[TitleId, List[TrophyTitleInfo]]:
        async def updater(title_id_slice: Iterable[TitleId]):
            delta.update(await
                         self._psn_client.async_get_game_trophy_title_info_map(
                             title_id_slice))

        delta: Dict[TitleId, List[TrophyTitleInfo]] = dict()
        await asyncio.gather(*[
            updater(title_ids[it:it + MAX_TITLE_IDS_PER_REQUEST])
            for it in range(0, len(title_ids), MAX_TITLE_IDS_PER_REQUEST)
        ])

        invalidate_time = UnixTimestamp(
            int(time.time()) + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC)
        for k, v in delta.items():
            self._trophy_title_info_cache.update(k, v, invalidate_time)
        try:
            self.persistent_cache[
                TROPHY_TITLE_INFO_CACHE_KEY] = serialization.dumps(
                    self._trophy_title_info_cache)
            self.push_cache()
        except (pickle.PicklingError, binascii.Error):
            logging.error("Can not serialize communication ids cache")

        return delta
Ejemplo n.º 2
0
    async def prepare_achievements_context(self, game_ids: List[str]) -> Any:
        games_cids = {
            title_id: [tti.communication_id for tti in ttis]
            for title_id, ttis in (
                await self.get_game_trophies_map(game_ids)).items()
        }
        trophy_titles = await self._psn_client.get_trophy_titles()

        pending_cid_tids, pending_tid_cids, tid_trophies = self._process_trophies_cache(
            games_cids, trophy_titles)

        # process pending trophies
        requests = []
        for comm_id in pending_cid_tids.keys():
            timestamp = trophy_titles[comm_id]
            pending_tids = pending_cid_tids[comm_id]
            requests.append(
                self._import_trophies(comm_id, pending_tids, pending_tid_cids,
                                      tid_trophies, timestamp))

        await asyncio.gather(*requests)

        # update cache
        if requests:
            try:
                self.persistent_cache[
                    TROPHIES_CACHE_KEY] = serialization.dumps(
                        self._trophies_cache)
                self.push_cache()
            except (pickle.PicklingError, binascii.Error):
                logging.error("Can not serialize trophies cache")

        return trophy_titles
Ejemplo n.º 3
0
async def test_cache_parsing(authenticated_plugin, mock_persistent_cache,
                             mock_trophy_title_info_cache):
    mock_persistent_cache.return_value = {
        TROPHY_TITLE_INFO_CACHE_KEY:
        serialization.dumps(mock_trophy_title_info_cache)
    }
    authenticated_plugin.handshake_complete()
    assert authenticated_plugin._trophy_title_info_cache._entries == mock_trophy_title_info_cache._entries
Ejemplo n.º 4
0
    def request(self, task, *args, **kwargs):
        """
        Basic api, send a request
        task is the function, whereas args and kwargs are the arguments

        returns a eventlet event function, call .wait() on this to obtain
        the result or raise the exception
        """
        # I can't partial without arguments, so I protect it here
        if args or kwargs:
            task = functools.partial(task, *args, **kwargs)

        # everything is transfered as a string from here to worker
        task_description = dumps(task)

        # the dispatcher does the actual interesting work
        dispatcher_event = self._dispatcher.do_task(self._configuration_id,
                                                    task_description)
        return eventlet.spawn(decode_response, dispatcher_event)