Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created April 10, 2011 16:20
Show Gist options
  • Save rummelonp/912478 to your computer and use it in GitHub Desktop.
Save rummelonp/912478 to your computer and use it in GitHub Desktop.
TwitterのRSSからTumblrにQuoteを投稿するRubyスクリプト
# -*- coding: utf-8 -*-
## tw_to_tmbr is the script that to post quote to tumblr from twitter rss feed.
##
## requirements:
## tumblr (gem install tumblr-rb)
##
require 'rubygems'
require 'tumblr'
require 'open-uri'
require 'rss'
require 'yaml'
module TwToTmblr
extend self
LOG = true
SOURCES = ['http://twitter.com/favorites/60032150.rss']
TUMBLR_EMAIL = '*** email ***'
TUMBLR_PASSWORD = '*** password ***'
DATA_DIR = File.join Dir.home, 'log'
PUBLISHED_PATH = File.join DATA_DIR, 'tw_to_tmbr_published.yaml'
def run
tumblr = Tumblr.new(TUMBLR_EMAIL, TUMBLR_PASSWORD)
published = YAML.load_file(PUBLISHED_PATH) rescue []
SOURCES.each do |source|
xml = open(source).read
data = RSS::Parser.parse(xml)
data.channel.items.each do |item|
next if published.include?(item.link)
doc = Tumblr.map(:quote).new(item.title)
doc.source = item.link
tumblr.post(doc).perform do |response|
raise unless response.success?
puts "---> published #{item.title}" if LOG
published.push << item.link
end rescue p $! if LOG
end
end
YAML.dump(published, open(PUBLISHED_PATH, 'w'))
end
end
if $0 == __FILE__
TwToTmblr.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment