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
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected $isOrginal = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param SimpleXMLElement $feed |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function buildFeed(SimpleXMLElement $feed) |
21
|
|
|
{ |
22
|
|
|
$output = [ |
23
|
|
|
'title' => (string) $feed->channel->title, |
24
|
|
|
'description' => (string) $feed->channel->description, |
25
|
|
|
'summary' => (string) $this->getValueByPath($feed->channel, "summary"), |
26
|
|
|
'image' => (string) $feed->channel->image->url, |
27
|
|
|
'site' => (string) $feed->channel->link, |
28
|
|
|
'author' => (string) $this->getValueByPath($feed->channel, "author"), |
29
|
|
|
'language' => (string) $feed->channel->language, |
30
|
|
|
'categories' => $this->fetchCategories($feed->channel), |
31
|
|
|
'episode_count' => (int) count($feed->channel->item), |
32
|
|
|
'episodes' => $this->getEpisodes($feed->channel) |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
return $output; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param SimpleXMLElement|mixed $channel |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
protected function getEpisodes($channel) |
44
|
|
|
{ |
45
|
|
|
$items = []; |
46
|
|
|
foreach ($channel->item as $value) { |
47
|
|
|
$items[] = [ |
48
|
|
|
'title' => (string) $value->title, |
49
|
|
|
'mp3' => $this->getAudioUrl($value), |
50
|
|
|
'size' => $this->getEpisodeSize($value), |
51
|
|
|
'duration' => $this->getEpisodeDuration($value), |
52
|
|
|
'description' => (string) $value->description, |
53
|
|
|
'keywords' => $this->getKeywords($value), |
54
|
|
|
'link' => (string) $value->link, |
55
|
|
|
'image' => $this->getEpisodeImage($value, $channel), |
56
|
|
|
'published_at' => $this->getPublishedDate($value), |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $items; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function getKeywords($item) |
67
|
|
|
{ |
68
|
|
|
return array_map('trim', explode(",", $this->getValueByPath($item, 'keywords'))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param SimpleXMLElement $value |
73
|
|
|
* |
74
|
|
|
* @return null|string |
75
|
|
|
*/ |
76
|
|
|
protected function getAudioUrl($value) |
77
|
|
|
{ |
78
|
|
|
return isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param SimpleXMLElement $value |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
protected function getEpisodeSize($value) |
87
|
|
|
{ |
88
|
|
|
return isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param SimpleXMLElement $item |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function getPublishedDate($item) |
97
|
|
|
{ |
98
|
|
|
$published_at = new DateTime(); |
99
|
|
|
$published_at->setTimestamp(strtotime($item->pubDate)); |
100
|
|
|
|
101
|
|
|
return $published_at->format('Y-m-d H:i:s'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param SimpleXMLElement|mixed $item |
106
|
|
|
* @param string $path |
107
|
|
|
* |
108
|
|
|
* @return SimpleXMLElement |
109
|
|
|
*/ |
110
|
|
|
protected function getValueByPath($item, $path) |
111
|
|
|
{ |
112
|
|
|
return empty($item->xpath("itunes:{$path}")) ? null : |
113
|
|
|
$item->xpath("itunes:{$path}")[0]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param SimpleXMLElement|mixed $item |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
protected function getEpisodeDuration($item) |
122
|
|
|
{ |
123
|
|
|
$duration = (string) $this->getValueByPath($item, "duration"); |
124
|
|
|
|
125
|
|
|
$durationArray = explode(":", $duration); |
126
|
|
|
if (count($durationArray) > 1) { |
127
|
|
|
sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds); |
128
|
|
|
|
129
|
|
|
$duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return (int) $duration; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param SimpleXMLElement $item |
137
|
|
|
* |
138
|
|
|
* @param SimpleXMLElement $channel |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function getEpisodeImage($item, $channel) |
143
|
|
|
{ |
144
|
|
|
$xmlImage = $this->getValueByPath($item, "image"); |
145
|
|
|
|
146
|
|
|
return $xmlImage ? (string) $xmlImage->attributes()->href : (string) $channel->image->url; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param SimpleXMLElement|mixed $channel |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function fetchCategories($channel) |
155
|
|
|
{ |
156
|
|
|
$categories = $channel->xpath('itunes:category'); |
157
|
|
|
|
158
|
|
|
$items = []; |
159
|
|
|
foreach ($categories as $category) { |
160
|
|
|
$item = ['title' => (string) $category->attributes()->text, 'children' => []]; |
161
|
|
|
|
162
|
|
|
if (! empty($category->xpath('itunes:category'))) { |
163
|
|
|
$inner = $this->fetchCategories($category); |
164
|
|
|
|
165
|
|
|
$item['children'] = $inner; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$items[] = $item; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $items; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|