Created
December 6, 2009 21:36
-
-
Save kyleslattery/250434 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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