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',
|
2022-03-24 14:11:01 +03:00
|
|
|
'exampleValue' => 'bird',
|
2017-02-11 18:16:56 +03:00
|
|
|
'required' => true
|
|
|
|
),
|
2022-03-25 03:25:53 +03:00
|
|
|
'noGif' => array(
|
|
|
|
'name' => 'Without gifs',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Exclude gifs from the results'
|
|
|
|
),
|
|
|
|
'noStick' => array(
|
|
|
|
'name' => 'Without stickers',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Exclude stickers from the results'
|
|
|
|
),
|
2017-02-11 18:16:56 +03:00
|
|
|
'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
|
|
|
|
2022-03-25 03:25:53 +03:00
|
|
|
protected function getGiphyItems($entries){
|
|
|
|
foreach($entries as $entry) {
|
2021-12-18 13:26:51 +03:00
|
|
|
$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
|
|
|
}
|
2022-03-25 03:25:53 +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);
|
|
|
|
$endpoints = array();
|
|
|
|
if (empty($this->getInput('noGif'))) {
|
|
|
|
$endpoints[] = 'gifs';
|
|
|
|
}
|
|
|
|
if (empty($this->getInput('noStick'))) {
|
|
|
|
$endpoints[] = 'stickers';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($endpoints as $endpoint) {
|
|
|
|
$uri = sprintf(
|
|
|
|
'https://api.giphy.com/v1/%s/search?q=%s&limit=%s&api_key=%s',
|
|
|
|
$endpoint,
|
|
|
|
rawurlencode($this->getInput('s')),
|
|
|
|
$limit,
|
|
|
|
$apiKey
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = json_decode(getContents($uri));
|
|
|
|
|
|
|
|
$this->getGiphyItems($result->data);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|