1
0

GiphyBridge.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * RssBridgeGiphy
  4. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  5. * 2014-12-05
  6. *
  7. * @name Giphy Bridge
  8. * @homepage http://giphy.com/
  9. * @description Bridge for giphy.com
  10. * @maintainer kraoc
  11. * @use1(s="search tag")
  12. * @use2(n="max number of returned items")
  13. */
  14. define(GIPHY_LIMIT, 10);
  15. class GiphyBridge extends BridgeAbstract{
  16. public function collectData(array $param){
  17. $html = '';
  18. $base_url = 'http://giphy.com';
  19. if (isset($param['s'])) { /* keyword search mode */
  20. $html = file_get_html($base_url.'/search/'.urlencode($param['s'].'/')) or $this->returnError('No results for this query.', 404);
  21. }
  22. else {
  23. $this->returnError('You must specify a search worf (?s=...).', 400);
  24. }
  25. $max = GIPHY_LIMIT;
  26. if (isset($param['n'])) {
  27. $max = (integer) $param['n'];
  28. }
  29. $limit = 0;
  30. $kw = urlencode($param['s']);
  31. foreach($html->find('div.hoverable-gif') as $entry) {
  32. if($limit < $max) {
  33. $node = $entry->first_child();
  34. $href = $node->getAttribute('href');
  35. $html2 = file_get_html($base_url . $href) or $this->returnError('No results for this query.', 404);
  36. $figure = $html2->getElementByTagName('figure');
  37. $img = $figure->firstChild();
  38. $caption = $figure->lastChild();
  39. $item = new \Item();
  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. public function getName(){
  63. return 'Giphy Bridge';
  64. }
  65. public function getURI(){
  66. return 'http://giphy.com/';
  67. }
  68. public function getCacheDuration(){
  69. return 300; // 5 minutes
  70. }
  71. public function getUsername(){
  72. return $this->items[0]->username;
  73. }
  74. }