def tweets_user(user): limit = request.args.get('limit') if limit is None: limit = DEFAULT_LIMIT twitter_api = TwitterApi() json_response = twitter_api.getTweetsByUsername(user, limit) summary = twitter_api.extractTweetInfo(json_response) return json.dumps(summary)
def tweets_hashtag(hashtag): limit = request.args.get('limit') if limit is None: limit = DEFAULT_LIMIT twitter_api = TwitterApi() json_response = twitter_api.getTweetsByHashtag(hashtag, limit) tweets = json_response['statuses'] summary = twitter_api.extractTweetInfo(tweets) return json.dumps(summary)
def test_extractTweetInfo(self): tweets = [{ "created_at": "Thu Apr 06 15:28:43 +0000 2017", "text": "tweet_text_first", "entities": { "hashtags": [{ "text": "first" }, { "text": "second" }], }, "user": { "id": 111, "name": "name_one", "screen_name": "screen_name_one", }, "retweet_count": 10, "favorite_count": 11, }, { "created_at": "Fri Apr 16 15:28:43 +0000 2017", "text": "tweet_text_second", "entities": { "hashtags": [{ "text": "one" }, { "text": "two" }], }, "user": { "id": 222, "name": "name_two", "screen_name": "screen_name_two", }, "retweet_count": 20, "favorite_count": 21, }] twitter_api = TwitterApi() actual = twitter_api.extractTweetInfo(tweets) expected = [ { 'account': { 'fullname': 'name_one', 'href': '/screen_name_one', 'id': 111, }, 'date': '15:28 PM - 06 Apr 2017', 'hashtags': ['first', 'second'], 'likes': 11, 'retweets': 10, 'text': 'tweet_text_first', }, { 'account': { 'fullname': 'name_two', 'href': '/screen_name_two', 'id': 222, }, 'date': '15:28 PM - 16 Apr 2017', 'hashtags': ['one', 'two'], 'likes': 21, 'retweets': 20, 'text': 'tweet_text_second', }, ] self.assertEqual(expected, actual)