Beispiel #1
0
    def fetch(tweet_id: int,
              index_no: int = 0,
              cutoff: int = 86400) -> typing.Optional['TweetSauceCache']:
        """
        Attempt to load a cached saucenao lookup
        Args:
            tweet_id(int): Tweet ID to look up
            index_no (int): The media indice for tweets with multiple media uploads
            cutoff (int): Only retrieve cache entries up to `cutoff` seconds old. (Default is 1-day)

        Returns:
            typing.Optional[TweetSauceCache]
        """
        now = int(time.time())
        cutoff_ts = 0 if not cutoff else (now - cutoff)

        sauce = TweetSauceCache.get(tweet_id=tweet_id, index_no=index_no)
        if sauce:
            log.debug(
                f'[SYSTEM] Sauce cache hit on index {index_no} for tweet {tweet_id}'
            )

            if sauce.created_at < cutoff_ts:
                log.info(
                    f'[SYSTEM] Sauce cache query on index {index_no} for tweet {tweet_id} has expired'
                )
                return None

        return sauce
Beispiel #2
0
    def fetch(tweet_id: int) -> typing.Optional['TweetCache']:
        """
        Attempt to retrieve a tweet from our local cache
        Args:
            tweet_id(int): Tweet ID to look up

        Returns:
            typing.Optional[TweetCache]
        """
        tweet = TweetCache.get(tweet_id=tweet_id)
        if tweet:
            log.debug(f'[SYSTEM] Tweet {tweet_id} cache hit')
        return tweet
Beispiel #3
0
    def set(tweet: TweetCache,
            sauce_results: typing.Optional[SauceNaoResults] = None,
            index_no: int = 0,
            trigger: str = TRIGGER_MENTION) -> 'TweetSauceCache':
        """
        Cache a SauceNao query
        Args:
            tweet (TweetCache): Cached Tweet entry
            sauce_results (Optional[SauceNaoResults]): Results to filter and process
            index_no (int): The media indice for tweets with multiple media uploads
            trigger (str): The event that triggered the sauce lookup (purely for analytics)

        Returns:
            TweetSauceCache
        """
        # Delete any existing cache entry. This is just for safety; it shouldn't actually be triggered.
        cache = TweetSauceCache.get(tweet_id=tweet.tweet_id, index_no=index_no)
        if cache:
            log.info(
                f'[SYSTEM] Overwriting sauce cache entry for tweet {tweet.tweet_id}'
            )
            cache.delete()
            commit()

        # If there are no results, we log a cache entry anyways to prevent making additional queries
        def no_results():
            log.info(
                f'[SYSTEM] Logging a failed Sauce lookup for tweet {tweet.tweet_id} on indice {index_no}'
            )
            _cache = TweetSauceCache(tweet_id=tweet.tweet_id,
                                     index_no=index_no,
                                     trigger=trigger,
                                     created_at=int(time.time()))
            return _cache

        if not sauce_results or not sauce_results.results:
            return no_results()

        # Get the first result and make sure it meets our minimum similarity requirement
        similarity_cutoff = int(
            config.getfloat('Twitter',
                            f"min_similarity_{trigger}",
                            fallback=50.0))
        sauce = sauce_results.results[0]

        # Finally, make sure the sauce result actually meets our minimum similarity requirements
        if (sauce.similarity < similarity_cutoff):
            log.debug(
                f"[SYSTEM] Sauce potentially found for tweet {tweet.tweet_id}, but it didn't meet the minimum {trigger} similarity requirements"
            )
            return no_results()

        log.info(
            f'[SYSTEM] Caching a successful sauce lookup query for tweet {tweet.tweet_id} on indice {index_no}'
        )
        cache = TweetSauceCache(tweet_id=tweet.tweet_id,
                                index_no=index_no,
                                sauce_header=sauce.header,
                                sauce_data=sauce.data,
                                sauce_class=type(sauce).__name__,
                                sauce_index=sauce.index,
                                trigger=trigger,
                                created_at=int(time.time()))
        return cache