Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created November 4, 2012 16:53
Show Gist options
  • Save francescoagati/4012582 to your computer and use it in GitHub Desktop.
Save francescoagati/4012582 to your computer and use it in GitHub Desktop.
celluloid streaming content and write to mysql db
class Writer
include Celluloid
include Celluloid::Logger
def write_tweet(tweet)
DB[:links].insert(tweet)
end
end
PoolWrite=Writer.pool(size:20)
class FetchUrl
include Celluloid
include Celluloid::Logger
def process_tweet(tweet)
PoolWrite.write_tweet!(transform_tweet(tweet)) if has_url?(tweet)
end
protected
def has_url?(tweet)
/http/ === tweet[:text]
end
def get_real_url(url)
follower=Unwind::RedirectFollower.new(url)
follower.resolve
follower.final_url
end
def transform_tweet(tweet)
url=URI.extract(tweet[:text]).select {|u| /http/ === u }.first
url = get_real_url(url)
content=Nokogiri::HTML(RestClient.get(url))
title=content.search("title").inner_text
{
url:url,
tweet:tweet[:text],
keywords:"",
status:0,
title:title
}
end
end
Pooler=FetchUrl.pool(size:50)
class TwitterTracker
include Celluloid
include Celluloid::Logger
attr_accessor :tracker, :words
def initialize(words)
@tracker=Tweedle.new
@words=words
end
def tracks()
predicates = {
track: words
}
tracker.statuses.filter(predicates: predicates) do |tweet|
next unless text = tweet[:text]
Pooler.process_tweet!(tweet)
end
end
end
#
TwitterTracker.new("keywords").tracks
loop {
sleep 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment