|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\ScrapePod\Vendors; |
|
3
|
|
|
|
|
4
|
|
|
use DateTime; |
|
5
|
|
|
use function GuzzleHttp\Psr7\str; |
|
6
|
|
|
use SimpleXMLElement; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractVendor |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param SimpleXMLElement $feed |
|
12
|
|
|
* |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function buildFeed(SimpleXMLElement $feed) |
|
16
|
|
|
{ |
|
17
|
|
|
$output = [ |
|
18
|
|
|
'title' => (string) $feed->channel->title, |
|
19
|
|
|
'description' => (string) $feed->channel->description, |
|
20
|
|
|
'summary' => (string) $this->getValueByPath($feed->channel, "summary"), |
|
21
|
|
|
'image' => (string) $feed->channel->image->url, |
|
22
|
|
|
'site' => (string) $feed->channel->link, |
|
23
|
|
|
'author' => (string) $this->getValueByPath($feed->channel, "author"), |
|
24
|
|
|
'language' => (string) $feed->channel->language, |
|
25
|
|
|
'categories' => $this->fetchCategories($feed->channel), |
|
26
|
|
|
'episode_count' => (int) count($feed->channel->item), |
|
27
|
|
|
'episodes' => $this->getEpisodes($feed->channel) |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
return $output; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param SimpleXMLElement|mixed $channel |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function getEpisodes($channel) |
|
39
|
|
|
{ |
|
40
|
|
|
$items = []; |
|
41
|
|
|
foreach($channel->item as $value) { |
|
42
|
|
|
$items[] = [ |
|
43
|
|
|
'title' => (string) $value->title, |
|
44
|
|
|
'mp3' => $this->getAudioUrl($value), |
|
45
|
|
|
'size' => $this->getEpisodeSize($value), |
|
46
|
|
|
'duration' => $this->getEpisodeDuration($value), |
|
47
|
|
|
'description' => (string) $value->description, |
|
48
|
|
|
'link' => (string) $value->link, |
|
49
|
|
|
'image' => $this->getEpisodeImage($value, $channel), |
|
50
|
|
|
'published_at' => $this->getPublishedDate($value), |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $items; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param SimpleXMLElement $value |
|
59
|
|
|
* |
|
60
|
|
|
* @return null|string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getAudioUrl($value) |
|
63
|
|
|
{ |
|
64
|
|
|
return isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param SimpleXMLElement $value |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getEpisodeSize($value) |
|
73
|
|
|
{ |
|
74
|
|
|
return isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param SimpleXMLElement $item |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getPublishedDate($item) |
|
83
|
|
|
{ |
|
84
|
|
|
$published_at = new DateTime(); |
|
85
|
|
|
$published_at->setTimestamp(strtotime($item->pubDate)); |
|
86
|
|
|
|
|
87
|
|
|
return $published_at->format('Y-m-d H:i:s'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param SimpleXMLElement|mixed $item |
|
92
|
|
|
* @param string $path |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getValueByPath($item, $path) |
|
97
|
|
|
{ |
|
98
|
|
|
return empty($item->xpath("itunes:{$path}")) ? null : |
|
99
|
|
|
$item->xpath("itunes:{$path}")[0]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param SimpleXMLElement|mixed $item |
|
104
|
|
|
* |
|
105
|
|
|
* @return int |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getEpisodeDuration($item) |
|
108
|
|
|
{ |
|
109
|
|
|
$duration = (string) $this->getValueByPath($item, "duration"); |
|
110
|
|
|
|
|
111
|
|
|
$durationArray = explode(":", $duration); |
|
112
|
|
|
if (count($durationArray) > 1) { |
|
113
|
|
|
sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return (int) $duration; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param SimpleXMLElement $item |
|
123
|
|
|
* |
|
124
|
|
|
* @param SimpleXMLElement $channel |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getEpisodeImage($item, $channel) |
|
129
|
|
|
{ |
|
130
|
|
|
$xmlImage = $this->getValueByPath($item, "image"); |
|
131
|
|
|
|
|
132
|
|
|
return $xmlImage ? (string) $xmlImage->attributes()->href : (string) $channel->image->url; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param SimpleXMLElement|mixed $channel |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function fetchCategories($channel) |
|
141
|
|
|
{ |
|
142
|
|
|
$categories = $channel->xpath('itunes:category'); |
|
143
|
|
|
|
|
144
|
|
|
$items = []; |
|
145
|
|
|
foreach ( $categories as $category ) { |
|
146
|
|
|
$item = ['title' => (string) $category->attributes()->text, 'children' => []]; |
|
147
|
|
|
|
|
148
|
|
|
if (! empty($category->xpath('itunes:category'))) { |
|
149
|
|
|
$inner = $this->fetchCategories($category); |
|
150
|
|
|
|
|
151
|
|
|
$item['children'] = $inner; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$items[] = $item; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $items; |
|
158
|
|
|
} |
|
159
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.