Created
October 7, 2021 19:15
-
-
Save cmaxw/8fe61376a24ead0f16fecf10a3fcdd7f to your computer and use it in GitHub Desktop.
compare 2 rss feeds and create nginx redirects
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
#!/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