[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] mymp3.plx



#!perl -w

=pod

=head1 NAME

mymp3 - Fetch my.mp3.com playlist and pass it to SoundJam MP

=head1 SYNOPSIS

=head1 DESCRIPTION

my.mp3.com is a site that lets you store what CDs you own, that also
exist in their database, so you can stream the MP3s from your account
there any time you want, from anywhere.

SoundJam MP has a problem with these playlists, that after a certain
bytesize of the lists, it won't add the tracks to the master playlist.
I have currently about 1,740 tracks in my playlist at my.mp3.com.
SoundJam MP handles about 13 or 14 tracks per file from my.mp3.com
(due in part because each URL in the playlist is so long).

This program loads in the main page using Netscape, allowing you
to log in if necessary, and then downloads your entire playlist.
It then chops it up into 13-track files, adding each one in turn
to SoundJam's master list, and then it sets the player to shuffle and
starts playing.

It can take a few minutes to add all of those files.  And a few times
a day, the links will stop working, because that is how my.mp3.com
works, so you need to empty your master playlist and run the script
again.  Yay!

Thanks to Peter Hartmann for the tips about Netscape's
C<register url echo> event.

=head1 REQUIREMENTS

MacPerl 5.2.0r4, cpan-mac 0.40, Mac::AppleEvents::Simple 0.82,
Mac::Glue 0.58.

=head1 AUTHOR

Chris Nandor E<lt>pudge@pobox.comE<gt>, http://pudge.net/

Copyright (c) 2000 Chris Nandor.

This program is free and open software. You may use, modify,
distribute, and sell this program (and any modified variants) in any
way you wish, provided you do not restrict others from doing the same.


=cut

use strict;
use File::Spec::Functions;
use Mac::Glue ':all';
use Mac::Events;
my($file1, $file2, $sj, $n, %done,
	$mainurl, $fileurl, $maxtracks, $x, $c);
local $/ = "\012";

config();

$n->activate;
$n->register_url_echo('McPL');  # echo finished downloads to us
$n->openurl($mainurl);  # log in so we can download our playlist

while (1) {
	last if $done{$mainurl} >= 1;  # wait until page finishes loading
	WaitNextEvent;
}

$n->geturl($fileurl, to => $file1);  # save playlist

while (1) {
	last if $done{$fileurl} >= 2;  # downloads need two windows
	WaitNextEvent;
}

$n->unregister_url_echo('McPL');  # turn of echos

$sj->activate;

process_playlist();

$sj->set( $sj->prop('shuffle'), to => enum('true') );
$sj->play( $sj->next_track );


# get playlist and cut it into playlists of $maxtracks tracks each
# because SoundJam MP seems to have problems with more than that

sub process_playlist {
	$|++;
	$ARGV[0] = $file1;
	while (<>) {
		$c++;
		if ($x == 0) {
			open FOO, "> $file2" or die "Can't open $file2: $!";
		}
		print FOO;
		$x++;
		add_file() if $x == $maxtracks;
	}
	add_file() if $x != 0;
}

sub add_file {
	close FOO;
#	print "start $c ... ";
	if ($sj->add($file2)) {
#		print "finish\n";
	} else {
		warn sprintf "Couldn't add file $file2 for tracks %d to $c\n",
			($c - $maxtracks);
	}
	$x = 0;
}

sub config {
	$maxtracks = 13;

	$file1 = catfile($ENV{TMPDIR}, 'foo1.m3u');
	$file2 = catfile($ENV{TMPDIR}, 'foo2.m3u');

	$mainurl = 'http://my.mp3.com/my';

	$fileurl = 'http://my.mp3.com/my.m3u?Action=Playlist&Command=' .
		'Play&masterlist=1&Data=type-masterlist,playlist_id-all,c' .
		'ds-all,nsongs-50start_song-0&stattracker=PlayList';

	@done{$mainurl, $fileurl} = (0, 0);
	$c = $x = 0;

	# install Apple Event handler for URL echoes
	$AppleEvent{'WWW?', 'URLE'} = sub {
		my $dobj = AEGetKeyDesc($_[0], keyDirectObject);
		my $got  = $dobj->get;
		AEDisposeDesc $dobj;

		# chop off query string if it is $mainurl
		$got =~ s/^(${mainurl})\?.+$/$1/;
		$done{$got}++;
		0;
	};

	$sj = new Mac::Glue 'SoundJam';
	$n = new Mac::Glue 'Netscape Communicator';
}

__END__

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org