view_model.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. from dataclasses import replace
  2. from flask import g, request
  3. from twitter_v2.types import Tweet, TweetExpansions
  4. from hogumathi_app.view_model import FeedServiceUser, FeedItem, FeedItemAction, CollectionPage, PublicMetrics, Card, MediaItem
  5. from . import oauth2_login
  6. url_for = oauth2_login.url_for_with_me
  7. def user_model_dc (user):
  8. fsu = FeedServiceUser(
  9. id = user.id,
  10. name = user.name,
  11. username = user.username,
  12. created_at = user.created_at,
  13. description = user.description,
  14. preview_image_url = user.profile_image_url,
  15. website = user.url,
  16. is_verified = user.verified,
  17. is_protected = user.protected,
  18. location = user.location,
  19. url = url_for('twitter_v2_facade.get_profile_html', user_id=user.id),
  20. source_url = f'https://twitter.com/{user.username}',
  21. raw_user = user
  22. )
  23. return fsu
  24. def tweet_model_dc_vm (includes: TweetExpansions, tweet: Tweet, me, my_url_for=url_for, reply_depth=0, expand_path=None) -> FeedItem:
  25. # retweeted_by, avi_icon_url, display_name, handle, created_at, text
  26. user = list(filter(lambda u: u.id == tweet.author_id, includes.users))[0]
  27. published_by = user_model_dc(user)
  28. url = my_url_for('twitter_v2_facade.get_tweet_html', tweet_id=tweet.id, view='tweet')
  29. source_url = 'https://twitter.com/{}/status/{}'.format(user.username, tweet.id)
  30. avi_icon_url = url_for('get_image', url=user.profile_image_url)
  31. retweet_of = None
  32. quoted = None
  33. replied_to = None
  34. if tweet.referenced_tweets:
  35. retweet_of = list(filter(lambda r: r.type == 'retweeted', tweet.referenced_tweets))
  36. quoted = list(filter(lambda r: r.type == 'quoted', tweet.referenced_tweets))
  37. replied_to = list(filter(lambda r: r.type == 'replied_to', tweet.referenced_tweets))
  38. if reply_depth:
  39. if expand_path:
  40. expand_path += f',{tweet.id}'
  41. else:
  42. expand_path = tweet.id
  43. actions = {
  44. 'view_replies': FeedItemAction('twitter_v2_facade.get_tweet_html', {'tweet_id': tweet.conversation_id, 'view': 'replies', 'expand': expand_path}),
  45. 'view_thread': FeedItemAction('twitter_v2_facade.get_tweet_html', {'tweet_id': tweet.conversation_id, 'view': 'thread'}),
  46. 'view_conversation': FeedItemAction('twitter_v2_facade.get_tweet_html', {'tweet_id': tweet.conversation_id, 'view': 'conversation'}),
  47. }
  48. if reply_depth:
  49. vr = actions['view_replies']
  50. url = url_for(vr.route, **vr.route_params)
  51. if g.get('twitter_user'):
  52. actions.update(
  53. bookmark = FeedItemAction('twitter_v2_facade.post_tweet_bookmark', {'tweet_id': tweet.id}),
  54. delete_bookmark = FeedItemAction('twitter_v2_facade.delete_tweet_bookmark', {'tweet_id': tweet.id}),
  55. retweet = FeedItemAction('twitter_v2_facade.post_tweet_retweet', {'tweet_id': tweet.id})
  56. )
  57. if g.twitter_live_enabled:
  58. actions.update(
  59. view_activity = FeedItemAction('twitter_v2_live_facade.get_tweet_activity_html', {'tweet_id': tweet.id})
  60. )
  61. t = FeedItem(
  62. id = tweet.id,
  63. text = tweet.text,
  64. created_at = tweet.created_at,
  65. published_by = published_by,
  66. author_is_verified = user.verified,
  67. url = url,
  68. conversation_id = tweet.conversation_id,
  69. avi_icon_url = avi_icon_url,
  70. display_name = user.name,
  71. handle = user.username,
  72. author_url = my_url_for('twitter_v2_facade.get_profile_html', user_id=user.id),
  73. author_id = user.id,
  74. source_url = source_url,
  75. source_author_url = 'https://twitter.com/{}'.format(user.username),
  76. #'is_edited': len(tweet['edit_history_tweet_ids']) > 1
  77. actions = actions,
  78. )
  79. if reply_depth:
  80. t = replace(t, reply_depth = reply_depth)
  81. # HACK we should not refer to the request directly...
  82. if request and request.args.get('marked_reply') == str(t.id):
  83. t = replace(t, is_marked = True)
  84. # This is where we should put "is_bookmark", "is_liked", "is_in_collection", etc...
  85. if tweet.entities:
  86. if tweet.entities.urls:
  87. urls = list(filter(lambda u: u.title and u.description, tweet.entities.urls))
  88. if len(urls):
  89. url = urls[0]
  90. card = Card(
  91. display_url = url.display_url.split('/')[0],
  92. source_url = url.unwound_url,
  93. content = url.description,
  94. title = url.title
  95. )
  96. if url.images:
  97. print(url.images)
  98. card = replace(card,
  99. preview_image_url = url_for('get_image', url=url.images[1].url),
  100. image_url = url_for('get_image', url=url.images[0].url)
  101. )
  102. t = replace(t, card = card)
  103. if tweet.public_metrics:
  104. public_metrics = PublicMetrics(
  105. reply_count = tweet.public_metrics.reply_count,
  106. quote_count = tweet.public_metrics.quote_count,
  107. retweet_count = tweet.public_metrics.retweet_count,
  108. like_count = tweet.public_metrics.like_count
  109. )
  110. t = replace(t, public_metrics = public_metrics)
  111. if tweet.non_public_metrics:
  112. non_public_metrics = NonPublicMetrics(
  113. impression_count = tweet.non_public_metrics.impression_count,
  114. user_profile_clicks = tweet.non_public_metrics.user_profile_clicks,
  115. url_link_clicks = tweet.non_public_metrics.url_link_clicks
  116. )
  117. t = replace(t, non_public_metrics = non_public_metrics)
  118. if retweet_of and len(retweet_of):
  119. print('found retweet_of')
  120. t = replace(t, retweeted_tweet_id = retweet_of[0].id)
  121. retweeted_tweet:Tweet = list(filter(lambda t: t.id == retweet_of[0].id, includes.tweets))[0]
  122. rt = tweet_model_dc_vm(includes, retweeted_tweet, me)
  123. t = replace(rt,
  124. retweeted_tweet_id = retweet_of[0].id,
  125. source_retweeted_by_url = 'https://twitter.com/{}'.format(user.username),
  126. retweeted_by = user.name,
  127. retweeted_by_url = url_for('.get_profile_html', user_id=user.id)
  128. )
  129. try:
  130. if tweet.attachments and tweet.attachments.media_keys and includes.media:
  131. media_keys = tweet.attachments.media_keys
  132. def first_media (mk):
  133. medias = list(filter(lambda m: m.media_key == mk, includes.media))
  134. if len(medias):
  135. return medias[0]
  136. return None
  137. media = list(filter(lambda m: m != None, map(first_media, media_keys)))
  138. photos = filter(lambda m: m.type == 'photo', media)
  139. videos = filter(lambda m: m.type == 'video', media)
  140. photo_media = map(lambda p: MediaItem(
  141. media_key = p.media_key,
  142. type = 'photo',
  143. preview_image_url = url_for('get_image', url=p.url + '?name=tiny&format=webp'),
  144. url = url_for('get_image', url=p.url),
  145. width = p.width,
  146. height = p.height
  147. ), photos)
  148. def video_to_mi (v):
  149. use_hls = False # mainly iOS
  150. max_bitrate = 100000000
  151. if use_hls:
  152. variants = list(filter(lambda var: var.content_type == 'application/x-mpegURL'))
  153. else:
  154. variants = list(filter(lambda var: var.content_type != 'application/x-mpegURL' and var.bit_rate <= max_bitrate, v.variants))
  155. variants.sort(key=lambda v: v.bit_rate, reverse=True)
  156. url = None
  157. content_type = None
  158. size = None
  159. if len(variants):
  160. if len(variants) > 1:
  161. print('multiple qualifying variants (using first):')
  162. print(variants)
  163. variant = variants[0]
  164. url = url_for('get_image', url=variant.url)
  165. content_type = variant.content_type
  166. size = int(v.duration_ms / 1000 * variant.bit_rate)
  167. public_metrics = None
  168. if v.public_metrics and v.public_metrics.view_count:
  169. public_metrics = PublicMetrics(
  170. view_count = v.public_metrics.view_count
  171. )
  172. mi = MediaItem(
  173. media_key = v.media_key,
  174. type = 'video',
  175. preview_image_url = url_for('get_image', url=v.preview_image_url + '?name=tiny&format=webp'),
  176. image_url = url_for('get_image', url=v.preview_image_url),
  177. width = v.width,
  178. height = v.height,
  179. url=url,
  180. content_type = content_type,
  181. duration_ms = v.duration_ms,
  182. size = size,
  183. public_metrics = public_metrics
  184. )
  185. return mi
  186. video_media = map(video_to_mi, videos)
  187. t = replace(t,
  188. photos = list(photo_media),
  189. videos = list(video_media)
  190. )
  191. elif tweet.attachments and tweet.attachments.media_keys and not includes.media:
  192. print('tweet had attachments and media keys, but no expansion media content was given')
  193. print(tweet.attachments.media_keys)
  194. except:
  195. # it seems like this comes when we have a retweeted tweet with media on it.
  196. print('exception adding attachments to tweet:')
  197. print(tweet)
  198. print('view tweet:')
  199. print(t)
  200. print('included media:')
  201. print(includes.media)
  202. raise 'exception adding attachments to tweet'
  203. try:
  204. if quoted and len(quoted):
  205. t = replace(t, quoted_tweet_id = quoted[0].id)
  206. quoted_tweets = list(filter(lambda t: t.id == quoted[0].id, includes.tweets))
  207. if len(quoted_tweets):
  208. t = replace(t, quoted_tweet = tweet_model_dc_vm(includes, quoted_tweets[0], me))
  209. except:
  210. raise 'error adding quoted tweet'
  211. try:
  212. if replied_to and len(replied_to) and includes.tweets:
  213. t = replace(t, replied_tweet_id = replied_to[0].id)
  214. if reply_depth < 1:
  215. replied_tweets = list(filter(lambda t: t.id == replied_to[0].id, includes.tweets))
  216. if len(replied_tweets):
  217. t = replace(t, replied_tweet = tweet_model_dc_vm(includes, replied_tweets[0], me, reply_depth=reply_depth + 1))
  218. else:
  219. print("No replied tweet found (t={}, rep={})".format(t.id, t.replied_tweet_id))
  220. except:
  221. raise 'error adding replied_to tweet'
  222. return t