#/bin/bash cd /Volumes/Filestore/Common/playlists/ ### I'm ripping this block of command line arguments from rdplauncher.sh, I'll remove this reminder when this no longer resembles that file. while getopts ":v:b:c:d:p:f:l:q:" options do case "${options}" in v) vnum=${OPTARG};; b) vblk=${OPTARG};; c) cblk=${OPTARG};; d) vdir=${OPTARG};; p) pdir="$OPTARG";; f) vfil="$OPTARG";; l) type="$OPTARG";; ?) printf "Usage %s: \n [-l: Select the playlist type: 1 - Create the TV marathon blocks 2 - Create TV Show Playlist 3 - Create Music Video Playlist ] \n \ [-v number of videos in the playlist (default: 200)] \n [-b how many \ videos before commercials (default 3)] \n [-c how many commercials per \ commercial break (default 2)] \n [-d video directory \ (eg. -d \"/Volumes/Filestore/Common/Music Video/Music Videos/\")] \n [-p \ playlist directory (eg. -p \"/Volumes/Filestore/Common/Music Video/Music \ Videos/Playlists/\")] \n [-f filename (eg. \"sitcoms\")] \ args\n" $0 exit 2;; esac done ### Command Line Variables and default settings. Sure hope this works right. cdef="20" cfgdir="/Volumes/Filestore/Common/playlists/cfg/" ### Holy shit was loading tvshows.txt into an array and then printing the array to search the wrong move! SLOOOOOOOWWWWWW ###### # like 5.5 seconds searching the array vs 0.34 for grepping a textfile mapfile -t tvshows < $cfgdir/tvshows.txt # printf -- '%s\n' "${tvshows[@]}" mapfile -t commercials < $cfgdir/commercials.txt # Search "tvshows.txt" with this printf -- '%s\n' "${tvshows.txt[@]}" | grep Alf || Wings blah blah ### Now we start actually doing work ### ### Create "blocks". So "for all these TV shows, create a marathon of 10 # random episodes". It's like watching TNT in a hotel room and is how I # live now. This is a cheat to just cram in my legacy "blocks" creation # because I don't know how to break that out and fit it in this framework. # I am grafting the arrays and commercials in but I need to handle the # files the way I'm handling the files. # tests - ignore echo "$type" # echo "$vnum" # echo "$pdir" # echo "$vfil" if [ "$type" = "1" ] then echo blocks is what again? $type workdir="/Volumes/Filestore/Common/TV Shows" cd "$workdir" pdir="/Volumes/Filestore/Common/TV Shows/1 - Playlists/Blocks/" # number of videos vnum="50" #play a block of NUM videos before showing a commercial vblk="1" #play NUM commercials per commercial break cblk="1" #vfil is the name of the "block" which will be the name of the .m3u file at the end. while IFS= read -r vfil do echo "line 84 echoing vfile=$vfil" rm "$pdir/$vfil.m3u" mapfile -t vids < <( grep "$workdir/$vfil" $cfgdir/tvshows.txt | grep -v -i -f $cfgdir/excludes | sort -R | head -n $vnum ###Leaving this here as a warning. grepping that text file is 0.34 seconds, printing and searching is 5.5 # printf -- '%s\n' "${tvshows[@]}" | grep "$workdir/$vfil" | grep -v -i -f $cfgdir/excludes | sort -R | head -n $vnum ) mapfile -t vids < <(shuf -e "${vids[@]}") vmin="0" mapfile -t comm < <( grep Commercials $cfgdir/commercials.txt | sort -R) cmin="0" i=1 while [ "$i" -le "$vnum" ] do # echo $vmin # echo $vfil ### I had a need to check to see if there were any videos in the current array # That proved to cause problems when arrays had fewer than "vnum" videos in them # I am leaving this here 'just in case' I need to refer to it sometime # empty=${vids[@]:$vmin} # if [ -z "$empty" ] # then # echo "vids is empty" # exit 0 # fi ### ### Create arrays based on how many videos we want to show between commercials # and for the number of commercials we want to show during a commercial break vid=$(printf '%s\n' "${vids[@]:$vmin:$vblk}") com=$(printf '%s\n' "${comm[@]:$cmin:$cblk}") ### echo these to the screen if you are troubleshooting by hand # echo "$vid" # echo "$com" ### Here is where I try to figure out if $vid is part 1 of multipart series and then add the rest in line # I think next steps are to break out the "$vid" array into parts and then put it back together in the right order # I also should chnge my loop into a "while match = 1" type thing so I can loop through and pick up episode 3, 4, etc. # Do that by setting match=0 and then match=1 when a match is found. When we complete the "find next ep" loop and # come up empty, set match=0 and it should move on to the next file ptval="" # ptval is "which part of a show is this". [pt 1] will create a ptval of 1, [pt 2] is a ptval of 2. nxt="" # If ptval is not null, then nxt is set to the value of the "Next" video after the one we're looking at. sval="" # sval will be ptval + 1 nxtval="" #nxtvid="" nxtvid=() prvval="" # idea here is gonna be if "ptval" is a number greater than 1, check and see # if ptmatch="1". That tells us if this is going to be an "episode 2" or higher. # If both ptmatch=0 and ptval is > 1 then set ptval to 1 and let it complete the cycle # until the next file is not a match. Then set match=0 and exit. ptmatch="0" ptval=$(echo "$vid" | awk -F "\\\[pt " '{print $2}' | sed 's/\].*//') # if [ "$ptval" -gt "0" ] if [ -n "$ptval" ] then echo "ptval is $ptval before the loop starts" ###I think I might want to use ptval, we'll see. # # if [ "$ptval" -gt "0" ] && [ "$ptmatch" -eq "0" ] # then # $ptval="0" # fi # Now we enter the loop for the first time with ptmatch=1 and ptval=0. # Now as long as we keep matching new episodes, we will loop through # more $ptvals. while [ $ptmatch -le "1" ] do echo "$ptval" if [ "$ptval" -eq "1" ] then echo "This one is part 1" vfsrch=$( echo "$vid" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' ) # nxt=$(find "$workdir/$fil" -type f | grep -A 1 "$vfsrch" | tail -n 1) nxt=$( grep -A 1 "$vfsrch" $cfgdir/tvshows.txt ) sval=$((ptval+1)) echo "sval = $sval" nxtval=$(echo "$nxt" | grep "pt $sval") echo "nxtval = $nxtval" fi if [ "$ptval" -eq "2" ] then echo "This one is part 2" vfsrch=$( echo "$vid" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' ) # nxt=$(find "$workdir/$fil" -type f | grep -A 1 "$vfsrch" | tail -n 1) nxt=$( grep -B 1 "$vfsrch" $cfgdir/tvshows.txt ) sval=$((ptval-1)) echo "sval = $sval" prvval=$(echo "$nxt" | grep "pt $sval") echo "nxtval = $nxtval" fi ptmatch="2" done fi echo "$prvval" >> "$pdir/$vfil.m3u" echo "$vid" >> "$pdir/$vfil.m3u" echo "$nxtval" >> "$pdir/$vfil.m3u" echo "$com" >> "$pdir/$vfil.m3u" vmin=$(( "$vmin" + "$vblk" )) cmin=$(( "$cmin" + "$cblk" )) # ((i++)) #I want to know what this does since I"m getting weird output at the end of my blocks. #I think it needs to be above the ((i++)) which is currently above this comment if [ "$i" = "$vnum" ] then # vmin="0" # cmin="0" bob="1" #exit fi i=$(($i + $vblk)) echo "$i" done # Below is for testing otherwise leave commented # echo "$vmin" # echo "$cmin" # echo "$i" vmin="0" cmin="0" i="1" done < <( cat $cfgdir/sitcoms.txt; cat $cfgdir/buddyblocks.txt) echo "1 and done" exit 0 fi if [ "$type" = "2" ] then # Setting Defaults - These can be overriden in $cfgdir/$vfil.cfg workdir="/Volumes/Filestore/Common/TV Shows" commdir="/Volumes/Filestore/Common/Commercials/" vnum="50" vblk="2" cblk="2" pdir="/Volumes/Filestore/Common/TV Shows/1 - Playlists/" cfgdir="/Volumes/Filestore/Common/playlists/cfg/" . "$cfgdir/$vfil.cfg" echo "vfil is $vfil" echo "plistname is $plistname" echo "cfgdir is $cfgdir" ### I am thinking relative directories might be a mistake # just hardcoding them will benefit me as I stamp out the # same FS everywhere (/Volumes/Filestore/Common/...). # # Also I think absolute paths "match" better in the Kodi # database so it looks better browsing a playlist # # reldir="../" # creldir="../../" cd "$workdir" rm "$pdir/$plistname.m3u" ### commented for testing # while IFS= read -r fil # do # # echo "$fil" # rm "$pdir/$fil.m3u" # mapfile -t vids < <( # printf "$vids" # find "$workdir/$fil" -type f | grep -v -i -f ./excludes | sort -R # ) # done < <( cat .$vfil.txt) ### This now takes 14 seconds to create the nickatnite playlist, down from 40! ### vids=() sed "s/^/\/Volumes\/Filestore\/Common\/TV Shows\//" $cfgdir/$vfil.txt > $cfgdir/$vfil.txt.bob mapfile -t vids < <(grep -f $cfgdir/$vfil.txt.bob $cfgdir/tvshows.txt | grep -v -i -f $cfgdir/excludes | sort -R | head -n $vnum) #### The below ran a nickatnite playlist in 40 seconds by iterating searches with the grep workdir/fil thing. The above should be much faster. # while IFS= read -r fil # do # # while IFS='' read -r entry # do # vids+=( "$entry" ) # # ###Leaving this here as a warning. grepping that text file is 0.34 seconds, printing and searching is 5.5 # # done < <( printf -- '%s\n' "${tvshows[@]}" | grep "$workdir/$fil" | grep -v -i -f $cfgdir/excludes | sort -R) # # ### I think it might be faster still to do this with grep reading from a file like the Excludes list does # # the nickatnite test takes 40 secons with this method # done < <( grep "$workdir/$fil" $cfgdir/tvshows.txt | grep -v -i -f $cfgdir/excludes | sort -R) # | for if we want relative paths which I don't | awk -F "/Volumes/Filestore/Common/TV Shows/" '{print $2}') # # done < <( cat $cfgdir/$vfil.txt) mapfile -t vids < <(shuf -e "${vids[@]}") vmin="0" mapfile -t comm < <( grep "$commdir" $cfgdir/commercials.txt | grep -v -i -f $cfgdir/excludes | sort -R) # | for if we want relative paths which I don't | awk -F "/Volumes/Filestore/Common/" '{print $2}') cmin="0" i=1 while [ "$i" -le "$vnum" ] do # echo $vmin # echo $fil ### I had a need to check to see if there were any videos in the current array # That proved to cause problems when arrays had fewer than "vnum" videos in them # I am leaving this here 'just in case' I need to refer to it sometime # empty=${vids[@]:$vmin} # if [ -z "$empty" ] # then # echo "vids is empty" # exit 0 # fi ### vid=$(printf '%s\n' "${vids[@]:$vmin:$vblk}") com=$(printf '%s\n' "${comm[@]:$cmin:$cblk}") ### Here is where I try to figure out if $vid is part 1 of multipart series and then add the rest in line # I think next steps are to break out the "$vid" array into parts and then put it back together in the right order # I also should chnge my loop into a "while match = 1" type thing so I can loop through and pick up episode 3, 4, etc. # Do that by setting match=0 and then match=1 when a match is found. When we complete the "find next ep" loop and # come up empty, set match=0 and it should move on to the next file ptval="" nxt="" sval="" nxtval="" #nxtvid="" nxtvid=() prvval="" echo "testing - vid = $vid" # idea here is gonna be if "ptval" is a number greater than 1, check and see # if ptmatch="1". That tells us if this is going to be an "episode 2" or higher. # If both ptmatch=0 and ptval is > 1 then set ptval to 1 and let it complete the cycle # until the next file is not a match. Then set match=0 and exit. ptmatch="0" ptval=$(echo "$vid" | awk -F "\\\[pt " '{print $2}' | sed 's/\].*//') # if [ "$ptval" -gt "0" ] if [ -n "$ptval" ] then echo "ptval=$ptval before the loop starts" ###I think I might want to use ptval, we'll see. # # if [ "$ptval" -gt "0" ] && [ "$ptmatch" -eq "0" ] # then # $ptval="0" # fi # Now we enter the loop for the first time with ptmatch=1 and ptval=0. # Now as long as we keep matching new episodes, we will loop through # more $ptvals. while [ $ptmatch -le "1" ] do echo "$ptval" if [ "$ptval" -eq "1" ] then echo "This one is part 1" vfsrch=$( echo "$vid" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' ) # nxt=$(find "$workdir/$fil" -type f | grep -A 1 "$vfsrch" | tail -n 1) nxt=$( grep -A 1 "$vfsrch" $cfgdir/tvshows.txt ) sval=$((ptval+1)) echo "sval = $sval" nxtval=$(echo "$nxt" | grep "pt $sval") echo "nxtval = $nxtval" fi if [ "$ptval" -eq "2" ] then echo "This one is part 2" vfsrch=$( echo "$vid" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' ) # nxt=$(find "$workdir/$fil" -type f | grep -A 1 "$vfsrch" | tail -n 1) nxt=$( grep -B 1 "$vfsrch" $cfgdir/tvshows.txt ) sval=$((ptval-1)) echo "sval = $sval" prvval=$(echo "$nxt" | grep "pt $sval") echo "nxtval = $nxtval" fi ptmatch="2" ptval="" done fi ### There ends the experiment to see if I can add multipart shows together ### echo these to the screen if you are troubleshooting by hand echo "$vid" echo "$com" echo "$prvval" >> "$pdir/$plistname.m3u" echo "$vid" >> "$pdir/$plistname.m3u" # echo "$nxtvid" >> "$pdir/$plistname.m3u" echo "$nxtval" >> "$pdir/$plistname.m3u" printf "%s\n" "${nxtvid[@]}" echo "$com" >> "$pdir/$plistname.m3u" nxtvid=() vmin=$(( "$vmin" + "$vblk" )) cmin=$(( "$cmin" + "$cblk" )) i=$(($i + $vblk)) if [ "$i" = "$vnum" ] then vmin="0" cmin="0" fi done echo "$vmin" echo "$cmin" echo "$i" vmin="0" cmin="0" i="1" # done < <( cat .$vfil.txt) echo "1 and done" exit 0 fi