Skip to content

Instantly share code, notes, and snippets.

Created November 3, 2010 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/661001 to your computer and use it in GitHub Desktop.
Save anonymous/661001 to your computer and use it in GitHub Desktop.
Converts an IMAP box contents of NZBMatrix Watchlist mails to an RSS feed
#!/usr/bin/env ruby
require 'net/imap'
require 'rss/maker'
require 'fileutils'
DEBUG = (ARGV.include?('--debug') ? true : false)
def debug(args,&block)
puts(args,&block) if DEBUG
end
server = "imap.yourmailserver.example" # change this for your system
user = "yourusername@yourmailserver.example" # change this for your system
pass = "y0urp4ssw0rd" # change this for your system
#folder = "INBOX.Sent" # shows how to specify a different folder
folder = "INBOX"
nzb_from_address = "emailaddress@where.nzbmails.come.from.example"
nzb_items = []
nzb_watchfolder = File.expand_path("~/downloads/nzb")
FileUtils.mkdir_p(nzb_watchfolder) unless File.exist?(nzb_watchfolder)
nzbmatrix_username = "yourNzbMatrixUsername"
nzbmatrix_apikey = "yourNzbMatrixAPIKEY"
rss_version = "2.0" # ["0.9", "1.0", "2.0"]
rss_filename = File.expand_path("~/sites/yoursite.example/nzbwatchlist.xml")
rss_url = "http://yoursite.example/nzbwatchlist.rss"
rss_title = "NZBMatrix Watcher"
rss_description = "NZBMatrix Watcher mails converted to RSS"
imap = Net::IMAP.new(server)
imap.login(user, pass)
imap.select(folder)
imap.search(["FROM", nzb_from_address, "BODY", "Download Link:"]).each do |msg_id|
msg = imap.fetch(msg_id, "(UID RFC822.SIZE ENVELOPE BODY[TEXT])")[0]
env = imap.fetch(msg_id, "ENVELOPE")[0].attr["ENVELOPE"]
date = imap.fetch(msg_id, "INTERNALDATE")[0].attr["INTERNALDATE"]
from = env.from[0].name
subject = env.subject
body = msg.attr["BODY[TEXT]"].gsub("\n"," ").gsub("\t", " ").gsub(13.chr," ").squeeze(" ")
#Parse format 1
body =~ /Download Link: ?<a href="([^"]+)"[^>]+>(.+?)<\/a><br>/
download_link = $1
description = $2
#Fallback to Parse format 2
unless download_link && description
body =~ /Download Link: ?(.+?)<([^>]+?)>/
download_link = $2
description = $1
end
unless download_link && description
puts "Could not parse this message: "
puts "From: #{from}"
puts "Subject: #{subject}"
puts "Body:"
puts '-' * 10
puts body
puts '-' * 10
puts "Description: #{description}"
puts "Download link: #{download_link}"
puts '-' * 80
next
end
#Remove redirects
download_link.gsub!(/https?:\/\/nzbmatrix\.com\/redirect\.php\?url=/,'')
#Force HTTPS links
download_link.gsub('http://','https://')
debug description
debug download_link
debug '-' * 80
nzb_items << {
:from => from,
:subject => subject,
:description => description,
:download_link => download_link,
:date => date
}
end
imap.logout
# unless imap.disconnected?
# imap.disconnect
# end
content = RSS::Maker.make(rss_version) do |m|
m.channel.title = rss_title
m.channel.link = rss_url
m.channel.description = rss_description
m.items.do_sort = true # sort items by date
nzb_items.each do |nzb_item|
i = m.items.new_item
i.title = nzb_item[:description]
i.link = nzb_item[:download_link]
i.date = Time.parse(nzb_item[:date])
end
end
File.open(rss_filename,"w") do |f|
f.write(content)
end
nzb_items.each do |nzb_item|
nzb_id = ''
if nzb_item[:download_link] =~ /id=(\d+)/
nzb_id = $1
end
download_link = "https://api.nzbmatrix.com/v1.1/download.php?id=#{nzb_id}&username=#{nzbmatrix_username}&apikey=#{nzbmatrix_apikey}"
filename = nzb_item[:description].gsub(/[^a-zA-Z0-9_-]/,'_') + '-' + nzb_id + '.nzb'
filepath = File.expand_path(File.join(nzb_watchfolder,filename))
unless File.exist?(filepath)
`wget "#{download_link}" -O #{filepath}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment