Skip to content

Instantly share code, notes, and snippets.

@bvandgrift
Created November 18, 2009 17:27
Show Gist options
  • Save bvandgrift/238082 to your computer and use it in GitHub Desktop.
Save bvandgrift/238082 to your computer and use it in GitHub Desktop.
yank twitter links into delicious
#!/usr/bin/env ruby
# = Delighter - pull your twitter links into delicious.
#
# Takes the first link and submits it to delicious. The description
# will be the text up to the beginning of the link, the tags whatever
# hashtags are on the tweet. Let me know if you improve on this, it was
# pretty q&d.
#
# author: Ben Vandgrift <br/>
# somethingfamiliar[at]gmail<br/>
# @sfamiliar (http://twitter.com/sfamiliar)
#
# twitter favorites and opt parse added by: Chase Southard
# on 12.2.2009
# chase[dot]southard[at]gmail
# @southard (http://twitter.com/southard)
#
# usage: ruby delighter.rb twitter_username delicious_user delicious_pass
#
# grab urls from your favorites
# usage: ruby delighter.rb twitter_name delicious_user delicious_pass --favs
#
# alternatively -
#
# delighter = Delighter.new(twitter_num, del_user, del_pass)
# delighter.go
require 'rubygems'
require 'logger'
require 'feedzirra'
require 'httparty'
require 'trollop'
class Delighter
include HTTParty
base_uri 'https://api.del.icio.us/v1'
def initialize(twitter_num, duser, dpasswd, log_level = Logger::DEBUG)
@twitter_num = twitter_num
# chase added this Trollop option thang
# add --favs or -f to the end of the cmd line arguments
# like ruby delighter.rb twitter_name delicious_user delicious_pass --favs
opts = Trollop::options do
opt :favs, "Pull from Twitter Favorites", :short => "-f"
end
if opts[:favs]
@twitter_feed = "http://twitter.com/favorites/#{@twitter_num}.rss"
else
@twitter_feed = "http://twitter.com/statuses/user_timeline/#{@twitter_num}.rss"
end
log.level = log_level
self.class.basic_auth duser, dpasswd
end
def go
log.debug("parsing: #{@twitter_feed}")
Feedzirra::Feed.fetch_and_parse(@twitter_feed,
:timeout => 15,
:on_success => parse_tweets,
:on_failure => fail)
end
private
def parse_tweets
lambda do |url, feed|
begin
log.warn("Feed entries are blank for #{url}") and return if feed.nil?
feed.entries.each do |tweet|
url = extract_url(tweet.summary)
if (url) then
title = extract_title(tweet.summary)
tags = extract_tags(tweet.summary)
post_to_delicious(url, title, tags)
sleep 1 # this is to avoid throttling.
end
end
rescue
log.error("wtf? #{$!}: #{$!.message}")
log.error($!.backtrace)
end
end
end
def post_to_delicious(url, description, tags)
resp = self.class.post('/posts/add',
:query => {
:description => description,
:url => url,
:tags => tags.join(' ')
},
:headers => {'User-Agent' => 'Delighter/0.1'}
)
log.debug("posted response: #{resp.inspect}")
end
def extract_url(text)
text.match(/(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/[^ ]*)?/ix)[0] rescue nil
end
def extract_title(text)
text.match(/^(.*)http/)[1].sub(/^\w*: /, '') rescue nil
end
def extract_tags(text)
hashes = text.scan /#\w+/
hashes.collect { |h| h.sub /#/, ''} << 'via+twitter'
end
def fail
lambda do |url, res_code, res_header, res_body|
log.error("failed to parse #{url}: #{res_code}, #{res_body}")
end
end
def log
@log ||= Logger.new("delighter.log")
end
end
if __FILE__ == $0 then
(puts "Usage: ruby delighter.rb twitter_num delicious_username delicious_passwd"; exit) if ARGV.size < 3
Delighter.new(ARGV[0], ARGV[1], ARGV[2]).go
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment