Skip to content

Commit 26452e1

Browse files
committed
spouts/reddit2: remove special imgur handling
The Reddit spout tried to detect if an item points to imgur.com and change the link directly to an image in that case. Unfortunately, it was flaky, for example, gifv files resulted in 403 error. Instead we are using the previews feature, which, as a bonus, also works on sites other than imgur.
1 parent 9c21ff0 commit 26452e1

2 files changed

Lines changed: 17 additions & 48 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## 2.19 – unreleased
33
### New features
44
- Thumbnails can be disabled ([#897](https://github.com/SSilence/selfoss/pull/897))
5+
- Reddit spout replaced fragile imgur heuristics with previews provided by the JSON API ([#1033](https://github.com/SSilence/selfoss/pull/1033))
56

67
### Bug fixes
78
- Reddit spout allows wider range of URLs, including absolute URLs and searches ([#1033](https://github.com/SSilence/selfoss/pull/1033))

spouts/reddit/reddit2.php

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,6 @@ public function getTitle() {
222222
*/
223223
public function getHtmlUrl() {
224224
if ($this->items !== null && $this->valid()) {
225-
if (preg_match('/imgur/', @current($this->items)['data']['url'])) {
226-
if (!preg_match('/\.(?:gif|jpg|png|svg)$/i', @current($this->items)['data']['url'])) {
227-
$response = $this->sendRequest(@current($this->items)['data']['url'] . '.jpg', 'HEAD');
228-
if ($response->getStatusCode() === 404) {
229-
return @current($this->items)['data']['url'] . '/embed';
230-
}
231-
232-
return @current($this->items)['data']['url'] . '.jpg';
233-
}
234-
}
235-
236225
return @current($this->items)['data']['url'];
237226
}
238227

@@ -248,23 +237,30 @@ public function getHtmlUrl() {
248237
*/
249238
public function getContent() {
250239
if ($this->items !== null && $this->valid()) {
251-
$text = @current($this->items)['data']['selftext_html'];
240+
$data = @current($this->items)['data'];
241+
$text = $data['selftext_html'];
252242
if (!empty($text)) {
253243
return htmlspecialchars_decode($text);
254244
}
255245

256-
if (preg_match('/\.(?:gif|jpg|png|svg)/i', $this->getHtmlUrl())) {
257-
return '<img src="' . $this->getHtmlUrl() . '" />';
258-
}
246+
if (isset($data['preview']) && isset($data['preview']['images'])) {
247+
$text = '';
248+
foreach ($data['preview']['images'] as $image) {
249+
if (isset($image['source']) && isset($image['source']['url'])) {
250+
$text .= '<img src="' . $image['source']['url'] . '">';
251+
}
252+
}
259253

260-
//albums, embeds other strange thigs
261-
if (preg_match('/embed$/i', $this->getHtmlUrl())) {
262-
$response = $this->sendRequest($this->getHtmlUrl());
254+
if ($text !== '') {
255+
return $text;
256+
}
257+
}
263258

264-
return '<a href="' . $this->getHtmlUrl() . '"><img src="' . preg_replace("/s\./", '.', $this->getImage($response->getBody())) . '"/></a>';
259+
if (preg_match('/\.(?:gif|jpg|png|svg)$/i', Url::fromString($this->getHtmlUrl())->getPath())) {
260+
return '<img src="' . $this->getHtmlUrl() . '" />';
265261
}
266262

267-
return @current($this->items)['data']['url'];
263+
return $data['url'];
268264
}
269265

270266
return false;
@@ -343,34 +339,6 @@ public function getXmlUrl($params) {
343339
return 'reddit://' . urlencode($params['url']);
344340
}
345341

346-
/**
347-
* taken from: http://zytzagoo.net/blog/2008/01/23/extracting-images-from-html-using-regular-expressions/
348-
* Searches for the first occurence of an html <img> element in a string
349-
* and extracts the src if it finds it. Returns boolean false in case an
350-
* <img> element is not found.
351-
*
352-
* @param string $str An HTML string
353-
*
354-
* @return mixed The contents of the src attribute in the
355-
* found <img> or boolean false if no <img>
356-
* is found
357-
*/
358-
private function getImage($html) {
359-
if (stripos($html, '<img') !== false) {
360-
$imgsrc_regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
361-
preg_match($imgsrc_regex, $html, $matches);
362-
unset($imgsrc_regex);
363-
unset($html);
364-
if (is_array($matches) && !empty($matches)) {
365-
return $matches[2];
366-
} else {
367-
return false;
368-
}
369-
} else {
370-
return false;
371-
}
372-
}
373-
374342
/**
375343
* @throws \GuzzleHttp\Exception\RequestException When an error is encountered
376344
* @throws \RuntimeException if the response body is not in JSON format

0 commit comments

Comments
 (0)