-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpodcast.php
executable file
·100 lines (95 loc) · 3.01 KB
/
podcast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
@license http://www.gnu.org/licenses/agpl.txt
@copyright 2014 Sourcefabric o.p.s.
@link http://www.sourcefabric.org
@author Micz Flor <[email protected]>
This php script will create a podcast XML on the fly
listing all mp3 files in the same directory.
*/
$channeltitle = "Streamplan files";
$channelauthor = "Various";
/* $sortby sets the order in which tracks are listed.
Options:
"newest" = newest on top
"oldest" = oldest on top
"filedesc" = alphabetically descending
"fileasc" = alphabetically ascending
default: "filedesc" (== how streamplan.sh works)
*/
$sortby = "filedesc";
$dir = "http://".$_SERVER['SERVER_NAME'];
$parts = explode('/',$_SERVER['REQUEST_URI']);
for ($i = 0; $i < count($parts) - 1; $i++) {
$dir .= $parts[$i] . "/";
}
header('Content-type: text/xml', true);
print"<?xml version='1.0' encoding='UTF-8'?>
<rss xmlns:itunes='http://www.itunes.com/DTDs/Podcast-1.0.dtd' version='2.0'>
<channel>
<title>$channeltitle</title>
<link>$dir</link>
<itunes:author>$channelauthor</itunes:author>
";
/**/
// read all mp3 files in the directory
$temp = glob("*.mp3");
// create array with timestamp.filename as key
foreach ($temp as $filename) {
$mp3files[filemtime($filename).$filename] = $filename;
}
// change the order of the list according to $sortby set above
switch ($sortby) {
case "newest":
krsort($mp3files);
break;
case "oldest":
ksort($mp3files);
break;
case "fileasc":
natcasesort($mp3files);
break;
default:
// filedesc
natcasesort($mp3files);
$mp3files = array_reverse($mp3files);
break;
}
// go through files and create <item> for podcast
foreach ($mp3files as $filename) {
// set empty array for metadata
$iteminfo = array(
"TPE1" => "",
"TIT2" => "",
"WOAF" => "",
"Filename" => ""
);
// read id3 from shell command
$idtag = explode("\n",shell_exec("id3v2 -R $filename"));
foreach($idtag as $line) {
// to to match key => value from each line
preg_match("/((\w+): (.*))/", $line, $results);
// if ID3 tag found, results will return four values
if(count($results) == 4) {
$iteminfo[$results[2]] = $results[3];
}
}
// if title too short, use filename as title
if (strlen($iteminfo['TIT2']) < 2) {
$iteminfo['TIT2'] = $filename;
}
print "
<item>
<title>".$iteminfo['TIT2']."</title>
<itunes:author>".$iteminfo['TPE1']."</itunes:author>
<itunes:subtitle>".$iteminfo['WOAF']."</itunes:subtitle>
<description>".$iteminfo['TIT2']." by ".$iteminfo['TPE1'].". ".$iteminfo['COMM']." Recorded on ".date ("r", filemtime($filename))." from stream URL: ".$iteminfo['WOAF']."</description>
<enclosure url=\"".$dir.$filename."\" length=\"".filesize($filename)."\" type=\"audio/mpeg\"/>
<guid>".$dir.$filename."</guid>
<pubDate>".date ("r", filemtime($filename))."</pubDate>
</item>";
}
print"
</channel>
</rss>";
?>