Sgt.Doakes
Sgt.Doakes

Reputation: 117

Want to launch youtube vids from a list using random number

I made this code to play random vids read from a playlist file (simple text file, with different link in each line). This is my second attempt. Please don't laugh at me, because the first attempt was working! So the effect of the script is an empty chrome window, just like a new tab. I've no clue why is this not working.

#!/bin/bash
#initializing the file and current time in millis
file="youtube.songs"
ct=`date +%s`
#counting the lines in the list file
num=`wc -l $file | cut -f1 -d' '`
rem=$(( $ct % ( $num - 1 ) ))
ln=$(( $rem + 1 ))
#geting the url by line number
url=`cat $file | head -n $ln | tail -n 1 `
google-chrome --incognito $url

My first attempt (which was working, but I was looking forward to challenge myself) looked sg. like this:

ct=`date +%s`
rem=$(( $ct % 22 ))
case $rem in
1)
  url="https://www.youtube.com"
;;
*)
;;

I've tried both of @shellter's advice:

#initializing the file and current time in millis
file="youtube.songs"
+ file=youtube.songs
ct=$( date +%s )
 date +%s )
 date +%s 
++ date +%s
+ ct=1409239606
#counting the lines in the list file
num=$( wc -l $file | cut -f1 -d' ' )
 wc -l $file | cut -f1 -d' ' )
 wc -l $file | cut -f1 -d' ' 
++ wc -l youtube.songs
++ cut -f1 '-d '
+ num=25
num=$(( $num - 1 ))
+ num=24
rem=$(( $ct % $num ))
+ rem=22
ln=$(( $rem + 1 ))
+ ln=23
#geting the url by line number
url=$( cat $file | head -n $ln | tail -n 1 )
 cat $file | head -n $ln | tail -n 1 )
 cat $file | head -n $ln | tail -n 1 
++ head -n 23
++ cat youtube.songs
++ tail -n 1
+ url='https://www.youtube.com/watch?v=DmeUuoxyt_E'
google-chrome --incognito $url
+ google-chrome --incognito 'https://www.youtube.com/watch?v=DmeUuoxyt_E'

So there is an issue with the variable substitution. I've tried $(echo $url) instead of $url, but got the same results. So I am clueless.

Upvotes: 0

Views: 134

Answers (1)

zerodiff
zerodiff

Reputation: 1700

What I usually do in this circumstance is try several commands and make sure that's working. For example, you could run this and see what the result is (subsituting urlfile with your file):

head -n $(( $( date +%s ) % $( wc -l < urlfile ) + 1 )) < urlfile | tail -1

This should pick random(ish) lines from your file. So, I'd get this running without calling google-chrome to actually try to open the URL. This uses sed and uses $RANDOM instead of the head/tail and date:

sed "$(( $RANDOM % $( wc -l < urlfile ) + 1 ))"'!d' < urlfile

Once you have that working, try passing the URL to the google-chrome command on the command line to test what this does. I hope this helps.

Upvotes: 1

Related Questions