1
|
|
|
<?php |
2
|
|
|
namespace Tzsk\ScrapePod\Vendors; |
3
|
|
|
|
4
|
|
|
use SimpleXMLElement; |
5
|
|
|
use Tzsk\ScrapePod\Contracts\VendorInterface; |
6
|
|
|
|
7
|
|
|
class DigitalPodcast extends AbstractVendor implements VendorInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
const APP_ID = "podcastsearchdemo"; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
const SEARCH_URL = "http://api.digitalpodcast.com/v2r/search"; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const FORMAT = "rss"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $limit = 15; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $defaultQuery = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* DigitalPodcast constructor. |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->setDefaultQuery(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function getLimit() |
46
|
|
|
{ |
47
|
|
|
return $this->limit; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $limit |
52
|
|
|
*/ |
53
|
|
|
public function setLimit($limit) |
54
|
|
|
{ |
55
|
|
|
// `-1` being used here because the API returns 3 items when `results=2`. |
56
|
|
|
$this->limit = ((int) $limit - 1); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function setDefaultQuery() |
63
|
|
|
{ |
64
|
|
|
$this->defaultQuery = http_build_query([ |
65
|
|
|
'results' => $this->limit, |
66
|
|
|
'appid' => self::APP_ID, |
67
|
|
|
'format' => self::FORMAT |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $value |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function generateUrl($value) |
76
|
|
|
{ |
77
|
|
|
$value = urlencode($value); |
78
|
|
|
$url = self::SEARCH_URL . "?keywords={$value}"; |
79
|
|
|
|
80
|
|
|
return $url . '&' . $this->defaultQuery; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $response |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function build(array $response) |
88
|
|
|
{ |
89
|
|
|
$xml = new SimpleXMLElement($response['search']); |
90
|
|
|
$xml = $xml->channel; |
91
|
|
|
|
92
|
|
|
$output['result_count'] = count($xml->item); |
93
|
|
|
|
94
|
|
|
foreach ($xml->item as $value) { |
95
|
|
|
$output['results'][] = [ |
96
|
|
|
'title' => (string) $value->title, |
97
|
|
|
'rss' => (string) $value->source, |
98
|
|
|
'link' => (string) $value->link, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $output; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function original() |
109
|
|
|
{ |
110
|
|
|
$this->isOrginal = true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|