Module: Downloader

Defined in:
lib/youtube_dlhelper/downloader.rb

Overview

Module for all Downloading things

Class Method Summary collapse

Class Method Details

.file_exist_ogg_m4a(filename) ⇒ Object

It checks what old file are available


63
64
65
66
67
68
69
70
71
72
# File 'lib/youtube_dlhelper/downloader.rb', line 63

def self.file_exist_ogg_m4a(filename)
  if File.exist?("#{filename}.ogg")
    ext = 'ogg'
  elsif File.exist?("#{filename}.m4a")
    ext = 'm4a'
  elsif File.exist?("#{filename}.webm")
    ext = 'webm'
  end
  return ext
end

.get(url) ⇒ Object

Accessing the url with get(url) via youtube-dl.rb


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/youtube_dlhelper/downloader.rb', line 21

def self.get(url)
  puts 'Downloading file...'.colour(:green)
  # Thanks for the gist https://gist.github.com/sapslaj/edb51218357fa33b6c4c
  video = YoutubeDL.download(url)
  video.filename # => "Adele - Hello-YQHsXMglC9A.f137.mp4"

  video_with_title = YoutubeDL.download(url, extract_audio: true, audio_format: 'best', audio_quality: 0, output: '%(title)s.%(ext)s')
  video_with_title.filename # => "Adele - Hello.mp4"

  title = YoutubeDL::Runner.new(url, get_title: true).run
  title # => "Adele - Hello"
  puts 'Downloading done...'.colour(:green)
  filename = video_with_title.filename

  rename(filename)
end

.rename(filenameorig) ⇒ String

rubocop:disable Metrics/AbcSize This method smells of :reek:TooManyStatements This method smells of :reek:UncommunicativeVariableName Method for renaming the orig file with blanks to underscores


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/youtube_dlhelper/downloader.rb', line 44

def self.rename(filenameorig)
  extn = File.extname filenameorig # .mp4
  filename = File.basename filenameorig, extn
  ext = file_exist_ogg_m4a(filename)
  # @note Replacing blanks with underscrores and delete non standard chars in
  # filename
  filenamenew0 = filename.gsub(/ /, '_')
  pattern = /[a-zA-Z0-9\-\s\_]/
  filenamenew = filenamenew0.split(//).keep_if do |chr|
    chr =~ pattern
  end.join
  puts 'Renaming the downloaded file'.colour(:green)
  FileUtils.mv("#{filename}.#{ext}", "#{filenamenew}.#{ext}")
  [filenamenew, filename]
end