1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Bernhard Posselt <[email protected]> |
9
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\News\Fetcher; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class YoutubeFetcher implements IFeedFetcher { |
16
|
|
|
|
17
|
|
|
private $feedFetcher; |
18
|
|
|
|
19
|
|
|
public function __construct(FeedFetcher $feedFetcher){ |
20
|
|
|
$this->feedFetcher = $feedFetcher; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
private function buildUrl($url) { |
25
|
|
|
$baseRegex = '%(?:https?://|//)?(?:www.)?youtube.com'; |
26
|
|
|
$playRegex = $baseRegex . '.*?list=([^&]*)%'; |
27
|
|
|
|
28
|
|
|
if (preg_match($playRegex, $url, $matches)) { |
29
|
|
|
$id = $matches[1]; |
30
|
|
|
return 'http://gdata.youtube.com/feeds/api/playlists/' . $id; |
31
|
|
|
} else { |
32
|
|
|
return $url; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* This fetcher handles all the remaining urls therefore always returns true |
39
|
|
|
*/ |
40
|
|
|
public function canHandle($url){ |
41
|
|
|
return $this->buildUrl($url) !== $url; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetch a feed from remote |
47
|
|
|
* @param string $url remote url of the feed |
48
|
|
|
* @param boolean $getFavicon if the favicon should also be fetched, |
49
|
|
|
* defaults to true |
50
|
|
|
* @param string $lastModified a last modified value from an http header |
51
|
|
|
* defaults to false. If lastModified matches the http header from the feed |
52
|
|
|
* no results are fetched |
53
|
|
|
* @param string $etag an etag from an http header. |
54
|
|
|
* If lastModified matches the http header from the feed |
55
|
|
|
* no results are fetched |
56
|
|
|
* @param bool fullTextEnabled if true tells the fetcher to enhance the |
57
|
|
|
* articles by fetching custom enhanced content |
58
|
|
|
* @param string $basicAuthUser if given, basic auth is set for this feed |
59
|
|
|
* @param string $basicAuthPassword if given, basic auth is set for this |
60
|
|
|
* feed. Ignored if user is null or an empty string |
61
|
|
|
* @throws FetcherException if it fails |
62
|
|
|
* @return array an array containing the new feed and its items, first |
63
|
|
|
* element being the Feed and second element being an array of Items |
64
|
|
|
*/ |
65
|
|
|
public function fetch($url, $getFavicon=true, $lastModified=null, |
66
|
|
|
$etag=null, $fullTextEnabled=false, |
67
|
|
|
$basicAuthUser=null, $basicAuthPassword=null) { |
68
|
|
|
$transformedUrl = $this->buildUrl($url); |
69
|
|
|
|
70
|
|
|
$result = $this->feedFetcher->fetch( |
71
|
|
|
$transformedUrl, $getFavicon, $lastModified, $etag, |
72
|
|
|
$fullTextEnabled, $basicAuthUser, $basicAuthPassword |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
// reset feed url so we know the correct added url for the feed |
76
|
|
|
$result[0]->setUrl($url); |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|