2014-12-05 15:18:37 +03:00
|
|
|
<?php
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
class GiphyBridge extends BridgeAbstract {
|
2014-12-05 15:18:37 +03:00
|
|
|
|
2021-12-18 13:26:51 +03:00
|
|
|
const MAINTAINER = 'dvikan';
|
2017-02-11 18:16:56 +03:00
|
|
|
const NAME = 'Giphy Bridge';
|
2019-10-16 22:34:28 +03:00
|
|
|
const URI = 'https://giphy.com/';
|
2021-12-18 13:26:51 +03:00
|
|
|
const CACHE_TIMEOUT = 60 * 60 * 8; // 8h
|
2017-02-11 18:16:56 +03:00
|
|
|
const DESCRIPTION = 'Bridge for giphy.com';
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
's' => array(
|
|
|
|
'name' => 'search tag',
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'n' => array(
|
2021-12-18 13:26:51 +03:00
|
|
|
'name' => 'max number of returned items (max 50)',
|
|
|
|
'type' => 'number',
|
|
|
|
'exampleValue' => 3,
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
));
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2021-12-18 13:26:51 +03:00
|
|
|
public function collectData() {
|
|
|
|
/**
|
|
|
|
* This uses a public beta key which has severe rate limiting.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
);
|
2014-12-05 15:18:37 +03:00
|
|
|
|
2022-01-02 12:36:09 +03:00
|
|
|
$result = json_decode(getContents($uri));
|
2016-07-08 20:06:35 +03:00
|
|
|
|
2021-12-18 13:26:51 +03:00
|
|
|
foreach($result->data as $entry) {
|
|
|
|
$createdAt = new \DateTime($entry->import_datetime);
|
2016-07-08 20:06:35 +03:00
|
|
|
|
2021-12-18 13:26:51 +03:00
|
|
|
$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
|
2022-03-20 22:43:25 +03:00
|
|
|
loading="lazy"
|
2021-12-18 13:26:51 +03:00
|
|
|
src="{$entry->images->downsized->url}"
|
|
|
|
width="{$entry->images->downsized->width}"
|
|
|
|
height="{$entry->images->downsized->height}" />
|
|
|
|
</a>
|
|
|
|
HTML
|
|
|
|
);
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2021-12-18 13:26:51 +03:00
|
|
|
|
|
|
|
usort($this->items, function ($a, $b) {
|
|
|
|
return $a['timestamp'] < $b['timestamp'];
|
|
|
|
});
|
2014-12-05 15:18:37 +03:00
|
|
|
}
|
|
|
|
}
|