Skip to content

Instantly share code, notes, and snippets.

@chetan
Created July 29, 2013 15:12
Show Gist options
  • Save chetan/6105043 to your computer and use it in GitHub Desktop.
Save chetan/6105043 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
require 'torrent-ruby' # gem install torrent-ruby
buff = []
while true do
begin
buff << STDIN.read_nonblock(64000)
rescue
break
end
end
if buff.empty? then
STDERR.puts "no files given"
exit 1
end
input = buff.join('')
files = input.split(/\n/)
files.each do |f|
f = File.expand_path(f)
t = TorrentFile.open(f)
contents = t.to_h
# cleanup filename
# replace special chars with _
# set name in torrent itself (so download dir is updated)
ff = File.basename(f).gsub(/\.torrent$/, '').gsub(/[ .!&'"]/, '_')
contents["info"]["name"] = ff
t.create(contents)
t.save(f) # save back to input file
# rename to new name
ff = File.join(File.dirname(f), ff + ".torrent")
if f != ff then
STDERR.puts "renamed #{ff}"
FileUtils.mv(f, ff)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment