item_collections.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. from dataclasses import asdict, replace
  2. from importlib.util import find_spec
  3. import os
  4. import json
  5. from flask import request, g, jsonify, render_template, Blueprint, url_for, session
  6. from tweet_source import ApiV2TweetSource
  7. from view_model import FeedItem, cleandict
  8. twitter_enabled = False
  9. if find_spec('twitter_v2_facade'):
  10. from twitter_v2_facade import tweet_model_dc_vm
  11. twitter_enabled = True
  12. youtube_enabled = False
  13. if find_spec('youtube_facade'):
  14. from youtube_facade import youtube_model, get_youtube_builder
  15. youtube_enabled = True
  16. DATA_DIR=".data"
  17. item_collections_bp = Blueprint('item_collections', 'item_collections',
  18. static_folder='static',
  19. static_url_path='',
  20. url_prefix='/')
  21. def get_tweet_collection (collection_id):
  22. with open(f'{DATA_DIR}/collection/{collection_id}.json', 'rt', encoding='utf-8') as f:
  23. collection = json.loads(f.read())
  24. return collection
  25. def collection_from_card_source (url):
  26. """
  27. temp1 = await fetch('http://localhost:5000/notes/cards/search?q=twitter.com/&limit=10').then(r => r.json())
  28. re = /(http[^\s]+twitter\.com\/[^\/]+\/status\/[\d]+)/ig
  29. tweetLinks = temp1.cards.map(c => c.card.content).map(c => c.match(re))
  30. tweetLinks2 = tweetLinks.flat().filter(l => l)
  31. tweetLinksS = Array.from(new Set(tweetLinks2))
  32. statusUrls = tweetLinksS.map(s => new URL(s))
  33. //users = Array.from(new Set(statusUrls.map(s => s.pathname.split('/')[1])))
  34. ids = Array.from(new Set(statusUrls.map(s => parseInt(s.pathname.split('/')[3]))))
  35. """
  36. """
  37. temp1 = JSON.parse(document.body.innerText)
  38. // get swipe note + created_at + tweet user + tweet ID
  39. tweetCards = temp1.cards.map(c => c.card).filter(c => c.content.match(re))
  40. tweets = tweetCards.map(c => ({created_at: c.created_at, content: c.content, tweets: c.content.match(re).map(m => new URL(m))}))
  41. tweets.filter(t => t.tweets.filter(t2 => t2.user.toLowerCase() == 'stephenmpinto').length)
  42. // HN
  43. re = /(http[^\s]+news.ycombinator\.com\/[^\s]+\=[\d]+)/ig
  44. linkCards = temp1.cards.map(c => c.card).filter(c => c.content.match(re))
  45. links = linkCards.map(c => ({created_at: c.created_at, content: c.content, links: c.content.match(re).map(m => new URL(m))}))
  46. // YT (I thnk I've already done this one)
  47. """
  48. # more in 2022 twitter report
  49. return None
  50. def expand_item (item, me, tweets = None, includes = None, yt_videos = None):
  51. if 'id' in item:
  52. t = list(filter(lambda t: item['id'] == t.id, tweets))
  53. if not len(t):
  54. print("no tweet for item: " + item['id'])
  55. feed_item = FeedItem(
  56. id = item['id'],
  57. text = "(Deleted, suspended or blocked)",
  58. created_at = "",
  59. handle = "error",
  60. display_name = "Error"
  61. )
  62. # FIXME 1) put this in relative order to the collection
  63. # FIXME 2) we can use the tweet link to get the user ID...
  64. else:
  65. t = t[0]
  66. feed_item = tweet_model_dc_vm(includes, t, me)
  67. note = item.get('note')
  68. feed_item = replace(feed_item, note = note)
  69. elif 'yt_id' in item:
  70. yt_id = item['yt_id']
  71. vid = list(filter(lambda v: v['id'] == yt_id, yt_videos))[0]
  72. feed_item = youtube_model(vid)
  73. note = item.get('note')
  74. feed_item.update({'note': note})
  75. return feed_item
  76. # pagination token is the next tweet_ID
  77. @item_collections_bp.get('/collection/<collection_id>.html')
  78. def get_collection_html (collection_id):
  79. me = request.args.get('me')
  80. acct = session.get(me)
  81. max_results = int(request.args.get('max_results', 10))
  82. pagination_token = int(request.args.get('pagination_token', 0))
  83. collection = get_tweet_collection(collection_id)
  84. if 'authorized_users' in collection and (not acct or not me in collection['authorized_users']):
  85. return 'access denied.', 403
  86. items = collection['items'][pagination_token:(pagination_token + max_results)]
  87. if not len(items):
  88. return 'no tweets', 404
  89. twitter_token = os.environ.get('BEARER_TOKEN')
  90. if me and me.startswith('twitter:') and acct:
  91. twitter_token = acct['access_token']
  92. tweet_source = ApiV2TweetSource(twitter_token)
  93. tweet_ids = filter(lambda i: 'id' in i, items)
  94. tweet_ids = list(map(lambda item: item['id'], tweet_ids))
  95. tweets_response = tweet_source.get_tweets( tweet_ids, return_dataclass=True )
  96. yt_ids = filter(lambda i: 'yt_id' in i, items)
  97. yt_ids = list(map(lambda item: item['yt_id'], yt_ids))
  98. youtube = get_youtube_builder()
  99. videos_response = youtube.videos().list(id=','.join(yt_ids), part='snippet,contentDetails,liveStreamingDetails,statistics,recordingDetails', maxResults=1).execute()
  100. #print(response_json)
  101. if tweets_response.errors:
  102. # types:
  103. # https://api.twitter.com/2/problems/not-authorized-for-resource (blocked or suspended)
  104. # https://api.twitter.com/2/problems/resource-not-found (deleted)
  105. #print(response_json.get('errors'))
  106. for err in tweets_response.errors:
  107. if not 'type' in err:
  108. print('unknown error type: ' + str(err))
  109. elif err['type'] == 'https://api.twitter.com/2/problems/not-authorized-for-resource':
  110. print('blocked or suspended tweet: ' + err['value'])
  111. elif err['type'] == 'https://api.twitter.com/2/problems/resource-not-found':
  112. print('deleted tweet: ' + err['value'])
  113. else:
  114. print('unknown error')
  115. print(json.dumps(err, indent=2))
  116. includes = tweets_response.includes
  117. tweets = tweets_response.data
  118. feed_items = list(map(lambda item: expand_item(item, me, tweets, includes, videos_response['items']), items))
  119. if request.args.get('format') == 'json':
  120. return jsonify({'ids': tweet_ids,
  121. 'tweets': cleandict(asdict(tweets_response)),
  122. 'feed_items': feed_items,
  123. 'items': items,
  124. 'pagination_token': pagination_token})
  125. else:
  126. query = {}
  127. if pagination_token:
  128. query['next_data_url'] = url_for('.get_collection_html', collection_id=collection_id, pagination_token=pagination_token)
  129. if 'HX-Request' in request.headers:
  130. return render_template('partial/tweets-timeline.html', tweets = feed_items, user = {}, query = query)
  131. else:
  132. if pagination_token:
  133. query['next_page_url'] = url_for('.get_collection_html', collection_id=collection_id, pagination_token=pagination_token)
  134. return render_template('tweet-collection.html', tweets = feed_items, user = {}, query = query)
  135. # pagination token is the next tweet_ID
  136. @item_collections_bp.get('/collections.html')
  137. def get_collections_html ():
  138. me = request.args.get('me')
  139. acct = session.get(me)
  140. collections = []
  141. with os.scandir('.data/collection') as collections_files:
  142. for collection_file in collections_files:
  143. if not collection_file.name.endswith('.json'):
  144. continue
  145. with open(collection_file.path, 'rt', encoding='utf-8') as f:
  146. coll = json.load(f)
  147. if 'authorized_users' in coll and (not acct or not me in coll['authorized_users']):
  148. continue
  149. collection_id = collection_file.name[:-len('.json')]
  150. coll_info = dict(
  151. collection_id = collection_id,
  152. href = url_for('.get_collection_html', collection_id=collection_id)
  153. )
  154. collections.append(coll_info)
  155. return jsonify(collections)
  156. @item_collections_bp.post('/data/collection/create/from-cards')
  157. def post_data_collection_create_from_cards ():
  158. """
  159. // create collection from search, supporting multiple Tweets per card and Tweets in multiple Cards.
  160. re = /(https?[a-z0-9\.\/\:]+twitter\.com\/[0-9a-z\_]+\/status\/[\d]+)/ig
  161. temp1 = await fetch('http://localhost:5000/notes/cards/search?q=twitter.com/').then(r => r.json())
  162. cardMatches = temp1.cards
  163. .map(cm => Object.assign({}, cm, {tweetLinks: Array.from(new Set(cm.card.content.match(re)))}))
  164. .filter(cm => cm.tweetLinks && cm.tweetLinks.length)
  165. .map(cm => Object.assign({}, cm, {tweetUrls: cm.tweetLinks.map(l => new URL(l))}))
  166. .map(cm => Object.assign({}, cm, {tweetInfos: cm.tweetUrls.map(u => ({user: u.pathname.split('/')[1], tweetId: u.pathname.split('/')[3]}))}));
  167. collectionCards = {}
  168. cardMatches.forEach(function (cm) {
  169. if (!cm.tweetLinks.length) { return; }
  170. cm.tweetInfos.forEach(function (ti) {
  171. if (!collectionCards[ti.tweetId]) {
  172. collectionCards[ti.tweetId] = [];
  173. }
  174. collectionCards[ti.tweetId].push(cm.card);
  175. })
  176. })
  177. var collectionItems = [];
  178. Object.entries(collectionCards).forEach(function (e) {
  179. var tweetId = e[0], cards = e[1];
  180. var note = cards.map(function (card) {
  181. return card.created_at + "\n\n" + card.content;
  182. }).join("\n\n-\n\n");
  183. collectionItems.push({id: tweetId, note: note, tweet_infos: cm.tweetInfos, card_infos: cards.map(c => 'card#' + c.id)});
  184. })
  185. """
  186. collection = {
  187. 'items': [], # described in JS function above
  188. 'authorized_users': [g.twitter_user['id']]
  189. }
  190. return jsonify(collection)