forked from VerifiedJoseph/rss-bridge-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBCProgrammesBridge.php
108 lines (80 loc) · 2.87 KB
/
BBCProgrammesBridge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
class BBCProgrammesBridge extends BridgeAbstract {
const NAME = 'BBC Programmes Bridge';
const URI = 'https://www.bbc.co.uk/programmes';
const DESCRIPTION = 'Returns programme episodes available on iPlayer';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = array(array(
'id' => array(
'name' => 'Programme ID',
'type' => 'text',
'required' => true,
'exampleValue' => 'b006t14n',
)
)
);
const CACHE_TIMEOUT = 1800; // 30 mins
private $feedName = '';
public function collectData() {
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request: ' . $this->getURI());
$this->feedName = $html->find('div[class="br-masthead__title"]', 0)->plaintext;
$results = $html->find('div.br-box-page.programmes-page', 0);
foreach($results->find('div.programme.programme--tv') as $index => $div) {
$item = array();
$programmeId = $div->attr['data-pid'];
$programmeTitle = $div->find('h2[class="programme__titles"]', 0)->plaintext;
$programmePath = 'https://www.bbc.co.uk/iplayer/episode/' . $programmeId;
$programmePage = getSimpleHTMLDOMCached($programmePath, 3600)
or returnServerError('Could not request: ' . $programmePath);
$image = $programmePage->find('meta[property="og:image"]', 0)->content;
$json = $this->extractJson($programmePage);
$description = $this->getSynopsis($json);
$duration = $json->versions[0]->duration->text;
$date = $json->versions[0]->firstBroadcast;
$availability = $json->versions[0]->availability->remaining->text;
$item['uri'] = $programmePath;
$item['title'] = $json->episode->subtitle . ' (' . $duration . ')';
$item['content'] = <<<EOD
<a title="Watch on iPlayer" href="{$programmePath}"><img src="{$image}"></a><hr>Published: {$date} - Duration: {$duration} - {$availability}<hr><p>{$description}</p>
EOD;
$item['timestamp'] = $date;
$item['enclosures'][] = $image;
$this->items[] = $item;
if (count($this->items) >= 10) {
break;
}
}
}
public function getURI() {
if (!is_null($this->getInput('id'))) {
return self::URI . '/' . $this->getInput('id') . '/episodes/player';
}
return parent::getURI();
}
public function getName() {
if ($this->feedName) {
return $this->feedName . ' - BBC';
}
return parent::getName();
}
private function extractJson($html) {
$data = $html->find('script#tvip-script-app-store', 0)->innertext;
$data = str_replace('window.__IPLAYER_REDUX_STATE__ = ', '', $data);
$data = substr($data, 0, -1);
$data = json_decode($data);
if ($data === false) {
returnServerError('Failed to decode extracted data');
}
return $data;
}
private function getSynopsis($json) {
$sizes = array('large', 'medium' , 'small', 'editorial');
foreach ($sizes as $size) {
if(isset($json->episode->synopses->{$size})) {
return nl2br($json->episode->synopses->{$size});
}
}
return '';
}
}