2014-12-05 15:18:37 +03:00
|
|
|
<?php
|
2015-11-04 02:05:10 +03:00
|
|
|
define('GIPHY_LIMIT', 10);
|
2014-12-05 15:18:37 +03:00
|
|
|
|
|
|
|
class GiphyBridge extends BridgeAbstract{
|
|
|
|
|
2015-11-05 18:50:18 +03:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = "kraoc";
|
|
|
|
$this->name = "Giphy Bridge";
|
|
|
|
$this->uri = "http://giphy.com/";
|
|
|
|
$this->description = "Bridge for giphy.com";
|
|
|
|
|
2016-08-25 01:33:25 +03:00
|
|
|
$this->parameters[] = array(
|
|
|
|
's'=>array('name'=>'search tag'),
|
|
|
|
'n'=>array(
|
|
|
|
'name'=>'max number of returned items',
|
|
|
|
'type'=>'number'
|
|
|
|
)
|
2016-08-22 02:25:56 +03:00
|
|
|
);
|
2015-11-05 18:50:18 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
|
|
|
$param=$this->parameters[$this->queriedContext];
|
2016-07-08 20:06:35 +03:00
|
|
|
$html = '';
|
2014-12-05 15:18:37 +03:00
|
|
|
$base_url = 'http://giphy.com';
|
2016-08-25 02:24:53 +03:00
|
|
|
if (isset($param['s']['value'])) { /* keyword search mode */
|
|
|
|
$html = $this->getSimpleHTMLDOM($base_url.'/search/'.urlencode($param['s']['value'].'/')) or $this->returnServerError('No results for this query.');
|
2014-12-05 15:18:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2016-08-17 15:45:08 +03:00
|
|
|
$this->returnClientError('You must specify a search worf (?s=...).');
|
2014-12-05 15:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$max = GIPHY_LIMIT;
|
2016-08-25 02:24:53 +03:00
|
|
|
if (isset($param['n']['value'])) {
|
|
|
|
$max = (integer) $param['n']['value'];
|
2014-12-05 15:18:37 +03:00
|
|
|
}
|
2016-07-08 20:06:35 +03:00
|
|
|
|
2014-12-05 15:18:37 +03:00
|
|
|
$limit = 0;
|
2016-08-25 02:24:53 +03:00
|
|
|
$kw = urlencode($param['s']['value']);
|
2014-12-05 15:18:37 +03:00
|
|
|
foreach($html->find('div.hoverable-gif') as $entry) {
|
|
|
|
if($limit < $max) {
|
2016-07-08 20:06:35 +03:00
|
|
|
$node = $entry->first_child();
|
|
|
|
$href = $node->getAttribute('href');
|
|
|
|
|
|
|
|
$html2 = $this->getSimpleHTMLDOM($base_url . $href) or $this->returnServerError('No results for this query.');
|
2014-12-05 15:18:37 +03:00
|
|
|
$figure = $html2->getElementByTagName('figure');
|
|
|
|
$img = $figure->firstChild();
|
|
|
|
$caption = $figure->lastChild();
|
2016-07-08 20:06:35 +03:00
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = array();
|
|
|
|
$item['id'] = $img->getAttribute('data-gif_id');
|
|
|
|
$item['uri'] = $img->getAttribute('data-bitly_gif_url');
|
|
|
|
$item['username'] = 'Giphy - '.ucfirst($kw);
|
2014-12-05 15:18:37 +03:00
|
|
|
$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) {
|
2016-08-22 19:55:59 +03:00
|
|
|
$title = $item['id'];
|
2014-12-05 15:18:37 +03:00
|
|
|
}
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['title'] = trim($title);
|
|
|
|
$item['content'] =
|
|
|
|
'<a href="'.$item['uri'].'">'
|
2014-12-05 15:18:37 +03:00
|
|
|
.'<img src="'.$img->getAttribute('src').'" width="'.$img->getAttribute('data-original-width').'" height="'.$img->getAttribute('data-original-height').'" />'
|
|
|
|
.'</a>';
|
2016-07-08 20:06:35 +03:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
2014-12-05 15:18:37 +03:00
|
|
|
$limit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration(){
|
|
|
|
return 300; // 5 minutes
|
|
|
|
}
|
|
|
|
}
|