Skip to content

Instantly share code, notes, and snippets.

@CyberShadow
Created August 2, 2021 10:39
Show Gist options
  • Select an option

  • Save CyberShadow/af726e0cf93870a559176fa453a07ac2 to your computer and use it in GitHub Desktop.

Select an option

Save CyberShadow/af726e0cf93870a559176fa453a07ac2 to your computer and use it in GitHub Desktop.
Scripts for generating / managing di.fm playlists
<?php
// Error handling / strict mode bootstrap
error_reporting(-1);
function error_handler($errno, $errstr, $errfile, $errline) { throw new ErrorException($errstr, $errno, 1, $errfile, $errline); }
set_error_handler('error_handler');
function error($message = 'An error occurred') { throw new Exception($message); }
function enforce($condition, $message = 'Enforcement failed') { if (!$condition) error($message); return $condition; }
enforce(!get_magic_quotes_gpc(), "Magic quotes are on!");
$url_base = "http://$_SERVER[HTTP_HOST]:$_SERVER[SERVER_PORT]" . dirname($_SERVER['REQUEST_URI'] . 'foo');
$key = file_get_contents('key.txt');
$streams = array(
array('name' => 'Premium', 'id' => 'premium' ),
array('name' => 'Premium High Quality', 'id' => 'premium_high' ),
);
$sites = array(
array('domain' => 'di.fm' ),
array('domain' => 'radiotunes.com'),
array('domain' => 'jazzradio.com'),
array('domain' => 'classicalradio.com'),
array('domain' => 'rockradio.com'),
array('domain' => 'zenradio.com'),
);
function get_channels($stream)
{
global $sites;
global $key;
$entries = array();
$c = curl_init();
foreach ($sites as $site)
{
curl_setopt($c, CURLOPT_URL, "http://listen.{$site['domain']}/$stream");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($c);
$channels = json_decode($json, true);
foreach ($channels as $channel)
{
$entries[] = array(strtoupper($site['domain']) . ' - ' . $channel['name'], $channel['key'], $key, $site['domain']);
}
}
return $entries;
}
<?php
require 'common.php';
?>
<!doctype html>
<title>di.fm playlist generator</title>
<p>Playlists:</p>
<table border="1">
<?php
foreach ($streams as $stream)
{
echo "<tr><td>$stream[name]</td><td>" .
"<a href='plspls.php?stream=$stream[id]'>[Playlist]</a> " .
"<a href='zip-direct.php?stream=$stream[id]'>[ZIP (direct)]</a> " .
"<a href='zip-redir.php?stream=$stream[id]&fmt=pls'>[ZIP (pls)]</a> " .
"<a href='zip-redir.php?stream=$stream[id]&fmt=m3u'>[ZIP (m3u)]</a> " .
"</td></tr>";
}
?>
</table>
<p>Channels:</p>
<table border="1">
<tr><th>Name</th>
<?php
foreach ($streams as $stream)
echo "<th>$stream[name]</li>";
?>
</tr>
<?php
$entries = array();
$all_entries = array();
foreach ($streams as $stream)
{
$stream_entries = get_channels($stream['id']);
$entries[$stream['id']] = $stream_entries;
foreach ($stream_entries as $entry)
$all_entries[$entry[1]] = $entry;
}
foreach ($all_entries as $entry)
{
list($name, $id, $key, $site) = $entry;
echo "<tr><td>" . htmlspecialchars($name) . "</td>";
foreach ($streams as $stream)
{
echo
"<td>" .
"<a href='$url_base/stream.php?key=$key&stream=$stream[id]&site=$site&id=$id'>[Stream]</a> " .
"<a href='$url_base/pls.php?key=$key&stream=$stream[id]&site=$site&id=$id'>[Playlist]</a>" .
"</td>";
}
echo "</tr>";
}
?>
</table>
<?php
require 'common.php';
$stream = $_GET['stream'];
$entries = get_channels($stream);
header("Content-type: text/plain");
foreach ($entries as $entry)
{
list($name, $id, $key, $site) = $entry;
echo "$site/$id\t$name\n";
}
<?php
require 'common.php';
$id = $_GET['id'];
$key = $_GET['key'];
$site = $_GET['site'];
$stream = $_GET['stream'];
header("Location: http://listen.$site/$stream/$id.pls?$key");
<?php
require 'common.php';
$stream = $_GET['stream'];
$entries = get_channels($stream);
header("Content-type: text/plain");
header("Content-disposition: inline; filename=difm_" . $stream . ".pls");
print "[playlist]\n";
print "NumberOfEntries=" . count($entries) . "\n";
$i = 0;
foreach ($entries as $entry)
{
list($name, $id, $key, $site) = $entry;
$i++;
echo "File$i=$url_base/stream.php?key=$key&stream=$stream&site=$site&id=$id\n";
echo "Title$i=$name\n";
echo "Length$i=-1\n";
}
echo "Version=2";
<?php
require 'common.php';
$id = $_GET['id'];
$key = $_GET['key'];
$site = $_GET['site'];
$stream = $_GET['stream'];
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://listen.$site/$stream/$id.pls?$key");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($c);
$ini = parse_ini_string($html);
$n = (int)$ini['NumberOfEntries'];
$i = 1 + rand() % $n;
header('Location: ' . $ini["File$i"]);
<?php
require 'common.php';
$stream = $_GET['stream'];
$entries = get_channels($stream);
$zip = new ZipArchive();
$filename = "./tmp.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
die("cannot open <$filename>\n");
}
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
foreach ($entries as $entry)
{
list($name, $id, $key, $site) = $entry;
curl_setopt($c, CURLOPT_URL, "http://listen.$site/$stream/$id.pls?$key");
$pls = curl_exec($c);
$zip->addFromString("$name.pls", $pls);
}
$zip->close();
header("Content-type: application/zip");
header("Content-disposition: inline; filename=difm_" . $stream . ".zip");
readfile($filename);
unlink($filename);
<?php
require 'common.php';
$stream = $_GET['stream'];
$fmt = $_GET['fmt'];
$entries = get_channels($stream);
$zip = new ZipArchive();
$filename = "./tmp.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
die("cannot open <$filename>\n");
}
$i=0;
foreach ($entries as $entry)
{
list($name, $id, $key, $site) = $entry;
$url = "$url_base/stream.php?key=$key&stream=$stream&site=$site&id=$id";
if ($fmt == 'pls')
{
ob_start();
print "[playlist]\n";
print "NumberOfEntries=1\n";
echo "File1=$url\n";
echo "Title1=$name\n";
echo "Length1=-1\n";
echo "Version=2";
$pls = ob_get_contents();
ob_end_clean();
}
else
if ($fmt = 'm3u')
{
$pls = "$url\n";
}
$zip->addFromString(sprintf("DigitallyImported - %03d - %s.%s", ++$i, $name, $fmt), $pls);
}
$zip->close();
header("Content-type: application/zip");
header("Content-disposition: inline; filename=difm_" . $stream . "_" . $fmt . ".zip");
readfile($filename);
unlink($filename);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment