Created
January 8, 2009 09:18
-
-
Save jaigouk/44652 to your computer and use it in GitHub Desktop.
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 TwitterOnMerb < Merb::Controller | |
def _template_location(action, type = nil, controller = controller_name) | |
controller == "layout" ? "layout.#{action}.#{type}" : "#{action}.#{type}" | |
end | |
#shows twitter | |
def index | |
render | |
end | |
#aggregate rss feeds and edge merb rdoc | |
def commits | |
@edgemerb_commits = [] | |
merb = 'http://github.com/wycats/merb/commits/1.0.x' | |
get_commits_and_bark(merb, @edgemerb_commits, "edgemerb") | |
@negotiation_commits = [] | |
negotiation = 'http://github.com/wycats/rails/commits/content_negotiation' | |
get_commits_and_bark(negotiation, @negotiation_commits, "negotiation") | |
@katz_blog = [] | |
blog= 'http://yehudakatz.com' | |
get_blog_and_bark(blog, @katz_blog) | |
render | |
end | |
# just another plain html | |
def about | |
render | |
end | |
#parsing github commits | |
def get_commits_and_bark(url, array, category) | |
connect | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('div.human').each do |h| | |
h.search('div.message').each {|k| @message = k.content} | |
h.search('div.name').each do |k| | |
@name = (k.content).to_s.strip.gsub("(author)", "").gsub("(committer)","") | |
end | |
h.search('div.date').each do |k| | |
@date = k.content | |
@new_date = Time.parse(@date.strip) | |
end | |
h.search('pre a[href]').each do |k| | |
@link = (k.attributes) | |
@new_link = ShortURL.shorten("http://github.com" + @link.to_s.strip.gsub("href", ''), :lns) | |
end | |
array <<{:message => @message, :name => @name, :date => @new_date, :link => @new_link, :category => category} | |
end | |
array.slice!(4, array.length) | |
array.each do |tweet| | |
bark(tweet) | |
end | |
end | |
#parsing a blog | |
def get_blog_and_bark(url, array) | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('div.entry').each do |h| | |
@name = "Katz" | |
h.search('h3.entrytitle').each {|k| @title = k.content } | |
h.search('a[href]').each do |k| | |
@link = (k.attributes) | |
@new_link = ShortURL.shorten(@link.to_s.strip.gsub("href", ''), :lns) | |
end | |
h.search('div.meta').each do |k| | |
@date = k.content | |
@new_date = Time.parse(@date.strip) | |
end | |
array <<{:message => @title, :name => @name, :date=> Time.now, :category => "Katz's blog'", :link => @new_link} | |
end | |
array.slice!(4, array.length) | |
array.each do |tweet| | |
bark(tweet) | |
end | |
end | |
def connect | |
@post_client = Twitter::Client.new(:login => 'edgemerb', :password => '******') | |
end | |
#create tweets | |
#Since twitter API does not provide unique message check, | |
#I had to build db to validiate it. | |
def bark(tweet) | |
@tweet = Tweet.new(tweet) | |
if @tweet.save | |
############ RUN LATER BLOCK ################ | |
run_later do | |
begin | |
post(tweet) | |
rescue Twitter::RESTError => re | |
unless re.code == "403" | |
puts re | |
end | |
end #begin-rescue end | |
end #run_later end | |
############ RUN LATER BLOCK ################ | |
else | |
puts "failed to save them" | |
end | |
end | |
def post(tweet) | |
sleep(15) # sleep. because of twitter api limit | |
@poster = tweet[:message] + tweet[:name] +", " + time_lost_in_words(tweet[:date]) +"ago)" + tweet[:link] | |
status =@post_client.status(:post, @poster) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment