GiphyBridge.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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'] = '<a href="'
  54. . $item['uri']
  55. . '"><img src="'
  56. . $img->getAttribute('src')
  57. . '" width="'
  58. . $img->getAttribute('data-original-width')
  59. . '" height="'
  60. . $img->getAttribute('data-original-height')
  61. . '" /></a>';
  62. $this->items[] = $item;
  63. $limit++;
  64. }
  65. }
  66. }
  67. }