From 5922e37bc0777f5309cb8bed523bca3a5514abdf Mon Sep 17 00:00:00 2001 From: Olivier Date: Fri, 5 Dec 2014 13:18:37 +0100 Subject: [PATCH] Create GiphyBridge.php Add bridge for Giphy.com --- bridges/GiphyBridge.php | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bridges/GiphyBridge.php diff --git a/bridges/GiphyBridge.php b/bridges/GiphyBridge.php new file mode 100644 index 00000000..83121747 --- /dev/null +++ b/bridges/GiphyBridge.php @@ -0,0 +1,85 @@ +returnError('No results for this query.', 404); + } + else { + $this->returnError('You must specify a search worf (?s=...).', 400); + } + + $max = GIPHY_LIMIT; + if (isset($param['n'])) { + $max = (integer) $param['n']; + } + + $limit = 0; + $kw = urlencode($param['s']); + foreach($html->find('div.hoverable-gif') as $entry) { + if($limit < $max) { + $node = $entry->first_child(); + $href = $node->getAttribute('href'); + + $html2 = file_get_html($base_url . $href) or $this->returnError('No results for this query.', 404); + $figure = $html2->getElementByTagName('figure'); + $img = $figure->firstChild(); + $caption = $figure->lastChild(); + + $item = new \Item(); + $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 = + '' + .'' + .''; + + $this->items[] = $item; + $limit++; + } + } + } + + public function getName(){ + return 'Giphy Bridge'; + } + + public function getURI(){ + return 'http://giphy.com/'; + } + + public function getCacheDuration(){ + return 300; // 5 minutes + } + + public function getUsername(){ + return $this->items[0]->username; + } +}