Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Save iTMS Music videos in iTunes 4.7.1' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Save iTMS Music videos in iTunes 4.7.1
Authored by: ehlers on May 05, '05 06:41:54AM

Nice script, I like it..

I replaced "/sw/bin/wget -O " by "curl -o " so it now works without installing wget. curl is part of OSX.



[ Reply to This | # ]
Save iTMS Music videos in iTunes 4.7.1
Authored by: hypert on May 05, '05 07:43:59AM

Good tip, thanks! I've seen curl used (by Perl's CPAN?), but never looked into it. Looks like it runs on just about all platforms, too, like wget. Cool. Anyone know any other advantages of one over the other, besides that curl is bundled with our OS?



[ Reply to This | # ]
wget is GPL, curl is MIT
Authored by: ash7 on May 05, '05 11:19:16AM

wget is also getting stale... I had noticed and didn't understand why. FreeBSD has a security advisory against wget that has yet to be resolved as well.



[ Reply to This | # ]
Save iTMS Music videos in iTunes 4.7.1
Authored by: patyny on May 10, '05 11:47:21AM

if it's not too much trouble ehlers, can you post an updated working script with your changes. I don't know how to do it myself. Thanks
(do I need fink for this)



[ Reply to This | # ]
Save iTMS Music videos in iTunes 4.7.1
Authored by: ehlers on May 10, '05 01:40:44PM
You don't need fink or something similar, just plain OS X. But I'm quite sure you need the BSD subsystem installed. With the finder look into /Library/Receipts. If you have a BSD.pkg you should be fine. Otherwise install the BSD subsystem from your MAC OS system cd.

In the meantime I added some try blocks to catch some errors.

Here's my current version.


-- Original code by "Umpa-Spain", posted at http://www.macosxhints.com/article.php?story=20050425164738812
-- Modified by "hypert" to extract artist and song name, and download directly to disk using "wget"
-- wget can be installed by using "fink" (http://fink.sourceforge.net/index.php)
-- Modified by Bernhard Ehlers to use curl instead of wget

on run
	--We get the iTunes's url
	tell application "Finder"
		try
			set ITMS_URL to the clipboard as text
		on error
			display dialog "No iTunes URL found in the clipboard." & return & ¬
				"Please, go iTunes, and right click on the video you want to save and copy the url to the clipboard." buttons {"Cancel"}
		end try
	end tell
	
	--We extract de Video_ID of the selected video
	set AppleScript's text item delimiters to {"videoId="}
	try
		set VIDEOID to item 2 of (every text item of ITMS_URL)
	on error
		display dialog "No iTunes URL found in the clipboard." & return & ¬
			"Please, go iTunes, and right click on the video you want to save and copy the url to the clipboard." buttons {"Cancel"}
	end try
	
	--We tell to Safari open a new Url based in the previous ID, from we get the exact location of the movie
	set UrlBase to "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/viewVideo?b=a&videoId=" & VIDEOID & "&videoIndex=2" as text
	tell application "Safari"
		make new document with properties {URL:UrlBase, visible:false}
	end tell
	
	--We wait 3 seconds while Safari loads the page, if not the script return an error
	set x to 0
	set curd to current date
	repeat until x = 15
		if curd + 3 < (current date) then set x to 15
	end repeat
	
	--We search in the source code of the page teh reference to the ".mov" file and set it's exactly location
	tell application "Safari"
		set videoURL to source of front document as text
		set AppleScript's text item delimiters to {"MovieView autoplay="}
		set videoURL to item 2 of (every text item of videoURL)
		set AppleScript's text item delimiters to {"http://"}
		set videoURL to item 2 of (every text item of videoURL)
		set AppleScript's text item delimiters to {".mov"}
		set videoURL to item 1 of (every text item of videoURL)
		set videoURL to "http://" & videoURL & ".mov"
		--display dialog videoURL
		
		try
			set artist to source of front document as text
			set AppleScript's text item delimiters to {"<key>artistName</key> <string>"}
			-- Could also have used "<key>playlistArtistName</key><string>", which is sometimes
			-- different than "artistName" (note the space after the artistName key though!)
			set artist to item 2 of (every text item of artist)
			set AppleScript's text item delimiters to {"</string>"}
			set artist to item 1 of (every text item of artist)
		on error
			set artist to "Artist"
		end try
		
		--display dialog artist
		
		try
			set song to source of front document as text
			set AppleScript's text item delimiters to {"<key>songName</key><string>"}
			set song to item 2 of (every text item of song)
			set AppleScript's text item delimiters to {"</string>"}
			set song to item 1 of (every text item of song)
		on error
			set song to "Song"
		end try
		
		--display dialog song
		
		--Close tab or window created before
		close front document
		
		--Opens the movie in Safari (or default web browser), whre we wait to the load is complete, and then we save it where we want ;)
		--open location codigo
	end tell
	
	set artist to clean(artist)
	set song to clean(song)
	
	set newFile to quoted form of POSIX path of (choose file name with prompt "Save movie as:" default name artist & " - " & song & ".mov")
	set theCommand to "curl -o " & newFile & " " & videoURL
	--display dialog theCommand
	
	tell application "Terminal"
		activate
		do script theCommand
	end tell
end run

on clean(myString)
	-- Get rid of 
	set myString to searchReplace(myString, "&", "&")
	set myString to searchReplace(myString, "'", "'")
	return myString
end clean

on searchReplace(origStr, searchStr, replaceStr)
	-- Got this routine from
	-- http://www.builderau.com.au/program/development/0,39024626,20283486,00.htm
	set old_delim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchStr
	set origStr to text items of origStr
	set AppleScript's text item delimiters to replaceStr
	set origStr to origStr as string
	set AppleScript's text item delimiters to old_delim
	return origStr
end searchReplace


[ Reply to This | # ]
Save iTMS Music videos in iTunes 4.7.1
Authored by: rmiller021 on May 17, '05 01:23:53PM
I fixed this so that it does not use safari at all now, it will just open up a single terminal window for the download.

-- Original code by "Umpa-Spain", posted at http://www.macosxhints.com/article.php?story=20050425164738812
-- Modified by "hypert" to extract artist and song name, and download directly to disk using "wget"
-- wget can be installed by using "fink" (http://fink.sourceforge.net/index.php)
-- Modified by Bernhard Ehlers to use curl instead of wget
-- Modifined by Rob Miller to stop the safari unpleasantness now uses only command line applications to retrieve the url
-- 			In short it makes it only spawn one window 

on run
	--We get the iTunes's url
	tell application "Finder"
		try
			set ITMS_URL to the clipboard as text
		on error
			display dialog "No iTunes URL found in the clipboard." & return & ¬
				"Please, go iTunes, and right click on the video you want to save and copy the url to the clipboard." buttons {"Cancel"}
		end try
	end tell
	
	--We extract de Video_ID of the selected video
	set AppleScript's text item delimiters to {"videoId="}
	try
		set VIDEOID to item 2 of (every text item of ITMS_URL)
	on error
		display dialog "No iTunes URL found in the clipboard." & return & ¬
			"Please, go iTunes, and right click on the video you want to save and copy the url to the clipboard." buttons {"Cancel"}
	end try
	
	--We tell to Safari open a new Url based in the previous ID, from we get the exact location of the movie
	set UrlBase to "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/viewVideo?b=a&videoId=" & VIDEOID & "&videoIndex=2" as text
	
	--Use curl and gunzip to download the page and decompress it
	set rob to (do shell script "curl -s \"" & UrlBase & "\" | gunzip")
	
	
	--We search in the source code of the page teh reference to the ".mov" file and set it's exactly location
	
	set videoURL to rob as text
	set AppleScript's text item delimiters to {"MovieView autoplay="}
	set videoURL to item 2 of (every text item of videoURL)
	set AppleScript's text item delimiters to {"http://"}
	set videoURL to item 2 of (every text item of videoURL)
	set AppleScript's text item delimiters to {".mov"}
	set videoURL to item 1 of (every text item of videoURL)
	set videoURL to "http://" & videoURL & ".mov"
	--display dialog videoURL
	
	try
		--set artist to source of front document as text
		set artist to rob as text
		set AppleScript's text item delimiters to {"<key>artistName</key> <string>"}
		-- Could also have used "<key>playlistArtistName</key><string>", which is sometimes
		-- different than "artistName" (note the space after the artistName key though!)
		set artist to item 2 of (every text item of artist)
		set AppleScript's text item delimiters to {"</string>"}
		set artist to item 1 of (every text item of artist)
	on error
		set artist to "Artist"
	end try
	
	--display dialog artist
	
	try
		set song to source of front document as text
		set AppleScript's text item delimiters to {"<key>songName</key><string>"}
		set song to item 2 of (every text item of song)
		set AppleScript's text item delimiters to {"</string>"}
		set song to item 1 of (every text item of song)
	on error
		set song to "Song"
	end try
	
	set artist to clean(artist)
	set song to clean(song)
	
	set newFile to quoted form of POSIX path of (choose file name with prompt "Save movie as:" default name artist & " - " & song & ".mov")
	set theCommand to "curl -o " & newFile & " " & videoURL
	--display dialog theCommand
	--exit the terminal session
	set theCommand to theCommand & "; echo \"download complete\"; exit"
	tell application "Terminal"
		activate
		do script theCommand
	end tell
end run

on clean(myString)
	-- Get rid of 
	set myString to searchReplace(myString, "&", "&")
	set myString to searchReplace(myString, "'", "'")
	return myString
end clean

on searchReplace(origStr, searchStr, replaceStr)
	-- Got this routine from
	-- http://www.builderau.com.au/program/development/0,39024626,20283486,00.htm
	set old_delim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchStr
	set origStr to text items of origStr
	set AppleScript's text item delimiters to replaceStr
	set origStr to origStr as string
	set AppleScript's text item delimiters to old_delim
	return origStr
end searchReplace

---
Do you think at 900 char system call is a problem? I love embedding applescripts in c++ :)

[ Reply to This | # ]

Save iTMS Music videos in iTunes 4.7.1
Authored by: hypert on May 18, '05 08:22:31AM

Thanks for the "curl | gunzip" trick! Wish I had thought of that... :-)



[ Reply to This | # ]
Save iTMS Music videos in iTunes 4.7.1
Authored by: rmiller021 on May 18, '05 10:25:11AM
I tried it on a hunch, i am glad it worked

I did notice a problem with my code though
set song to source of front document as text

needs to be changed to
set song to rob as text


[ Reply to This | # ]