Help

Re: Get Youtube V=ID Script

2106 3
cancel
Showing results for 
Search instead for 
Did you mean: 
wjb
6 - Interface Innovator
6 - Interface Innovator

Hey all. I’ve got an Automation script seemingly close to get the Youtube Video ID from a full URL which I found here.

Script from link above
<script type="text/javascript">
function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}
</script>

Goal:

I’m trying to take a Youtube URL in an existing field: myInput
or https://www.youtube.com/watch?v=YAIlMUX9VLg

and strip out only YAIlMUX9VLg to pass onto the next step (or output to the field Video ID, doesn’t matter).

I’m feel like I’m so close but at the end of my copy/paste/adapt coding skills. Here’s my code:

let myTable = base.getTable("Video Tracker");
let query = await myTable.selectRecordsAsync();
let myUpdateField = myTable.getField('Video ID');
console.log(query.records);

function youtube_parser(myInput){
    var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
    var match = myInput.match(regExp);
if (match && match[2].length == 11) {
  return match[2];
} else {
  //error
  }
}
var ytOUT = youtube_parser;
output.set('myOutput', ytOUT);

And the error I get that I cannot make heads or tails of:

ERROR

TypeError: Invalid arguments passed to output.set(key, value): • value should be a JSON-serializable type, not a function (undefined)

at main on line 16


Here’s my Edit Script screen overall

image

and the Automation overall if it helps:

image

Thanks for any help.

5 Replies 5

I think this is where the issue is - I think it needs to be:

var ytOUT = youtube_parser(myUpdateField);

Maybe I’m missing something, but I get the sense that you’re missing something that definitively sets the value of the URL that needs to be parsed. I see you open the table and get the records, but where do you decide which record to use in the parse process? I assume you want to run this process for all records, perhaps?

Another thing - to get the value of the youtube ID from the URL, you don’t need the youtube_parser() function; rather, I think this is all you need given the URL is in a variable…

  let myInput = "https://www.youtube.com/watch?v=YAIlMUX9VLg";
  let ytOUT = myInput.split("?v=")[1];
  output.set('myOutput', ytOUT);

Thanks, Bill. My intent of using the youtube_parser script was because it could handle so many variations of the initial URL. Just trying to prevent future human error. I’m wanting the Automation to just always be looking for that Live URL to land to be able to strip the video ID out into the field for that string. I can use this for a bunch of other stuff (Zapier, buttons, edit links). That’s not necessary, obviously.

Types of URLS

These are the types of URLs supported

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&amp;hl=en_US&amp;rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM3nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg

source https://stackoverflow.com/a/8260383

I wondered if I hadn’t selected the right record, thanks. Also, I was missing that youtube_parser(liveURL) bit.

After a bit more guessing and testing, I found this article that helped me revise the input variables I had entered on the left (wasn’t calling those and didn’t know how till I saw the example).

Thanks for pointing me in the right direction. After turning this automation on I’ve realized that it’s not going to process all my existing entries but only new ones :thinking: . Not the end of the world I suppose.

Here’s my working script (for new entries)

let inputConfig = input.config();
var liveURL = inputConfig.myInput;

function youtube_parser(liveURL){
    var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
    var match = liveURL.match(regExp);
if (match && match[2].length == 11) {
  return match[2];
} else {
  //error
  }
}
var ytOUT = youtube_parser(liveURL);
output.set('myOutput', ytOUT);

image

Yep, the variants do require regex.

Just create a Script Block version of the same process and allow it to process all existing records.

Great idea. You’re awesome.