mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-09 16:38:50 +00:00
[GiphyBridge] Repair broken bridge (#2347)
This commit is contained in:
parent
a0bbbd6978
commit
67e33186ce
98
bridges/GiphyBridge.php
Normal file → Executable file
98
bridges/GiphyBridge.php
Normal file → Executable file
@ -1,12 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
define('GIPHY_LIMIT', 10);
|
|
||||||
|
|
||||||
class GiphyBridge extends BridgeAbstract {
|
class GiphyBridge extends BridgeAbstract {
|
||||||
|
|
||||||
const MAINTAINER = 'kraoc';
|
const MAINTAINER = 'dvikan';
|
||||||
const NAME = 'Giphy Bridge';
|
const NAME = 'Giphy Bridge';
|
||||||
const URI = 'https://giphy.com/';
|
const URI = 'https://giphy.com/';
|
||||||
const CACHE_TIMEOUT = 300; //5min
|
const CACHE_TIMEOUT = 60 * 60 * 8; // 8h
|
||||||
const DESCRIPTION = 'Bridge for giphy.com';
|
const DESCRIPTION = 'Bridge for giphy.com';
|
||||||
|
|
||||||
const PARAMETERS = array( array(
|
const PARAMETERS = array( array(
|
||||||
@ -15,62 +14,53 @@ class GiphyBridge extends BridgeAbstract {
|
|||||||
'required' => true
|
'required' => true
|
||||||
),
|
),
|
||||||
'n' => array(
|
'n' => array(
|
||||||
'name' => 'max number of returned items',
|
'name' => 'max number of returned items (max 50)',
|
||||||
'type' => 'number'
|
'type' => 'number',
|
||||||
|
'exampleValue' => 3,
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
public function collectData(){
|
public function collectData() {
|
||||||
$html = '';
|
/**
|
||||||
$base_url = 'http://giphy.com';
|
* This uses a public beta key which has severe rate limiting.
|
||||||
$html = getSimpleHTMLDOM(self::URI . '/search/' . urlencode($this->getInput('s') . '/'))
|
*
|
||||||
or returnServerError('No results for this query.');
|
* https://giphy.api-docs.io/1.0/welcome/access-and-api-keys
|
||||||
|
* https://giphy.api-docs.io/1.0/gifs/search-1
|
||||||
|
*/
|
||||||
|
$apiKey = 'dc6zaTOxFJmzC';
|
||||||
|
$limit = min($this->getInput('n') ?: 10, 50);
|
||||||
|
$uri = sprintf(
|
||||||
|
'https://api.giphy.com/v1/gifs/search?q=%s&limit=%s&api_key=%s',
|
||||||
|
rawurlencode($this->getInput('s')),
|
||||||
|
$limit,
|
||||||
|
$apiKey
|
||||||
|
);
|
||||||
|
|
||||||
$max = GIPHY_LIMIT;
|
$result = json_decode(getContents($uri))
|
||||||
if($this->getInput('n')) {
|
or returnServerError('Unable to fetch and decode json');
|
||||||
$max = $this->getInput('n');
|
|
||||||
|
foreach($result->data as $entry) {
|
||||||
|
$createdAt = new \DateTime($entry->import_datetime);
|
||||||
|
|
||||||
|
$this->items[] = array(
|
||||||
|
'id' => $entry->id,
|
||||||
|
'uri' => $entry->url,
|
||||||
|
'author' => $entry->username,
|
||||||
|
'timestamp' => $createdAt->format('U'),
|
||||||
|
'title' => $entry->title,
|
||||||
|
'content' => <<<HTML
|
||||||
|
<a href="{$entry->url}">
|
||||||
|
<img
|
||||||
|
src="{$entry->images->downsized->url}"
|
||||||
|
width="{$entry->images->downsized->width}"
|
||||||
|
height="{$entry->images->downsized->height}" />
|
||||||
|
</a>
|
||||||
|
HTML
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$limit = 0;
|
usort($this->items, function ($a, $b) {
|
||||||
$kw = urlencode($this->getInput('s'));
|
return $a['timestamp'] < $b['timestamp'];
|
||||||
foreach($html->find('div.hoverable-gif') as $entry) {
|
});
|
||||||
if($limit < $max) {
|
|
||||||
$node = $entry->first_child();
|
|
||||||
$href = $node->getAttribute('href');
|
|
||||||
|
|
||||||
$html2 = getSimpleHTMLDOM(self::URI . $href)
|
|
||||||
or returnServerError('No results for this query.');
|
|
||||||
$figure = $html2->getElementByTagName('figure');
|
|
||||||
$img = $figure->firstChild();
|
|
||||||
$caption = $figure->lastChild();
|
|
||||||
|
|
||||||
$item = array();
|
|
||||||
$item['id'] = $img->getAttribute('data-gif_id');
|
|
||||||
$item['uri'] = $img->getAttribute('data-bitly_gif_url');
|
|
||||||
$item['username'] = 'Giphy - ' . ucfirst($kw);
|
|
||||||
$title = $caption->innertext();
|
|
||||||
$title = preg_replace('/\s+/', ' ', $title);
|
|
||||||
$title = str_replace('animated GIF', '', $title);
|
|
||||||
$title = str_replace($kw, '', $title);
|
|
||||||
$title = preg_replace('/\s+/', ' ', $title);
|
|
||||||
$title = trim($title);
|
|
||||||
if(strlen($title) <= 0) {
|
|
||||||
$title = $item['id'];
|
|
||||||
}
|
|
||||||
$item['title'] = trim($title);
|
|
||||||
$item['content'] = '<a href="'
|
|
||||||
. $item['uri']
|
|
||||||
. '"><img src="'
|
|
||||||
. $img->getAttribute('src')
|
|
||||||
. '" width="'
|
|
||||||
. $img->getAttribute('data-original-width')
|
|
||||||
. '" height="'
|
|
||||||
. $img->getAttribute('data-original-height')
|
|
||||||
. '" /></a>';
|
|
||||||
|
|
||||||
$this->items[] = $item;
|
|
||||||
$limit++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user