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: 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 | # ]