-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoddie.php
More file actions
executable file
·215 lines (175 loc) · 7.13 KB
/
poddie.php
File metadata and controls
executable file
·215 lines (175 loc) · 7.13 KB
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
#!/usr/bin/env php
<?php
$poddie_verbose = false;
poddie_setup();
$opts = getopt("v");
foreach (array_keys($opts) as $opt) {
switch ($opt) {
case 'v':
$poddie_verbose = true;
}
}
$poddie_already_fetched = file_exists(PODDIE_FETCHED_LOGFILE) ? file_get_contents(PODDIE_FETCHED_LOGFILE) : "";
$downloaded_files_count = $poddie_config_line_number = $downloaded_files_size = 0;
$poddie_config = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", file(PODDIE_FEEDS_FILE));
foreach($poddie_config as $poddie_config_line) {
$poddie_config_line_number++;
$episodes_kept = 0;
if(empty(trim($poddie_config_line))) {
continue; //silently into the night
}
if(!is_valid_poddie_config_line($poddie_config_line)) {
echo "Feed config '$poddie_config' at line $poddie_config_line_number is not valid - skipping.\n";
continue;
}
list($podcast_title, $podcast_url, $episodes_to_keep) = explode(';', trim($poddie_config_line));
if(!is_feed_alive($podcast_url)) {
echo "$podcast_title ($podcast_url) does not exist - skipping.\n";
continue;
}
echo is_verbose() ? "Processing podcast: $podcast_title ($podcast_url), keeping last $episodes_to_keep episode" . plural($episodes_to_keep). "\n" : '';
$podcast_simplexml = simplexml_load_string(file_get_contents(trim($podcast_url)));
if(!$podcast_simplexml) {
echo "$podcast_title ($podcast_url) is not providing valid XML - skipping.\n";
continue;
}
if(!file_exists(PODDIE_PODCAST_STORAGE . "/$podcast_title")) {
echo "New podcast subscription detected: $podcast_title.\n";
mkdir(PODDIE_PODCAST_STORAGE . "/$podcast_title");
}
foreach($podcast_simplexml->channel->item as $item) {
if(++$episodes_kept >= $episodes_to_keep) break;
$url = (string) $item->enclosure['url'];
$episode_title_filename_extension = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
$episode_title_filename = date('Y-m-d', strtotime((string) $item->pubDate)) . " - " . sanitize_filename(remove_timestamp((string) $item->title)) . ".$episode_title_filename_extension";
if($url != '' && strpos($poddie_already_fetched, $url) === false) {
echo "$podcast_title: Fetching '$url' into '" . PODDIE_PODCAST_STORAGE . "/$podcast_title/$episode_title_filename'\n";
$downloaded_files_size += download($url, PODDIE_PODCAST_STORAGE . "/$podcast_title/$episode_title_filename");
$id3tag = substr($episode_title_filename, 0, strrpos($episode_title_filename, '.'));
exec(PODDIE_ID3TAG_BIN . " --song='$id3tag' '" . PODDIE_PODCAST_STORAGE . "/$podcast_title/$episode_title_filename'");
log_fetched($url);
$downloaded_files_count++;
}
}
$downloaded_files = scan_dir(PODDIE_PODCAST_STORAGE . "/$podcast_title");
for($index = intval($episodes_to_keep); $index <= count($downloaded_files) - 1; $index++) {
$file_to_remove = PODDIE_PODCAST_STORAGE . "/$podcast_title/{$downloaded_files[$index]}";
echo "Removing #$index from $podcast_title ($file_to_remove)\n";
unlink($file_to_remove);
}
}
$number_of_podcasts = count($poddie_config);
$human_downloaded_files_size = human_filesize($downloaded_files_size);
$output_downloaded = "Downloaded $downloaded_files_count file" . plural($downloaded_files_count) . " ($human_downloaded_files_size) from $number_of_podcasts podcast feed" . plural($number_of_podcasts) . ".\n";
if(is_verbose() || $downloaded_files_count > 0) {
echo $output_downloaded;
}
function poddie_setup() {
define("PODDIE_CONFIG_FILE", dirname($_SERVER['SCRIPT_FILENAME']) . "/poddie.config");
define("PODDIE_FEEDS_FILE", dirname($_SERVER['SCRIPT_FILENAME']) . "/poddie.feeds");
define("PODDIE_FETCHED_LOGFILE", dirname($_SERVER['SCRIPT_FILENAME']) . "/poddie.fetched");
define("PODDIE_ID3TAG_BIN", get_poddie_config("id3tag"));
define("PODDIE_PODCAST_STORAGE", get_poddie_config("podcast_storage"));
define("PODDIE_VERBOSE", FALSE);
date_default_timezone_set(get_poddie_config("timezone"));
verify_requirements();
}
function verify_requirements() {
require_php_extensions();
require_binaries();
}
function phpmodule_exists($module) {
return extension_loaded($module);
}
function require_php_extensions() {
foreach(array('SimpleXML') as $phpmodule) {
if(!phpmodule_exists($phpmodule)) {
poddie_die("ERROR: Poddie requirement - Missing PHP module: $phpmodule", 1);
}
}
}
function require_binaries() {
foreach(array(PODDIE_ID3TAG_BIN) as $binary) {
if(!command_exist($binary)) {
poddie_die("ERROR: Poddie requirement: Missing binary: $binary", 2);
}
}
}
function command_exist($cmd) {
$return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
return !empty($return);
}
function poddie_die($msg, $exitcode) {
fwrite(STDERR, "$msg\n");
exit($exitcode); // A response code other than 0 is a failure
}
function scan_dir($dir) {
$ignored = array('.', '..', '.svn', '.htaccess');
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}
arsort($files);
$files = array_keys($files);
return ($files) ? $files : false;
}
function log_fetched($podcast_url) {
$logfile = fopen(PODDIE_FETCHED_LOGFILE, 'a');
fwrite($logfile, "$podcast_url\n");
fclose($logfile);
}
function is_feed_alive($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($handle, CURLOPT_MAXREDIRS, 10);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return ($httpCode == 200);
}
function get_poddie_config($key) {
return parse_ini_file(PODDIE_CONFIG_FILE)[$key];
}
function download($file_source, $file_target) {
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
if ($rh===false || $wh===false) {
return false;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
return true;
}
}
fclose($rh);
fclose($wh);
return filesize($file_target);
}
function remove_timestamp($str) {
return preg_replace('/(\d{1,2})[[:punct:]](\d{1,2})[[:punct:]](\d{2,4})/', '', $str);
}
function sanitize_filename($str) {
$str = trim(preg_replace("/[^a-zæøåA-ZÆØÅ0-9.\-]/", " ", $str));
$str = str_replace('..', '.', $str);
$str = str_replace(" ", " ", $str);
return $str;
}
function is_valid_poddie_config_line($str) {
return substr_count($str, ';') == 2;
}
function plural($num) {
if(!empty($num) && is_numeric($num) && $num > 1)
return 's';
}
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor > 0) $sz = 'KMGT';
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B';
}
function is_verbose() {
global $poddie_verbose;
return $poddie_verbose;
}
?>