HOLY FUCK PLAYLISTS AND SHIT

This commit is contained in:
Rob Nelson
2014-03-11 22:53:28 -07:00
parent ca5d6beaee
commit ab43b7a28a
76 changed files with 135 additions and 53 deletions

View File

@@ -0,0 +1,17 @@
<?php
// Change this.
define('BASE_URL', 'http://yoursite.tld/media');
// What file extensions do you want to look for?
// Files must be tagged and cannot have spaces in the name.
$validExtensions = array('mp3', 'ogg');
// Names of your playlists, without the .PHP at the end.
// See playlists/example.php for an example.
// Must correspond with your jukebox's playlists list.
$playlists = array(
'bar', // Bar-specific shit.
'jazz',
'rock',
);

View File

@@ -0,0 +1 @@
Your music files go here. This file is here to prevent people from just leeching everything.

View File

@@ -1,61 +1,107 @@
<?php
//header('Content-type: text/plain');
require_once ('config.php');
define('BASE_URL', 'http://ss13.nexisonline.net/media');
define('MEDIA_ROOT', dirname(__FILE__));
define('FILE_DIR', MEDIA_ROOT . '/files');
define('LIB_DIR', MEDIA_ROOT . '/lib');
define('PLAYLIST_DIR', MEDIA_ROOT . '/playlists');
// include getID3() library (can be in a different directory if full path is specified)
require_once('getid3/getid3.php');
require_once (LIB_DIR . '/getid3/getid3.php');
// Initialize getID3 engine
// Don't fuck with this shit.
$getID3 = new getID3;
$validExtensions = array('mp3', 'ogg');
$json = array();
foreach (glob('*.*') as $file) {
list(, $ext) = explode('.', $file);
if(!in_array($ext, $validExtensions))
continue;
$data = $getID3->analyze($file);
getid3_lib::CopyTagsToComments($data);
if(file_exists($file.'.cache.php'))
require($file.'.cache.php');
else{
$row = array(
'title' => '???',
'artist' => 'Unknown',
'album' => '',
'url' => BASE_URL.'/'.urlencode($file),
'length' => ''.(int)($data['playtime_seconds']*10)
);
if(array_key_exists('artist',$data['comments_html']) || array_key_exists('album',$data['comments_html'])){
$row['title']=$data['comments_html']['title'][0];
if(isset($data['comments']['artist']))
$row['artist']=$data['comments']['artist'][0];
if(isset($data['comments']['album']))
$row['album'] = $data['comments']['album'][0];
} else {
$matches=array();
// Search for something of the form "artist - title (album).ext"
if (preg_match('/([^\\-]+)\\-([^\\(\\.]+)(\\(([^\\)])\\))?\\.([a-z0-9]{3})/', $file, $matches) !== FALSE)
{
if(count($matches)>=2){
$row['artist'] = trim($matches[1]);
$row['title'] = trim($matches[2]);
$row['album'] = trim($matches[3]);
}
}
}
if (!isset($_GET['playlist']))
die(json_encode($playlists));
$playlist = $_GET['playlist'];
if (!in_array($playlist, $playlists))
die('No');
function loadFilesIn($subdir) {
$pl = array();
$striplen=strlen(FILE_DIR)+1;
foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator (FILE_DIR.'/'.$subdir)) as $name => $file)
{
$relfile = substr($file->getPathname(),$striplen);// . '/' . $file;
if(substr($relfile,-1)=='.') continue;
$pl[$relfile] = array();
}
$json[]=$row;
/*
echo '<pre>';
#echo json_encode($row);
echo htmlentities(print_r($data['comments'], true));
echo '</pre>';
*/
return $pl;
}
echo json_encode($json);
require (PLAYLIST_DIR . '/' . $playlist . '.php');
if (!file_exists('cache/'))
mkdir('cache/');
$json = array();
foreach ($playlist as $file => $data) {
list(, $ext) = explode('.', $file);
if (!in_array($ext, $validExtensions))
continue;
$json[]=analyzeFile($file, $data);
}
function analyzeFile($file,$playlist_data) {
global $validExtensions, $getID3;
$fileURL = BASE_URL . '/files/' . $file;
$file = FILE_DIR . '/' . $file;
// use cache, if available
$cachefile = 'cache/' . md5($file) . '.cache.php';
if (file_exists($cachefile)) {
require ($cachefile);
$row = array_merge($row, $playlist_data);
return $row;
}
// Get ID3 tags and whatnot
$data = $getID3->analyze($file);
getid3_lib::CopyTagsToComments($data);
// We need a playtime. Abort if not found.
if (!isset($data['playtime_seconds'])) {
echo "<h2>{$file}</h2>";
var_dump($data);
return;
}
//@formatter:off
$row = array(
'title' => '???',
'artist' => 'Unknown',
'album' => '',
'url' => $fileURL,
'length' => '' . (int)($data['playtime_seconds'] * 10)
);
//@formatter:on
if (isset($data['comments_html']) && (array_key_exists('artist', $data['comments_html']) || array_key_exists('album', $data['comments_html']))) {
$row['title'] = $data['comments_html']['title'][0];
if (isset($data['comments']['artist']))
$row['artist'] = $data['comments']['artist'][0];
if (isset($data['comments']['album']))
$row['album'] = $data['comments']['album'][0];
} else {
$matches = array();
// Search for something of the form "artist - title (album).ext"
if (preg_match('/([^\\-]+)\\-([^\\(\\.]+)(\\(([^\\)])\\))?\\.([a-z0-9]{3})/', $file, $matches) !== FALSE) {
if (count($matches) >= 2) {
$row['artist'] = trim($matches[1]);
$row['title'] = trim($matches[2]);
$row['album'] = trim($matches[3]);
}
}
}
file_put_contents($cachefile, '<?php $row=' . var_export($row, true).';');
$row = array_merge($row, $playlist_data);
return $row;
}
header('Content-type: text/plain');
#echo json_encode($json);
echo json_encode($json,JSON_PRETTY_PRINT);

View File

@@ -0,0 +1,18 @@
<?php
// Simple playlist:
// Files MUST be under files/.
$playlist=array(
'file1.mp3'=>array(),
'subfolder/file2.mp3'=>array(
'title'=>'Just a Friend',
'artist'=>'Biz Markie'
)
);
// Dynamic Playlist
// Loads files from files/subfolder.
$playlist=loadFilesIn('subfolder');
// Adjust playlist entry's tags.
$playlist['file1.mp3']=array('artist'=>'Kingston','title'=>'SS13 sucks','album'=>'Fuck You');