Class: DownloadTV::Torrent

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

Overview

Class in charge of managing the link grabbers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_grabber = nil) ⇒ Torrent

Returns a new instance of Torrent.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/download_tv/torrent.rb', line 14

def initialize(default_grabber = nil)
  g_names = grabbers

  # Silently ignores bad names
  found_default = g_names.find_index(default_grabber)
  g_names.rotate! found_default if found_default

  @g_instances = g_names.map { |g| (DownloadTV.const_get g).new }
  reset_tries

  check_grabber_online
end

Instance Attribute Details

#g_instancesObject (readonly)

Returns the value of attribute g_instances.



7
8
9
# File 'lib/download_tv/torrent.rb', line 7

def g_instances
  @g_instances
end

#triesObject (readonly)

Returns the value of attribute tries.



7
8
9
# File 'lib/download_tv/torrent.rb', line 7

def tries
  @tries
end

Instance Method Details

#change_grabbersObject



41
42
43
44
45
# File 'lib/download_tv/torrent.rb', line 41

def change_grabbers
  # Rotates the instantiated grabbers
  @g_instances.rotate!
  check_grabber_online
end

#check_grabber_onlineObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/download_tv/torrent.rb', line 27

def check_grabber_online
  if @g_instances.empty?
    warn 'There are no available grabbers.'
    exit 1
  end
  return if @g_instances.first.online?

  # We won't be using this grabber
  warn "Problem accessing #{@g_instances.first.class.name}"
  @tries -= 1
  @g_instances.shift # Removes first element
  check_grabber_online
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/download_tv/torrent.rb', line 47

def get_links(show)
  links = @g_instances.first.get_links(show)

  reset_grabbers_order
  reset_tries

  links
rescue NoTorrentsError
  # Use next grabber
  if @tries.positive?
    @tries -= 1
    change_grabbers
    retry

  else
    reset_grabbers_order
    reset_tries
    puts "No torrents found for #{show}"
    []
  end
end

#grabbersObject



9
10
11
12
# File 'lib/download_tv/torrent.rb', line 9

def grabbers
  # %w[TorrentAPI ThePirateBay Eztv KAT]
  %w[TorrentAPI Eztv]
end

#reset_grabbers_orderObject



73
74
75
# File 'lib/download_tv/torrent.rb', line 73

def reset_grabbers_order
  @g_instances.rotate!(@tries + 1)
end

#reset_triesObject



69
70
71
# File 'lib/download_tv/torrent.rb', line 69

def reset_tries
  @tries = @g_instances.size - 1
end