def get_cached(self, r_type, order_by_influence=True): """ Return latest feed or actions from the cache """ # Get all json content corresponding to the specified pathname/filename pattern files = self.get_json_files(f"project/cache/{self.username}-{r_type}*") # Return False (and an empty string) if no files could be found if len(files) == 0: return False, "" # Get the latest date from the keys of the json content dict first_bf_date = self.first_before_date(r_type) # Get the json of the latest file in the dict last_file = files[first_bf_date] tweets = [] # Go trough every tweet in the json content of the file for t in last_file: # Reconstruct a tweet object from the json data tweet = self.json_to_tweet(t, order_by_influence) add_even_if_self_tweeted = False if not (tweet.username.lower() == self.username and r_type == "feed") or add_even_if_self_tweeted: # Add the tweet object to the list of tweets constituing the feed/actions tweets.append(tweet) print( f"Getting {r_type} of {self.username} from cache ({self.before_date.strftime('%d/%m/%Y %H:%M')})" ) if r_type == "feed": # If the results are a feed, return true, and a feed object containing all of the tweets return True, TE.feed(self.username, feed_tweets=tweets) elif r_type == "latest": # If the results are actions, return true, and an actions object containing all of the tweets return True, TE.actions(self.username, tweets=tweets)
def test_add_list_to_empty(self): ac = TE.actions("testU") new_ac = [1, 2] ac.add_actions(new_ac) self.assertEqual(len(ac), 2)
def test_index(self): ac = TE.actions("testU", tweets=[0, 1, 5]) ac.remove_action(2) self.assertEqual(ac[2], 5)
def test_remove_no_existing_actions_from_full(self): ac = TE.actions("testU", tweets=[0, 1, 5]) ac.remove_action(2) self.assertEqual(ac.tweets, [0, 1, 5])
def test_remove_actions_from_empty(self): ac = TE.actions("testU", tweets=[]) ac.remove_action(0) self.assertEqual(ac.tweets, [])
def test_add_actions_to_full(self): ac = TE.actions("testU", tweets=[0, 1]) new_ac = TE.actions("testU", tweets=[2, 3]) ac.add_actions(new_ac) self.assertEqual(len(ac), 4)