Skip to content

Instantly share code, notes, and snippets.

@cmaxw
Created October 7, 2021 19:15
Show Gist options
  • Save cmaxw/8fe61376a24ead0f16fecf10a3fcdd7f to your computer and use it in GitHub Desktop.
Save cmaxw/8fe61376a24ead0f16fecf10a3fcdd7f to your computer and use it in GitHub Desktop.
compare 2 rss feeds and create nginx redirects
#!/usr/bin/env ruby
require 'httparty'
require 'feedjira'
def get_feed(source)
if source.start_with?("http")
xml = feed_from_url(source)
else
xml = feed_from_file(source)
end
Feedjira.parse(xml)
end
def feed_from_url(url)
HTTParty.get(url).body
end
def feed_from_file(path)
File.open(path, 'r').read
end
def feed_to_hash(feed)
feed.entries.inject(Hash.new) do |memo, entry|
memo[entry.id] = entry.url.sub(/https?\:\/\/devchat\.tv/, '') if entry.url && !entry.url.include?("/bonus/")
memo
end
end
original = feed_to_hash(get_feed(ARGV[0]))
new_feed = feed_to_hash(get_feed(ARGV[1]))
redirects = original.inject("") do |memo, (guid, orig_url)|
new_url = new_feed[guid]
memo << "location = #{orig_url} {\n return 301 #{new_url};\n}\n\n"
memo
end
File.open("./redirects", "wb") do |file|
file.puts redirects
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment