#!/opt/local/bin/perl

# download and process an array of arrays of artist playlists.
# in other words, the discographies of all the artists in a genre - 
# or any set of artists you want.
# google search parameters: 
#  " albumName " "inurl:playlist" ["artistName - topic"] [+inurl:("/channel/")] site:youtube.com

# 4-dimensional array for defining the collection of artists. 
# bash doesn't support lists, objects, or multidimensional arrays, 
# so I implemented this in perl.
# the scalar function gives the true length of the array; the $#AoA expression gives the last index of the array.
# scalar won't evaluate properly inside a print string, but it will evaluate properly in a loop.

# how to dynamically reference the albums collection for an artist
# https://stackoverflow.com/questions/16793937/size-of-2d-array-in-perl
#  ?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

# this line is critical; the arrays won't work without it

use 5.010;

# the array.
@AoA =
(
  [ "Lemonheads", 
    [
      ["HateYourFriends", "https://www.youtube.com/watch?v=SwNELIKMW-o&list=OLAK5uy_lJ_OqRufH9FHbW_8Agj4ZGtaEmLFcvkPw"],
      ["Creator", "https://www.youtube.com/watch?v=8QaMxFpQafE&list=OLAK5uy_nMKVNRxL9vmiKWyFZNAkxhG8YpbqeqwG8"],
      ["Lick", "https://www.youtube.com/watch?v=T2LvxoLmV4o&list=OLAK5uy_muE3hpTTLEAgT6TFFYihMi6jBxPP7uBJI"],
      ["Lovey", "https://www.youtube.com/watch?v=oNu5rLp-WUI&list=OLAK5uy_lZFfzNsNYoupCOyrmk_UVoisW4nhPn5Lw"],
      ["ItsAshameAboutRay", "https://www.youtube.com/watch?v=cERRATGT6UE&list=OLAK5uy_nBhEpljPROdBThFfvHHbmORaLPhgGFYkU"],
      ["ComeOnFeelTheLemonheads", "https://www.youtube.com/watch?v=Dw5J3Ws8VPo&list=OLAK5uy_l3vriqJVAxxwlTDE94gQFgqLI3t859BnQ"],
      ["CarButtonCloth", "https://www.youtube.com/watch?v=8HaZtHKVgao&list=OLAK5uy_lgECz33kzRx_cymM_EzuaYJafbCBMKI78"],
      ["TheLemonheads", "https://www.youtube.com/watch?v=xUH3a9SFDKI&list=OLAK5uy_khCCQt5lj-VU2Xxf0BHmnndH_GrVSW-KM"],
      ["VarshonsI", "https://www.youtube.com/watch?v=dv2TE4KRYBw&list=OLAK5uy_lfE5DNAPkGykUCE7Hp3fXNf-G7FWZX0dc"],
      ["VarshonsII", "https://www.youtube.com/watch?v=h7cMeu8QHvg&list=OLAK5uy_mcPyLTc51owZTKQ5PqtQtSYznyW2yvXHY"],
      ["FearOfLiving", "https://www.youtube.com/watch?v=jMqUJ0eu0rs&list=OLAK5uy_k1qo4cfMymenp7Egdss5Y-xQTpg-5tqAc"],
      ["IfOnlyYouWereDead", "https://www.youtube.com/watch?v=jTG52T84En8&list=OLAK5uy_mmFLTTouUQA53iMP02I8Q8EYe61zPNZVg"],
      ["TheHotelSessions", "https://www.youtube.com/watch?v=xUH3a9SFDKI&list=OLAK5uy_khCCQt5lj-VU2Xxf0BHmnndH_GrVSW-KM"]
    ]
  ]
  ,
  [ "HuskerDu", 
    [
      ["LandSpeedRecord", "https://www.youtube.com/watch?v=eEKH1jjrpLo&list=OLAK5uy_lfaVC2_zQ4-sKdBmeqeF9n9ZYd6Ka8avs"], 
      ["EverythingFallsApart", "https://www.youtube.com/watch?v=M60Q80MAGeQ&list=OLAK5uy_ldGluZGYKWZqIrD9ayeYKN1YLem92JZxE"],
      ["ZenArcade", "https://www.youtube.com/watch?v=EQfrHkjvx6g&list=OLAK5uy_mzlf4vXNGcLEeb93rlXuSQQXtV2GqVTiA"], 
      ["NewDayRising", "https://www.youtube.com/watch?v=1YTJ807KK7I&list=OLAK5uy_m5LIe6kFZEplo7Wl0uEFK-WG82T6OV2bI"], 
      ["FlipYourWig", "https://www.youtube.com/watch?v=OWq_XWXRdV8&list=OLAK5uy_nDOWftfSQMp184-XD7wnmdJlyqxwnYmRk"],
      ["CandyAppleGrey", "https://www.youtube.com/watch?v=P0cAWxZzF4E&list=OLAK5uy_mgAZNWea3Df5fXmy_V2Yw-ENmMnyGTTsQ"], 
      ["Warehouse_SongsAndStories", "https://www.youtube.com/watch?v=JCgmTLdQoSU&list=OLAK5uy_k1PJt2XdJDokuvlEzq5AmoqPH5ZJyBkeA"],
      ["TheLivingEnd", "https://www.youtube.com/watch?v=yV-R7QnFDqI&list=OLAK5uy_nKP--ZHe7ApSiq4J5fI8TTO6lrQJLWyyI"],
      ["Euphoria", "https://www.youtube.com/watch?v=ZijW_FM50i0&list=OLAK5uy_muxaJL0E6k5GMT7RurVTEHjiIQ_WuSpIk"],
      ["TonightLonghorn", "https://www.youtube.com/watch?v=m3EvScFqAJA&list=OLAK5uy_kWZPvoCt74pMEWj2FiQConEf9H6bnosvQ"]
    ]
  ]
);
  
# the length of the array.
$artistCount = scalar(@AoA);

# artists collection loop
for ($l=0; $l<=scalar(@AoA)-1; $l++)
{
  # create the artists' directory and cd into it
  mkdir $AoA[$l][0];
  chdir $AoA[$l][0];
  # loop through the artist's discography
  for ($k=0; $k<=scalar(@{$AoA[$l][1]})-1; $k++)
  {
    # create a directory for the album and cd into it
    mkdir $AoA[$l][1][$k][0];
    chdir $AoA[$l][1][$k][0];
    # download the songs for the album
    system("/Library/Frameworks/Python.framework/Versions/3.9/bin/yt-dlp", "--no-check-certificate", "-ci", "$AoA[$l][1][$k][1]") == 0;
    
    # process each song for the album. First, convert to mp3 44x128.
    # then, normalize the volume.
    @files = glob("*.mkv *.mp4 *.webm");
		  foreach $file (@files)
		  {
      # if the file exists
      if ( -f $file )
			   {
        # remove its extension (sub supports multiple periods)
        $thefilename = remove_extension($file);
				    # build the ffmpeg - sox command line
        $thepipingcommand = "ffmpeg -i '$file' -hide_banner -ar 44100 -b:a 128k '$thefilename.mp3'";
				    # run it then delete the video file
        system($thepipingcommand) == 0;
        unlink($file);
			   }
		  }
    # cd out and move on to the next album
    chdir "..";
  }
  # cd out and move on to the next artist
  chdir "..";
}

# sub (void function) to properly remove a filespecs' extension
sub remove_extension
{
  my $filename = shift @_;
$filename =~ s/
						 (.)             # matches any character
						 \.              # the literal dot starting an extension
						 [^.]+           # one or more NON-dots
						 $               # end of the string
						 /$1/x;
  return $filename;
}
