WebfailBridge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 self::URI;
  37. // e.g.: https://en.webfail.com
  38. return 'https://' . $this->getInput('language') . '.webfail.com';
  39. }
  40. public function collectData(){
  41. $html = getSimpleHTMLDOM($this->getURI() . $this->getInput('type'));
  42. $type = array_search($this->getInput('type'),
  43. self::PARAMETERS[$this->queriedContext]['type']['values']);
  44. switch(strtolower($type)){
  45. case 'facebook':
  46. case 'videos':
  47. $this->extractNews($html, $type);
  48. break;
  49. case 'none':
  50. case 'images':
  51. case 'gifs':
  52. $this->extractArticle($html);
  53. break;
  54. default: returnClientError('Unknown type: ' . $type);
  55. }
  56. }
  57. private function extractNews($html, $type){
  58. $news = $html->find('#main', 0)->find('a.wf-list-news');
  59. foreach($news as $element){
  60. $item = array();
  61. $item['title'] = $this->fixTitle($element->find('div.wf-news-title', 0)->innertext);
  62. $item['uri'] = $this->getURI() . $element->href;
  63. $img = $element->find('img.wf-image', 0)->src;
  64. // Load high resolution image for 'facebook'
  65. switch(strtolower($type)){
  66. case 'facebook':
  67. $img = $this->getImageHiResUri($item['uri']);
  68. break;
  69. default:
  70. }
  71. $description = '';
  72. if(!is_null($element->find('div.wf-news-description', 0))){
  73. $description = $element->find('div.wf-news-description', 0)->innertext;
  74. }
  75. $item['content'] = '<p>'
  76. . $description
  77. . '</p><br><a href="'
  78. . $item['uri']
  79. . '"><img src="'
  80. . $img
  81. . '"></a>';
  82. $this->items[] = $item;
  83. }
  84. }
  85. private function extractArticle($html){
  86. $articles = $html->find('article');
  87. foreach($articles as $article){
  88. $item = array();
  89. $item['title'] = $this->fixTitle($article->find('a', 1)->innertext);
  90. // Webfail shares videos or images
  91. if(!is_null($article->find('img.wf-image', 0))){ // Image type
  92. $item['uri'] = $this->getURI() . $article->find('a', 2)->href;
  93. $item['content'] = '<a href="'
  94. . $item['uri']
  95. . '"><img src="'
  96. . $article->find('img.wf-image', 0)->src
  97. . '">';
  98. } elseif(!is_null($article->find('div.wf-video', 0))){ // Video type
  99. $videoId = $this->getVideoId($article->find('div.wf-play', 0)->onclick);
  100. $item['uri'] = 'https://youtube.com/watch?v=' . $videoId;
  101. $item['content'] = '<a href="'
  102. . $item['uri']
  103. . '"><img src="http://img.youtube.com/vi/'
  104. . $videoId
  105. . '/0.jpg"></a>';
  106. }
  107. $this->items[] = $item;
  108. }
  109. }
  110. private function fixTitle($title){
  111. // This fixes titles that include umlauts (in German language)
  112. return html_entity_decode($title, ENT_QUOTES | ENT_HTML401, 'UTF-8');
  113. }
  114. private function getVideoId($onclick){
  115. return substr($onclick, 21, 11);
  116. }
  117. private function getImageHiResUri($url){
  118. // https://de.webfail.com/ef524fae509?tag=ffdt
  119. // http://cdn.webfail.com/upl/img/ef524fae509/post2.jpg
  120. $id = substr($url, strrpos($url, '/') + 1, strlen($url) - strrpos($url, '?') + 2);
  121. return 'http://cdn.webfail.com/upl/img/' . $id . '/post2.jpg';
  122. }
  123. }