Class: Ektoplayer::Trackloader
- Inherits:
-
Object
- Object
- Ektoplayer::Trackloader
- Defined in:
- lib/ektoplayer/trackloader.rb
Instance Attribute Summary collapse
-
#downloads ⇒ Object
readonly
Returns the value of attribute downloads.
Instance Method Summary collapse
- #download_album(url) ⇒ Object
- #get_track_file(url, reload: false, http_okay: false) ⇒ Object
- #get_track_infos(url) ⇒ Object
-
#initialize(database) ⇒ Trackloader
constructor
A new instance of Trackloader.
Constructor Details
#initialize(database) ⇒ Trackloader
Returns a new instance of Trackloader.
18 19 20 21 |
# File 'lib/ektoplayer/trackloader.rb', line 18 def initialize(database) @downloads = [] @database = database end |
Instance Attribute Details
#downloads ⇒ Object (readonly)
Returns the value of attribute downloads.
16 17 18 |
# File 'lib/ektoplayer/trackloader.rb', line 16 def downloads @downloads end |
Instance Method Details
#download_album(url) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ektoplayer/trackloader.rb', line 36 def download_album(url) track_info = get_track_infos(url) return if File.exists? track_info['album_path'] archive_file = File.join(Config[:download_dir], track_info['archive_filename']) return if File.exists? archive_file archive_url = Application.archive_url(track_info['archive_url']) Application.log(self, 'starting download:', archive_url) dl = DownloadThread.new(archive_url, archive_file) if Config[:auto_extract_to_archive_dir] dl.events.on(:completed) do begin extract_dir = track_info['album_path'] FileUtils::mkdir_p(extract_dir) Common::extract_zip(archive_file, extract_dir) FileUtils::rm(archive_file) if Config[:delete_after_extraction] rescue Application.log(self, "extraction of '#{archive_file}' to '#{extract_dir}' failed:", $!) end end end dl.events.on(:failed) do |reason| Application.log(self, dl.filename, dl.url, reason) FileUtils::rm(dl.filename) rescue nil end @downloads << dl.start! rescue Application.log(self, $!) end |
#get_track_file(url, reload: false, http_okay: false) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ektoplayer/trackloader.rb', line 70 def get_track_file(url, reload: false, http_okay: false) begin track_info = get_track_infos(url) album_files = Dir.glob(File.join(track_info['album_path'], '*.mp3')) track_file = album_files.sort[track_info['number']] return track_file if track_file rescue Application.log(self, 'could not load track from archive_dir:', $!) end real_url = Application.track_url(url) url_obj = URI.parse(real_url) basename = File.basename(url_obj.path) cache_file = File.join(Config[:cache_dir], basename) temp_file = File.join(Config[:temp_dir], '~ekto-' + basename) (File.delete(cache_file) rescue nil) if reload (File.delete(temp_file) rescue nil) if reload return cache_file if File.file?(cache_file) return temp_file if File.file?(temp_file) return real_url if http_okay Application.log(self, 'starting download:', real_url) dl = DownloadThread.new(real_url, temp_file) if Config[:use_cache] dl.events.on(:completed) do FileUtils::mv(temp_file, cache_file) rescue ( Application.log(self, 'mv failed', temp_file, cache_file, $!) ) end end dl.events.on(:failed) do |reason| Application.log(self, dl.filename, dl.url, reason) FileUtils::rm(dl.filename) rescue nil end @downloads << dl.start! return temp_file rescue Application.log(self, $!) end |
#get_track_infos(url) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ektoplayer/trackloader.rb', line 23 def get_track_infos(url) r = @database.select(filters: [{tag: :url, operator: :==, value: url}])[0] r.update( @database.get_archives(url).select {|_|_['archive_type'] == 'MP3'}[0] ) r['archive_filename'] = URI.unescape(r['archive_url']) r['archive_basename'] = File.basename(r['archive_filename'], '.zip') r['album_path'] = File.join(Config[:archive_dir], r['archive_basename']) r end |