Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def mk_tweet(self, prefix, twitter_ids, video_name, authors, video_url):
- """
- Generates tweet, conditionally reducing text if too long.
- Should return (examples):
- #lca2017 My Talk Title - @CarlFK http://youtu.be/123456
- #lca2017 My Talk Title - Carl Karsten http://youtu.be/123456
- #lca2017 My Really Long Talk Title That Got Shortened Because It Is Lon... - Carl Karsten http://youtu.be/123456
- """
- TWITTER_LENGTH_MAX = 140
- # always shorten video_url's (if not already shortened i.e. youtu.be links)
- if len(video_url) > 25:
- video_url = self.shorten(video_url)
- # We have to display certain characters always (i.e. #lca2017, video URL, spaces/dashes between data)
- AVAILABLE_CHARS = TWITTER_LENGTH_MAX - len(prefix) - len(video_url) - 5
- message = '#{prefix} {{video_name}} - {{authors}} {video_url}'.format(
- prefix=prefix,
- video_url=video_url
- )
- # If user has a twitter handle, tweet this, not their name
- if twitter_ids:
- authors = twitter_ids
- # If everything fits, tweet all fields (unshortened)
- if AVAILABLE_CHARS - len(video_name) - len(authors) >= 0:
- return message.format(video_name=video_name, authors=authors)
- # If not, shorten the title
- else:
- MAX_AVAILABLE_TITLE_CHARS = AVAILABLE_CHARS - len(authors)
- shortened_video_name = video_name[:AVAILABLE_CHARS]
- return message.format(video_name=shortened_video_name, authors=authors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement