Created
February 27, 2009 14:36
-
-
Save jystewart/71499 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
#!/usr/bin/env ruby | |
# | |
# This was a super quick script to turn a text file of links into the structure | |
# I use for my "Saturday Links Summary" posts. | |
# | |
# I've extended it a bit so it will also get posts from delicious that use a | |
# certain tag, and uses trollop to parse command line arguments a bit more | |
# cleanly. | |
# | |
# James Stewart - [email protected] - 27th February 2009 / 6th March 2009 | |
# | |
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
require 'httparty' | |
# http://trollop.rubyforge.org/ | |
require 'trollop' | |
class DeliciousLinkage | |
include HTTParty | |
base_uri 'api.del.icio.us:443/v1' | |
def self.find_for_blog_since(date, auth) | |
options = { :basic_auth => auth} | |
get("/posts/all?tag=fridayblog&&fromdt=#{date.strftime("%Y-%m-%dT00:00:00Z")}", options) | |
end | |
end | |
class LinkProcessor | |
attr_accessor :file | |
attr_accessor :tags | |
attr_accessor :links | |
attr_accessor :delicious_auth | |
def initialize(file = nil, delicious_username = nil, delicious_password = nil) | |
self.tags = [] | |
self.file = file | |
self.links = [] | |
self.delicious_auth = {:username => delicious_username, :password => delicious_password} | |
end | |
def handle_links(post) | |
output = " <li>" | |
output += " <h3><a href=\"#{post['href']}\">#{post['description']}</a></h3>" | |
output += " <p>post['extended']</p>" | |
output += " </li>" | |
self.links << output | |
self.tags += post['tag'].split(' ') | |
end | |
def run | |
if self.file | |
file = YAML.load_file(self.file) | |
file['posts'].each { |post| handle_links(post) } | |
end | |
posts = DeliciousLinkage.find_for_blog_since(Date.today - 7, self.delicious_auth) | |
posts['posts']['post'].each { |post| handle_links(post) } if posts.is_a?(Hash) | |
end | |
def output | |
"<ul>\n#{self.links.join("\n")}\n</ul>\n" + "<p>#{self.tags.uniq.join(', ')}</p>" | |
end | |
end | |
begin | |
opts = Trollop::options do | |
opt :file, "YAML file to parse", :type => :string | |
opt :username, "Delicious Username", :type => :string | |
opt :password, "Delicious Password", :type => :string | |
end | |
# TODO: Tidy up output and use trollop error handling | |
raise "issue" unless opts[:file] || (opts[:username] && opts[:password]) | |
parser = LinkProcessor.new(opts[:file], opts[:username], opts[:password]) | |
parser.run | |
puts parser.output | |
rescue => e | |
# TODO: This shouldn't be needed. Trollop should do it for us | |
puts "Usage ./blog-links.rb -f FILENAME -u username -p password" | |
exit(0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment