-
Notifications
You must be signed in to change notification settings - Fork 1
/
TheGuardianCartoonsBridge.php
94 lines (72 loc) · 2.84 KB
/
TheGuardianCartoonsBridge.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
<?php
class TheGuardianCartoonsBridge extends FeedExpander {
const NAME = 'The Guardian Cartoons Bridge';
const URI = 'https://www.theguardian.com';
const DESCRIPTION = 'Returns the newest cartoons from The Guardian.';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = array(array(
'cartoon' => array(
'name' => 'Cartoon',
'type' => 'list',
'values' => array(
'All Cartoons' => 'cartoons/archive',
'Guardian Opinion cartoon' => 'commentisfree/series/guardian-comment-cartoon',
'Tom Gauld\'s cultural cartoons' => 'books/series/tom-gauld-s-cultural-cartoons',
'Modern Toss' => 'culture/series/modern-toss',
'Clare in the Community' => 'society/series/clareinthecommunity',
'Loomus' => 'lifeandstyle/series/loomus',
'Steve Bell\'s If...' => 'commentisfree/series/if',
'Berger & Wyse' => 'lifeandstyle/series/berger-wyse',
'First Dog on the Moon' => 'profile/first-dog-on-the-moon',
'The Stephen Collins cartoon' => 'lifeandstyle/series/the-stephen-collins-cartoon',
'The Simone Lia cartoon' => 'culture/series/the-simone-lia-cartoon',
),
)
)
);
const CACHE_TIMEOUT = 3600; // 1 hour
protected function parseItem($item) {
$item = parent::parseItem($item);
$articleHtml = getSimpleHTMLDOMCached($item['uri'])
or returnServerError('Could not request: ' . $item['uri']);
if ($articleHtml->find('picture', 0)) {
$picture = $articleHtml->find('picture', 0);
$explodeParts = explode(' ', $picture->find('source', 0)->attr['srcset']);
$imageUrl = $explodeParts[0];
} else if ($articleHtml->find('figure', 0)) {
$figure = $articleHtml->find('figure', 0);
preg_match('/srcs-desktop=(.*)&/', $figure->attr['data-canonical-url'], $match)
or returnServerError('Could not extract details');
$explodeParts = explode('&', $match[1]);
$imageUrl = $explodeParts[0];
}
$description = $articleHtml->find('meta[name="description"]', 0)->content;
$item['content'] = <<<EOD
<img src="{$imageUrl}"><p>{$description}</p>
EOD;
// Get categories
if ($articleHtml->find('meta[name="keywords"]', 0)) {
$categories = explode(',', $articleHtml->find('meta[name="keywords"]', 0)->content);
$item['categories'] = array_map('trim', $categories);
}
$item['enclosures'][] = $imageUrl;
return $item;
}
public function collectData() {
$this->collectExpandableDatas($this->getURI() . '/rss', 10);
}
public function getURI() {
if (!is_null($this->getInput('cartoon'))) {
return self::URI . '/' . $this->getInput('cartoon');
}
return parent::getURI();
}
public function getName() {
if (!is_null($this->getInput('cartoon'))) {
$parameters = $this->getParameters();
$contentValues = array_flip($parameters[0]['cartoon']['values']);
return $contentValues[$this->getInput('cartoon')] . ' - The Guardian';
}
return parent::getName();
}
}