Skip to content

Instantly share code, notes, and snippets.

@kyleslattery
Created December 6, 2009 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleslattery/250434 to your computer and use it in GitHub Desktop.
Save kyleslattery/250434 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'httparty'
FEED_URL = 'http://cdevroe.com/category/mobile-photos/feed/'
CACHE_FILE = './rss2twitter.cache'
TWITTER_USER = 'user'
TWITTER_PASS = 'pass'
TIMEOUT = 12*60*60 # 12 hours in seconds
class WP; include HTTParty; format :xml; end
class Twitter
include HTTParty
base_uri 'http://twitter.com'
basic_auth TWITTER_USER, TWITTER_PASS
end
cache = []
new_cache = []
# Open cache file, creating it if it doesn't exist
f = File.open(CACHE_FILE, File::RDONLY|File::CREAT)
# Read file into cache array
f.each_line {|line| cache << line.strip}
f.close
# Loop through RSS items
WP.get(FEED_URL)['rss']['channel']['item'].each do |item|
# Change guid into Colin's short url format
url = item['guid'].gsub(/\?p=/, "p/")
# Get post date
time = Time.parse(item["pubDate"])
# Check if it's postable: not cached and not outside of timeout
if !cache.include?(url) && (Time.now - time) < TIMEOUT
Twitter.post('/statuses/update.json', {
:status => "Mobile Photo \"#{item['title']}\": #{url}"
})
end
new_cache << url
end
# Update cache file
File.open(CACHE_FILE, File::WRONLY|File::TRUNC) do |file|
file.puts new_cache.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment