0

I want to develop an Applescript to extract youtube live stream video ID using the Channel ID.

Currently, I'm doing it manually as Youtube changes live stream video ID time to time. For an example, I'm practising following;

  1. Open https://www.youtube.com/embed/live_stream?channel=UCu7cGbQEMgGk8TD0ZYucM5g
  2. Right-click on the player and select "Copy video URL"
  3. This URL is used to an Applescript I wrote to automate Livestreamer. (This script is given below.)

As Youtube changes this URL time to time, following script has to be changed the time to time. My intention is to automate the whole process.

-- Shraddha TV and Radio Recorder --
-- Developed by Anoma --
set pathToShraddha to ((path to downloads folder as text) & "Shraddha:")
set outputExtension to ""
set ls to ""
set sourceURL to ""
set con to ""
set windowInfo to ""
set theTime to ""
set endTime to ""

display dialog "Shraddha TV or Radio" buttons {"TV", "Radio", "Cancel"} default button 1
if result = {button returned:"TV"} then
  set outputExtension to ".ts"
  set sourceURL to "https://www.youtube.com/watch?v=1yv7JjMP4Dw"
  set ls to "livestreamer"
  set con to "480p -o"
else if result = {button returned:"Radio"} then
  set outputExtension to ".mp3"
  set sourceURL to "http://92.222.236.128:8006"
  set ls to "ffmpeg -i"
  set con to "-c copy"
else
return
end if

set fn to (setFileName(outputExtension))

display dialog "Record now or later?" buttons {"Now", "Later", "Cancel"} default button 1
if result = {button returned:"Now"} then
    set endTime to text returned of (display dialog "Please set the time to end recording. (Leave 'hhmm' if you want to record continuously.) " with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)
    set windowInfo to recordMedia(ls, sourceURL, con, (POSIX path of pathToShraddha as string), fn)
    finishTime(endTime, windowInfo)
 else if result = {button returned:"Later"} then
    -- get time to be set---
    set theTime to text returned of (display dialog "Please set the time to start recording." with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)
 if ((theTime as string) is equal to "hhmm") then
    display dialog "Time is not set correctly"
    return
 end if
 set endTime to text returned of (display dialog "Please set the time to end recording. (Leave 'hhmm' if you want to record continuously.) " with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)
 display dialog (getTimeInHoursAndMinutes())
 display dialog theTime

 set i to 0
 repeat while (getTimeInHoursAndMinutes()) is less than theTime
   if (i = 0) then
     set i to (i + 1)
     recordMedia("", "", "", "", "")
   end if
 delay 60
 end repeat
 finishTime(endTime, (recordMedia(ls, sourceURL, con, (POSIX path of pathToShraddha as string), fn)))
  else
  return
end if

-- This method generates the file name 
on setFileName(outputExt)
set fileName to do shell script "date +'%Y-%m-%d_%H-%M-%S'"
set outputExt to the outputExt as string
set fileName to ("STV_" & fileName as string) & outputExt
return fileName as string
end setFileName

-- This method gives the current time in "hhmm" format (24hr) 
on getTimeInHoursAndMinutes()
set timeStr to time string of (current date)
set hrStr to (characters 1 thru -10 of timeStr as string)
if ((count hrStr) is less than 2) then
   set timeStr to ((0 & timeStr) as string)
end if
set ampm to (characters -2 thru -1 of timeStr as string)
if ((ampm as string) is equal to "PM") then
 if ((hrStr as integer) is less than 12) then
    set hrStr to (((hrStr as integer) + 12) as string)
 end if
else
 if ((hrStr as integer) = 12) then
    set hrStr to (0 as string)
 end if
 if ((count hrStr) is less than 2) then
    set hrStr to ((0 & hrStr) as string)
 end if
end if
set mStr to (characters 4 thru 5 of timeStr as string)
set timeStr to (hrStr) & (mStr)
return timeStr as string
end getTimeInHoursAndMinutes

-- This method Record the stream --
on recordMedia(ls, sourceURL, con, pathToShraddhaString, fn)
tell application "Terminal"
set windowInfo to do script "caffeinate -i " & ls & space & sourceURL & space & con & space & pathToShraddhaString & fn
activate of windowInfo
end tell
return windowInfo
end recordMedia

-- This method end recording --
on finishTime(endTime, windowInfo)
if ((endTime as string) is equal to "hhmm") then
else
repeat while (getTimeInHoursAndMinutes()) is less than endTime
    delay 60
end repeat
tell application "Terminal"
    -- reopen
    activate of windowInfo
    --tell application "System Events" to keystroke "q"
    tell application "System Events" to keystroke "c" using {control down}
end tell
end if
end finishTime

Could you please help me in developing the script to automate the extraction of the Livestream URL every time I run the script?

Thank you.

2
  • We are not a script debugging service. You need to tell us what is wrong with this script. What error is it giving you? Where is the error?
    – DavidPostill
    Oct 7, 2017 at 19:39
  • Sorry for the confusion. The script works really fine. No need to debug it. The only issue is that Youtube changes Livestream ID time to time. Here, as I have hardcoded the Livestream URL in the script, I also have to change the URL of the Livestream in the script time to time. Therefore, I wanted a way to extract Livestream ID using theYoutube Channel ID (which is static). Then I can edit the above script to extract the Livestream ID and auto-generate the URL every time I run the script. I posted the script just as I did not want to hide my work and get your help. Hope it is now clear. Thanks
    – Anoma
    Oct 8, 2017 at 4:04

2 Answers 2

0

Somthing like this?

Property LivestreamURL : {}
-- set youtube_channel to choose URL -- just remove comment tag to choose this option
   set youtube_channel to "https://www.youtube.com/embed/live_stream?channel=UCu7cGbQEMgGk8TD0ZYucM5g"
tell application "Safari"
   launch  -- background OR use 'activate' if preferred    
   open location youtube_channel
end tell
-- wait for Safari to  load the webpage
   if load_webpage(20) is false then return

tell application "Safari" to set end of LivestreamURL to do JavaScript "document.links[0].href" in document 1
   set this_ID to item 1 of LivestreamURL
   set channel_ID to (characters 51 thru -1 of youtube_channel) as string
   set video_ID to (characters 33 thru -1 of this_ID) as string

-- return LivestreamURL -- or;
   return return & "Channel ID : " & channel_ID & linefeed & "Video ID     : " & video_ID & linefeed & linefeed

-- This is the Handler 'load_webpage'
    on load_webpage(timeout_variable)
        delay 2
        repeat with i from 1 to the timeout_variable
            tell application "Safari"
                if (do JavaScript "document.readyState" in document 1) is "complete" then
                    return true
                else if i is the timeout_variable then
                    return false
                else
                    delay 1
                end if
            end tell
        end repeat
        return false
    end load_webpage
2
  • Is this a question or an answer? Oct 13, 2017 at 17:09
  • @ Capitainos, This is what I was looking for. Thank you very much. I'll update my script once I integrate this with it.
    – Anoma
    Oct 16, 2017 at 7:35
0

Following is the updated Apple Script (Thanks to Capitainos). This works really fine.

-- Shraddha TV and Radio Recorder --
-- Created by Dr Anoma Jayaratne --
set pathToShraddha to ((path to downloads folder as text) & "Shraddha:")
set outputExtension to ""
set ls to ""
set sourceURL to ""
set con to ""
set windowInfo to ""
set theTime to ""
set endTime to ""
set this_ID to ""
set tor to ""
property LivestreamURL : {}
set youtube_channel to "https://www.youtube.com/embed/live_stream?channel=UCu7cGbQEMgGk8TD0ZYucM5g"

display dialog "Shraddha TV or Radio" buttons {"TV", "Radio", "Cancel"} default button 1
if result = {button returned:"TV"} then
   set outputExtension to ".ts"
   set ls to "livestreamer"
   set con to "best -o"
   set tor to "TV"
else if result = {button returned:"Radio"} then
   set outputExtension to ".mp3"
   set ls to "ffmpeg -i"
   set con to "-c copy"
   set tor to "Radio"
else
   return
end if

set fn to (setFileName(outputExtension))

display dialog "Record now or later?" buttons {"Now", "Later", "Cancel"} default button 1
if result = {button returned:"Now"} then
    set endTime to text returned of (display dialog "Please set the time to end recording. (Leave 'hhmm' if you want to record continuously.) " with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)

if ((tor as string) is equal to "TV") then
    set video_ID to (get_video_ID(youtube_channel))
    set sourceURL to "https://www.youtube.com/watch?v=" & video_ID
else
    set sourceURL to "http://92.222.236.128:8006"
end if

set windowInfo to recordMedia(ls, sourceURL, con, (POSIX path of pathToShraddha as string), fn)
finishTime(endTime, windowInfo)

else if result = {button returned:"Later"} then
-- get time to be set---
set theTime to text returned of (display dialog "Please set the time to start recording." with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)

if ((theTime as string) is equal to "hhmm") then
    display dialog "Time is not set correctly"
    return
end if

set endTime to text returned of (display dialog "Please set the time to end recording. (Leave 'hhmm' if you want to record continuously.) " with title "Timer" default answer "hhmm" buttons {"Cancel", "Set"} default button 2)

set i to 0
repeat while (getTimeInHoursAndMinutes()) is less than theTime
    if (i = 0) then
        set i to (i + 1)
        recordMedia("", "", "", "", "")
    end if
    delay 60
end repeat

if ((tor as string) is equal to "TV") then
    set video_ID to (get_video_ID(youtube_channel))
    set sourceURL to "https://www.youtube.com/watch?v=" & video_ID
else
    set sourceURL to "http://92.222.236.128:8006"
end if

finishTime(endTime, (recordMedia(ls, sourceURL, con, (POSIX path of pathToShraddha as string), fn)))

else
   return
end if

-- This method generates the file name
on setFileName(outputExt)
   set fileName to do shell script "date +'%Y-%m-%d_%H-%M-%S'"
   set outputExt to the outputExt as string
   set fileName to ("STV_" & fileName as string) & outputExt
return fileName as string
end setFileName

-- This method gives the current time in "hhmm" format (24hr)
on getTimeInHoursAndMinutes()
   set timeStr to time string of (current date)
   set hrStr to (characters 1 thru -10 of timeStr as string)
   if ((count hrStr) is less than 2) then
      set timeStr to ((0 & timeStr) as string)
   end if
   set ampm to (characters -2 thru -1 of timeStr as string)
   if ((ampm as string) is equal to "PM") then
       if ((hrStr as integer) is less than 12) then
           set hrStr to (((hrStr as integer) + 12) as string)
       end if
   else
       if ((hrStr as integer) = 12) then
           set hrStr to (0 as string)
       end if
       if ((count hrStr) is less than 2) then
           set hrStr to ((0 & hrStr) as string)
       end if
   end if
set mStr to (characters 4 thru 5 of timeStr as string)
set timeStr to (hrStr) & (mStr)
return timeStr as string
end getTimeInHoursAndMinutes

-- This method Record the stream --
on recordMedia(ls, sourceURL, con, pathToShraddhaString, fn)
tell application "Terminal"
    --reopen
    --activate
    set windowInfo to do script "caffeinate -i " & ls & space & sourceURL & space & con & space & pathToShraddhaString & fn
    activate of windowInfo
end tell
return windowInfo
end recordMedia

-- This method end recording --
on finishTime(endTime, windowInfo)
   if ((endTime as string) is equal to "hhmm") then
   else
    repeat while (getTimeInHoursAndMinutes()) is less than endTime
        delay 60
    end repeat
    tell application "Terminal"
        -- reopen
        activate of windowInfo
        --tell application "System Events" to keystroke "q"
        tell application "System Events" to keystroke "c" using {control down}
    end tell
  end if
end finishTime

-- This method extracts the Youtube Livestream video ID from Youtube Channel ID
on get_video_ID(youtube_channel)
   tell application "Safari"
     run
     open location youtube_channel
end tell

if load_webpage(20) is false then return

tell application "Safari" to set end of LivestreamURL to do JavaScript "document.links[0].href" in document 1
set this_ID to item 1 of LivestreamURL
set Live_video_ID to (characters 33 thru -1 of this_ID) as string
return Live_video_ID
end get_video_ID

-- This method check errors for js running in the web page
on load_webpage(timeout_variable)
   delay 2
repeat with i from 1 to the timeout_variable
    tell application "Safari"
        if (do JavaScript "document.readyState" in document 1) is "complete" then
            return true
        else if i is the timeout_variable then
            return false
        else
            delay 1
        end if
    end tell
 end repeat
return false
end load_webpage

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .