Class: Budik::Storage
- Inherits:
-
Object
- Object
- Budik::Storage
- Includes:
- Singleton
- Defined in:
- lib/budik/storage.rb
Overview
‘Storage’ class downloads and manages media sources/items.
Instance Attribute Summary collapse
-
#dir ⇒ Object
Gets sources, download directory and download method.
-
#method ⇒ Object
Gets sources, download directory and download method.
-
#sources ⇒ Object
Gets sources, download directory and download method.
Instance Method Summary collapse
-
#download_sources(source = nil) ⇒ Object
Downloads specified source or all sources.
-
#download_youtube(id, address) ⇒ Object
Downloads video from YouTube by ID with specified options.
-
#initialize ⇒ Storage
constructor
Loads sources, download directory and download method.
-
#locate_item(item) ⇒ Object
Gets downloaded item’s location.
-
#remove_sources(source = nil) ⇒ Object
Removes specified source or all sources.
Constructor Details
#initialize ⇒ Storage
Loads sources, download directory and download method.
16 17 18 19 20 21 |
# File 'lib/budik/storage.rb', line 16 def initialize @sources = Sources.instance.sources dir = Config.instance.['sources']['download']['dir'] @dir = File.(dir) + '/' @method = Config.instance.['sources']['download']['method'] end |
Instance Attribute Details
#dir ⇒ Object
Gets sources, download directory and download method.
24 25 26 |
# File 'lib/budik/storage.rb', line 24 def dir @dir end |
#method ⇒ Object
Gets sources, download directory and download method.
24 25 26 |
# File 'lib/budik/storage.rb', line 24 def method @method end |
#sources ⇒ Object
Gets sources, download directory and download method.
24 25 26 |
# File 'lib/budik/storage.rb', line 24 def sources @sources end |
Instance Method Details
#download_sources(source = nil) ⇒ Object
Downloads specified source or all sources.
-
Args:
-
source
-> Source to download (Hash).
-
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/budik/storage.rb', line 31 def download_sources(source = nil) if source IO.instance.storage_download_info(source) source[:path].each do |path| download_youtube(YouTubeAddy.extract_video_id(path), path) end else @sources.each { |src| download_sources(src) } end end |
#download_youtube(id, address) ⇒ Object
Downloads video from YouTube by ID with specified options.
-
Args:
-
id
-> YouTube video ID (String). -
address
-> YouTube video address (String).
-
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/budik/storage.rb', line 48 def download_youtube(id, address) return unless id && !File.file?(@dir + id + '.mp4') # TODO: Update youtube-dl if fail # TODO: username + password = { output: @dir + '%(id)s.%(ext)s', format: 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', playlist: false } YoutubeDL.download '"' + address + '"', end |
#locate_item(item) ⇒ Object
Gets downloaded item’s location.
-
Args:
-
item
-> Item to locate (String).
-
-
Returns:
-
Path to downloaded file.
-
Unaltered path if streaming or if the item is local.
-
67 68 69 70 71 |
# File 'lib/budik/storage.rb', line 67 def locate_item(item) return item if @method == 'stream' is_url = (item =~ /\A#{URI.regexp(%w(http https))}\z/) is_url ? @dir + YouTubeAddy.extract_video_id(item) + '.mp4' : item end |
#remove_sources(source = nil) ⇒ Object
Removes specified source or all sources.
-
Args:
-
source
-> Source to remove (Hash).
-
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/budik/storage.rb', line 78 def remove_sources(source = nil) return unless @method == 'remove' if source source[:path].each do |path| next if locate_item(path) == path FileUtils.rm File.(locate_item(path)), force: true end else @sources.each { |src| remove_sources(src) } end end |