五一放假无聊,想把死神来了翻出来再看一遍,本来想在百度网盘上离线下载后在线看,结果百度表示种子文件有违禁文字不让离线下载(莫非是死神二字?),突然想起之前写的一个改编种子文件名称的一个gem,就完善了一下。
- 首先创建一个.gemspec文件,包含名称、版本等信息
Gem::Specification.new do |s|
s.name = 'torrent-rename'
s.version = '0.1.2'
s.date = '2016-05-02'
s.summary = "rename torrent file"
s.description = "rename torrent file"
s.authors = ["secondrocker"]
s.email = 'secondrocker@gmail.com'
s.files = Dir['lib/**/*','bin/*']
s.homepage =
'http://secondrocker.github.com'
s.license = 'MIT'
s.executables = ['torrent-rename']
end
- 写测试用例,主要测试torrent格式的编码
require "rspec"
require "torrent-rename"
describe "encode" do
describe "int encode" do
it " should equals the encode string" do
expect(12.bencode).to eq("i12e")
end
end
describe "string encode" do
it "should equals the encode string" do
expect("what".bencode).to eq("4:what")
end
end
describe "hash encode" do
it "should equals the encode string" do
expect({"author"=>"wangdong","city"=>"beijing"}.bencode).to eq("d6:author8:wangdong4:city7:beijinge")
end
end
describe "array encode" do
it "should equals the encode array" do
expect([1,2,3].bencode).to eq("li1ei2ei3ee")
end
end
describe "bencode string decode" do
it " should equals the obj" do
expect("d4:name9:sishen3:agei12e5:filmsli1ei2ei3eee".bdecode).to eq({"name"=>"sishen","age"=>12,"films"=>[1,2,3]})
end
end
end
- 再创建lib文件夹,在目录下创建用于替换文件名的torrent-rename.rb文件和对torrent文件进行编码、解码、读取的becoding.rb文件,代码较长、较烂,不再列出.
- 创建bin目录添加torrent-rename文件,使安装gem后可用 torrent-rename对种子文件进行改编
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'torrent-rename.rb'
require 'pathname'
if ARGV.empty?
puts "参数错误"
else
if ARGV[0] =~ /\.torrent$/
if File.exists? ARGV[0]
begin
tf = TorrentRename.new ARGV[0]
tf.replace_file
rescue
puts '种子文件不合法'
end
else
puts ARGV[0].to_s + "doesn't exist"
end
else
puts "非种子文件!"
end
end
- 执行测试
- 生成 gem
gem build torrent-rename.gemspec
- 发布到rubygem上
gem push torrent-rename-0.1.2.gem
安装gem后 执行 torrent-rename 种子文件的绝对路径,即可在其所在的目录下生成一个改编过的新种子文件