1 Replies - 2427 Views - Last Post: 26 May 2012 - 10:44 AM Rate Topic: -----

#1 russelluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-April 12

How to get a string from javascript variable with python?

Posted 26 May 2012 - 08:55 AM

I've been writing a small program as a project to download and manage a library of videos from Khan Academy to watch offline for slow connections.

The program uses pythons urllib to get page sources, and to download the video will use another site keepvid.com which provides a list of direct download links for the video in different formats using the youtube url of the video. So if you paste the following string...

"http://keepvid.com/?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D" plus the youtube id "kpCJyQ2usJ4"

...of any youtube video in the address bar of your browser is should take you directly to the page to download the video(in this case a video about algebra) bypassing the steps of pasting the youtube link and click download, etc. So using the urllib to get the page source prevents from having to use the browser and I've managed to build the entire library of subjects,topic,subtopic,video titles and youtube id's from just page sources. The problem is the final download link which is the final prize is stored in some javascript variable in the page source and I have no way of getting at it that I know of except for making the program open up a browser and paste the link in there so the browser can run the client side script of keepvid, then it can right-click and copy link or something like that which would be a waste of time and very clumsy(I know because I did that before with another script).

So how do I get that string from the javascript variable but using python from the page source, if that's even possible?

This is the javascript on the page source after pasting that link above. It's long so I put it on pastebin instead:

http://pastebin.com/YCiwCAsY

In there I think it's the parameter kv_url used in the kv_info and kv_ds functions which contains the string.

The entire page source:

http://pastebin.com/CFkTq5Mf

A small part of the script which I think is where it is:

function kv_ds(a_id, kv_type, kv_res, kv_url, kv_title){
var kv_dri='';var kv_more='';
if(kv_url.match("veoh.com") || kv_url.match("zshare.net")) kv_more=' (Drag download link to a new tab, then press <b>Ctrl+S</b> if it starts playing in the browser)';
if(kv_url.match("youtube.com") == null && (kv_type == "MP3" || kv_type == "MP4")){
kv_dri='javascript:alert(\'Right-Click the download link and choose \\\'Save Link As...\\\'\');return false;';
//kv_more=' (<b>Right-Click</b> the download link and choose <b>Save Link As...</b>)';
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to get a string from javascript variable with python?

#2 russelluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-April 12

Re: How to get a string from javascript variable with python?

Posted 26 May 2012 - 10:44 AM

Oops!

It seems this question is invalid. I made a small but crucial mistake in that if you paste the keepvid url in the address bar in the format described above, it will display the page with a list of download links for that video but only if you have already requested the download with the bar and download button displayed on the page. Otherwise it says "processing links, please wait..." forever.

I will have to find another way to download youtube videos, unless perhaps you can see a better solution?? Worth a try...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1