lib/download_tv/downloader.rb in download_tv-2.5.3 vs lib/download_tv/downloader.rb in download_tv-2.5.4
- old
+ new
@@ -18,14 +18,25 @@
]
Thread.abort_on_exception = true
end
- def download_single_show(show)
+ def download_single_show(show, season = nil)
t = Torrent.new(@config.content[:grabber])
show = fix_names([show]).first
- download(get_link(t, show))
+ if season
+ season.insert(0, '0') if season.size == 1
+ episode = "#{show} s#{season}e01"
+ loop do
+ link = get_link(t, episode)
+ break if link.empty?
+ download(link)
+ episode = episode.next
+ end
+ else
+ download(get_link(t, show))
+ end
end
##
# Given a file containing a list of episodes (one per line)
# it tries to find download links for each
@@ -49,69 +60,100 @@
def run(dont_update_last_run, offset = 0)
pending = @config.content[:pending].clone
@config.content[:pending].clear
pending ||= []
date = check_date(offset)
- if pending.empty? && date.nil?
- puts 'Everything up to date'
- exit
- end
- to_download = shows_to_download(date)
- to_download.concat pending
+ pending.concat shows_to_download(date) if date
- if to_download.empty?
+ if pending.empty?
puts 'Nothing to download'
-
else
- t = Torrent.new
+ find_and_download(pending)
+ puts 'Completed. Exiting...'
+ end
- queue = Queue.new
+ @config.content[:date] = [Date.today, @config.content[:date]].max unless dont_update_last_run
+ @config.serialize
+ rescue InvalidLoginError
+ warn 'Wrong username/password combination'
+ end
- # Adds a link (or empty string to the queue)
- link_t = Thread.new do
- to_download.each { |show| queue << get_link(t, show, true) }
- end
+ ##
+ # Finds download links for all the episodes set to air today.
+ # TODO: Refactor with #run()
+ def run_ahead(dont_update_last_run)
+ pending = @config.content[:pending].clone
+ @config.content[:pending].clear
+ pending ||= []
- # Downloads the links as they are added
- download_t = Thread.new do
- to_download.size.times do
- magnet = queue.pop
- next if magnet == '' # Doesn't download if no torrents are found
+ # Has the program already been run with --tomorrow
+ if @config.content[:date] < Date.today.next
+ pending.concat today_shows_to_download
+ end
- download(magnet)
- end
- end
-
- # Downloading the subtitles
- # subs_t = @config.content[:subs] and Thread.new do
- # to_download.each { |show| @s.get_subs(show) }
- # end
-
- link_t.join
- download_t.join
- # subs_t.join
-
+ if pending.empty?
+ puts 'Nothing to download'
+ else
+ find_and_download(pending)
puts 'Completed. Exiting...'
end
- @config.content[:date] = Date.today unless dont_update_last_run
+ @config.content[:date] = Date.today.next unless dont_update_last_run
@config.serialize
rescue InvalidLoginError
warn 'Wrong username/password combination'
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, true) }
+ end
+
+ # 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(magnet)
+ end
+ end
+
+ # Downloading the subtitles
+ # subs_t = @config.content[:subs] and Thread.new do
+ # shows.each { |show| @s.get_subs(show) }
+ # end
+
+ link_t.join
+ download_t.join
+ # subs_t.join
+ end
+
def shows_to_download(date)
myepisodes = MyEpisodes.new(@config.content[:myepisodes_user],
@config.content[:cookie])
# Log in using cookie by default
myepisodes.load_cookie
- shows = myepisodes.get_shows(date)
+ shows = myepisodes.get_shows_since(date)
shows = reject_ignored(shows)
fix_names(shows)
end
+ def today_shows_to_download
+ myepisodes = MyEpisodes.new(@config.content[:myepisodes_user],
+ @config.content[:cookie])
+ myepisodes.load_cookie
+ shows = myepisodes.today_shows
+ shows = reject_ignored(shows)
+ fix_names(shows)
+ end
+
##
# 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
@@ -150,15 +192,19 @@
puts
print 'Select the torrent you want to download [-1 to skip]: '
end
##
- # Returns the date from which to check for shows
- # Or nil if the date is today
+ # Returns the date from which to check shows
+ # or nil if the program was already ran today
+ # Passing an offset skips this check
def check_date(offset)
- last = @config.content[:date]
- last -= offset
- last if last != Date.today
+ if offset.zero?
+ last = @config.content[:date]
+ last if last < Date.today
+ else
+ Date.today - offset
+ end
end
##
# Given a list of shows and episodes:
#