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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
<?php
// A script to convert an RSS feed into a podcast
// Configuration
$feed = 'https://www.deutschlandfunk.de/kalenderblatt.870.de.rss';
$downloads = 'audio';
$temp = 'temp';
// Setup
if (!is_dir($downloads)) {
mkdir($downloads);
}
if (!is_dir($temp)) {
mkdir($temp);
}
// Set proper MIME type and encoding
header('Content-Type: application/rss+xml; charset=utf-8');
// Get the original feed
$xml = simplexml_load_file($feed);
// Write podcast feed intro
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">';
echo '<channel>';
echo '<title>'.$xml->channel->title.'</title>';
echo '<link>https://www.deutschlandfunk.de/kalenderblatt.870.de.html</link>';
echo '<description>Das Kalenderblatt stellt historische Ereignisse von Bedeutung oder von Relevanz in anschaulicher Weise dar.</description>';
echo '<category>'.$xml->channel->category.'</category>';
echo '<copyright>'.$xml->channel->copyright.'</copyright>';
echo '<language>'.$xml->channel->language.'</language>';
echo '<pubDate>'.$xml->channel->pubDate.'</pubDate>';
echo '<lastBuildDate>'.$xml->channel->lastBuildDate.'</lastBuildDate>';
echo '<ttl>'.$xml->channel->ttl.'</ttl>';
echo '<image>';
echo ' <url>https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/image.png</url>';
echo ' <title>'.$xml->channel->image->title.'</title>';
echo ' <link>'.$xml->channel->image->link.'</link>';
echo ' <description>'.$xml->channel->image->description.'</description>';
echo '</image>';
echo '<atom:link rel="self" type="application/rss+xml" href="https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'" />';
echo '<itunes:subtitle>Die Beiträge zur Sendung</itunes:subtitle>';
echo '<itunes:image href="https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/image.png"/>';
echo '<itunes:new-feed-url>https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'</itunes:new-feed-url>';
echo '<itunes:owner>';
echo ' <itunes:name>Redaktion deutschlandradio.de</itunes:name>';
echo ' <itunes:email>podcast@deutschlandradio.de</itunes:email>';
echo '</itunes:owner>';
echo '<itunes:author>Deutschlandfunk</itunes:author>';
echo '<itunes:explicit>No</itunes:explicit>';
echo '<itunes:category text="History" />';
// Write podcast episodes
$containing = array();
foreach ($xml->channel->item as $item) {
$content = getPageContent($item->link);
// Only add episode to feed if media file is available
if ($content[0] != 'XXX') {
$id = md5($item->guid);
$filename = '';
$filetype = '';
if (str_ends_with($content[1], '.mp3')) {
$filename = $id.'.mp3';
$filetype = 'audio/mpeg';
if (!file_exists($downloads.'/'.$filename)) {
downloadSingleMediaFile($content[1], $id, $downloads);
}
} else {
$filename = $id.'.aac';
$filetype = 'audio/aac';
// Download episode if not yet done
if (!file_exists($downloads.'/'.$filename)) {
foreach (glob($temp.'/*') as $file) {
if (is_file($file)) {
unlink($file);
}
}
downloadMediaFile($content[1], $id, $temp, $downloads);
}
}
array_push($containing, $downloads.'/'.$filename);
// Write episode to podcast feed
echo '<item>';
echo ' <title>'.$item->title.'</title>';
echo ' <link>https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/'.$downloads.'/'.$filename.'</link>';
echo ' <description>'.str_replace("www.deutschlandfunk.de,", "", strip_tags($item->description)).'</description>';
echo ' <pubDate>'.$item->pubDate.'</pubDate>';
echo ' <guid>'.$item->guid.'</guid>';
echo ' <enclosure url="https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/'.$downloads.'/'.$filename.'" length="'.filesize($downloads.'/'.$filename).'" type="'.$filetype.'"/>';
echo ' <itunes:author>'.$content[0].'</itunes:author>';
echo ' <itunes:duration>'.$content[2].'</itunes:duration>';
echo '</item>';
}
}
// Remove unused episode media files
foreach (glob($downloads.'/*') as $file) {
if(is_file($file) && !in_array($file, $containing)) {
unlink($file);
}
}
// Write podcast feed end
echo '</channel>';
echo '</rss>';
///// --- FUNCTIONS --- \\\\\
// Get selected content from episode website
function getPageContent($site) {
$dom = new DomDocument();
$dom->loadHTML(download($site));
$xpath = new DOMXpath($dom);
$player = $xpath->query("//button[@data-audio-series='Kalenderblatt']");
$author = $xpath->query("//p[@class='article-header-author']");
if ($player->length == 0) {
return array('XXX');
}
$authorname = str_replace('Von ', '', $author->item(0)->nodeValue);
$authorname = trim(substr($authorname , 0, strpos($authorname, '|')));
$minutes = intdiv(intval($player->item(0)->getAttribute('data-audio-duration')), 60);
$seconds = intval($player->item(0)->getAttribute('data-audio-duration')) % 60;
$duration = $minutes.':'.sprintf('%02d', $seconds);
// Returns array [episode author name | media file url | episode duration]
return array($authorname, $player->item(0)->getAttribute('data-audio-src'), $duration);
}
// Download episode media file
function downloadMediaFile($url, $id, $temp, $downloads) {
$regex = "/([^\n\r]+)/m";
// Download first playlist and get "inner" playlist
preg_match_all($regex, download($url), $lines);
$playlist = getUrlBase($url).(array_values(array_filter($lines[1], "isNotCommentLine"))[0]);
// Download contents of "inner" playlist
preg_match_all($regex, download($playlist), $urls);
// Download all media file segments
$counter = 0;
foreach (array_filter($urls[1], "isNotCommentLine") as $url) {
$outfile = fopen($temp.'/'.$counter.'.ts', 'wb') or exit('File open failed');
$curl = curl_init();
curl_setopt($curl, CURLOPT_FILE, $outfile);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_URL, getUrlBase($playlist).$url);
curl_exec($curl);
curl_close($curl);
fclose($outfile);
file_put_contents($temp.'/list.txt', 'file '.$counter.'.ts'."\n", FILE_APPEND);
$counter++;
}
// Concatenate segments to media file
$ffmpeg = './ffmpeg -f concat -i '.$temp.'/list.txt -c copy -bsf:a aac_adtstoasc '.$downloads.'/'.$id.'.aac';
exec($ffmpeg);
}
// Download episode media file
function downloadSingleMediaFile($url, $id, $downloads) {
$outfile = fopen($downloads.'/'.$id.'.mp3', 'wb') or exit('File open failed');
$curl = curl_init();
curl_setopt($curl, CURLOPT_FILE, $outfile);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_URL, $url);
curl_exec($curl);
curl_close($curl);
fclose($outfile);
}
// Download URL and return content
function download($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$download = curl_exec($curl);
curl_close($curl);
return $download;
}
// Check if a string does not look like a comment line
function isNotCommentLine($var) {
return (strcmp( substr( $var , 0, 1 ) , '#' ) !== 0);
}
// Get the base of an URL (= URL without last element)
function getUrlBase($url) {
return substr($url, 0, strripos($url, '/') + 1);
}
?>
|