Mercurial > rsstweet
annotate app/lib/legit_client.rb @ 241:4bca1528675e legit-client
Wrong entity lookup key
| author | nanaya <me@nanaya.net> |
|---|---|
| date | Sat, 15 Jul 2023 17:06:21 +0900 |
| parents | c454ea4f7b34 |
| children | bc2f45058c9e |
| rev | line source |
|---|---|
| 234 | 1 module LegitClient |
| 2 def self.timeline(user_id) | |
| 3 resp = fetch("https://twitter.com/i/api/graphql/1-5o8Qhfc2kWlu_2rWNcug/UserTweetsAndReplies?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22count%22%3A50%2C%22includePromotedContent%22%3Atrue%2C%22withCommunity%22%3Atrue%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22rweb_lists_timeline_redesign_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22view_counts_everywhere_api_enabled%22%3Atrue%2C%22longform_notetweets_consumption_enabled%22%3Atrue%2C%22responsive_web_twitter_article_tweet_consumption_enabled%22%3Afalse%2C%22tweet_awards_web_tipping_enabled%22%3Afalse%2C%22freedom_of_speech_not_reach_fetch_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Atrue%2C%22longform_notetweets_rich_text_read_enabled%22%3Atrue%2C%22longform_notetweets_inline_media_enabled%22%3Atrue%2C%22responsive_web_media_download_video_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Afalse%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%2C%22withArticleRichContentState%22%3Afalse%7D") | |
| 4 | |
| 5 begin | |
| 6 json = JSON.parse(resp) | |
| 7 { | |
|
238
a04b4830eef2
Filter out non-own tweets included for replies
nanaya <me@nanaya.net>
parents:
237
diff
changeset
|
8 timeline: normalize_timeline(json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id), |
| 234 | 9 raw: resp, |
| 10 } | |
| 11 rescue => e | |
| 12 return if (json || {}).dig('data').is_a? Hash | |
| 13 Rails.logger.error("timeline fail: #{user_id}: #{resp}") | |
| 14 nil | |
| 15 end | |
| 16 end | |
| 17 | |
| 18 def self.user_by_id(user_id) | |
| 19 resp = fetch("https://twitter.com/i/api/graphql/i_0UQ54YrCyqLUvgGzXygA/UserByRestId?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D") | |
| 20 | |
| 21 begin | |
| 22 json = JSON.parse(resp) | |
| 23 { | |
| 24 user: normalize_user(json['data']['user']['result']), | |
| 25 raw: resp, | |
| 26 } | |
| 27 rescue | |
| 28 return if (json || {}).dig('data').is_a? Hash | |
| 29 Rails.logger.error("user_by_id fail: #{user_id}: #{resp}") | |
| 30 nil | |
| 31 end | |
| 32 end | |
| 33 | |
| 34 def self.user_by_username(username) | |
| 35 resp = fetch("https://twitter.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName?variables=%7B%22screen_name%22%3A#{escape_param username}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22subscriptions_verification_info_verified_since_enabled%22%3Atrue%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D") | |
| 36 | |
| 37 begin | |
| 38 json = JSON.parse(resp) | |
| 39 { | |
| 40 user: normalize_user(json['data']['user']['result']), | |
| 41 raw: resp, | |
| 42 } | |
| 43 rescue | |
| 44 return if (json || {}).dig('data').is_a? Hash | |
| 45 Rails.logger.error("user_by_username fail: #{username}: #{resp}") | |
| 46 nil | |
| 47 end | |
| 48 end | |
| 49 | |
| 50 def self.escape_param(param) | |
| 51 CGI.escape JSON.dump(param) | |
| 52 end | |
| 53 | |
| 54 def self.fetch(uri) | |
| 236 | 55 Net::HTTP.get(URI(uri), $cfg[:headers].sample) |
| 234 | 56 end |
| 57 | |
| 58 def self.normalize_entity_media(json) | |
| 59 ret = {} | |
| 60 | |
| 61 json.each do |entity_media| | |
| 62 val = {} | |
| 63 | |
| 64 if entity_media['type'] == 'photo' | |
|
237
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
65 val[:image_url] = entity_media['media_url_https'] |
| 234 | 66 elsif entity_media['type'] == 'video' |
| 67 val[:variants] = entity_media['video_info']['variants'] | |
| 68 .filter { |variant| variant['bitrate'].present? } | |
| 69 .map do |variant| | |
| 70 { | |
| 71 bitrate: variant['bitrate'], | |
| 72 url: variant['url'], | |
| 73 } | |
| 74 end | |
| 75 end | |
| 76 | |
| 77 if !val.empty? | |
|
237
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
78 val[:url] = entity_media['expanded_url'] |
| 234 | 79 val[:type] = entity_media['type'] |
| 80 val[:id] = entity_media['media_key'] | |
| 81 end | |
| 82 | |
| 241 | 83 key = if ret[entity_media['url']].nil? |
| 84 entity_media['url'] | |
|
237
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
85 else |
|
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
86 entity_media['media_key'] |
|
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
87 end |
|
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
88 |
|
961d362e42c7
The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents:
236
diff
changeset
|
89 ret[key] = val |
| 234 | 90 end |
| 91 | |
| 92 ret | |
| 93 end | |
| 94 | |
| 95 def self.normalize_entity_urls(json) | |
| 96 ret = {} | |
| 97 | |
|
240
c454ea4f7b34
Add support for note tweets (with no formatting)
nanaya <me@nanaya.net>
parents:
238
diff
changeset
|
98 (json || {}).each do |entity_url| |
| 234 | 99 ret[entity_url['url']] = entity_url['expanded_url'] |
| 100 end | |
| 101 | |
| 102 ret | |
| 103 end | |
| 104 | |
|
238
a04b4830eef2
Filter out non-own tweets included for replies
nanaya <me@nanaya.net>
parents:
237
diff
changeset
|
105 def self.normalize_timeline(json, user_id) |
| 234 | 106 json.find { |instruction| instruction['type'] == 'TimelineAddEntries' }['entries'] |
| 107 .filter { |entry| entry['entryId'] =~ /\A(profile-conversation|tweet)-/ } | |
| 108 .reduce([]) do |acc, entry| | |
| 109 if entry['content']['entryType'] == 'TimelineTimelineItem' | |
| 110 acc.push(entry['content']) | |
| 111 else | |
| 112 entry['content']['items'].each do |item| | |
| 113 acc.push(item['item']) | |
| 114 end | |
| 115 end | |
| 116 acc | |
| 117 end.map { |rawTweet| normalize_tweet(rawTweet['itemContent']['tweet_results']['result']) } | |
|
238
a04b4830eef2
Filter out non-own tweets included for replies
nanaya <me@nanaya.net>
parents:
237
diff
changeset
|
118 .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id } |
| 234 | 119 end |
| 120 | |
| 121 def self.normalize_tweet(json) | |
| 122 return nil if json.nil? | |
| 123 | |
| 124 return normalize_tweet(json['tweet']) if json['__typename'] == 'TweetWithVisibilityResults' | |
| 125 | |
| 126 { | |
| 127 id: json['rest_id'], | |
| 128 created_at: Time.parse(json['legacy']['created_at']), | |
| 129 user: normalize_user(json['core']['user_results']['result']), | |
|
240
c454ea4f7b34
Add support for note tweets (with no formatting)
nanaya <me@nanaya.net>
parents:
238
diff
changeset
|
130 message: json.dig('note_tweet', 'note_tweet_results', 'result', 'text') || json['legacy']['full_text'], |
| 234 | 131 retweet: normalize_tweet(json.dig('legacy', 'retweeted_status_result', 'result')), |
| 132 quote: normalize_tweet(json.dig('quoted_status_result', 'result')), | |
| 133 quote_id: json['legacy']['quoted_status_id_str'], | |
| 134 reply_to_id: json['legacy']['in_reply_to_status_id_str'], | |
| 135 reply_to_user_id: json['legacy']['in_reply_to_user_id_str'], | |
| 136 reply_to_username: json['legacy']['in_reply_to_screen_name'], | |
|
240
c454ea4f7b34
Add support for note tweets (with no formatting)
nanaya <me@nanaya.net>
parents:
238
diff
changeset
|
137 entity_urls: { **normalize_entity_urls(json['legacy']['entities']['urls']), **normalize_entity_urls(json.dig('note_tweet', 'note_tweet_results', 'result', 'entity_set', 'urls')) }, |
| 234 | 138 entity_media: normalize_entity_media(json.dig('legacy', 'extended_entities', 'media') || []), |
| 139 } | |
| 140 end | |
| 141 | |
| 142 def self.normalize_user(json) | |
| 143 { | |
| 144 avatar_url: json['legacy']['profile_image_url_https'], | |
| 145 id: json['rest_id'], | |
| 146 name: json['legacy']['name'], | |
| 147 protected: json['legacy']['protected'] == true, | |
| 148 username: json['legacy']['screen_name'], | |
| 149 } | |
| 150 end | |
| 151 end |
