GiphyBridge.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. define('GIPHY_LIMIT', 10);
  3. class GiphyBridge extends BridgeAbstract{
  4. const MAINTAINER = "kraoc";
  5. const NAME = "Giphy Bridge";
  6. const URI = "http://giphy.com/";
  7. const CACHE_TIMEOUT = 300; //5min
  8. const DESCRIPTION = "Bridge for giphy.com";
  9. const PARAMETERS = array( array(
  10. 's'=>array(
  11. 'name'=>'search tag',
  12. 'required'=>true
  13. ),
  14. 'n'=>array(
  15. 'name'=>'max number of returned items',
  16. 'type'=>'number'
  17. )
  18. ));
  19. public function collectData(){
  20. $html = '';
  21. $base_url = 'http://giphy.com';
  22. $html = getSimpleHTMLDOM(self::URI.'/search/'.urlencode($this->getInput('s').'/'))
  23. or returnServerError('No results for this query.');
  24. $max = GIPHY_LIMIT;
  25. if ($this->getInput('n')) {
  26. $max = $this->getInput('n');
  27. }
  28. $limit = 0;
  29. $kw = urlencode($this->getInput('s'));
  30. foreach($html->find('div.hoverable-gif') as $entry) {
  31. if($limit < $max) {
  32. $node = $entry->first_child();
  33. $href = $node->getAttribute('href');
  34. $html2 = getSimpleHTMLDOM(self::URI . $href)
  35. or returnServerError('No results for this query.');
  36. $figure = $html2->getElementByTagName('figure');
  37. $img = $figure->firstChild();
  38. $caption = $figure->lastChild();
  39. $item = array();
  40. $item['id'] = $img->getAttribute('data-gif_id');
  41. $item['uri'] = $img->getAttribute('data-bitly_gif_url');
  42. $item['username'] = 'Giphy - '.ucfirst($kw);
  43. $title = $caption->innertext();
  44. $title = preg_replace('/\s+/', ' ',$title);
  45. $title = str_replace('animated GIF', '', $title);
  46. $title = str_replace($kw, '', $title);
  47. $title = preg_replace('/\s+/', ' ',$title);
  48. $title = trim($title);
  49. if (strlen($title) <= 0) {
  50. $title = $item['id'];
  51. }
  52. $item['title'] = trim($title);
  53. $item['content'] =
  54. '<a href="'.$item['uri'].'">'
  55. .'<img src="'.$img->getAttribute('src').'" width="'.$img->getAttribute('data-original-width').'" height="'.$img->getAttribute('data-original-height').'" />'
  56. .'</a>';
  57. $this->items[] = $item;
  58. $limit++;
  59. }
  60. }
  61. }
  62. }