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
|
|
|
public function get($value) |
30
|
|
|
{ |
31
|
|
|
if (strlen($value) < 1) { |
32
|
|
|
return [ |
33
|
|
|
'status' => false, |
34
|
|
|
'message' => "Search keyword cannot be empty" |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$response = $this->search(new Request, $value); |
40
|
|
|
$output = [ |
41
|
|
|
'status' => true, |
42
|
|
|
'data' => $this->vendor->build($response) |
43
|
|
|
]; |
44
|
|
|
} catch (Exception $except) { |
45
|
|
|
$output = [ |
46
|
|
|
'status' => false, |
47
|
|
|
'message' => $except->getMessage() |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $output; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param string $value |
57
|
|
|
* @return array |
58
|
|
|
* @throws Exception |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
private function search(Request $request, $value) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$response = $request->create($this->vendor->generateUrl($value)); |
63
|
|
|
|
64
|
|
|
if (is_null($response)) { |
65
|
|
|
throw new Exception("Request to Itunes API failed", $request->getStatusCode()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return [ |
69
|
|
|
'search' => $response, |
70
|
|
|
'status' => true, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $feedUrl |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function find($feedUrl) |
79
|
|
|
{ |
80
|
|
|
if (strlen($feedUrl) < 1) { |
81
|
|
|
return [ |
82
|
|
|
'status' => false, |
83
|
|
|
'message' => "Feed Url cannot be empty" |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$response = $this->read(new Request, $feedUrl); |
89
|
|
|
|
90
|
|
|
libxml_use_internal_errors(true); |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
$feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false); |
94
|
|
|
} catch (Exception $except) { |
95
|
|
|
$response_repaired = Xml::repair($response['feed']); |
96
|
|
|
$feed = new SimpleXMLElement($response_repaired, LIBXML_NOCDATA, false); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
'status' => true, |
101
|
|
|
'data' => $this->vendor->buildFeed($feed) |
102
|
|
|
]; |
103
|
|
|
} catch (Exception $except) { |
104
|
|
|
return [ |
105
|
|
|
'status' => true, |
106
|
|
|
'message' => $except->getMessage() |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $limit |
113
|
|
|
* @return PodcastScraper |
114
|
|
|
*/ |
115
|
|
|
public function limit($limit) |
116
|
|
|
{ |
117
|
|
|
$this->vendor->setLimit($limit); |
118
|
|
|
$this->vendor->setDefaultQuery(); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Request $request |
125
|
|
|
* @param string $feedUrl |
126
|
|
|
* @return array |
127
|
|
|
* @throws Exception |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
private function read(Request $request, $feedUrl) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$output = $request->create($feedUrl); |
132
|
|
|
|
133
|
|
|
if (is_null($output)) { |
134
|
|
|
throw new Exception("Request to RSS failed", $request->getStatusCode()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
'feed' => $output, |
139
|
|
|
'status' => true, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.