Skip to content

Instantly share code, notes, and snippets.

@Micronarrativ
Created September 22, 2015 10:33
Show Gist options
  • Save Micronarrativ/f32b5fd0660ecac2da26 to your computer and use it in GitHub Desktop.
Save Micronarrativ/f32b5fd0660ecac2da26 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# 20150922/JT
# Parse twitter feed to rss
#
# Input: twitter handle
# Output: xml
#
require 'nokogiri'
require 'open-uri'
require 'rss'
# Input checking and Help
if ARGV[0].nil?
puts 'No argument given.'
puts 'Please provide twitterhandle as argument'
puts ''
puts 'Syntax:'
puts " #{__FILE__} <twitterhandle>"
puts ''
puts 'Example:'
puts " #{__FILE__} rubygems"
exit 1
else
url = 'https://twitter.com/' + ARGV[0].to_s.gsub(/^@/, '')
end
page = Nokogiri::HTML(open(url))
# RSS feed
rss = RSS::Maker.make('atom') do |maker|
maker.channel.author = page.css('title')
maker.channel.updated = Time.now.to_s
maker.channel.about = url
maker.channel.title = page.css('title')
# Feed Logo
logo = page.css('img.ProfileAvatar-image')
logo.each{|logourl| maker.channel.logo = logourl['src']}
page.css('.content').each do |tweet|
maker.items.new_item do |item|
item.content.content = tweet.css('.tweet-text').text
item.description = tweet.css('.tweet-text').text
item.title = tweet.css('.tweet-text').text
item.updated = Time.now.to_s
dates = tweet.css('a.tweet-timestamp')
dates.each do |link|
item.pubDate = Time.parse(link['title'].gsub(/^(\d{2})\.(\d{2})/,'\1:\2'))
item.link = 'https://twitter.com' + link['href']
item.id = link['href']
end
end
end
end
# Output RSS(XML)
puts rss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment