-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpodarchiver.php
161 lines (122 loc) · 4.48 KB
/
podarchiver.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
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
<?php
$archiver = new PodArchiver();
$archiver->run();
class PodArchiver
{
private $config;
private $targetDir;
public function __construct(string $configFileName = 'config/config.yaml')
{
if (!file_exists($configFileName)) {
throw new Exception("Config file not found: {$configFileName}");
}
$this->config = yaml_parse_file($configFileName);
$this->targetDir = array_key_exists('target_dir', $this->config) ? $this->config['target_dir'] : 'podcasts';
}
public function run()
{
echo "\nPodArchiver STARTED\n";
if (!file_exists($this->targetDir)) {
echo "\nCreating target directory";
mkdir($this->targetDir);
}
foreach ($this->config['feeds'] as $feedName => $feed) {
echo "\n\n\tFeed: {$feedName}";
$feed['name'] = $feedName;
$this->treatFeed($feed);
}
echo "\n\nPodArchiver DONE\n\n";
}
public function treatFeed(array $feedConfig)
{
if (array_key_exists('disabled', $feedConfig) && $feedConfig['disabled']) {
echo "\n\n\t\tSkipping podcast because it is disabled";
return;
}
$feedUrl = $feedConfig['feed_url'];
$feedDirName = $feedConfig['directory_name'];
$filenameRegexp = !empty($feedConfig['filename_regexp']) ? $feedConfig['filename_regexp'] : false;
$filenameOutput = !empty($feedConfig['filename_output']) ? $feedConfig['filename_output'] : false;
$feedDir = sprintf('%s/%s', $this->targetDir, $feedDirName);
if (!file_exists($feedDir)) {
echo "\n\tCreating podcast's target directory";
mkdir($feedDir);
chmod($feedDir, 0777);
}
$blogFeed = new BlogFeed($feedUrl);
foreach ($blogFeed->getPosts() as $post) {
$filename = explode('?', basename($post->enclosure))[0];
echo "\n\n\t\tTreating file ".$filename;
echo "\n\t\tTitle: ".$post->title;
if ($filenameRegexp && $filenameOutput) {
$filename = preg_replace($filenameRegexp, $filenameOutput, $filename);
$filename = $this->replaceFilenameVariables($filename, $post);
if ('.' !== dirname($filename)) {
$subDir = sprintf('%s/%s', $feedDir, dirname($filename));
if (!file_exists($subDir)) {
mkdir($subDir);
chmod($subDir, 0777);
}
}
}
$targetFilePath = sprintf('%s/%s', $feedDir, $filename);
if ($filenameRegexp && $filenameOutput) {
echo "\n\t\tas target filename ".$filename;
}
if (file_exists($targetFilePath)) {
echo "\n\t\tSkipping because file already exists";
continue;
}
echo "\n\t\tDownloading ... ";
$file = file_get_contents($post->enclosure);
file_put_contents($targetFilePath, $file);
chmod($targetFilePath, 0666);
echo 'finished';
sleep(1);
}
}
private function replaceFilenameVariables(string $filename, BlogPost $post): string
{
foreach ($this->getVariablesFromTimestamp($post->timestamp) as $field => $value) {
$filename = str_replace($field, $value, $filename);
}
return str_replace('{title}', $post->title, $filename);
}
private function getVariablesFromTimestamp($timestamp): array
{
$fieldNames = str_split('YmdHis');
$variables = [];
foreach ($fieldNames as $field) {
$variables[sprintf('{%s}', $field)] = date($field, $timestamp);
}
return $variables;
}
}
class BlogPost
{
public $timestamp;
public $enclosure;
public $title;
}
class BlogFeed
{
private $posts = [];
public function __construct($url)
{
if (!($x = simplexml_load_file($url))) {
echo "\n\t\tERROR: Feed could not be loaded: {$url}\n";
return;
}
foreach ($x->channel->item as $item) {
$post = new BlogPost();
$post->timestamp = strtotime($item->pubDate);
$post->enclosure = (string) $item->enclosure->attributes()['url'];
$post->title = trim($item->title);
$this->posts[] = $post;
}
}
public function getPosts()
{
return $this->posts;
}
}