Skip to content

Commit

Permalink
Added FriendFeed posting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistos committed Nov 18, 2009
1 parent f68aa40 commit 97dd3d4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
config.rb
6 changes: 6 additions & 0 deletions config.rb.sample
@@ -0,0 +1,6 @@
module TopHN
FRIENDFEED_NICK = 'FriendFeedNick'
FRIENDFEED_REMOTE_KEY = 'remotekey'
TRIM_USERNAME = 'YourUsername'
TRIM_PASSWORD = 'ThePassword'
end
92 changes: 65 additions & 27 deletions poll.rb
Expand Up @@ -2,51 +2,89 @@

require 'nokogiri'
require 'open-uri'
require 'friendfeed'

__DIR__ = File.expand_path( File.dirname( __FILE__ ) )
require "#{__DIR__}/config"
require "#{__DIR__}/model/init"

module TopHN
class Poller
MIN_SCORE = 40

def initialize
@friendfeed = FriendFeed::Client.new
@friendfeed.api_login FRIENDFEED_NICK, FRIENDFEED_REMOTE_KEY
end

def shortened( uri )
escaped_uri = CGI.escape( uri )
shortened_uri = nil

3.times do
begin
doc = Nokogiri::XML(
open( "http://api.tr.im/api/trim_url.xml?url=#{ escaped_uri }" )
)
shortened_uri = doc.css( 'trim url' ).text
break
rescue OpenURI::HTTPError => e
if e.message !~ /502 Bad Gateway/
raise e
end
sleep 2
end
end

shortened_uri
end

def poll
@doc = Nokogiri::HTML( open( 'http://news.ycombinator.com/active' ) )
@doc.css( 'td.title' ).each do |td|
a = td.at( 'a' )
if a
tr = td.parent.next
if tr
subtext = tr.at( 'td.subtext' )
span = subtext.at( 'span' )
score = span.text.to_i

next if score < MIN_SCORE

id = span[ 'id' ][ /score_(\d+)/, 1 ]
item = Models::Item[ id ]
next if item

title = a.text.strip
uri_hn = subtext.css( 'a' )[ 1 ][ 'href' ]
uri = a[ 'href' ]
if uri =~ /^item\?/
uri = "http://news.ycombinator.com/#{uri}"
end
next if a.nil?
tr = td.parent.next
next if tr.nil?
subtext = tr.at( 'td.subtext' )
span = subtext.at( 'span' )
score = span.text.to_i

Models::Item.create(
id: id,
title: title,
uri: uri,
uri_hn: uri_hn,
score: score
)
puts title
next if score < MIN_SCORE

id = span[ 'id' ][ /score_(\d+)/, 1 ]
item = Models::Item[ id ]
next if item

title = a.text.strip
uri_hn = subtext.css( 'a' )[ 1 ][ 'href' ]
uri = a[ 'href' ]
if uri =~ /^item\?/
uri = "http://news.ycombinator.com/#{uri}"
end

shortened_uri = shortened( uri )
if shortened_uri
entry = @friendfeed.add_entry( "#{title} #{shortened_uri}" )
begin
@friendfeed.add_comment( entry[ 'id' ], "http://news.ycombinator.com/#{uri_hn}" )
rescue WWW::Mechanize::ResponseCodeError => e
if e.response_code != "404"
raise e
end
end

Models::Item.create(
id: id,
title: title,
uri: uri,
uri_hn: uri_hn,
score: score
)

puts title
end

end
end
end
Expand Down

0 comments on commit 97dd3d4

Please sign in to comment.