def test_fetch_tweet_with_cursor(self): tweet_id = "1238007172786577408" cursor = "TBwcFoDAotWMz6SuIhUCAAAYJmNvbnZlcnNhdGlvblRocmVhZC0xMjM4MDA3MzM4NzYzNTcxMjAwAAA=" tokens = tweet_fetcher.get_tokens() result = tweet_fetcher.fetch_tweet(tweet_id, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"], cursor=cursor) self.assertNotEqual(result, None)
def test_append_timeline(self): tweet_id = "1239125355866099712" tokens = get_tokens() tweet = Tweet(tweet_id, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"]) out = tweet.get_tweets() storage = JsonStorage(TEST_DATA_FOLDER) storage.save_tweet(out) output_path = os.path.join(TEST_DATA_FOLDER, "{}.json".format(tweet_id)) self.assertTrue(os.path.isfile(output_path)) storage.append_timeline(tweet_id, out["timelines"]) with open(output_path, "r", encoding="utf-8") as f: json_str = f.read() obj = json.loads(json_str) self.assertEqual(obj["tweet_id"], tweet_id) self.assertEqual(obj["timelines"][-1][0], "POP") # clean up os.remove(output_path)
def test_get_next_timelines(self): tweet_id = "1238007172786577408" tokens = get_tokens() tweet = Tweet(tweet_id, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"]) out = tweet.get_main_tweet(timeline_length=-1) self.assertNotEqual(out, None) timelines = tweet.get_next_timelines(timeline_length=-1) self.assertNotEqual(timelines, None)
def test_get_tweets_with_bound(self): tweet_id = "1238007172786577408" tokens = get_tokens() tweet = Tweet(tweet_id, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"]) out = tweet.get_main_tweet(timeline_length=2) max_len = -1 for timeline in out["timelines"]: max_len = max(max_len, len(timeline)) self.assertTrue(len(timeline) <= 2) self.assertEqual(max_len, 2)
def refresh_token(self, force=False): """refresh access tokens Args: force: ignore `self.token_refresh_duration` to force update the tokens. Returns: None """ if force or self.last_token_refresh is None or time.time( ) - self.last_token_refresh >= self.token_refresh_duration: logger.info("Start refresh tokens") self.tokens = tweet_fetcher.get_tokens() logger.info("Tokens refreshed") self.last_token_refresh = time.time()
def test_get_next_ids(self): keyword = "spacex" tokens = get_tokens() search = TwitterSearch(keyword, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"]) result1 = search.get_next_ids() self.assertEqual(type(result1), list) self.assertGreater(len(result1), 0) result2 = search.get_next_ids() self.assertEqual(type(result2), list) self.assertGreater(len(result2), 0) self.assertNotEqual(search.next_cursor, None) self.assertNotEqual(search.previous_cursor, None)
def test_get_tokens(self): tokens = tweet_fetcher.get_tokens() self.assertTrue( tokens["access_token"] is not None and tokens["csrf_token"] is not None and tokens["guest_token"] is not None )
def test_get_main_tweet(self): tweet_id = "1239125355866099712" tokens = get_tokens() tweet = Tweet(tweet_id, access_token=tokens["access_token"], csrf_token=tokens["csrf_token"], guest_token=tokens["guest_token"]) out = tweet.get_main_tweet() self.assertEqual(out["tweet"], "TWEEEET!")