class ParserTest(unittest.TestCase): def __init__(self, *args, **kwargs): super(ParserTest, self).__init__(*args, **kwargs) self.parser = Parser() @data(('Sat May 04 15:00:33 +0000 2019', '3:00 PM - 4 May 2019'), ('', '')) @unpack def test_parse_date(self, input_date, formatted_date): self.assertEqual(self.parser.parse_date(input_date), formatted_date) def test_tweet_result(self): with open('testdata/search.json', encoding='utf-8') as f: tweet_data = json.load(f) with open('testdata/user_tweet.json', encoding='utf-8') as f: user_data = json.load(f) tweet_outcome = self.parser.parse_tweets_result(tweet_data, 'search') user_outcome = self.parser.parse_tweets_result(user_data, 'user') self.assertEqual(tweet_outcome[0]['account']['fullname'], 'Twitter Dev') self.assertEqual(tweet_outcome[1]['likes'], 27) self.assertEqual(user_outcome[0]['retweets'], 284) self.assertEqual(user_outcome[1]['hashtags'], [])
def user_tweets(username, limit=30): """ Get user tweets. Get the list of tweets that the user has on his feed in JSON format. Optional parameters: limit: integer, specifies the number of tweets to retrieve, the default should be 30 """ # Return if Limit is Not in the range of 0 to 200 if limit > 200 or limit < 0: return {"Message": "Please Provide a valid limit between 0-200"}, 422 # Initiate the TwitterAPI instance with the given Credentials try: api = TwitterAPI(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET) except TwitterError as e: # Return with 401 if can't authenticate the user with given credentials return {"Message": e.message}, 401 # Query Param for twitter API query = {'screen_name': username, 'count': limit} try: x = api.GetTweets(query) except TwitterError as e: return {"Message": e.message}, 400 return Parser.parse_tweets_result(x, 'user'), 200
def search_tweets(hashtag, limit=30): """ Get tweets by a hashtag. Get the list of tweets with the given hashtag. Optional parameters: limit: integer, specifies the number of tweets to retrieve, the default should be 30 """ # Return if Limit is Not in the range of 0 to 200 if limit > 200 or limit < 0: return {"Message": "Please Provide a valid limit between 0-200"}, 422 # Initiate the TwitterAPI instance with the given Credentials try: api = TwitterAPI(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET) except TwitterError as e: # Return with 401 if can't authenticate the user with given credentials return {"Message": e.message}, 401 # Query Param for twitter API query = {'q': '#{}'.format(hashtag), 'count': limit} try: x = api.GetSearch(query) except TwitterError as e: return {"Message": e.message}, 400 return Parser.parse_tweets_result(x, 'search'), 200