2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-10 20:46:20 -04:00
|
|
|
# lightweight Twitter api calls
|
|
|
|
class TwitterApi
|
|
|
|
|
|
|
|
class << self
|
2018-07-17 03:29:40 -04:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2013-10-10 20:46:20 -04:00
|
|
|
|
|
|
|
def prettify_tweet(tweet)
|
2017-03-17 16:36:53 -04:00
|
|
|
text = tweet["full_text"].dup
|
2013-10-10 20:46:20 -04:00
|
|
|
if (entities = tweet["entities"]) && (urls = entities["urls"])
|
|
|
|
urls.each do |url|
|
|
|
|
text.gsub!(url["url"], "<a target='_blank' href='#{url["expanded_url"]}'>#{url["display_url"]}</a>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
text = link_hashtags_in link_handles_in text
|
|
|
|
|
2016-05-11 16:11:26 -04:00
|
|
|
result = Rinku.auto_link(text, :all, 'target="_blank"').to_s
|
|
|
|
|
|
|
|
if tweet['extended_entities'] && media = tweet['extended_entities']['media']
|
|
|
|
media.each do |m|
|
|
|
|
if m['type'] == 'photo'
|
|
|
|
if large = m['sizes']['large']
|
2018-03-23 08:05:17 -04:00
|
|
|
result << "<div class='tweet-images'><img class='tweet-image' src='#{m['media_url_https']}' width='#{large['w']}' height='#{large['h']}'></div>"
|
2016-05-11 16:11:26 -04:00
|
|
|
end
|
2017-03-17 16:49:29 -04:00
|
|
|
elsif m['type'] == 'video'
|
|
|
|
if large = m['sizes']['large']
|
2018-07-19 00:52:36 -04:00
|
|
|
result << "<div class='tweet-images'><iframe class='tweet-video' src='https://twitter.com/i/videos/#{tweet['id_str']}' width='#{large['w']}' height='#{large['h']}' frameborder='0' allowfullscreen></iframe></div>"
|
2017-03-17 16:49:29 -04:00
|
|
|
end
|
2016-05-11 16:11:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-10-10 20:46:20 -04:00
|
|
|
end
|
|
|
|
|
2018-07-17 03:29:40 -04:00
|
|
|
def prettify_number(count)
|
2018-07-17 03:45:44 -04:00
|
|
|
number_to_human(count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })
|
2018-07-17 03:29:40 -04:00
|
|
|
end
|
|
|
|
|
2013-10-10 20:46:20 -04:00
|
|
|
def user_timeline(screen_name)
|
|
|
|
JSON.parse(twitter_get(user_timeline_uri_for screen_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweet_for(id)
|
|
|
|
JSON.parse(twitter_get(tweet_uri_for id))
|
|
|
|
end
|
|
|
|
|
2014-01-28 15:51:29 -05:00
|
|
|
alias_method :status, :tweet_for
|
|
|
|
|
2013-10-10 20:46:20 -04:00
|
|
|
def raw_tweet_for(id)
|
|
|
|
twitter_get(tweet_uri_for id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_credentials_missing?
|
|
|
|
consumer_key.blank? || consumer_secret.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def link_handles_in(text)
|
2017-07-07 12:17:44 -04:00
|
|
|
text.scan(/(?:^|\s)@(\w+)/).flatten.uniq.each do |handle|
|
2018-05-16 14:48:19 -04:00
|
|
|
text.gsub!(/(?:^|\s)@#{handle}/, [
|
|
|
|
" <a href='https://twitter.com/#{handle}' target='_blank'>",
|
2013-10-10 20:46:20 -04:00
|
|
|
"@#{handle}",
|
|
|
|
"</a>"
|
|
|
|
].join)
|
|
|
|
end
|
|
|
|
|
2018-05-16 14:48:19 -04:00
|
|
|
text.strip
|
2013-10-10 20:46:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_hashtags_in(text)
|
2017-07-07 12:17:44 -04:00
|
|
|
text.scan(/(?:^|\s)#(\w+)/).flatten.uniq.each do |hashtag|
|
2018-05-16 15:04:17 -04:00
|
|
|
text.gsub!(/(?:^|\s)##{hashtag}/, [
|
|
|
|
" <a href='https://twitter.com/search?q=%23#{hashtag}' ",
|
2013-10-10 20:46:20 -04:00
|
|
|
"target='_blank'>",
|
|
|
|
"##{hashtag}",
|
|
|
|
"</a>"
|
|
|
|
].join)
|
|
|
|
end
|
|
|
|
|
2018-05-16 15:04:17 -04:00
|
|
|
text.strip
|
2013-10-10 20:46:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_timeline_uri_for(screen_name)
|
|
|
|
URI.parse "#{BASE_URL}/1.1/statuses/user_timeline.json?screen_name=#{screen_name}&count=50&include_rts=false&exclude_replies=true"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweet_uri_for(id)
|
2017-03-17 16:36:53 -04:00
|
|
|
URI.parse "#{BASE_URL}/1.1/statuses/show.json?id=#{id}&tweet_mode=extended"
|
2013-10-10 20:46:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? BASE_URL
|
2020-04-30 02:48:34 -04:00
|
|
|
BASE_URL = 'https://api.twitter.com'
|
2013-10-10 20:46:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_get(uri)
|
|
|
|
request = Net::HTTP::Get.new(uri)
|
|
|
|
request.add_field 'Authorization', "Bearer #{bearer_token}"
|
|
|
|
http(uri).request(request).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorization
|
|
|
|
request = Net::HTTP::Post.new(auth_uri)
|
|
|
|
|
|
|
|
request.add_field 'Authorization',
|
|
|
|
"Basic #{bearer_token_credentials}"
|
|
|
|
request.add_field 'Content-Type',
|
|
|
|
'application/x-www-form-urlencoded;charset=UTF-8'
|
|
|
|
|
|
|
|
request.set_form_data 'grant_type' => 'client_credentials'
|
|
|
|
|
|
|
|
http(auth_uri).request(request).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def bearer_token
|
|
|
|
@access_token ||= JSON.parse(authorization).fetch('access_token')
|
|
|
|
end
|
|
|
|
|
|
|
|
def bearer_token_credentials
|
|
|
|
Base64.strict_encode64(
|
2019-12-11 23:54:26 -05:00
|
|
|
"#{UrlHelper.encode_component(consumer_key)}:#{UrlHelper.encode_component(consumer_secret)}"
|
2013-10-10 20:46:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_uri
|
|
|
|
URI.parse "#{BASE_URL}/oauth2/token"
|
|
|
|
end
|
|
|
|
|
|
|
|
def http(uri)
|
|
|
|
Net::HTTP.new(uri.host, uri.port).tap { |http| http.use_ssl = true }
|
|
|
|
end
|
|
|
|
|
|
|
|
def consumer_key
|
|
|
|
SiteSetting.twitter_consumer_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def consumer_secret
|
|
|
|
SiteSetting.twitter_consumer_secret
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|