module Downloader

Module for all Downloading things

Public Class Methods

file_exist_ogg_m4a(filename) click to toggle source

It checks what old file are available @param [String] title @return ext

# 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) click to toggle source

Accessing the url with get(url) via youtube-dl.rb @param [String] url Is the given URL to the Youtube file

# 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) click to toggle source

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 @param [String] url Is the given URL to the Youtube file @return [String] filenamenew The fixed filename with underscores

# 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