GiphyBridge.php 2.5 KB

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