Group Abstract Group Abstract

Message Boards Message Boards

Playing with YouTube from Mathematica

Posted 7 years ago
POSTED BY: b3m2a1 ​ 
19 Replies
Posted 3 years ago
POSTED BY: julian
POSTED BY: Daniel Bigham
Posted 6 years ago

I'm well aware of that, but mostly I don't care. This was a fun side project for me, but I didn't sink enough time into it to make it worth figuring out some easy way for others to supply their own credentials inside the link. I've done that for my GoogleAPI extension stack in BTools, but the ServiceConnect framework is clunky.

In any case, if you want to use your own API credentials it's actually pretty easy. They're cooked directly into the paclet. Go to YouTube.m in Kernel and swap them out in youtubeclientid and youtubeclientsecret. I'm violating the TOS by directly exposing them in the first place so you'd be doing me a favor.

POSTED BY: b3m2a1 ​ 

Thanks for that info

Thanks, that solved the error.

Now I'm getting a response that contains an error:

{<|"domain" -> "global", "reason" -> "authError", "message" -> "Invalid Credentials", "locationType" -> "header", "location" -> "Authorization"|>}

When I ran:

$so = ServiceConnect["YouTube"]

... a couple of days ago, I followed your above instructions and checked the box for it to remember. When I evaluate $so = ServiceConnect["YouTube"] now, it doesn't launch the web browser but instead immediately returns a ServiceObject[...], that when used, results in the above error.

POSTED BY: Daniel Bigham
Posted 6 years ago
POSTED BY: b3m2a1 ​ 

Did ServiceDisconnect[$so].

Then re-ran ServiceConnect["YouTube"] and got a ServiceObject showing "Connected". (didn't redirect me to the web browser, just evaluated immediately)

Did $so["UploadVideo", ...] and got same failure.

POSTED BY: Daniel Bigham
Posted 6 years ago

If it didn’t redirect it’s not connected. Maybe try ServiceConnect[..., "New"]? Past that I don’t really know what’s wrong. If it continues I’d ask the team in charge of service connect because it is something on their end.

POSTED BY: b3m2a1 ​ 

That did the trick. Thanks, and well done!

POSTED BY: Daniel Bigham

Thank for your reply. Wrapping it in File[...] led to the same result. Is there a particular source file and line that I should go to to inspect how you tried to work around this?

POSTED BY: Daniel Bigham
Posted 6 years ago

Turns out the issue is here: https://github.com/b3m2a1/mathematica-BTools/blob/eef23617d0fc565745cabf79cfb7b38000a5cb8f/Resources/Templates/Frameworks/%24ServiceConnection/Kernel/%24ServiceConnectionFunctions.m#L882

Mathematica doesn't know how to handle MP4 as a "MIMEType", but it looks like I set it up so you can specify your own as an argument, try adding "MIMEType"->the/right/mimetype" as an argument to the function. Alternately overload ImportExport`GetMIMEType to understand MP4

POSTED BY: b3m2a1 ​ 

I was delighted to see that code already exists to upload videos to YouTube from WL, so I gave it a try with an MP4 file, and got this: enter image description here

POSTED BY: Daniel Bigham
Posted 6 years ago

Ah looks like it's trying to treat the "BodyContent" as a file to be exported first... let me think... I'm reasonably sure you can use something like "file" as the upload keyword but I can't remember. Want to check what the "RequestParameters" for this are? The right keyword will be somewhere in there.

Edit:

Nevermind, I have to use the appropriate multipart stuff to upload it but it looks like my check to see if "BodyContent" is a file depends on Mathematica knowing it's a file but Mathematica is still stupid about MP4. Try wrapping it in File and if that fails take a look inside the paclet to see exactly how I tried to work around this.

POSTED BY: b3m2a1 ​ 

"but you can get a link to the actual URL on YouTube where the video will display or use some YouTube downloader with that URL to get the video." Exactly that is what I want. How to do it?

Posted 7 years ago

With a response dataset you can get the ID for the video or list IDs for a list of videos by inspecting the response dataset structure and using the appropriate extractors.

Then you use the request $so["VideoURL", "id"->id] to get the display URL. You can map this over the IDs if you want to get a bunch of URLs.

POSTED BY: b3m2a1 ​ 

Thanks for the instruction. I verified that it works on V11.3 win 10.

enter image description here

POSTED BY: Shenghui Yang

Hats off!! really impressive. Question: isn't there a way simply to get all the urls of your query? This would be quite useful. Even if they can't be played from Mathematica...

Posted 7 years ago

It depends on what you mean by the URLs. You can't download YouTube videos with the API, but you can get a link to the actual URL on YouTube where the video will display or use some YouTube downloader with that URL to get the video.

POSTED BY: b3m2a1 ​ 

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming!

POSTED BY: EDITORIAL BOARD
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard