File: dump-metainfo.rb

package info (click to toggle)
libtorrent-ruby 0.3-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 260 kB
  • ctags: 330
  • sloc: ruby: 2,751; makefile: 1
file content (55 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## dump-metainfo.rb -- command-line .torrent dumper
## Copyright 2004 William Morgan.
##
## This file is part of RubyTorrent. RubyTorrent is free software;
## you can redistribute it and/or modify it under the terms of version
## 2 of the GNU General Public License as published by the Free
## Software Foundation.
##
## RubyTorrent is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License (in the file COPYING) for more details.

require 'rubytorrent'

def dump_metainfoinfo(mii)
  if mii.single?
      <<EOS
       length: #{mii.length / 1024}kb
     filename: #{mii.name}
EOS
  else
    mii.files.map do |f|
        <<EOS
   - filename: #{File.join(mii.name, f.path)}
       length: #{f.length}
EOS
    end.join + "\n"
  end + <<EOS
 piece length: #{mii.piece_length / 1024}kb 
       pieces: #{mii.pieces.length / 20}
EOS
end

def dump_metainfo(mi)
    <<EOS
#{dump_metainfoinfo(mi.info).chomp}
     announce: #{mi.announce}
announce-list: #{(mi.announce_list.nil? ? "<not specified>" : mi.announce_list.map { |x| x.join(', ') }.join('; '))}
creation date: #{mi.creation_date || "<not specified>"}
   created by: #{mi.created_by || "<not specified>"}
      comment: #{mi.comment || "<not specified>"}
EOS
end

if ARGV.length == 1
  fn = ARGV[0]
  begin
    puts dump_metainfo(RubyTorrent::MetaInfo.from_location(fn))
  rescue RubyTorrent::MetaInfoFormatError, RubyTorrent::BEncodingError => e
    puts "Can't parse #{fn}: maybe not a .torrent file?"
  end
else
  puts "Usage: dump-metainfo <filename>"
end