GiphyBridge.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. define('GIPHY_LIMIT', 10);
  3. class GiphyBridge extends BridgeAbstract{
  4. public function loadMetadatas() {
  5. $this->maintainer = "kraoc";
  6. $this->name = "Giphy Bridge";
  7. $this->uri = "http://giphy.com/";
  8. $this->description = "Bridge for giphy.com";
  9. $this->update = "2014-12-05";
  10. $this->parameters["By tag"] =
  11. '[
  12. {
  13. "name" : "search tag",
  14. "identifier" : "s"
  15. }
  16. ]';
  17. $this->parameters["Without tag"] =
  18. '[
  19. {
  20. "name" : "max number of returned items",
  21. "type" : "number",
  22. "identifier" : "n"
  23. }
  24. ]';
  25. }
  26. public function collectData(array $param){
  27. $html = '';
  28. $base_url = 'http://giphy.com';
  29. if (isset($param['s'])) { /* keyword search mode */
  30. $html = $this->file_get_html($base_url.'/search/'.urlencode($param['s'].'/')) or $this->returnError('No results for this query.', 404);
  31. }
  32. else {
  33. $this->returnError('You must specify a search worf (?s=...).', 400);
  34. }
  35. $max = GIPHY_LIMIT;
  36. if (isset($param['n'])) {
  37. $max = (integer) $param['n'];
  38. }
  39. $limit = 0;
  40. $kw = urlencode($param['s']);
  41. foreach($html->find('div.hoverable-gif') as $entry) {
  42. if($limit < $max) {
  43. $node = $entry->first_child();
  44. $href = $node->getAttribute('href');
  45. $html2 = $this->file_get_html($base_url . $href) or $this->returnError('No results for this query.', 404);
  46. $figure = $html2->getElementByTagName('figure');
  47. $img = $figure->firstChild();
  48. $caption = $figure->lastChild();
  49. $item = new \Item();
  50. $item->id = $img->getAttribute('data-gif_id');
  51. $item->uri = $img->getAttribute('data-bitly_gif_url');
  52. $item->username = 'Giphy - '.ucfirst($kw);
  53. $title = $caption->innertext();
  54. $title = preg_replace('/\s+/', ' ',$title);
  55. $title = str_replace('animated GIF', '', $title);
  56. $title = str_replace($kw, '', $title);
  57. $title = preg_replace('/\s+/', ' ',$title);
  58. $title = trim($title);
  59. if (strlen($title) <= 0) {
  60. $title = $item->id;
  61. }
  62. $item->title = trim($title);
  63. $item->content =
  64. '<a href="'.$item->uri.'">'
  65. .'<img src="'.$img->getAttribute('src').'" width="'.$img->getAttribute('data-original-width').'" height="'.$img->getAttribute('data-original-height').'" />'
  66. .'</a>';
  67. $this->items[] = $item;
  68. $limit++;
  69. }
  70. }
  71. }
  72. public function getName(){
  73. return 'Giphy Bridge';
  74. }
  75. public function getURI(){
  76. return 'http://giphy.com/';
  77. }
  78. public function getCacheDuration(){
  79. return 300; // 5 minutes
  80. }
  81. public function getUsername(){
  82. return $this->items[0]->username;
  83. }
  84. }