創(chuàng)建一個(gè)改寫(xiě)種子文件名稱(chēng)的gem

五一放假無(wú)聊,想把死神來(lái)了翻出來(lái)再看一遍,本來(lái)想在百度網(wǎng)盤(pán)上離線下載后在線看,結(jié)果百度表示種子文件有違禁文字不讓離線下載(莫非是死神二字?),突然想起之前寫(xiě)的一個(gè)改編種子文件名稱(chēng)的一個(gè)gem,就完善了一下。

  • 首先創(chuàng)建一個(gè).gemspec文件,包含名稱(chēng)、版本等信息
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
  • 寫(xiě)測(cè)試用例,主要測(cè)試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

  • 再創(chuàng)建lib文件夾,在目錄下創(chuàng)建用于替換文件名的torrent-rename.rb文件和對(duì)torrent文件進(jìn)行編碼、解碼、讀取的becoding.rb文件,代碼較長(zhǎng)、較爛,不再列出.
  • 創(chuàng)建bin目錄添加torrent-rename文件,使安裝gem后可用 torrent-rename對(duì)種子文件進(jìn)行改編
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'torrent-rename.rb'
require 'pathname'
if ARGV.empty?
  puts "參數(shù)錯(cuò)誤"
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
  • 執(zhí)行測(cè)試
Paste_Image.png
  • 生成 gem
gem build torrent-rename.gemspec
  • 發(fā)布到rubygem上
gem push torrent-rename-0.1.2.gem  

安裝gem后 執(zhí)行 torrent-rename 種子文件的絕對(duì)路徑,即可在其所在的目錄下生成一個(gè)改編過(guò)的新種子文件

完整代碼:https://github.com/secondrocker/torrent-rename

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容