-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractRssSource.php
157 lines (131 loc) · 3.91 KB
/
AbstractRssSource.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace PhlyComic\ComicSource;
use PhlyComic\Comic;
use SimpleXMLElement;
abstract class AbstractRssSource extends AbstractComicSource
{
/**
* @var string Base URL to landing page for comic
*/
protected $comicBase;
/**
* @var string Short name of comic
*/
protected $comicShortName;
/**
* @var null|string Content of most recent item investigated in feed
*/
protected $content;
/**
* @var string URI to a feed
*/
protected $feedUrl;
/**
* The namespace of the tag that holds the image, if any
*
* @var false|string
*/
protected $tagNamespace = false;
/**
* What tag in the feed contains the image?
*
* @var string
*/
protected $tagWithImage = 'description';
public function fetch()
{
$this->content = null;
// Retrieve feed to parse
$rawFeed = $this->fetchFeed($this->feedUrl);
$sxl = new SimpleXMLElement($rawFeed);
$data = $this->getDataFromFeed($sxl);
if ($data instanceof Comic) {
return $data;
}
if (!$data) {
return $this->registerError(sprintf(
static::$comics[$this->comicShortName]
. ' feed does not include image description containing image URL: %s',
$this->content
));
}
$comic = new Comic(
/* 'name' => */ static::$comics[$this->comicShortName],
/* 'link' => */ $this->comicBase,
/* 'daily' => */ $data['daily'],
/* 'image' => */ $data['image']
);
return $comic;
}
protected function getContent(SimpleXMLElement $item)
{
if (!$this->tagNamespace) {
return (string) $item->{$this->tagWithImage};
}
$namespacedChildren = $item->children($this->tagNamespace);
return (string) $namespacedChildren->{$this->tagWithImage};
}
protected function getDataFromFeed(SimpleXMLElement $feed)
{
foreach ($feed->channel->item as $latest) {
if (! $this->isOfInterest($latest)) {
continue;
}
// daily is <link> element
$daily = (string) $latest->link;
$content = $this->getContent($latest);
$image = $this->getImageFromContent($content);
if ($image instanceof Comic) {
return $image;
}
if ($image) {
return array(
'daily' => $daily,
'image' => $image,
);
}
// First item seeds content
if (null === $this->content) {
$this->content = $content;
}
}
return false;
}
protected function getImageFromContent($content)
{
// image is in content -- /src="([^"]+)"
if (preg_match('/\<img [^>]*src="(?P<src>[^"]+)"/', $content, $matches)) {
return $matches['src'];
}
return false;
}
protected function registerError($message)
{
$comic = new Comic(
/* 'name' => */ static::$comics[$this->comicShortName],
/* 'link' => */ $this->comicBase
);
$comic->setError($message);
return $comic;
}
protected function fetchFeed($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$curl,
CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1'
);
$content = curl_exec($curl);
curl_close($curl);
return $content;
}
protected function isOfInterest($item)
{
return true;
}
}