Page 1 of 1

Using MediaMonkey with PHP

Posted: Sun May 02, 2004 3:02 pm
by hadur
More than likely, this won't be useful for many people since normal people usually don't have servers running PHP on their computers...

Anyways, here is a PHP script to download the Rick Dee's and Ryan Seacrest top 40 countdown lists and create a M3U playlist that can be imported onto an iPod if so inclined ;)

Again, probably not useful for anyone, but I just thought that I would share. You would need IIS/Apache and PHP to run this. Just point a web browser to http://127.0.0.1/filename.php and you are ready to roll :)

Code: Select all

<?php
if (isset($_GET['type'])) {
	if ($_GET['type'] == 1) {
		// $handle = fopen("rest_chart.html", "rb");
		$handle = fopen("http://www.at40.com/rest_chart.html", "rb");
		$regex = "class='chart_song'><b>([^\=]*)</b>[^\=]*<a href='[^\=]*'>([^\=]*)</a>";
		$name = 'Top 40';
	} elseif ($_GET['type'] == 2) {
		$handle = fopen("top40chart.html", "rb");
		//$handle = fopen("http://www.rick.com/top40chart.html", "rb");
		$regex = '<tr>[^/]*">[0-9]*[DEBUT]*</font></[^\=]*d>[^/]*">[0-9]*</font></[^\=]*d>[^/]*"><?([^<]*)<[^v]*[^/]*">([^<]*)';
		$name = 'Rick Dees';
	}
} else {
	?>
	<a href="?type=1">Ryan Seacrest Top 40</a><br>
	<a href="?type=2">Rick Dees Top 40</a>
	<?
	exit;
}
if (isset($_GET['dl'])) {
	function callback($buffer) {
		return (str_replace("<br />", "\n", $buffer));
	}
	ob_start("callback");
} else {
	?>
	<style type="text/css">
	BODY {
		font-size: 11px;
		font-family: Verdana;
		padding-right: 425px;
	}
	#problems {
		width: 400px;
		position: absolute;
		top: 0;
		right: 0;
	}
	</style>
	<?
	echo '<a href="?dl=1&type='.$_GET['type'].'">Download</a><br><br>';
}
echo '#EXTM3U<br />';
$contents = "";
do {
	$data = fread($handle, 8192);
	if (strlen($data) == 0) {
		break;
	}
	$contents .= $data;
} while (true);
fclose($handle);
$odbc = odbc_connect('Music', '', '') or die( "Could    Not Connect to ODBC Database!" );
while(ereg($regex,$contents,$temp)) {
	$t = $temp[2];
	$start = strlen($t) + strpos($contents,$t);
	$length = strlen($contents);
	$contents = substr($contents,$start,$length-$start);
	$list[] = $t;
	$t = $temp[1];
	if ($_GET['type'] == 2) {
		if(ereg('a href="[^"]*" target="_blank">([^%]*)',$t,$tt)) {
			$t = $tt[1];
		}
	}
	$artists[] = $t;
}
$i=0;
$problems = '';
while (list ($key, $t) = each ($list)) {
	$te = ereg_replace (' +', ' ', ereg_replace ("\n", '', trim(str_replace('\'', '\'\'', $t))));
	$ta = ereg_replace (' +', ' ', ereg_replace ("\n", '', trim(str_replace('\'', '\'\'', $artists[$i]))));
//	$te = preg_replace("( *)", " ", $te);
//	$ta = preg_replace("(  *)", " ", $ta);
	// Perfect Match
	$sql = odbc_exec($odbc, 'SELECT Songs.SongPath, Songs.SongTitle FROM Songs, Artists WHERE SongTitle=\''.$te.'\' AND Artist=\''.$ta.'\' AND Artists.ID=Songs.IDArtist');
	if ($query = odbc_result($sql, 1)) {
		echo '#EXTINF:,'.odbc_result($sql, 2).'<br />C'.$query.'<br />';
	} else {
		// Incorrect Artist
		$sql = odbc_exec($odbc,'SELECT Songs.SongPath, Artists.Artist, Songs.SongTitle FROM Songs, Artists WHERE SongTitle=\''.$te.'\' AND Artists.ID=Songs.IDArtist');
		if ($query = odbc_result($sql, 1)) {
			echo '#EXTINF:,'.odbc_result($sql, 3).'<br />C'.$query.'<br />';
			$problems .= 'A: '.$artists[$i].' & '.odbc_result($sql, 2).' & '.odbc_result($sql, 3).'<br />';
		} else {
			// Incorrect Song
			$sql = 'SELECT COUNT(Songs.SongPath) FROM Songs, Artists WHERE Artist=\''.$ta.'\' AND Artists.ID=Songs.IDArtist';
			if (odbc_result(odbc_exec($odbc, $sql), 1) == 1) {
				$sql = odbc_exec($odbc,'SELECT Songs.SongPath, Songs.SongTitle FROM Songs, Artists WHERE Artist=\''.$ta.'\' AND Artists.ID=Songs.IDArtist');
				$query = odbc_result($sql, 1);
				echo '#EXTINF:,'.odbc_result($sql, 2).'<br />C'.$query.'<br />';
				$problems .= 'S: '.$t.' & '.odbc_result($sql, 2).' & '.$artists[$i].'<br />';
			} else {
				// Missing Song
				$missed[] = $t;
				$martists[] = $artists[$i];
				$id[] = $i+2;
			}
		}
	}
	$i++;
}
if (isset($_GET['dl'])) {
	header('Content-type: text/plain'); 
	header('Content-Disposition: attachment; filename='.$name.'.m3u');
	header('Pragma: public');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=');
	header('Content-Type: application/force-download');
	ob_end_flush();
} else {
	echo '<div id="problems">';
	if (isset($missed)) {
		$i = 0;
		while (list ($key, $val) = each ($missed)) {
			echo $id[$i].'. '.$val.' - '.$martists[$i].'<br>';
			$i++;
		}
		echo '<br /><br />';
	}
	echo $problems.'</div>';
}
?>