class MultimediaParadise::Genre

Constants

DEFAULT_FILE
#

DEFAULT_FILE

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

[](i = '') click to toggle source
#

MultimediaParadise::Genre[]

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 688
def self.[](i = '')
  new(i)
end
new( i = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 64
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_yaml_file(
    DEFAULT_FILE
  )
  set_commandline_arguments(i)
  run if run_already
end

Public Instance Methods

dataset?() click to toggle source
#

dataset?

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 438
def dataset?
  @dataset
end
Also aliased as: full_dataset?
default_action()
do_the_default_action() click to toggle source
#

do_the_default_action

Presently, the default action is to show all available songs.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 325
def do_the_default_action
  show_available_songs(:then_exit)
end
Also aliased as: default_action
download_file_at_number(i) click to toggle source
#

download_file_at_number

Invocation example:

eurodance 62
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 538
def download_file_at_number(i)
  i = i.to_i
  _ = full_dataset?
  hash = {}
  _.each_pair.with_index {|data, index|
    if index == (i-1)
      data = Hash[*data]
      hash.update(data)
    end
  }
  download_remote_file(hash)
end
download_remote_file(this_dataset = full_dataset?)
download_remote_files(this_dataset = full_dataset?) click to toggle source
#

download_remote_files

Use this method when you want to download the remote files.

The input to this method should be a Hash.

Invocation example:

eurodance --download
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 489
def download_remote_files(this_dataset = full_dataset?)
  ::MultimediaParadise.ensure_that_the_output_directory_exists
  store_here = ::MultimediaParadise.output_directory?+return_name_from_class_name+'/'
  mkdir(store_here)
  cd(store_here)
  this_dataset.each_pair {|name_of_the_song, array|
    url = array[1]
    dataset = { name_of_the_song => array }
    _ = dataset
    key = _.keys.first
    name_of_the_group = _[key][0]
    opnn; e
    e '  Now downloading the song '+dodgerblue(name_of_the_song)+
      ' (Group: '+simp(name_of_the_group)+') from the URL '+sfancy(url)
    name_of_the_song = key.to_s.delete('!')
    url = _[key][1]
    release_year = _[key][2].to_s # Need a String.
    # ======================================================================= #
    # Next, we will use youtube-dl to download this.
    # ======================================================================= #
    result = `youtube-dl #{url}`
    use_this_regex = /Merging formats into "(.+)"/
    result.scan(use_this_regex)
    e 'Working on the song '+simp(name_of_the_song)+' next.'
    new_file_name = $1.to_s.dup
    # ======================================================================= #
    # The filename has to be sanitized and renamed.
    # ======================================================================= #
    new_file_name = rename_this_file(new_file_name)
    # ======================================================================= #
    # Ok, the file is already a bit sanitized, but we wish to make it even
    # nicer. So, we rename it based on 1) the name of the group, 2) the
    # name of the song and 3) the release year.
    # ======================================================================= #
    extension_name = File.extname(new_file_name)
    _ = name_of_the_group.tr(' ','_')+'-'+name_of_the_song.tr(' ','_')+
        '_Year_'+release_year+extension_name
    mv(new_file_name, _)
    opnn; e 'The file name now is `'+sfile(_)+'`, in the '\
            'directory `'+sdir(store_here)+'`.'
  } 
end
Also aliased as: download_remote_file
full_dataset?()
Alias for: dataset?
generate_homepage() click to toggle source
#

generate_homepage

This will generate a webpage/homepage, by using the links from the genre.

Invocation example:

eurodance --homepage
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 650
def generate_homepage
  _ = '<html><body>'.dup # This will be the return string.
  _ << '<title>'+guess_the_title_based_on_the_yaml_file+
       ' ('+@dataset.keys.size.to_s+' entries)</title>'
  index = 0
  dataset?.each_pair {|name_of_the_song, array|
    index += 1
    name_of_the_group = array.first
    remote_URL = array[1]
    _ << HtmlTags.a(remote_URL,
      description: '('+index.to_s+') '+
                   name_of_the_song+' - '+
                   name_of_the_group
    )
    _ << "<br>\n"
  }
  _ << '</body></html>'
  # ======================================================================= #
  # We determine into which file we will generate. Since as of October
  # 2020 we will use the general log-directory for the
  # MultimediaParadise project for this.
  # ======================================================================= #
  into = self.class.inspect.to_s.split(':').last.downcase
  into = log_dir?+into+'.html'
  opnn; e "Now storing into the file `#{sfile(into)}`."
  write_what_into(_, into)
end
generate_m3u_playlist() click to toggle source
#

generate_m3u_playlist

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 426
def generate_m3u_playlist
  # ======================================================================= #
  # We only need an Array of all URLs.
  # ======================================================================= #
  dataset = return_all_remote_urls
  into = '/Depot/j/'+return_name_from_class_name
  CreateM3uPlaylist.new(dataset, into)
end
guess_the_title_based_on_the_yaml_file() click to toggle source
#

guess_the_title_based_on_the_yaml_file

This method will try to guess the filename. It may use strings such as:

"/home/Programs/Ruby/2.7.2/lib/ruby/site_ruby/2.7.0/multimedia_paradise/yaml/genres/trance.yml"
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 617
def guess_the_title_based_on_the_yaml_file
  File.basename(@yaml_file).sub(/\.yml$/,'')
end
load_yaml_dataset() click to toggle source
#

load_yaml_dataset

This method will also make @dataset available.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 156
def load_yaml_dataset
  _ = yaml_file?
  begin # Must protect it because the yaml file may be buggy and raise Psych::SyntaxError.
    @dataset = YAML.load_file(_)
  rescue ArgumentError # This clause happens when syck raises an error.
    @dataset = File.read(_)
  rescue Exception => error
    opnn; e 'An error happened on line '+__LINE__.to_s+'.'
    pp error
    pp error.class
  end
  sanitize_yaml_dataset # Must sanitize it.
end
menu(i = commandline_arguments?) click to toggle source
#

menu (menu tag)

#
name_of_the_genre?() click to toggle source
#

name_of_the_genre?

This method will return the genre in use.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 626
def name_of_the_genre?
  _ = self.class.name.to_s
  _ = _.split(':').last if _.include? ':'
  return _.gsub(/The/,'')
end
obtain_url_from_entry_at_this_position(i) click to toggle source
#

obtain_url_from_entry_at_this_position

We obtain (and return) the remote URL from this entry.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 565
def obtain_url_from_entry_at_this_position(i)
  # ======================================================================= #
  # Input must be an Integer. We also must deduct -1 because Ruby Array
  # start to count at 0.
  # ======================================================================= #
  i = i.to_i - 1
  _ = full_dataset?
  keys = _.keys
  url = _[keys[i]][1]
  return url
end
open_playlist_in_your_editor() click to toggle source
#

open_playlist_in_your_editor

Invoke such as by doing:

trance --open
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 145
def open_playlist_in_your_editor
  _ = yaml_file?
  _ = 'bluefish '+_
 esystem(_)
end
opnn(i = NAMESPACE) click to toggle source
#

opnn

#
Calls superclass method MultimediaParadise::Base#opnn
# File lib/multimedia_paradise/audio/genres/genre.rb, line 635
def opnn(i = NAMESPACE)
  super(i)
end
rename_this_file(i) click to toggle source
#

rename_this_file

This will rename the file and sanitize it, then return the new file name.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 469
def rename_this_file(i)
  old_filename = i.to_s.dup
  new_filename = old_filename.delete('(').delete(')').
                 gsub(/ - /,'-').tr(' ','_').delete('!')
  mv(old_filename, new_filename)
  return new_filename
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method MultimediaParadise::Base#reset
# File lib/multimedia_paradise/audio/genres/genre.rb, line 79
def reset
  super()
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = nil
  set_commandline_arguments
  set_namespace
end
return_all_remote_urls() click to toggle source
#

return_all_remote_urls

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 445
def return_all_remote_urls
  dataset = @dataset
  dataset = dataset.map {|key, inner_array|
    inner_array[1] # This is the URL.
  }
  return dataset
end
return_dataset_from_one_random_song() click to toggle source
#

return_dataset_from_one_random_song

This is sort of a debug-method. What we do here is that we will randomly return one element from the main dataset.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 459
def return_dataset_from_one_random_song
  return dataset?.to_a.sample(1).to_h
end
return_name_from_class_name(i = @namespace) click to toggle source
#

return_name_from_class_name

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 554
def return_name_from_class_name(i = @namespace)
  _ = @namespace.to_s.downcase
  _ = _.split(':').last if _.include? ':'
  return _
end
run() click to toggle source
#

run (run tag)

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 681
def run
  menu
end
sanitize_yaml_dataset() click to toggle source
#

sanitize_yaml_dataset

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 173
def sanitize_yaml_dataset
  if @dataset.is_a? String
    # ===================================================================== #
    # Do the manual parsing on your own if it is a String.
    # ===================================================================== #
    _ = @dataset.split(N)
    _.reject! {|entry| entry.start_with? '#' }
    _.reject! {|entry| entry.strip.empty? }
    # ===================================================================== #
    # This is presently unfinished. I may continue working on this if
    # I happen to use a plain textfile rather than a yaml file.
    # ===================================================================== #
  end
end
set_commandline_arguments(i = DEFAULT_FILE) click to toggle source
#

set_commandline_arguments

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 124
def set_commandline_arguments(i = DEFAULT_FILE)
  i = [i] unless i.is_a? Array
  i = [DEFAULT_FILE] if i.compact.empty?
  @commandline_arguments = i
end
set_namespace(i = NAMESPACE) click to toggle source
#

set_namespace

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 133
def set_namespace(i = NAMESPACE)
  @namespace = i
end
set_yaml_file(i) click to toggle source
#

set_yaml_file

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 92
def set_yaml_file(i)
  @yaml_file = i
  # ======================================================================= #
  # === Initialize the yaml dataset
  # ======================================================================= #
  load_yaml_dataset # Must re-initialize @dataset whenever set_input() is called.
end
show_available_songs( use_this_dataset = @dataset, shall_we_exit = :then_exit ) click to toggle source
#

show_available_songs

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 332
def show_available_songs(
    use_this_dataset = @dataset,
    shall_we_exit    = :then_exit
  )
  if use_this_dataset == :then_exit
    use_this_dataset = @dataset
    shall_we_exit = :then_exit
  end
  case shall_we_exit
  when :then_exit
    shall_we_exit = true
  end
  # ======================================================================= #
  # Work with a pointer variable.
  # ======================================================================= #
  _ = use_this_dataset # We assume that this will always be a Hash.
  n_entries = _.keys.size
  opnn; e simp(n_entries.to_s)+' entries were registered in the yaml file.'
  e
  _.each_pair.with_index {|inner_hash, index|
    key, _array = *inner_hash
    url = _array[1]
    index += 1
    group_name = _array[0]
    release_year = _array[2].to_s
    group_and_release_year = ' ('+group_name+', '+release_year+')'
    e slateblue(index.to_s.rjust(3)+') ')+simp(key)+dodgerblue(group_and_release_year)
    # ===================================================================== #
    # Next line displays the URL
    # ===================================================================== #
    left_pad = 7
    e powderblue((' ' * left_pad)+url) # Left-padded URL.
  }; e
  exit if shall_we_exit
end
show_help( shall_we_exit = true, show_opn = false ) click to toggle source
#

show_help (help tag)

To invoke this, try:

eurodance --help
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 585
def show_help(
    shall_we_exit = true,
    show_opn      = false
  )
  if shall_we_exit == :then_exit
    shall_we_exit = true
  end
  e
  opnn if show_opn; e '  --download           # to download all '+name_of_the_genre?+
                      ' songs locally'
  opnn if show_opn; e '  --generate-homepage  # to generate a homepage'
  opnn if show_opn; e '  --m3u                # to generate a .m3u playlist (a file)'
  opnn if show_opn; e '  --open               # to open the respective playlist in your editor'
  opnn if show_opn; e '  --overview           # to show which entries are registered'
  opnn if show_opn; e '  --years              # to sort via years (most recent year will come last)'
  opnn if show_opn; e '  --file?              # to show where the yaml file is stored'
  opnn if show_opn; e '  --reverse-years      # to sort via years in a reverse manner '\
                      '(most recent year will come first)'
  opnn if show_opn; e '  play 17              # open the track at position 17 in the browser'
  opnn if show_opn; e '                       # so that you can play it'
  e
  exit if shall_we_exit
end
show_only_names() click to toggle source
#

show_only_names

To invoke this, do:

eurodance --onlynames
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 402
def show_only_names
  @dataset.each_pair {|key, dataset|
    e slateblue(key.to_s)
  }
end
show_only_the_URLs() click to toggle source
#

show_only_the_URLs

To invoke this, do:

eurodance --only-URLs
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 416
def show_only_the_URLs
  @dataset.each_pair {|key, dataset|
    url = dataset[1]
    e slateblue(url.to_s)
  }
end
show_the_location_of_the_yaml_file() click to toggle source
#

show_the_location_of_the_yaml_file

To invoke this method, try:

eurodance --file?
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 117
def show_the_location_of_the_yaml_file
  e sfile(@yaml_file)
end
sort_playlist_via_years( sort_mode_to_use = :normal ) click to toggle source
#

sort_playlist_via_years

This method sorts the playlist via the years. We can work on the @dataset variable for this to work.

You can try this method like this:

trance --sort-by-years
#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 377
def sort_playlist_via_years(
    sort_mode_to_use = :normal
  )
  if sort_mode_to_use == :normal
    _ = @dataset.sort_by {|key, array|
      value = array[2]
      value = 0 if value.nil? # "Rescue".
      value
    }
  else
    _ = @dataset.sort_by {|key, array|
      -array[2]
    }
  end
  @dataset = Hash[_]
end
yaml_file?() click to toggle source
#

yaml_file?

This will always point at the main yaml file.

#
# File lib/multimedia_paradise/audio/genres/genre.rb, line 105
def yaml_file?
  @yaml_file
end