Ejemplo n.º 1
0
    async def load_latest_tweets(self, data_url):
        cache_key = ContentCache.key_from_args(data_url, "tweets")
        cache_valid = "180d"
        with ContentCache() as cc:
            cache_data = cc.get(cache_key)
            if cache_data:
                cache_data = json.loads(cache_data)
                # automatically update cache in background every 12 hours
                if cache_data["time"] < (time.time() - (3600 * 12)):
                    create_task(
                        self._preload_latest_tweets(data_url, cache_key, cache_valid)
                    )
                return cache_data["result"]

        return await self._preload_latest_tweets(data_url, cache_key, cache_valid)
Ejemplo n.º 2
0
 def fetch_json_data(self, method, path, **kwargs):
     cache_valid = kwargs.pop(
         "cache_valid") if "cache_valid" in kwargs else None
     if not cache_valid:
         return self.raise_error_from_response(
             self.send_request(method, path, **kwargs))
     cache_key = ContentCache.key_from_args(method, path,
                                            kwargs.get("params"),
                                            kwargs.get("data"))
     with ContentCache("http") as cc:
         result = cc.get(cache_key)
         if result is not None:
             return json.loads(result)
         response = self.send_request(method, path, **kwargs)
         cc.set(cache_key, response.text, cache_valid)
         return self.raise_error_from_response(response)
Ejemplo n.º 3
0
 async def _preload_latest_tweets(data_url, cache_key, cache_valid):
     result = json.loads((await OSRPC.fetch_content(data_url)))
     with ContentCache() as cc:
         cc.set(
             cache_key,
             json.dumps({"time": int(time.time()), "result": result}),
             cache_valid,
         )
     return result
Ejemplo n.º 4
0
    async def fetch_content(uri, data=None, headers=None, cache_valid=None):
        if not headers:
            headers = {
                "User-Agent":
                ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) "
                 "AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 "
                 "Safari/603.3.8")
            }
        cache_key = ContentCache.key_from_args(uri,
                                               data) if cache_valid else None
        with ContentCache() as cc:
            if cache_key:
                result = cc.get(cache_key)
                if result is not None:
                    return result

        # check internet before and resolve issue with 60 seconds timeout
        ensure_internet_on(raise_exception=True)

        session = helpers.requests_session()
        if data:
            r = await session.post(uri,
                                   data=data,
                                   headers=headers,
                                   timeout=__default_requests_timeout__)
        else:
            r = await session.get(uri,
                                  headers=headers,
                                  timeout=__default_requests_timeout__)

        r.raise_for_status()
        result = r.text
        if cache_valid:
            with ContentCache() as cc:
                cc.set(cache_key, result, cache_valid)
        return result
Ejemplo n.º 5
0
 def _kill_previous_session(self):
     assert self._session_id
     pid = None
     with ContentCache() as cc:
         pid = cc.get(self._session_id)
         cc.delete(self._session_id)
     if not pid:
         return
     if "windows" in util.get_systype():
         kill = ["Taskkill", "/PID", pid, "/F"]
     else:
         kill = ["kill", pid]
     try:
         proc.exec_command(kill)
     except:  # pylint: disable=bare-except
         pass
Ejemplo n.º 6
0
 def _unlock_session(self):
     if not self._session_id:
         return
     with ContentCache() as cc:
         cc.delete(self._session_id)
Ejemplo n.º 7
0
 def _lock_session(self, pid):
     if not self._session_id:
         return
     with ContentCache() as cc:
         cc.set(self._session_id, str(pid), "1h")