Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. def mk_tweet(self, prefix, twitter_ids, video_name, authors, video_url):
  2. """
  3. Generates tweet, conditionally reducing text if too long.
  4.  
  5. Should return (examples):
  6. #lca2017 My Talk Title - @CarlFK http://youtu.be/123456
  7. #lca2017 My Talk Title - Carl Karsten http://youtu.be/123456
  8. #lca2017 My Really Long Talk Title That Got Shortened Because It Is Lon... - Carl Karsten http://youtu.be/123456
  9. """
  10.  
  11. TWITTER_LENGTH_MAX = 140
  12.  
  13. # always shorten video_url's (if not already shortened i.e. youtu.be links)
  14. if len(video_url) > 25:
  15. video_url = self.shorten(video_url)
  16.  
  17. # We have to display certain characters always (i.e. #lca2017, video URL, spaces/dashes between data)
  18. AVAILABLE_CHARS = TWITTER_LENGTH_MAX - len(prefix) - len(video_url) - 5
  19.  
  20. message = '#{prefix} {{video_name}} - {{authors}} {video_url}'.format(
  21. prefix=prefix,
  22. video_url=video_url
  23. )
  24.  
  25. # If user has a twitter handle, tweet this, not their name
  26. if twitter_ids:
  27. authors = twitter_ids
  28.  
  29. # If everything fits, tweet all fields (unshortened)
  30. if AVAILABLE_CHARS - len(video_name) - len(authors) >= 0:
  31. return message.format(video_name=video_name, authors=authors)
  32.  
  33. # If not, shorten the title
  34. else:
  35. MAX_AVAILABLE_TITLE_CHARS = AVAILABLE_CHARS - len(authors)
  36. shortened_video_name = video_name[:AVAILABLE_CHARS]
  37. return message.format(video_name=shortened_video_name, authors=authors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement