Skip to content

Instantly share code, notes, and snippets.

@evan
Created October 13, 2012 08:12
Show Gist options
  • Save evan/3883770 to your computer and use it in GitHub Desktop.
Save evan/3883770 to your computer and use it in GitHub Desktop.
Twitter to Atom feed converter with media/conversations
require "rubygems"
require "twitter"
require "atom/pub"
Twitter.configure do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.oauth_token = ""
config.oauth_token_secret = ""
end
timeline = Twitter.user_timeline(:include_rts => true, :count => 50, :screen_name => "iano", :include_entities => true)
module Twitter
class Status
def to_atom_content
html = "<img style=\"display: block; align: left;\" src=\"#{user.profile_image_url}\"><p style=\"font-size: 12px\">#{user.name}</p><p style=\"font-size: 16px\">#{full_text}</p>"
urls.each do |url|
real_url = url.expanded_url
url_html = if real_url =~ /(gif|jpg|png)$/
"</p><p><a href=\"#{real_url}\"><img src=\"#{real_url}\"></a>"
elsif real_url =~ /twitpic.com\/(.*)/
"</p><p><a href=\"#{real_url}\"><img src=\"http://twitpic.com/show/large/#{$1}.jpg\"></a>"
else
"<a href=\"#{real_url}\">#{real_url}</a>"
end
html.sub!(url.url, url_html)
end
media.map do |media|
html << "<p><img src=\"#{media.media_url}\"></p>"
html.sub!(media.url, "")
end
html
end
end
end
seen = []
feed = Atom::Feed.new do |f|
f.title = "@iano"
f.id = "http://twitter.com/iano"
f.updated = timeline.first.created_at
f.entries = timeline.map do |tweet|
next if seen.include?(tweet.id)
Atom::Entry.new do |e|
tweet = tweet.retweeted_status if tweet.retweeted_status
e.title = "Tweet"
e.authors << Atom::Person.new(:name => tweet.user.name)
e.id = "http://twitter.com/iano/statuses/#{tweet.id}"
e.updated = tweet.created_at
content = tweet.to_atom_content
seen << tweet.id
begin
while tweet.in_reply_to_status_id
tweet = Twitter.status(tweet.in_reply_to_status_id, :include_entities => true)
seen << tweet.id
content << tweet.to_atom_content
end
rescue
end
e.content = Atom::Content::Html.new(content)
end
end.compact
end
puts feed.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment