WebfailBridge.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. class WebfailBridge extends BridgeAbstract {
  3. const MAINTAINER = 'logmanoriginal';
  4. const URI = 'https://webfail.com';
  5. const NAME = 'Webfail';
  6. const DESCRIPTION = 'Returns the latest fails';
  7. const PARAMETERS = array(
  8. 'By content type' => array(
  9. 'language' => array(
  10. 'name' => 'Language',
  11. 'type' => 'list',
  12. 'title' => 'Select your language',
  13. 'values' => array(
  14. 'English' => 'en',
  15. 'German' => 'de'
  16. ),
  17. 'defaultValue' => 'English'
  18. ),
  19. 'type' => array(
  20. 'name' => 'Type',
  21. 'type' => 'list',
  22. 'title' => 'Select your content type',
  23. 'values' => array(
  24. 'None' => '/',
  25. 'Facebook' => '/ffdts',
  26. 'Images' => '/images',
  27. 'Videos' => '/videos',
  28. 'Gifs' => '/gifs'
  29. ),
  30. 'defaultValue' => 'None'
  31. )
  32. )
  33. );
  34. public function getURI(){
  35. if(is_null($this->getInput('language')))
  36. return parent::getURI();
  37. // e.g.: https://en.webfail.com
  38. return 'https://' . $this->getInput('language') . '.webfail.com';
  39. }
  40. public function collectData(){
  41. ini_set('user_agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0');
  42. $html = getSimpleHTMLDOM($this->getURI() . $this->getInput('type'));
  43. $type = array_search($this->getInput('type'),
  44. self::PARAMETERS[$this->queriedContext]['type']['values']);
  45. switch(strtolower($type)){
  46. case 'facebook':
  47. case 'videos':
  48. $this->extractNews($html, $type);
  49. break;
  50. case 'none':
  51. case 'images':
  52. case 'gifs':
  53. $this->extractArticle($html);
  54. break;
  55. default: returnClientError('Unknown type: ' . $type);
  56. }
  57. }
  58. private function extractNews($html, $type){
  59. $news = $html->find('#main', 0)->find('a.wf-list-news');
  60. foreach($news as $element){
  61. $item = array();
  62. $item['title'] = $this->fixTitle($element->find('div.wf-news-title', 0)->innertext);
  63. $item['uri'] = $this->getURI() . $element->href;
  64. $img = $element->find('img.wf-image', 0)->src;
  65. // Load high resolution image for 'facebook'
  66. switch(strtolower($type)){
  67. case 'facebook':
  68. $img = $this->getImageHiResUri($item['uri']);
  69. break;
  70. default:
  71. }
  72. $description = '';
  73. if(!is_null($element->find('div.wf-news-description', 0))){
  74. $description = $element->find('div.wf-news-description', 0)->innertext;
  75. }
  76. $item['content'] = '<p>'
  77. . $description
  78. . '</p><br><a href="'
  79. . $item['uri']
  80. . '"><img src="'
  81. . $img
  82. . '"></a>';
  83. $this->items[] = $item;
  84. }
  85. }
  86. private function extractArticle($html){
  87. $articles = $html->find('article');
  88. foreach($articles as $article){
  89. $item = array();
  90. $item['title'] = $this->fixTitle($article->find('a', 1)->innertext);
  91. // Images, videos and gifs are provided in their own unique way
  92. if(!is_null($article->find('img.wf-image', 0))){ // Image type
  93. $item['uri'] = $this->getURI() . $article->find('a', 2)->href;
  94. $item['content'] = '<a href="'
  95. . $item['uri']
  96. . '"><img src="'
  97. . $article->find('img.wf-image', 0)->src
  98. . '"></a>';
  99. } elseif(!is_null($article->find('div.wf-video', 0))){ // Video type
  100. $videoId = $this->getVideoId($article->find('div.wf-play', 0)->onclick);
  101. $item['uri'] = 'https://youtube.com/watch?v=' . $videoId;
  102. $item['content'] = '<a href="'
  103. . $item['uri']
  104. . '"><img src="http://img.youtube.com/vi/'
  105. . $videoId
  106. . '/0.jpg"></a>';
  107. } elseif(!is_null($article->find('video[id*=gif-]', 0))){ // Gif type
  108. $item['uri'] = $this->getURI() . $article->find('a', 2)->href;
  109. $item['content'] = '<video controls src="'
  110. . $article->find('video[id*=gif-]', 0)->src
  111. . '" poster="'
  112. . $article->find('video[id*=gif-]', 0)->poster
  113. . '"></video>';
  114. }
  115. $this->items[] = $item;
  116. }
  117. }
  118. private function fixTitle($title){
  119. // This fixes titles that include umlauts (in German language)
  120. return html_entity_decode($title, ENT_QUOTES | ENT_HTML401, 'UTF-8');
  121. }
  122. private function getVideoId($onclick){
  123. return substr($onclick, 21, 11);
  124. }
  125. private function getImageHiResUri($url){
  126. // https://de.webfail.com/ef524fae509?tag=ffdt
  127. // http://cdn.webfail.com/upl/img/ef524fae509/post2.jpg
  128. $id = substr($url, strrpos($url, '/') + 1, strlen($url) - strrpos($url, '?') + 2);
  129. return 'http://cdn.webfail.com/upl/img/' . $id . '/post2.jpg';
  130. }
  131. }