diff --git a/twootfeed/twitter/routes.py b/twootfeed/twitter/routes.py index 8ac3568..0a9d302 100644 --- a/twootfeed/twitter/routes.py +++ b/twootfeed/twitter/routes.py @@ -10,13 +10,99 @@ twitter_bp = Blueprint('twitter', __name__) +def format_tweet(i): + tweet = {'text': i.full_text, 'screen_name': i.user.screen_name, + 'profile_image_url': i.user.profile_image_url_https, + 'user_name': i.user.name, 'user_url': i.user.url, + 'id_str': i.id_str, 'created_at': i.created_at, 'source': i.source, + 'retweets': i.retweet_count, 'favorites': i.favorite_count} + tweet['tweet_url'] = 'https://twitter.com/' + tweet[ + 'user_name'] + '/status/' + tweet['id_str'] + + tweet['htmltext'] = '
' + tweet['screen_name'] + \
+        ' ' + tweet['user_name'] + \ + ': ' + tweet['text'] + \ + '
Source: ' + tweet[ + 'source'] + '' + + user_mentionslist = i.entities.get('user_mentions') + for user in user_mentionslist: + if user != '': + tweet['htmltext'] = re.sub( + ('@' + user.get('screen_name')), + ('@' + + user.get('screen_name') + ''), + tweet['htmltext']) + + hashtaglist = i.entities.get('hashtags') + for hashtag in hashtaglist: + if hashtag != '': + tweet['htmltext'] = re.sub( + ('#' + hashtag.get('text')), + ('#' + + hashtag.get('text') + ''), + tweet['htmltext'] + ) + + urllist = i.entities.get('urls') + for url in urllist: + if url != '': + tweet['htmltext'] = re.sub( + (url.get('url')), + ('' + + url.get('display_url') + + ''), + tweet['htmltext']) + try: + medialist = i.extended_entities.get('media') + except AttributeError: + pass + else: + if medialist is not None: + tweet['htmltext'] = tweet['htmltext'] + '
' + for media in medialist: + if media != '': + if media.get('type') == 'photo': + tweet['htmltext'] = re.sub( + media.get('url'), '', + tweet['htmltext'] + ) + tweet['htmltext'] = tweet['htmltext'] \ + + ' ' \ + ' ' \ + + '' + + location = i.place + if location is not None: + tweet['htmltext'] = tweet['htmltext'] \ + + '
Location: ' \ + + location.full_name + '' + + tweet['htmltext'] = tweet['htmltext'] + '
♻ : ' + str( + tweet['retweets']) + ', ' + '♥ : ' + str( + tweet['favorites']) + '
' + + return tweet + + @twitter_bp.route('/', methods=['GET']) @twitter_bp.route('/tweets/', methods=['GET']) def tweetfeed(query_feed): """ generate a rss feed from parsed twitter search """ if twitter_api: - tweet = {} buffered = [] for i in tweepy.Cursor(twitter_api.search, @@ -34,95 +120,7 @@ def tweetfeed(query_feed): retweeted_status = False if not retweeted_status: # only the original tweets - - tweet['text'] = i.full_text - tweet['screen_name'] = i.user.screen_name - tweet['profile_image_url'] = i.user.profile_image_url_https - tweet['user_name'] = i.user.name - tweet['user_url'] = i.user.url - tweet['id_str'] = i.id_str - tweet['created_at'] = i.created_at - tweet['source'] = i.source - tweet['retweets'] = i.retweet_count - tweet['favorites'] = i.favorite_count - tweet['tweet_url'] = 'https://twitter.com/' + tweet[ - 'user_name'] + '/status/' + tweet['id_str'] - - tweet['htmltext'] = '
' + tweet['screen_name'] + \
-                        ' ' + tweet['user_name'] + \ - ': ' + tweet['text'] + \ - '
Source: ' + tweet[ - 'source'] + '' - - user_mentionslist = i.entities.get('user_mentions') - for j in user_mentionslist: - if j != '': - tweet['htmltext'] = re.sub( - ('@' + j.get('screen_name')), - ('@' - + j.get('screen_name') + ''), - tweet['htmltext']) - - hashtaglist = i.entities.get('hashtags') - for j in hashtaglist: - if j != '': - tweet['htmltext'] = re.sub( - ('#' + j.get('text')), - ('#' - + j.get('text') + ''), - tweet['htmltext'] - ) - - urllist = i.entities.get('urls') - for j in urllist: - if j != '': - tweet['htmltext'] = re.sub( - (j.get('url')), - ('' - + j.get('display_url') - + ''), - tweet['htmltext']) - try: - medialist = i.extended_entities.get('media') - except AttributeError: - pass - else: - if medialist is not None: - tweet['htmltext'] = tweet['htmltext'] + '
' - for j in medialist: - if j != '': - if j.get('type') == 'photo': - tweet['htmltext'] = re.sub( - j.get('url'), '', - tweet['htmltext'] - ) - tweet['htmltext'] = tweet['htmltext'] \ - + ' ' \ - ' ' \ - + '' - - location = i.place - if location is not None: - tweet['htmltext'] = tweet['htmltext'] \ - + '
Location: ' \ - + location.full_name + '' - - tweet['htmltext'] = tweet['htmltext'] + '
♻ : ' + str( - tweet['retweets']) + ', ' + '♥ : ' + str( - tweet['favorites']) + '
' - + tweet = format_tweet(i) buffered.append(tweet.copy()) utc = pytz.utc