Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Oct 14, 2018
1 parent aa09e88 commit d1ead9f
Showing 1 changed file with 88 additions and 90 deletions.
178 changes: 88 additions & 90 deletions twootfeed/twitter/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<blockquote><div><img src="' + tweet[
'profile_image_url'] + \
'" alt="' + tweet['screen_name'] + \
'" /> <strong>' + tweet['user_name'] + \
': </strong>' + tweet['text'] + \
'<br><i>Source: ' + tweet[
'source'] + '</i>'

user_mentionslist = i.entities.get('user_mentions')
for user in user_mentionslist:
if user != '':
tweet['htmltext'] = re.sub(
('@' + user.get('screen_name')),
('<a href="https://twitter.com/'
+ user.get('screen_name')
+ '" target="_blank">@'
+ user.get('screen_name') + '</a>'),
tweet['htmltext'])

hashtaglist = i.entities.get('hashtags')
for hashtag in hashtaglist:
if hashtag != '':
tweet['htmltext'] = re.sub(
('#' + hashtag.get('text')),
('<a href="https://twitter.com/hashtag/'
+ hashtag.get('text')
+ '?src=hash" target="_blank">#'
+ hashtag.get('text') + '</a>'),
tweet['htmltext']
)

urllist = i.entities.get('urls')
for url in urllist:
if url != '':
tweet['htmltext'] = re.sub(
(url.get('url')),
('<a href="'
+ url.get('expanded_url')
+ '" target="_blank">'
+ url.get('display_url')
+ '</a>'),
tweet['htmltext'])
try:
medialist = i.extended_entities.get('media')
except AttributeError:
pass
else:
if medialist is not None:
tweet['htmltext'] = tweet['htmltext'] + '<br> '
for media in medialist:
if media != '':
if media.get('type') == 'photo':
tweet['htmltext'] = re.sub(
media.get('url'), '',
tweet['htmltext']
)
tweet['htmltext'] = tweet['htmltext'] \
+ '<a href="' \
+ media.get('media_url_https') \
+ '" target="_blank"> ' \
'<img src="' \
+ media.get('media_url_https') \
+ ':thumb' + '"> ' \
+ '</a>'

location = i.place
if location is not None:
tweet['htmltext'] = tweet['htmltext'] \
+ '<br><i>Location: ' \
+ location.full_name + '</i>'

tweet['htmltext'] = tweet['htmltext'] + '<br> ♻ : ' + str(
tweet['retweets']) + ', ' + '♥ : ' + str(
tweet['favorites']) + '</div></blockquote>'

return tweet


@twitter_bp.route('/<query_feed>', methods=['GET'])
@twitter_bp.route('/tweets/<query_feed>', 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,
Expand All @@ -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'] = '<blockquote><div><img src="' + tweet[
'profile_image_url'] + \
'" alt="' + tweet['screen_name'] + \
'" /> <strong>' + tweet['user_name'] + \
': </strong>' + tweet['text'] + \
'<br><i>Source: ' + tweet[
'source'] + '</i>'

user_mentionslist = i.entities.get('user_mentions')
for j in user_mentionslist:
if j != '':
tweet['htmltext'] = re.sub(
('@' + j.get('screen_name')),
('<a href="https://twitter.com/'
+ j.get('screen_name')
+ '" target="_blank">@'
+ j.get('screen_name') + '</a>'),
tweet['htmltext'])

hashtaglist = i.entities.get('hashtags')
for j in hashtaglist:
if j != '':
tweet['htmltext'] = re.sub(
('#' + j.get('text')),
('<a href="https://twitter.com/hashtag/'
+ j.get('text')
+ '?src=hash" target="_blank">#'
+ j.get('text') + '</a>'),
tweet['htmltext']
)

urllist = i.entities.get('urls')
for j in urllist:
if j != '':
tweet['htmltext'] = re.sub(
(j.get('url')),
('<a href="'
+ j.get('expanded_url')
+ '" target="_blank">'
+ j.get('display_url')
+ '</a>'),
tweet['htmltext'])
try:
medialist = i.extended_entities.get('media')
except AttributeError:
pass
else:
if medialist is not None:
tweet['htmltext'] = tweet['htmltext'] + '<br> '
for j in medialist:
if j != '':
if j.get('type') == 'photo':
tweet['htmltext'] = re.sub(
j.get('url'), '',
tweet['htmltext']
)
tweet['htmltext'] = tweet['htmltext'] \
+ '<a href="' \
+ j.get('media_url_https')\
+ '" target="_blank"> ' \
'<img src="' \
+ j.get('media_url_https') \
+ ':thumb' + '"> ' \
+ '</a>'

location = i.place
if location is not None:
tweet['htmltext'] = tweet['htmltext'] \
+ '<br><i>Location: ' \
+ location.full_name + '</i>'

tweet['htmltext'] = tweet['htmltext'] + '<br> ♻ : ' + str(
tweet['retweets']) + ', ' + '♥ : ' + str(
tweet['favorites']) + '</div></blockquote>'

tweet = format_tweet(i)
buffered.append(tweet.copy())

utc = pytz.utc
Expand Down

0 comments on commit d1ead9f

Please sign in to comment.