Skip to content

Instantly share code, notes, and snippets.

@Freaky
Created March 13, 2020 04:51
Show Gist options
  • Save Freaky/4d56ce547a97946ef2e49a0122ca4257 to your computer and use it in GitHub Desktop.
Save Freaky/4d56ce547a97946ef2e49a0122ca4257 to your computer and use it in GitHub Desktop.
Split an NZB into multiple smaller NZBs
#!/usr/bin/env ruby
def usage() abort("#{$0} [base nzb] [number of new nzbs]") end
file = ARGV.shift
nzb = File.read(file)
num = Integer(ARGV.shift) rescue usage
header, nzb = nzb.split(/(?=<file)/, 2)
files = nzb.scan(/<file.*?<\/file>/m)
files_per_nzb = (files.size / num.to_f).ceil
1.upto(num) do |n|
fn = "#{File.basename(file, ".nzb")}.#{n}.nzb"
puts fn
File.open(fn, "w") do |f|
f.write header
files.shift(files_per_nzb).each do |file|
f.write(file)
end
f.write("</nzb>")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment