Skip to content

Commit

Permalink
1 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
id774 committed Feb 19, 2012
0 parents commit 9157e07
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bookmarker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require "feed_parser"
require "hb"
require 'active_record'

user = []
IO.foreach("./user.txt") {|u|
user << u
}

hb = HatenaBookmark.new
hb.user = {
"hatena_id" => user[0].chomp,
"password" => user[1].chomp
}

ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "./bookmark.db"
)

class Bookmark < ActiveRecord::Base
end

unless Bookmark.table_exists?()
ActiveRecord::Migration.create_table :bookmarks do |t|
t.column :url, :string
t.column :created_at, :string
end
end

bookmark = Bookmark.find(:all)
IO.foreach("./feeds.txt") {|feed|
begin
t = Time.now.strftime("%Y/%m/%d %X")
puts "#{t} [info] Parsing #{feed}"
links = FeedParser.get_rss(feed)
links.each {|link|
unless bookmark.detect {|b|b.url == link}
t = Time.now.strftime("%Y/%m/%d %X")
print "#{t} [info] Bookmarking #{link}\n"
new_bookmark = Bookmark.new(:url => link, :created_at => t)
new_bookmark.save
hb.postBookmark(link, nil)
sleep 5
end
}
rescue
t = Time.now.strftime("%Y/%m/%d %X")
puts "#{t} [error] Fault in parsing #{feed}"
end
}
32 changes: 32 additions & 0 deletions feed_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

class FeedParser
require 'rss'
require 'uri'

def self.get_rss(url)
target_links = []
begin
unless url.nil?
feed = URI.parse(url).normalize
open(feed) do |http|
response = http.read
rss_results = RSS::Parser.parse(response, false)
rss_results.items.each do |item|
target_links << item.link
end
end
end
rescue => e
raise e
end
return target_links
end
end

if __FILE__ == $0
url = ARGV.shift || abort("Usage: feed_parser.rb <url>")
links = FeedParser.get_rss(url)
p links
end
1 change: 1 addition & 0 deletions feeds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write your feeds per line in this text.
74 changes: 74 additions & 0 deletions hb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

class HatenaBookmark
require 'rubygems'
require 'time'
require 'digest/sha1'
require 'net/http'
require 'uri'
#require 'nkf'

attr_accessor :user

def initialize
@user = {
"hatena_id" => "",
"password" => ""
}
end

def wsse(hatena_id, password)
# Unique value
nonce = [Time.now.to_i.to_s].pack('m').gsub(/\n/, '')
now = Time.now.utc.iso8601

# Base64 encoding for SHA1 Digested strings
digest = [Digest::SHA1.digest(nonce + now + password)].pack("m").gsub(/\n/, '')

{'X-WSSE' => sprintf(
%Q<UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s">,
hatena_id, digest, nonce, now)
}
end

def toXml(link, summary)
%Q(
<entry xmlns="http://purl.org/atom/ns#">
<title>dummy</title>
<link rel="related" type="text/html" href="#{link}" />
<summary type="text/plain">#{summary}</summary>
</entry>
)
end

def post(b_url, b_comment)
url = "http://b.hatena.ne.jp/atom/post"
header = wsse(user["hatena_id"], user["password"])
uri = URI.parse(url)
proxy_class = Net::HTTP::Proxy(ENV["PROXY"], 8080)
http = proxy_class.new(uri.host)
http.start do |http|
# b_url = NKF.nkf('-w', b_url)
# b_comment = NKF.nkf('-w', b_comment)
res = http.post(uri.path, toXml(b_url, b_comment), header)
t = Time.now.strftime("%Y/%m/%d %X")
if res.code == "201" then
unless b_comment.nil?
print "#{t} [info] Success: #{b_url} Comment: #{b_comment}\n"
else
print "#{t} [info] Success: #{b_url}\n"
end
else
print "#{t} [error] #{res.code} Error: #{b_url}\n"
end
end
end
end

if __FILE__ == $0
b_url = ARGV.shift || abort("Usage: hatenabookmark.rb <url> <comment>")
b_comment = ARGV.shift
hb = HatenaBookmark.new
hb.post(b_url, b_comment)
end
2 changes: 2 additions & 0 deletions user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
username
password

0 comments on commit 9157e07

Please sign in to comment.