view_model.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, my_url_for=url_for):
  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 = my_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, my_g=g, 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, my_url_for=my_url_for)
  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 = my_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 = my_url_for(vr.route, **vr.route_params)
  51. if my_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 my_g.get('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 = my_url_for('get_image', url=url.images[1].url),
  100. image_url = my_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. impression_count = tweet.public_metrics.impression_count,
  110. bookmark_count = tweet.public_metrics.bookmark_count,
  111. )
  112. t = replace(t, public_metrics = public_metrics)
  113. if tweet.non_public_metrics:
  114. non_public_metrics = NonPublicMetrics(
  115. impression_count = tweet.non_public_metrics.impression_count,
  116. user_profile_clicks = tweet.non_public_metrics.user_profile_clicks,
  117. url_link_clicks = tweet.non_public_metrics.url_link_clicks
  118. )
  119. t = replace(t, non_public_metrics = non_public_metrics)
  120. if retweet_of and len(retweet_of):
  121. print('found retweet_of')
  122. t = replace(t, retweeted_tweet_id = retweet_of[0].id)
  123. retweeted_tweet:Tweet = list(filter(lambda t: t.id == retweet_of[0].id, includes.tweets))[0]
  124. rt = tweet_model_dc_vm(includes, retweeted_tweet, me)
  125. t = replace(rt,
  126. retweeted_tweet_id = retweet_of[0].id,
  127. source_retweeted_by_url = 'https://twitter.com/{}'.format(user.username),
  128. retweeted_by = user.name,
  129. retweeted_by_url = my_url_for('twitter_v2_facade.get_profile_html', user_id=user.id)
  130. )
  131. try:
  132. if tweet.attachments and tweet.attachments.media_keys and includes.media:
  133. media_keys = tweet.attachments.media_keys
  134. def first_media (mk):
  135. medias = list(filter(lambda m: m.media_key == mk, includes.media))
  136. if len(medias):
  137. return medias[0]
  138. return None
  139. media = list(filter(lambda m: m != None, map(first_media, media_keys)))
  140. photos = filter(lambda m: m.type == 'photo', media)
  141. videos = filter(lambda m: m.type == 'video', media)
  142. photo_media = map(lambda p: MediaItem(
  143. media_key = p.media_key,
  144. type = 'photo',
  145. preview_image_url = my_url_for('get_image', url=p.url + '?name=tiny&format=webp'),
  146. url = my_url_for('get_image', url=p.url),
  147. width = p.width,
  148. height = p.height
  149. ), photos)
  150. def video_to_mi (v):
  151. use_hls = False # mainly iOS
  152. max_bitrate = 100000000
  153. if use_hls:
  154. variants = list(filter(lambda var: var.content_type == 'application/x-mpegURL'))
  155. else:
  156. variants = list(filter(lambda var: var.content_type != 'application/x-mpegURL' and var.bit_rate <= max_bitrate, v.variants))
  157. variants.sort(key=lambda v: v.bit_rate, reverse=True)
  158. url = None
  159. content_type = None
  160. size = None
  161. if len(variants):
  162. if len(variants) > 1:
  163. print('multiple qualifying variants (using first):')
  164. print(variants)
  165. variant = variants[0]
  166. url = my_url_for('get_image', url=variant.url)
  167. content_type = variant.content_type
  168. size = int(v.duration_ms / 1000 * variant.bit_rate)
  169. public_metrics = None
  170. if v.public_metrics and v.public_metrics.view_count:
  171. public_metrics = PublicMetrics(
  172. view_count = v.public_metrics.view_count
  173. )
  174. mi = MediaItem(
  175. media_key = v.media_key,
  176. type = 'video',
  177. preview_image_url = my_url_for('get_image', url=v.preview_image_url + '?name=tiny&format=webp'),
  178. image_url = my_url_for('get_image', url=v.preview_image_url),
  179. width = v.width,
  180. height = v.height,
  181. url=url,
  182. content_type = content_type,
  183. duration_ms = v.duration_ms,
  184. size = size,
  185. public_metrics = public_metrics
  186. )
  187. return mi
  188. video_media = map(video_to_mi, videos)
  189. t = replace(t,
  190. photos = list(photo_media),
  191. videos = list(video_media)
  192. )
  193. elif tweet.attachments and tweet.attachments.media_keys and not includes.media:
  194. print('tweet had attachments and media keys, but no expansion media content was given')
  195. print(tweet.attachments.media_keys)
  196. except:
  197. # it seems like this comes when we have a retweeted tweet with media on it.
  198. print('exception adding attachments to tweet:')
  199. print(tweet)
  200. print('view tweet:')
  201. print(t)
  202. print('included media:')
  203. print(includes.media)
  204. raise 'exception adding attachments to tweet'
  205. try:
  206. if quoted and len(quoted):
  207. t = replace(t, quoted_tweet_id = quoted[0].id)
  208. quoted_tweets = list(filter(lambda t: t.id == quoted[0].id, includes.tweets))
  209. if len(quoted_tweets):
  210. t = replace(t, quoted_tweet = tweet_model_dc_vm(includes, quoted_tweets[0], me))
  211. except:
  212. raise 'error adding quoted tweet'
  213. try:
  214. if replied_to and len(replied_to) and includes.tweets:
  215. t = replace(t, replied_tweet_id = replied_to[0].id)
  216. if reply_depth < 1:
  217. replied_tweets = list(filter(lambda t: t.id == replied_to[0].id, includes.tweets))
  218. if len(replied_tweets):
  219. t = replace(t, replied_tweet = tweet_model_dc_vm(includes, replied_tweets[0], me, reply_depth=reply_depth + 1))
  220. else:
  221. print("No replied tweet found (t={}, rep={})".format(t.id, t.replied_tweet_id))
  222. except:
  223. raise 'error adding replied_to tweet'
  224. return t