1
|
|
|
<?php |
2
|
|
|
namespace Tzsk\ScrapePod; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use SimpleXMLElement; |
6
|
|
|
use Tzsk\ScrapePod\Contracts\VendorInterface; |
7
|
|
|
use Tzsk\ScrapePod\Helpers\Request; |
8
|
|
|
use Tzsk\ScrapePod\Helpers\Xml; |
9
|
|
|
|
10
|
|
|
class PodcastScraper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var VendorInterface |
14
|
|
|
*/ |
15
|
|
|
private $vendor; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param VendorInterface $vendor |
19
|
|
|
*/ |
20
|
|
|
public function __construct(VendorInterface $vendor) |
21
|
|
|
{ |
22
|
|
|
$this->vendor = $vendor; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $value |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function get($value) |
30
|
|
|
{ |
31
|
|
|
if (strlen($value) < 1) { |
32
|
|
|
return $this->failedResponse("Search keyword cannot be empty"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$response = $this->search(new Request, $value); |
37
|
|
|
return [ |
38
|
|
|
'status' => true, |
39
|
|
|
'data' => $this->vendor->build($response) |
40
|
|
|
]; |
41
|
|
|
} catch (Exception $except) { |
42
|
|
|
return $this->failedResponse($except->getMessage()); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Request $request |
48
|
|
|
* @param string $value |
49
|
|
|
* @return array |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
private function search(Request $request, $value) |
53
|
|
|
{ |
54
|
|
|
$response = $request->create($this->vendor->generateUrl($value)); |
55
|
|
|
|
56
|
|
|
if (is_null($response)) { |
57
|
|
|
throw new Exception("Request to Itunes API failed", $request->getStatusCode()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'search' => $response, |
62
|
|
|
'status' => true, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $feedUrl |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function find($feedUrl) |
71
|
|
|
{ |
72
|
|
|
if (strlen($feedUrl) < 1) { |
73
|
|
|
return $this->failedResponse("Feed Url cannot be empty"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
return [ |
78
|
|
|
'status' => true, |
79
|
|
|
'data' => $this->vendor->buildFeed($this->getFeedFromUrl($feedUrl)) |
80
|
|
|
]; |
81
|
|
|
} catch (Exception $except) { |
82
|
|
|
return $this->failedResponse($except->getMessage()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $limit |
88
|
|
|
* @return PodcastScraper |
89
|
|
|
*/ |
90
|
|
|
public function limit($limit) |
91
|
|
|
{ |
92
|
|
|
$this->vendor->setLimit($limit); |
93
|
|
|
$this->vendor->setDefaultQuery(); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return PodcastScraper |
100
|
|
|
*/ |
101
|
|
|
public function original() |
102
|
|
|
{ |
103
|
|
|
$this->vendor->original(); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Request $request |
110
|
|
|
* @param string $feedUrl |
111
|
|
|
* @return array |
112
|
|
|
* @throws Exception |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
private function read(Request $request, $feedUrl) |
115
|
|
|
{ |
116
|
|
|
$output = $request->create($feedUrl); |
117
|
|
|
|
118
|
|
|
if (is_null($output)) { |
119
|
|
|
throw new Exception("Request to RSS failed", $request->getStatusCode()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return [ |
123
|
|
|
'feed' => $output, |
124
|
|
|
'status' => true, |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $feedUrl |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
protected function getFeedFromUrl($feedUrl) |
134
|
|
|
{ |
135
|
|
|
$response = $this->read(new Request, $feedUrl); |
136
|
|
|
|
137
|
|
|
libxml_use_internal_errors(true); |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false); |
141
|
|
|
} catch (Exception $except) { |
142
|
|
|
$feed = new SimpleXMLElement(Xml::repair($response['feed']), LIBXML_NOCDATA, false); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $feed; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $message |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
protected function failedResponse($message) |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
'status' => false, |
157
|
|
|
'message' => $message |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|