UsbekEtRicaBridge.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class UsbekEtRicaBridge extends BridgeAbstract {
  3. const MAINTAINER = 'logmanoriginal';
  4. const NAME = 'Usbek & Rica Bridge';
  5. const URI = 'https://usbeketrica.com';
  6. const DESCRIPTION = 'Returns latest articles from the front page';
  7. const PARAMETERS = array(
  8. array(
  9. 'limit' => array(
  10. 'name' => 'Number of articles to return',
  11. 'type' => 'number',
  12. 'required' => false,
  13. 'title' => 'Specifies the maximum number of articles to return',
  14. 'defaultValue' => -1
  15. ),
  16. 'fullarticle' => array(
  17. 'name' => 'Load full article',
  18. 'type' => 'checkbox',
  19. 'required' => false,
  20. 'title' => 'Activate to load full articles',
  21. )
  22. )
  23. );
  24. public function collectData(){
  25. $limit = $this->getInput('limit');
  26. $fullarticle = $this->getInput('fullarticle');
  27. $html = getSimpleHTMLDOM($this->getURI());
  28. $articles = $html->find('div.details');
  29. foreach($articles as $article) {
  30. $item = array();
  31. $title = $article->find('div.card-title', 0);
  32. if($title) {
  33. $item['title'] = $title->plaintext;
  34. } else {
  35. // Sometimes we get rubbish, ignore.
  36. continue;
  37. }
  38. $author = $article->find('div.author span', 0);
  39. if($author) {
  40. $item['author'] = $author->plaintext;
  41. }
  42. $uri = $article->find('a.read', 0)->href;
  43. if(substr($uri, 0, 1) === 'h') { // absolute uri
  44. $item['uri'] = $uri;
  45. } else { // relative uri
  46. $item['uri'] = $this->getURI() . $uri;
  47. }
  48. if($fullarticle) {
  49. $content = $this->loadFullArticle($item['uri']);
  50. }
  51. if($fullarticle && !is_null($content)) {
  52. $item['content'] = $content;
  53. } else {
  54. $excerpt = $article->find('div.card-excerpt', 0);
  55. if($excerpt) {
  56. $item['content'] = $excerpt->plaintext;
  57. }
  58. }
  59. $image = $article->find('div.card-img img', 0);
  60. if($image) {
  61. $item['enclosures'] = array(
  62. $image->src
  63. );
  64. }
  65. $this->items[] = $item;
  66. if($limit > 0 && count($this->items) >= $limit) {
  67. break;
  68. }
  69. }
  70. }
  71. /**
  72. * Loads the full article and returns the contents
  73. * @param $uri The article URI
  74. * @return The article content
  75. */
  76. private function loadFullArticle($uri){
  77. $html = getSimpleHTMLDOMCached($uri);
  78. $content = $html->find('section.main', 0);
  79. if($content) {
  80. return $this->replaceUriInHtmlElement($content);
  81. }
  82. return null;
  83. }
  84. /**
  85. * Replaces all relative URIs with absolute ones
  86. * @param $element A simplehtmldom element
  87. * @return The $element->innertext with all URIs replaced
  88. */
  89. private function replaceUriInHtmlElement($element){
  90. return str_replace('href="/', 'href="' . $this->getURI() . '/', $element->innertext);
  91. }
  92. }