1: <?php
2: /**
3: * This file is part of the Podcast Crawler package.
4: *
5: * Copyright (c) 2016 Dorian Neto
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: namespace PodcastCrawler;
12:
13: use SimpleXMLElement;
14: use Exception;
15:
16: /**
17: * Class Podcastcrawler\Podcastcrawler enables the search for podcasts to get details and mp3 files through many APIs
18: *
19: * @version v1.1.0
20: * @link https://github.com/podcastcrawler/podcastcrawler
21: * @license https://github.com/podcastcrawler/podcastcrawler/blob/master/LICENSE.md MIT
22: * @copyright 2016 Podcast Crawler
23: * @author Dorian Neto <doriansampaioneto@gmail.com>
24: */
25: class PodcastCrawler
26: {
27: /**
28: * The provider
29: *
30: * @var ProviderInterface
31: */
32: private $provider;
33:
34: /**
35: * The construct of the object
36: *
37: * @param ProviderInterface $provider
38: */
39: public function __construct(ProviderInterface $provider)
40: {
41: $this->provider = $provider;
42: }
43:
44: /**
45: * Returns the podcasts
46: *
47: * @param string $value The keyword
48: * @return array
49: */
50: public function get($value)
51: {
52: try {
53: $response = $this->search(new Request, $value);
54: $output = $this->provider->build($response);
55: } catch (Exception $except) {
56: $output = [
57: 'status_code' => $except->getCode(),
58: 'message' => $except->getMessage()
59: ];
60: }
61:
62: return $output;
63: }
64:
65: /**
66: * Get podcasts sought by the term
67: *
68: * @param Request $request The Request object
69: * @param string $value The URL-encoded keyword you want to search for
70: * @return array
71: * @throws Exception
72: */
73: private function search(Request $request, $value)
74: {
75: $response = $request->create($this->provider->generateUrl($value));
76:
77: if (is_null($response)) {
78: throw new Exception("Request to Itunes API failed", $request->getStatusCode());
79: }
80:
81: return [
82: 'search' => $response,
83: 'status_code' => $request->getStatusCode(),
84: ];
85: }
86:
87: /**
88: * Returns the podcast details
89: *
90: * @param string $feedUrl The podcast feed URL
91: * @return array
92: */
93: public function find($feedUrl)
94: {
95: try {
96: $response = $this->read(new Request, $feedUrl);
97:
98: libxml_use_internal_errors(true);
99:
100: try {
101: $feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false);
102: } catch (Exception $except) {
103: $response_repaired = Xml::repair($response['feed']);
104: $feed = new SimpleXMLElement($response_repaired, LIBXML_NOCDATA, false);
105: }
106:
107: return $this->provider->buildFeed($feed);
108: } catch (Exception $except) {
109: return [
110: 'status_code' => $except->getCode(),
111: 'message' => $except->getMessage()
112: ];
113: }
114: }
115:
116: /**
117: * Set a limit to search
118: * @param int $limit
119: * @return PodcastCrawler\PodcastCrawler
120: */
121: public function limit($limit)
122: {
123: $this->provider->setLimit($limit);
124: $this->provider->setDefaultQuery();
125:
126: return $this;
127: }
128:
129: /**
130: * Get podcasts RSS sought by feed URL
131: *
132: * @param Request $request The Request object
133: * @param string $feedUrl The podcast feed URL
134: * @return array
135: * @throws Exception
136: */
137: private function read(Request $request, $feedUrl)
138: {
139: $output = $request->create($feedUrl);
140:
141: if (is_null($output)) {
142: throw new Exception("Request to RSS failed", $request->getStatusCode());
143: }
144:
145: return [
146: 'feed' => $output,
147: 'status_code' => $request->getStatusCode(),
148: ];
149: }
150: }
151: