lib/download_tv/downloader.rb in download_tv-2.6.3 vs lib/download_tv/downloader.rb in download_tv-2.6.4
- old
+ new
@@ -95,29 +95,35 @@
@config.serialize
rescue InvalidLoginError
warn 'Wrong username/password combination'
end
+ def find_links(torrent, shows, queue)
+ Thread.new do
+ shows.each { |show| queue << get_link(torrent, show, save_pending: true) }
+ queue.close
+ end
+ end
+
+ def download_from_queue(queue)
+ Thread.new do
+ until queue.closed?
+ magnet = queue.pop
+ download(magnet) if magnet # Doesn't download if no torrents are found
+ end
+ end
+ end
+
def find_and_download(shows)
t = Torrent.new
queue = Queue.new
- # Adds a link (or empty string to the queue)
- link_t = Thread.new do
- shows.each { |show| queue << get_link(t, show, save_pending: true) }
- end
+ link_t = find_links(t, shows, queue)
# Downloads the links as they are added
- download_t = Thread.new do
- shows.size.times do
- magnet = queue.pop
- next if magnet == '' # Doesn't download if no torrents are found
+ download_t = download_from_queue(queue)
- download(magnet)
- end
- end
-
link_t.join
download_t.join
end
def shows_to_download(date)
@@ -141,22 +147,21 @@
##
# Uses a Torrent object to obtain links to the given tv show
# When :auto is true it will try to find the best match
# based on a set of filters.
# When it's false it will prompt the user to select the preferred result
- # Returns either a magnet link or an emptry string
+ # Returns either a magnet link or nil
def get_link(torrent, show, save_pending: false)
links = torrent.get_links(show)
if links.empty?
@config.content[:pending] << show if save_pending
- return ''
+ return
end
if @config.content[:auto]
- links = filter_shows(links)
- links.first[1]
+ filter_shows(links).first[1]
else
prompt_links(links)
get_link_from_user(links)
end
end
@@ -167,11 +172,11 @@
until i.between?(-1, links.size - 1)
puts 'Index out of bounds. Try again [-1 to skip]: '
i = $stdin.gets.chomp.to_i
end
- i == -1 ? '' : links[i][1]
+ i == -1 ? nil : links[i][1]
end
def prompt_links(links)
links.each_with_index { |data, i| puts "#{i}\t\t#{data[0]}" }
@@ -202,12 +207,12 @@
##
# Removes links whose names don't match the user filters
# Runs until no filters are left to be applied or applying
# a filter would leave no results
def filter_shows(links)
- f = Filterer.new(@config.content[:filters])
- f.filter(links)
+ @filterer ||= Filterer.new(@config.content[:filters])
+ @filterer.filter(links)
end
##
# Spawns a silent process to download a given magnet link
def download(link)
@@ -223,10 +228,10 @@
when /linux/
'xdg-open'
when /darwin/
'open'
else
- warn "You're using an unsupported platform."
+ warn "You're using an unsupported platform (#{RbConfig::CONFIG['host_os']})."
exit 1
end
end
end
end