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

Click here to return to the 'Use any song snippet as a cell phone ringtone' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use any song snippet as a cell phone ringtone
Authored by: on Jan 31, '06 12:46:02AM
My perl hacker friend Dave Cash tackled this problem with me by writing a perl script. My only contribution is the hackish applescript contained therein. Here's the script:


use strict;
use LWP::UserAgent;
use Audio::M4P::Decrypt;

my $ua = new LWP::UserAgent;
my $deDRMS = new Audio::M4P::Decrypt;

my $host  = '';
my $path  = '';
my $file  = '';
my $agent = '';
my $info  = '';

#figure out how to know which network interface is currenlty active, en1 or en0 etc
open TCPFLOW, "sudo tcpflow -c -i en0 port 80 |";
#when bundling this as an application with perlwrapper, maybe I can just include a binary of tcpflow and have the path to tcpflow be something like /Applications/
while ( <TCPFLOW> ) {
	$path  ||= ( /GET *(.*?)[^\/]+\.m4p/ )[ 0 ];
	$file  ||= ( /GET *.*?([^\/]+\.m4p)/ )[ 0 ];
	$host  ||= ( /^Host: (.*?)[\n\r]*$/ )[ 0 ] if $path;
	$agent ||= ( /^User-Agent: (.*?)[\n\r]*$/ )[ 0 ];
	if ( $path && $host ) {
		if ( $agent =~ /^iTunes/ ) {

			# Use applescript and UI scripting to hackishly get the track name and title.  Returns something like this:
			# Song_Title	Track_Length	Artist	Album	Price
			# Jesus Doesn't Want Me for a Sunbeam (Live)	4:37	Nirvana	MTV Unplugged in New York: Nirvana	$0.99
			my $info = `osascript -e 'tell application "iTunes" to activate' -e 'tell application "System Events" to keystroke "c" using {command down}' -e 'return the clipboard'`;

			my ( $title, $length, $artist, $album, $price ) = split /\t/, $info;
			my $out_filename = $title ? "$ENV{ HOME }/Desktop/$artist - $album - $title.m4p" : $file;
			print qq!Getting $file to "$out_filename"...!;
			my $response = $ua->get( "http://$host$path$file" );
               		if ( $response->is_success ) {
                      		open OUT, qq!> /tmp/$file!;
                       		print OUT $response->content;
                       		close OUT;
				rename "/tmp/$file", $out_filename;
				print "done.$/";
                       		#$deDRMS->DeDRMS( "/Users/simon/Desktop/$file", "/Users/simon/Desktop/DONE_$file" );
                       		#unlink "/Users/simon/Desktop/$file";
			else {
				print "failed!$/";
       		$host = $path = $file = '';
close TCPLOW;
To use this script: save it somewhere as In Terminal, cd to the folder where you saved it. Type this command perl (You will probably need to install Audio::M4P::Decrypt from CPAN unless you already have it installed.) Once the script is running, go to iTunes and browse the music store. Each song you preview should be saved to the desktop. If you want to convert the protected AAC files to MP3, open hymn and process each file manually. It would be cool to make this script into a simple app using something like PerlWrapper. Then people who wanted to use it wouldn't have to know how to install CPAN or the command line. It could even include a binary of tcpflow so people wouldn't have to install that either.

[ Reply to This | # ]
Use any song snippet as a cell phone ringtone
Authored by: on Jan 31, '06 01:25:46AM
P.S. - the script seems to not work quite right the first couple of songs it tries to download. But keep trying and it will work eventually. I don't know how to debug it, but maybe an adventurous perl programmer can take a look. Also, if the UI applescript part that copies the text from the iTunes current selection fails (because you changed applications just as it was about to copy, for example), the file will be save to the directory where you saved the script instead of the desktop and it will have the original name that iTunes stores it as (something like s05.ypqjdnud.p.m4p). I saved this applescript:

tell application "Terminal"
	tell application "System Events"
		keystroke "n" using command down
		delay 1
	end tell
	set bounds of window 1 to {0, 22, 1280, 854}
	do script "cd ~/Library/Scripts/Applications/iTunes/" in window 1
	do script "perl" in window 1
end tell ~/Library/iTunes/Scripts/Save Music Store Previews to Desktop.scpt so I can easily start up the perl script from the iTunes script menu.

[ Reply to This | # ]