Created
August 27, 2016 19:50
-
-
Save soberstadt/02786033123f1b3312545e0a360e1e52 to your computer and use it in GitHub Desktop.
Gizmodo Once Workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GizmodoTweet < ApplicationRecord | |
def self.follow_link(short) | |
result = Net::HTTP.get_response(URI.parse(short))['location'] | |
return short unless result | |
follow_link(result) | |
end | |
def self.find_first_link(body) | |
body.match(/https:\/\/t.co\/\w+/).to_s | |
end | |
def self.latest_tweets(client) | |
options = {count: 200, include_rts: false} | |
since_id = last.try(:tweet_id) | |
options[:since_id] = since_id if since_id | |
client.user_timeline("gizmodo", options) | |
end | |
def self.lookup_dup(tweet) | |
short_link = find_first_link(tweet.full_text) | |
link = follow_link(short_link).split('?')[0] | |
dup = find_by(link: link) | |
return dup if dup | |
nil | |
end | |
def self.retweet(client, tweet) | |
client.retweet tweet | |
short_link = find_first_link(tweet.full_text) | |
link = follow_link(short_link).split('?')[0] | |
create(link: link, tweet_id: tweet.id) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :jobs do | |
task work: :environment do | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"] | |
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"] | |
config.access_token = ENV["TWITTER_ACCESS_TOKEN"] | |
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"] | |
end | |
tweets = GizmodoTweet.latest_tweets(client).reverse | |
tweets.each do |tweet| | |
dup = GizmodoTweet.lookup_dup(tweet) | |
GizmodoTweet.retweet(client, tweet) unless dup | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment