2014-12-05 13:18:37 +01:00
|
|
|
<?php
|
2015-11-04 00:05:10 +01:00
|
|
|
define('GIPHY_LIMIT', 10);
|
2014-12-05 13:18:37 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
class GiphyBridge extends BridgeAbstract {
|
2014-12-05 13:18:37 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'kraoc';
|
|
|
|
const NAME = 'Giphy Bridge';
|
|
|
|
const URI = 'http://giphy.com/';
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 300; //5min
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Bridge for giphy.com';
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
's' => array(
|
|
|
|
'name' => 'search tag',
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'n' => array(
|
|
|
|
'name' => 'max number of returned items',
|
|
|
|
'type' => 'number'
|
|
|
|
)
|
|
|
|
));
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2016-07-08 19:06:35 +02:00
|
|
|
$html = '';
|
2017-02-11 16:16:56 +01:00
|
|
|
$base_url = 'http://giphy.com';
|
|
|
|
$html = getSimpleHTMLDOM(self::URI . '/search/' . urlencode($this->getInput('s') . '/'))
|
|
|
|
or returnServerError('No results for this query.');
|
2014-12-05 13:18:37 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$max = GIPHY_LIMIT;
|
|
|
|
if($this->getInput('n')){
|
|
|
|
$max = $this->getInput('n');
|
|
|
|
}
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$limit = 0;
|
|
|
|
$kw = urlencode($this->getInput('s'));
|
|
|
|
foreach($html->find('div.hoverable-gif') as $entry){
|
|
|
|
if($limit < $max){
|
|
|
|
$node = $entry->first_child();
|
|
|
|
$href = $node->getAttribute('href');
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$html2 = getSimpleHTMLDOM(self::URI . $href)
|
|
|
|
or returnServerError('No results for this query.');
|
|
|
|
$figure = $html2->getElementByTagName('figure');
|
|
|
|
$img = $figure->firstChild();
|
|
|
|
$caption = $figure->lastChild();
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$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>';
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$this->items[] = $item;
|
|
|
|
$limit++;
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 13:18:37 +01:00
|
|
|
}
|
|
|
|
}
|