New YouTube downloader

Hello Everyone,

I’m trying to write a script that will download YouTube videos using curl, but as we already know, YouTube likes to make this hard. I searched the forum for the topic and tried all suggestions, but it still doesn’t work.

Here is my code:

[code]on searchAndReplace(txt, srch, rpl)

set oldtid to AppleScript's text item delimiters

set AppleScript's text item delimiters to {srch}

set temp to every text item of txt

set AppleScript's text item delimiters to {rpl}

set temp to (temp as string)

set AppleScript's text item delimiters to oldtid

return temp

end searchAndReplace
set y to 1

repeat while y is 1

set vID to ""


set site1 to text returned of (display dialog "Enter site: " with title "Flash Content" buttons {"Cancel", "Download"} default answer "" default button 2)

try
	if button returned of the result is "Cancel" then quit
	
end try
--set site1 to searchAndReplace(site1, "\","/")
--set site1 to searchAndReplace(site1,"/","/")

tell site1 to set url2 to "http://www.youtube.com/v/" & text ((my (offset of "=" in it)) + 1) thru -1
set locationURL to do shell script "curl -I " & url2 & " | grep ^Location | grep -o 'video_id=.*'"
set downloadURL to "http://youtube.com/get_video?" & locationURL

set gtheUrl to do shell script "curl -IL " & quoted form of downloadURL & "| grep ^Location "
set oldDelims to AppleScript's text item delimiters -- get normal dilm
set AppleScript's text item delimiters to {"Location: "} --sets new dilm
set line_start to text item -1 of gtheUrl as string ---

set AppleScript's text item delimiters to oldDelims -- resets dilm

do shell script "cd ~/Desktop; curl " & line_start & " -o ~/Desktop/Youtube_1.flv " -- downloads the file, may take some time.

display dialog "Done! Please check content in your download location. Again?" with title "Complete" buttons {"Cancel", "Again"} default button 2

try
	if button returned of the result is "Cancel" then quit
	if button returned of the result is "Again" then set y to 1
end try

end repeat[/code]
Nevermind with searchAndReplace. I put that in because I even tried downloading the .html then reading in a certain section replacing certain characters and then removing the .html.

Let me explain what this “certain” part is. When I download the video via Safari, it gives the location:

http:\/\/o-o.preferred.iad09s14.v5.lscache5.c.youtube.com\/generate_204?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Cratebypass%2Coc%3AU0hQS1BQV19FSkNOMF9ISkFD\u0026fexp=900502%2C904539%2C913519\u0026itag=43\u0026ip=0.0.0.0\u0026signature=6BF4908C7E93161975C735B4896C9BA63047C8D5.52B06C9DEF67FAF58C50FC221ABA7BACAA944F7F\u0026sver=3\u0026ratebypass=yes\u0026expire=1316059200\u0026key=yt1\u0026ipbits=0\u0026id=27a65694392cd274

Which if you notice, I’m forced to change certain characters. Now, when I try to go to that URL (after the change), it won’t work.

So anyone have any ideas?

I see two main problems-there are many redirects and I don’t think anyone knows which directory the videos are held.

Download this project. It is a java written application but the source code is free and the application works. So reading this source code (start reading the file DownloadVideosThread.java) and understanding what it does says more than 1000 words.

I took a look at that source code and there’s a bunch of stuff going on.

Okay, so I took a look at that code, I also looked at a site called kissyoutube.com. They both confirmed my there that the location of the video is in that “specific” location.

I updated my code:

[code]on searchAndReplace(txt, srch, rpl)

set oldtid to AppleScript's text item delimiters

set AppleScript's text item delimiters to {srch}

set temp to every text item of txt

set AppleScript's text item delimiters to {rpl}

set temp to (temp as string)

set AppleScript's text item delimiters to oldtid

return temp

end searchAndReplace
set y to 1

repeat while y is 1

set vID to ""


set site1 to text returned of (display dialog "Enter video ID: " with title "Flash Content" buttons {"Cancel", "Download"} default answer "http://www.youtube.com" default button 2)

try
	if button returned of the result is "Cancel" then quit
	
end try

--set AppleScript's text item delimiters to "?v="
--set v_id to item 2 of text items of text returned of site1
set pageURL to "http://www.youtube.com/v/" & site1

set vID to do shell script "curl -I " & pageURL & " | grep ^script | grep -o 'id=.*'"
set title1 to do shell script "curl -I " & pageURL & " | grep ^title | grep -o 'title=.*'"
set title to searchAndReplace(title1, " ", "%20")

display dialog title
--display dialog vID

set ds to "http://o-o.preferred.lga16g05.v8.lscache6.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hQS1BTTl9FSkNOMF9ITVJJ&algorithm=throttle-factor&itag=35&ip=0.0.0.0&burst=40&sver=3&signature=9D67497CA89C1B288BA6C92B081FB5FBAEF82BBF.7B2844FEDBD784AD4C0BE7A97E108DA1BB352968&expire=1316080800&key=yt1&ipbits=0&factor=1.25&id=" & vID & "&title=" & title & "%20[SaveYouTube.com]"

do shell script "cd ~/Desktop; curl " & ds & " -O"

display dialog "Done! Please check content in your download location. Again?" with title "Complete" buttons {"Cancel", "Again"} default button 2

try
	if button returned of the result is "Cancel" then quit
	if button returned of the result is "Again" then set y to 1
end try

end repeat[/code]
Theoretically, this should work, but I’m having issues with these lines:

set vID to do shell script "curl -I " & pageURL & " | grep ^script | grep -o 'id=.*'" set title1 to do shell script "curl -I " & pageURL & " | grep ^title | grep -o 'title=.*'" set title to searchAndReplace(title1, " ", "%20")
I’ve never used grep, but I believe it searches for tags and saved words in HTML, like and location. Any ideas?

Has anyone got this to work properly? I’d love to use it!:smiley:

I get this after i enter a video url.

error " % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
0 568 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0" number 1

I’m new to applescript so I have NO idea what this means.

Thanks!
Jenn