Class: Torrent

Inherits:
Object
  • Object
show all
Defined in:
lib/torrent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, name, uploaded, ratio, label, done) ⇒ Torrent

Returns a new instance of Torrent.



5
6
7
8
9
10
11
12
# File 'lib/torrent.rb', line 5

def initialize(hash, name, uploaded, ratio, label, done)
  @hash = hash
  @name = name
  @uploaded = uploaded
  @ratio = ratio
  @done = done
  @label = label
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



4
5
6
# File 'lib/torrent.rb', line 4

def basename
  @basename
end

#doneObject

Returns the value of attribute done.



4
5
6
# File 'lib/torrent.rb', line 4

def done
  @done
end

#hashObject

Returns the value of attribute hash.



4
5
6
# File 'lib/torrent.rb', line 4

def hash
  @hash
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/torrent.rb', line 4

def label
  @label
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/torrent.rb', line 4

def name
  @name
end

#ratioObject

Returns the value of attribute ratio.



4
5
6
# File 'lib/torrent.rb', line 4

def ratio
  @ratio
end

#uploadedObject

Returns the value of attribute uploaded.



4
5
6
# File 'lib/torrent.rb', line 4

def uploaded
  @uploaded
end

Class Method Details

.allObject



28
29
30
31
32
33
34
35
36
# File 'lib/torrent.rb', line 28

def self.all
  json = JSON.parse(open("http://#{@@settings.host}:#{@@settings.port}/gui/?list=1", :http_basic_authentication => [@@settings.username, @@settings.password]).read)
  torrents = Array.new

  json['torrents'].each do |item|
    torrents << Torrent.new(item[0], item[2], format_size(item[6].to_i), format_ratio(item[7]), item[11], (item[4].to_f / 10.00))
  end
  torrents
end

.find_by_name(name) ⇒ Object



79
80
81
82
83
# File 'lib/torrent.rb', line 79

def self.find_by_name(name)
  torrents = Array.new
  all.each { |torrent| torrents << torrent if torrent.name =~ Regexp.new(name, true)}
  torrents
end

.find_old_episodesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/torrent.rb', line 46

def self.find_old_episodes

  episodes = all.reject {|x| !x.name.match(/(S\d+(\.|)E\d+)|(\d+x\d+)/)}
  old_episodes = Array.new

  episodes.each { |e| e.basename = e.name.gsub(/(.+)(\.S\d+(\.|)E\d+|\d+x\d+).+/, '\1')}.sort! {|x,y| x.name <=> y.name }

  duplicates = Hash.new

  episodes.each { |e|
    if duplicates.has_key?(e.basename)
      duplicates[e.basename] += 1
    else
      duplicates[e.basename] = 1
    end
  }

  duplicates.reject { |k,v| v == 1 }

  episodes.each_with_index do |episode, i|

    if !duplicates.has_key?(episode.basename)
      next
    end

    next if i == episodes.length-1

    old_episodes << episode unless episodes[i+1].basename != episode.basename
  end

  old_episodes
end

.upload_torrent(file, category = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/torrent.rb', line 38

def self.upload_torrent(file, category=nil)
  result = system("curl --basic --user #{@@settings.username}:#{@@settings.password} -F \"torrent_file=@#{file};type=application/x-bittorrent\" 'http://#{@@settings.host}:#{@@settings.port}/gui/?action=add-file' &> /dev/null")

  if result and category
    Torrent.all.last.set_category(category)
  end
end

Instance Method Details

#list_filesObject



85
86
87
88
89
90
# File 'lib/torrent.rb', line 85

def list_files
  json = JSON.parse(open("http://#{@@settings.host}:#{@@settings.port}/gui/?action=getfiles&hash=#{@hash}", :http_basic_authentication => [@@settings.username, @@settings.password]).read)
  files = Array.new
  json['files'][1].each { |file| files << LsFile.new(file[0], Torrent.format_size(file[1]))}
  files
end

#removeObject



18
19
20
21
# File 'lib/torrent.rb', line 18

def remove
  template = "http://#{@@settings.host}:#{@@settings.port}/gui/?action=remove&hash=%s"
  open(template % @hash, :http_basic_authentication => [@@settings.username, @@settings.password]) {|f| f.read }
end

#removedataObject



23
24
25
26
# File 'lib/torrent.rb', line 23

def removedata
  template = "http://#{@@settings.host}:#{@@settings.port}/gui/?action=removedata&hash=%s"
  open(template % @hash, :http_basic_authentication => [@@settings.username, @@settings.password]) {|f| f.read }
end

#set_category(category) ⇒ Object



14
15
16
# File 'lib/torrent.rb', line 14

def set_category(category)
  open("http://#{@@settings.host}:#{@@settings.port}/gui/?action=setprops&s=label&hash=#{@hash}&v=#{category}", :http_basic_authentication => [@@settings.username, @@settings.password])
end