module Checker

The Checker module contains different methods to check anything

Public Class Methods

check_dir(music_dir, folder) click to toggle source

Checks if the targetdirectory are present. If not, it creates one @param [String] music_dir Path to the music directory @param [String] folder Path to the targetfolder

# File lib/youtube_dlhelper/checker.rb, line 88
def self.check_dir(music_dir, folder)
  # @note Checking if musicdir exists
  if Dir.exist?("#{music_dir}/#{folder}")
    puts 'Found directory. Im using it.'.colour(:green)
  else
    puts 'No directory found. Im creating it.'.colour(:green)
    # @note Creates the new directory in $music_dir/$folder
    FileUtils.mkdir_p("#{music_dir}/#{folder}")
    if Dir.exist?("#{music_dir}/#{folder}")
      puts 'Created new directory...'.colour(:green)
    else
      fail('Cant create directory')
    end
  end
end
check_target() click to toggle source

rubocop:disable Metrics/LineLength This method smells of :reek:TooManyStatements Ask for names, creates the folders and puts all into a $folder variable @return [String] folder

# File lib/youtube_dlhelper/checker.rb, line 62
def self.check_target

  entry = ask 'What kind of entry do you have? (Interpret or Group)'

  subdir = case entry
           when 'Interpret'
             [
               ask('Whats the surname of your interpret?'),
               ask('Whats the first name of your interpret?')
             ].join('_')

           when 'Group'
             ask 'Whats the name of the group?'

           else
             puts 'Just the entries "Interpret" or "Group" are allowed'.colour(:red)
             abort('Aborted')
           end
  subdir.gsub!(/ /, '_')
  folder = "#{ subdir }/Youtube-Music"
  return folder
end
cleanup(filename, filenameold) click to toggle source

Cleaner method for unneeded files This method smells of :reek:TooManyStatements @param [String] filename The name of the new produced file

# File lib/youtube_dlhelper/checker.rb, line 133
def self.cleanup(filename, filenameold)
  puts 'Cleaning up directory'.color(:green)
  # @note Cleanup the temp files
  Dir.glob("#{filename}*.mp4").each { |f| File.delete(f) if File.exist?(f) }
  Dir.glob("#{filename}*.m4a").each { |f| File.delete(f) if File.exist?(f) }
  Dir.glob("#{filename}*.webm").each { |f| File.delete(f) if File.exist?(f) }
  Dir.glob("#{filenameold}*.mp4").each { |f| File.delete(f) if File.exist?(f) }
  Dir.glob("#{filenameold}*.m4a").each { |f| File.delete(f) if File.exist?(f) }
  Dir.glob("#{filenameold}*.webm").each { |f| File.delete(f) if File.exist?(f) }

  puts 'Finished cleaning up'.color(:green)
end
external_url_is_valid?(url) click to toggle source

@note This method checks if a url is valid This method smells of :reek:TooManyStatements @param [String] url Is the given URL to the Youtube file @return [String] url

# File lib/youtube_dlhelper/checker.rb, line 21
def self.external_url_is_valid?(url)
  puts 'Checking prefix'.color(:green)
  puts url
  if url.include? 'https'
    puts 'Checking if https URL is valid'.colour(:green)
    https_url_valid?(url)
    return url
  else
    puts 'Checking if http URL is valid'.colour(:green)
    http_url_valid?(url)
    return url
  end
end
http_url_valid?(url) click to toggle source

Method to check http @param [String] url Is the given URL to the Youtube file

# File lib/youtube_dlhelper/checker.rb, line 49
def self.http_url_valid?(url)
  # @param [String] url Is the given URL to the Youtube file
  uri = URI.parse(url)
  response = Net::HTTP.start(uri.host, uri.port) do |http|
    http.head(uri.path)
  end
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
end
https_url_valid?(url) click to toggle source

Method to check https @param [String] url Is the given URL to the Youtube file

# File lib/youtube_dlhelper/checker.rb, line 37
def self.https_url_valid?(url)
  # @param [String] url Is the given URL to the Youtube file
  uri = URI.parse(url)
  response = Net::HTTP.start(uri.host, uri.port,
                  :use_ssl => uri.scheme == 'https') do |http|
    http.head(uri.path)
  end
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
end
oldconfig_exists?() click to toggle source

This method checks if a oldconfig is available @return [String] true or false

# File lib/youtube_dlhelper/checker.rb, line 106
def self.oldconfig_exists?
  home = Dir.home
  if File.exist?("#{home}/.youtube_dlhelper/youtube_dlhelper.conf")
    puts 'Found configuration file and using it...'.colour(:yellow)
  else
    # @raise
    puts 'Please run rake setup'.colour(:red)
    fail('Exiting now..').colour(:red)
  end
end
which_decoder?() click to toggle source

This method checks which decoder is available This method smells of :reek:TooManyStatements @return [String] ffmpeg_binary

# File lib/youtube_dlhelper/checker.rb, line 120
def self.which_decoder?
  getavconv = `which avconv`
  getffmpeg = `which ffmpeg`
  avconv = p getavconv.chomp
  ffmpeg = p getffmpeg.chomp
  ffmpeg_binary = ffmpeg if ffmpeg != ''
  ffmpeg_binary = avconv if avconv != ''
  return ffmpeg_binary
end