Skip to content

Instantly share code, notes, and snippets.

@keichan34 keichan34/msd.php
Created Oct 5, 2015

Embed
What would you like to do?
<?php
header("Content-Type: application/rss+xml");
date_default_timezone_set("Asia/Tokyo");
function formatTime($duration) //as hh:mm:ss
{
$hours = floor($duration / 3600);
$minutes = floor( ($duration - ($hours * 3600)) / 60);
$seconds = $duration - ($hours * 3600) - ($minutes * 60);
return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
$directory = "/var/www/kkob.us/msd";
$db = new SQLite3(dirname(__FILE__) . '/data/db_1.sqlite3');
$version = $db->query('PRAGMA user_version')->fetchArray();
if ($version['user_version'] < 1) {
$db->query('CREATE TABLE durations ( key TEXT PRIMARY KEY, duration INTEGER )');
$db->exec('PRAGMA user_version = 1');
}
$query_stmt = $db->prepare('SELECT * FROM "durations" WHERE "key" = :key;');
$files = [];
if ($d = opendir($directory)) {
while (false !== ($entry = readdir($d))) {
if ($entry[0] == "." || $entry[0] == "_" ||
substr($entry, -4, 4) !== ".mp3") {
continue;
}
$fullpath = $directory . "/" . $entry;
$size = filesize($fullpath);
$mtime = filemtime($fullpath);
$parts = explode("_", str_replace(".mp3", "", $entry));
$date = join("_", array_slice($parts, -2));
$time = DateTime::createFromFormat("Y-m-d_H-i-s", $date);
$utc = new DateTimeZone("GMT");
$time->setTimezone($utc);
$cache_key = md5($entry . "|" . $mtime);
$query_stmt->bindValue(':key', $cache_key);
$result = $query_stmt->execute()->fetchArray();
$query_stmt->reset();
if ($result === false) {
$duration = intval(trim(exec("/usr/bin/soxi -D $fullpath")));
$insert_stmt = $db->prepare('INSERT INTO "durations" ("key", "duration") VALUES (:key, :duration);');
$insert_stmt->bindValue(':key', $cache_key);
$insert_stmt->bindValue(':duration', $duration);
$insert_stmt->execute();
} else {
$duration = $result['duration'];
}
array_push($files, (object)array(
"entry" => $entry,
"size" => $size,
"time" => $time,
"duration" => $duration
));
}
usort($files, function($a, $b) {
if ($a->time == $b->time) {
return 0;
}
return $a->time < $b->time ? 1 : -1;
});
}
echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>MSD</title>
<link>http://msd.kkob.us/msd.php</link>
<language>en-us</language>
<description>MSD</description>
<itunes:author>MSD</itunes:author>
<itunes:summary>MSD</itunes:summary>
<?php
foreach ($files as $file) {
?>
<item>
<title><?php echo $file->entry; ?></title>
<description><?php echo $file->entry; ?></description>
<enclosure type="audio/mp3" length="<?php echo $file->size; ?>" url="https://kkob.us/msd/<?php echo $file->entry; ?>" />
<pubDate><?php echo $file->time->format("D, d M Y H:i:s T"); ?></pubDate>
<guid isPermaLink="false"><?php echo $file->entry; ?></guid>
<itunes:duration><?php echo formatTime($file->duration); ?></itunes:duration>
</item>
<?php
}
?>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.