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
|
|
|
|
|
|
|
class GiphyBridge extends BridgeAbstract{
|
|
|
|
|
2015-11-05 16:50:18 +01:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = "kraoc";
|
|
|
|
$this->name = "Giphy Bridge";
|
|
|
|
$this->uri = "http://giphy.com/";
|
|
|
|
$this->description = "Bridge for giphy.com";
|
2016-08-17 14:45:08 +02:00
|
|
|
$this->update = '2016-08-17';
|
2015-11-05 16:50:18 +01:00
|
|
|
|
|
|
|
$this->parameters["By tag"] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "search tag",
|
|
|
|
"identifier" : "s"
|
|
|
|
}
|
|
|
|
]';
|
|
|
|
|
|
|
|
$this->parameters["Without tag"] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "max number of returned items",
|
|
|
|
"type" : "number",
|
|
|
|
"identifier" : "n"
|
|
|
|
}
|
|
|
|
]';
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:18:37 +01:00
|
|
|
public function collectData(array $param){
|
2016-07-08 19:06:35 +02:00
|
|
|
$html = '';
|
2014-12-05 13:18:37 +01:00
|
|
|
$base_url = 'http://giphy.com';
|
|
|
|
if (isset($param['s'])) { /* keyword search mode */
|
2016-07-08 19:06:35 +02:00
|
|
|
$html = $this->getSimpleHTMLDOM($base_url.'/search/'.urlencode($param['s'].'/')) or $this->returnServerError('No results for this query.');
|
2014-12-05 13:18:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-08-17 14:45:08 +02:00
|
|
|
$this->returnClientError('You must specify a search worf (?s=...).');
|
2014-12-05 13:18:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$max = GIPHY_LIMIT;
|
|
|
|
if (isset($param['n'])) {
|
|
|
|
$max = (integer) $param['n'];
|
|
|
|
}
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2014-12-05 13:18:37 +01:00
|
|
|
$limit = 0;
|
|
|
|
$kw = urlencode($param['s']);
|
|
|
|
foreach($html->find('div.hoverable-gif') as $entry) {
|
|
|
|
if($limit < $max) {
|
2016-07-08 19:06:35 +02: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 13:18:37 +01:00
|
|
|
$figure = $html2->getElementByTagName('figure');
|
|
|
|
$img = $figure->firstChild();
|
|
|
|
$caption = $figure->lastChild();
|
2016-07-08 19:06:35 +02:00
|
|
|
|
2014-12-05 13:18:37 +01:00
|
|
|
$item = new \Item();
|
|
|
|
$item->id = $img->getAttribute('data-gif_id');
|
2016-07-08 19:06:35 +02:00
|
|
|
$item->uri = $img->getAttribute('data-bitly_gif_url');
|
2014-12-05 13:18:37 +01:00
|
|
|
$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
|
|
|
|
|
|
|
$this->items[] = $item;
|
2014-12-05 13:18:37 +01:00
|
|
|
$limit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration(){
|
|
|
|
return 300; // 5 minutes
|
|
|
|
}
|
|
|
|
}
|