-
-
Save mattetti/1169942 to your computer and use it in GitHub Desktop.
For Matt
This file contains hidden or 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
require 'pp' | |
require 'tweetstream' | |
require "hiredis" | |
require "redis/connection/hiredis" | |
require 'redis' | |
# Just so I can run without your rails stack | |
unless $redis | |
$redis = Redis.new(:host => "127.0.0.1", :port => 6379) | |
end | |
unless Object.const_defined?(:MyGivingTweetConf) && MyGivingTweetConf.const_defined?(:HASHTAG) | |
module MyGivingTweetConf | |
HASHTAG = "MGT" | |
end | |
end | |
# end of hacks | |
class StreamCampaign | |
attr_accessor :status_id, :retweet_count, :retweet_status_ids, :db_id | |
@cached_campaigns = {} | |
def self.find(status_id) | |
@cached_campaigns[status_id] ||= new_instance_if_exists(status_id) | |
end | |
def initialize(opts={}) | |
puts "creating a new instance with the following attributes: #{opts}" | |
# set the values passed as a hash | |
opts.each do |key, value| | |
setter = "#{key}=" | |
if self.respond_to?(setter) | |
self.send(setter, value) | |
end | |
end | |
self.retweet_status_ids ||= [] | |
self.retweet_count ||= 0 | |
end | |
private | |
def self.new_instance_if_exists(status_id) | |
db_id = $redis.get("campaign:#{status_id}") | |
if db_id | |
self.new(:status_id => status_id, :db_id => db_id) | |
else | |
nil | |
end | |
end | |
end | |
module EtienneStreamer | |
def self.start | |
@streamer ||= \ | |
puts "Starting Twitter Stream Client for Campaign ID #{MyGivingTweetConf::HASHTAG}" | |
TweetStream::Client.new('stevenbono','rom121').track(MyGivingTweetConf::HASHTAG) do |status| | |
begin | |
puts [status.user.screen_name, status.text, status.id].inspect | |
if retweet?(status) | |
status_id = status.retweeted_status.id | |
campaign = StreamCampaign.find(status_id) #based on status.id of original tweet | |
if campaign | |
campaign.update(status) #update with status id and retweet count | |
else | |
# this is orphan campaign retweet | |
# update_orphan_campaign(status) | |
end | |
else | |
# start_orphan_campaign(status) | |
end | |
rescue Exception => e | |
p e.inspect | |
end | |
end | |
end | |
def self.retweet?(status) | |
status.retweeted | |
end | |
def self.start_campaign(status) | |
create_originator(status) | |
end | |
def self.create_originator(status) | |
save | |
fetch_followers(user_id) | |
end | |
end | |
puts "class demo" | |
status_id = 42 | |
# pretending that we have a campaign with status_id 42 and db id 24 | |
$redis.set("campaign:#{status_id}", 24) | |
campaign = StreamCampaign.find(status_id) | |
puts campaign.inspect | |
puts "campaign status_id: #{campaign.status_id}" | |
puts "campaign db id: #{campaign.db_id}" | |
puts "campaign count: #{campaign.retweet_count}" | |
campaign.retweet_count = 12 | |
puts "campaign count: #{campaign.retweet_count}" | |
EtienneStreamer.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment