Class: Epodder::Download

Inherits:
Verb show all
Defined in:
lib/verb/download.rb

Instance Method Summary collapse

Methods inherited from Verb

#add_command, #each_argument, #lookup_args, #verb_struct

Methods inherited from Eclass

#initialize

Constructor Details

This class inherits a constructor from Epodder::Eclass

Instance Method Details

#download(args) ⇒ Object



5
6
7
8
9
# File 'lib/verb/download.rb', line 5

def download(args)
    each_argument(args) do |podcast|
        look_for_episodes podcast
    end
end

#download_episode(episode) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/verb/download.rb', line 22

def download_episode(episode)
    begin
        Downspout::Config.max_redirects = 100
        Downspout::Config.enable_curb! if Downspout::Config.curb_available?
        download = Downspout.download_url_to_path(episode.url, "download/#{episode.podcast.title.strip}/#{episode.url.to_s.match('((?!\/).)*$')}")
        puts download
        episode.downloaded = true
        success = episode.save
        if !success
            errors.each do |e|
                puts e
            end
        end
    rescue StandardError => e
        puts "#{episode.url} generated #{e}"
        puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
    end
end

#look_for_episodes(podcast) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/verb/download.rb', line 11

def look_for_episodes(podcast)
    episodes = Episode.all(downloaded: false, podcast: podcast)
    episodes.select { |ep| !ep.nil? }.each do |episode|
        puts episode.podcast.title
        title = (episode.podcast.title).strip
        Dir.mkdir "download/#{title}" unless Dir.exists? "download/#{title}"
        puts "Downloading #{title} - #{episode.title || episode.id}"
        download_episode episode
    end
end