T411Bridge.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. class T411Bridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = 'ORelio';
  5. $this->name = 'T411';
  6. $this->uri = $this->getURI();
  7. $this->description = 'Returns the 10 newest torrents with specified search terms <br /> Use url part after "?" mark when using their search engine.';
  8. $this->update = '2016-06-25';
  9. $this->parameters[] =
  10. '[
  11. {
  12. "name" : "Search criteria",
  13. "identifier" : "search"
  14. }
  15. ]';
  16. }
  17. public function collectData(array $param) {
  18. //Utility function for retrieving text based on start and end delimiters
  19. function ExtractFromDelimiters($string, $start, $end) {
  20. if (strpos($string, $start) !== false) {
  21. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  22. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  23. return $section_retrieved;
  24. } return false;
  25. }
  26. //Ensure proper parameters have been provided
  27. if (empty($param['search'])) {
  28. $this->returnError('You must specify a search criteria', 400);
  29. }
  30. //Retrieve torrent listing from search results, which does not contain torrent description
  31. $url = $this->getURI().'torrents/search/?'.$param['search'].'&order=added&type=desc';
  32. $html = $this->file_get_html($url) or $this->returnError('Could not request t411: '.$url, 500);
  33. $results = $html->find('table.results', 0);
  34. if (is_null($results))
  35. $this->returnError('No results from t411: '.$url, 500);
  36. $limit = 0;
  37. //Process each item individually
  38. foreach ($results->find('tr') as $element) {
  39. //Limit total amount of requests and ignore table header
  40. if ($limit < 10 && !is_object($element->find('th', 0))) {
  41. //Requests are rate-limited
  42. usleep(500000); //So we need to wait (500ms)
  43. //Retrieve data from RSS entry
  44. $item_uri = $this->getURI().'torrents/details/?id='.ExtractFromDelimiters($element->find('a.nfo', 0)->outertext, '?id=', '"');
  45. $item_title = ExtractFromDelimiters($element->outertext, '" title="', '"');
  46. $item_date = strtotime($element->find('dd', 0)->plaintext);
  47. //Retrieve full description from torrent page
  48. if ($item_html = $this->file_get_html($item_uri)) {
  49. //Retrieve data from page contents
  50. $item_desc = $item_html->find('div.description', 0);
  51. $item_author = $item_html->find('a.profile', 0)->innertext;
  52. //Retrieve image for thumbnail or generic logo fallback
  53. $item_image = $this->getURI().'themes/blue/images/logo.png';
  54. foreach ($item_desc->find('img') as $img) {
  55. if (strpos($img->src, 'prez') === false && strpos($img->src, '/ad/') === false) {
  56. $item_image = $img->src;
  57. break;
  58. }
  59. }
  60. //Cleanup advertisments
  61. $divs = explode('<div class="align-center">', $item_desc->innertext);
  62. $item_desc = '';
  63. foreach ($divs as $text)
  64. if (strpos($text, 'adprovider.adlure.net') === false)
  65. $item_desc = $item_desc.'<div class="align-center">'.$text;
  66. $item_desc = preg_replace('/<h2 class="align-center">LIENS DE T..?L..?CHARGEMENT<\/h2>/i', '', $item_desc);
  67. //Build and add final item
  68. $item = new \Item();
  69. $item->uri = $item_uri;
  70. $item->title = $item_title;
  71. $item->author = $item_author;
  72. $item->timestamp = $item_date;
  73. $item->thumbnailUri = $item_image;
  74. $item->content = $item_desc;
  75. $this->items[] = $item;
  76. $limit++;
  77. }
  78. }
  79. }
  80. }
  81. public function getName() {
  82. return "T411 Bridge";
  83. }
  84. public function getURI() {
  85. return 'https://t411.ch/';
  86. }
  87. public function getCacheDuration() {
  88. return 3600; // 1 hour
  89. }
  90. }