IsoHuntBridge.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. class IsoHuntBridge extends BridgeAbstract{
  3. public function loadMetadatas(){
  4. $this->maintainer = 'logmanoriginal';
  5. $this->name = 'isoHunt Bridge'; // Is replaced later!
  6. $this->uri = 'https://isohunt.to'; // Is replaced later!
  7. $this->description = 'Returns the latest results by category or search result';
  8. /*
  9. * Get feeds for one of the "latest" categories
  10. * Notice: The categories "News" and "Top Searches" are received from the main page
  11. * Elements are sorted by name ascending!
  12. */
  13. $this->parameters['By "Latest" category'] = array(
  14. 'latest_category'=>array(
  15. 'name'=>'Latest category',
  16. 'type'=>'list',
  17. 'required'=>true,
  18. 'title'=>'Select your category',
  19. 'defaultValue'=>'News',
  20. 'values'=>array(
  21. 'Hot Torrents'=>'hot_torrents',
  22. 'News'=>'news',
  23. 'Releases'=>'releases',
  24. 'Torrents'=>'torrents'
  25. )
  26. )
  27. );
  28. /*
  29. * Get feeds for one of the "torrent" categories
  30. * Make sure to add new categories also to get_torrent_category_index($)!
  31. * Elements are sorted by name ascending!
  32. */
  33. $this->parameters['By "Torrent" category'] = array(
  34. 'torrent_category'=>array(
  35. 'name'=>'Torrent category',
  36. 'type'=>'list',
  37. 'required'=>true,
  38. 'title'=>'Select your category',
  39. 'defaultValue'=>'Anime',
  40. 'values'=>array(
  41. 'Adult'=>'adult',
  42. 'Anime'=>'anime',
  43. 'Books'=>'books',
  44. 'Games'=>'games',
  45. 'Movies'=>'movies',
  46. 'Music'=>'music',
  47. 'Other'=>'other',
  48. 'Series & TV'=>'series_tv',
  49. 'Software'=>'software'
  50. )
  51. ),
  52. 'torrent_popularity'=>array(
  53. 'name'=>'Sort by popularity',
  54. 'type'=>'checkbox',
  55. 'title'=>'Activate to receive results by popularity'
  56. )
  57. );
  58. /*
  59. * Get feeds for a specific search request
  60. */
  61. $this->parameters['Search torrent by name'] = array(
  62. 'search_name'=>array(
  63. 'name'=>'Name',
  64. 'required'=>true,
  65. 'title'=>'Insert your search query',
  66. 'exampleValue'=>'Bridge'
  67. ),
  68. 'search_category'=>array(
  69. 'name'=>'Category',
  70. 'type'=>'list',
  71. 'title'=>'Select your category',
  72. 'defaultValue'=>'All',
  73. 'values'=>array(
  74. 'Adult'=>'adult',
  75. 'All'=>'all',
  76. 'Anime'=>'anime',
  77. 'Books'=>'books',
  78. 'Games'=>'games',
  79. 'Movies'=>'movies',
  80. 'Music'=>'music',
  81. 'Other'=>'other',
  82. 'Series & TV'=>'series_tv',
  83. 'Software'=>'software'
  84. )
  85. )
  86. );
  87. }
  88. public function collectData(array $params){
  89. $request_path = '/'; // We'll request the main page by default
  90. if(isset($params['latest_category'])){ // Requesting one of the latest categories
  91. $this->request_latest_category($params['latest_category']);
  92. } elseif(isset($params['torrent_category'])){ // Requesting one of the torrent categories
  93. $order_popularity = false;
  94. if(isset($params['torrent_popularity']))
  95. $order_popularity = $params['torrent_popularity'] === "on";
  96. $this->request_torrent_category($params['torrent_category'], $order_popularity);
  97. } else if(isset($params['search_name'])){ // Requesting search
  98. if(isset($params['search_category']))
  99. $this->request_search($params['search_name'], $params['search_category']);
  100. else
  101. $this->request_search($params['search_name']);
  102. } else {
  103. $this->returnClientError('Unknown request!');
  104. }
  105. }
  106. public function getCacheDuration(){
  107. return 300; // 5 minutes
  108. }
  109. #region Helper functions for "By "Torrent" category"
  110. private function request_torrent_category($category, $order_popularity){
  111. $category_name = $this->get_torrent_category_name($category);
  112. $category_index = $this->get_torrent_category_index($category);
  113. $this->name = 'Category: ' . $category_name . ' - ' . $this->name;
  114. $this->uri .= $this->build_category_uri($category_index, $order_popularity);
  115. $html = $this->load_html($this->uri);
  116. if(strtolower(trim($category)) === 'movies') // This one is special (content wise)
  117. $this->get_movie_torrents($html);
  118. else
  119. $this->get_latest_torrents($html);
  120. }
  121. private function get_torrent_category_name($category){
  122. $parameter = $this->parameters['By "Torrent" category'];
  123. $languages = $parameter['torrent_category']['values'];
  124. foreach($languages as $name=>$value)
  125. if(strtolower(trim($value)) === strtolower(trim($category)))
  126. return $name;
  127. return 'Unknown category';
  128. }
  129. private function get_torrent_category_index($category){
  130. switch(strtolower(trim($category))){
  131. case 'anime': return 1;
  132. case 'software' : return 2;
  133. case 'games' : return 3;
  134. case 'adult' : return 4;
  135. case 'movies' : return 5;
  136. case 'music' : return 6;
  137. case 'other' : return 7;
  138. case 'series_tv' : return 8;
  139. case 'books': return 9;
  140. default: return 0;
  141. }
  142. }
  143. #endregion
  144. private function request_latest_category($category){
  145. switch($category){
  146. case 'hot_torrents': // This is a special case! (that's why return)
  147. $this->name = 'Latest hot torrents - ' . $this->name;
  148. $this->uri .= '/statistic/hot/torrents';
  149. $html = $this->load_html($this->uri);
  150. $this->get_latest_hot_torrents($html);
  151. return;
  152. case 'news': // This is a special case! (that's why return)
  153. $this->name = 'Latest news - ' . $this->name;
  154. $this->uri .= '/';
  155. $html = $this->load_html($this->uri);
  156. $this->get_latest_news($html);
  157. return;
  158. case 'releases':
  159. $this->name = 'Latest releases - ' . $this->name;
  160. $this->uri .= '/releases.php';
  161. break;
  162. case 'torrents':
  163. $this->name = 'Latest torrents - ' . $this->name;
  164. $this->uri .= '/latest.php';
  165. break;
  166. default: // No category applies
  167. $this->returnClientError('Undefined category: ' . $category . '!');
  168. }
  169. $html = $this->load_html($this->uri);
  170. $this->get_latest_torrents($html);
  171. }
  172. #region Helper functions for "Search torrent by name"
  173. private function request_search($name, $category = 'all'){
  174. $category_name = $this->get_search_category_name($category);
  175. $category_index = $this->get_search_category_index($category);
  176. $this->name = 'Search: "' . $name . '" in category: ' . $category_name . ' - ' . $this->name;
  177. $this->uri .= $this->build_category_uri($category_index);
  178. if(strtolower(trim($category)) === 'movies'){ // This one is special (content wise)
  179. $html = $this->load_html($this->uri);
  180. $this->get_movie_torrents($html);
  181. } else {
  182. $this->uri .= '&ihq=' . urlencode($name);
  183. $html = $this->load_html($this->uri);
  184. $this->get_latest_torrents($html);
  185. }
  186. }
  187. private function get_search_category_name($category){
  188. $parameter = $this->parameters['Search torrent by name'];
  189. $languages = $parameter['search_category']['values'];
  190. foreach($languages as $name=>$value)
  191. if(strtolower(trim($value)) === strtolower(trim($category)))
  192. return $name;
  193. return 'Unknown category';
  194. }
  195. private function get_search_category_index($category){
  196. switch(strtolower(trim($category))){
  197. case 'all': return 0;
  198. default: return $this->get_torrent_category_index($category); // Uses the same index
  199. }
  200. }
  201. #endregion
  202. #region Helper functions for "Movie Torrents"
  203. private function get_movie_torrents($html){
  204. $container = $html->find('div#w0', 0);
  205. if(!$container)
  206. $this->returnServerError('Unable to find torrent container!');
  207. $torrents = $container->find('article');
  208. if(!$torrents)
  209. $this->returnServerError('Unable to find torrents!');
  210. foreach($torrents as $torrent){
  211. $anchor = $torrent->find('a', 0);
  212. if(!$anchor)
  213. $this->returnServerError('Unable to find anchor!');
  214. $date = $torrent->find('small', 0);
  215. if(!$date)
  216. $this->returnServerError('Unable to find date!');
  217. $item = array();
  218. $item['uri'] = $this->fix_relative_uri($anchor->href);
  219. $item['title'] = $anchor->title;
  220. // $item['author'] =
  221. $item['timestamp'] = strtotime($date->plaintext);
  222. $item['content'] = $this->fix_relative_uri($torrent->innertext);
  223. $this->items[] = $item;
  224. }
  225. }
  226. #endregion
  227. #region Helper functions for "Latest Hot Torrents"
  228. private function get_latest_hot_torrents($html){
  229. $container = $html->find('div#serps', 0);
  230. if(!$container)
  231. $this->returnServerError('Unable to find torrent container!');
  232. $torrents = $container->find('tr');
  233. if(!$torrents)
  234. $this->returnServerError('Unable to find torrents!');
  235. // Remove first element (header row)
  236. $torrents = array_slice($torrents, 1);
  237. foreach($torrents as $torrent){
  238. $cell = $torrent->find('td', 0);
  239. if(!$cell)
  240. $this->returnServerError('Unable to find cell!');
  241. $element = $cell->find('a', 0);
  242. if(!$element)
  243. $this->returnServerError('Unable to find element!');
  244. $item = array();
  245. $item['uri'] = $element->href;
  246. $item['title'] = $element->plaintext;
  247. // $item['author'] =
  248. // $item['timestamp'] =
  249. // $item['content'] =
  250. $this->items[] = $item;
  251. }
  252. }
  253. #endregion
  254. #region Helper functions for "Latest News"
  255. private function get_latest_news($html){
  256. $container = $html->find('div#postcontainer', 0);
  257. if(!$container)
  258. $this->returnServerError('Unable to find post container!');
  259. $posts = $container->find('div.index-post');
  260. if(!$posts)
  261. $this->returnServerError('Unable to find posts!');
  262. foreach($posts as $post){
  263. $item = array();
  264. $item['uri'] = $this->latest_news_extract_uri($post);
  265. $item['title'] = $this->latest_news_extract_title($post);
  266. $item['author'] = $this->latest_news_extract_author($post);
  267. $item['timestamp'] = $this->latest_news_extract_timestamp($post);
  268. $item['content'] = $this->latest_news_extract_content($post);
  269. $this->items[] = $item;
  270. }
  271. }
  272. private function latest_news_extract_author($post){
  273. $author = $post->find('small', 0);
  274. if(!$author)
  275. $this->returnServerError('Unable to find author!');
  276. // The author is hidden within a string like: 'Posted by {author} on {date}'
  277. preg_match('/Posted\sby\s(.*)\son/i', $author->innertext, $matches);
  278. return $matches[1];
  279. }
  280. private function latest_news_extract_timestamp($post){
  281. $date = $post->find('small', 0);
  282. if(!$date)
  283. $this->returnServerError('Unable to find date!');
  284. // The date is hidden within a string like: 'Posted by {author} on {date}'
  285. preg_match('/Posted\sby\s.*\son\s(.*)/i', $date->innertext, $matches);
  286. $timestamp = strtotime($matches[1]);
  287. // Make sure date is not in the future (dates are given like 'Nov. 20' without year)
  288. if($timestamp > time()){
  289. $timestamp = strtotime('-1 year', $timestamp);
  290. }
  291. return $timestamp;
  292. }
  293. private function latest_news_extract_title($post){
  294. $title = $post->find('a', 0);
  295. if(!$title)
  296. $this->returnServerError('Unable to find title!');
  297. return $title->plaintext;
  298. }
  299. private function latest_news_extract_uri($post){
  300. $uri = $post->find('a', 0);
  301. if(!$uri)
  302. $this->returnServerError('Unable to find uri!');
  303. return $uri->href;
  304. }
  305. private function latest_news_extract_content($post){
  306. $content = $post->find('div', 0);
  307. if(!$content)
  308. $this->returnServerError('Unable to find content!');
  309. // Remove <h2>...</h2> (title)
  310. foreach($content->find('h2') as $element){
  311. $element->outertext = '';
  312. }
  313. // Remove <small>...</small> (author)
  314. foreach($content->find('small') as $element){
  315. $element->outertext = '';
  316. }
  317. return $content->innertext;
  318. }
  319. #endregion
  320. #region Helper functions for "Latest Torrents", "Latest Releases" and "Torrent Category"
  321. private function get_latest_torrents($html){
  322. $container = $html->find('div#serps', 0);
  323. if(!$container)
  324. $this->returnServerError('Unable to find torrent container!');
  325. $torrents = $container->find('tr[data-key]');
  326. if(!$torrents)
  327. $this->returnServerError('Unable to find torrents!');
  328. foreach($torrents as $torrent){
  329. $item = array();
  330. $item['uri'] = $this->latest_torrents_extract_uri($torrent);
  331. $item['title'] = $this->latest_torrents_extract_title($torrent);
  332. $item['author'] = $this->latest_torrents_extract_author($torrent);
  333. $item['timestamp'] = $this->latest_torrents_extract_timestamp($torrent);
  334. $item['content'] = ''; // There is no valuable content
  335. $this->items[] = $item;
  336. }
  337. }
  338. private function latest_torrents_extract_title($torrent){
  339. $cell = $torrent->find('td.title-row', 0);
  340. if(!$cell)
  341. $this->returnServerError('Unable to find title cell!');
  342. $title = $cell->find('span', 0);
  343. if(!$title)
  344. $this->returnServerError('Unable to find title!');
  345. return $title->plaintext;
  346. }
  347. private function latest_torrents_extract_uri($torrent){
  348. $cell = $torrent->find('td.title-row', 0);
  349. if(!$cell)
  350. $this->returnServerError('Unable to find title cell!');
  351. $uri = $cell->find('a', 0);
  352. if(!$uri)
  353. $this->returnServerError('Unable to find uri!');
  354. return $this->fix_relative_uri($uri->href);
  355. }
  356. private function latest_torrents_extract_author($torrent){
  357. $cell = $torrent->find('td.user-row', 0);
  358. if(!$cell)
  359. return; // No author
  360. $user = $cell->find('a', 0);
  361. if(!$user)
  362. $this->returnServerError('Unable to find user!');
  363. return $user->plaintext;
  364. }
  365. private function latest_torrents_extract_timestamp($torrent){
  366. $cell = $torrent->find('td.date-row', 0);
  367. if(!$cell)
  368. $this->returnServerError('Unable to find date cell!');
  369. return strtotime('-' . $cell->plaintext, time());
  370. }
  371. #endregion
  372. #region Generic helper functions
  373. private function load_html($uri){
  374. $html = $this->getSimpleHTMLDOM($uri);
  375. if(!$html)
  376. $this->returnServerError('Unable to load ' . $uri . '!');
  377. return $html;
  378. }
  379. private function fix_relative_uri($uri){
  380. return preg_replace('/\//i', 'https://isohunt.to/', $uri, 1);
  381. }
  382. private function build_category_uri($index, $order_popularity = false){
  383. return '/torrents/?iht=' . $index . '&ihs=' . ($order_popularity ? 1 : 0) . '&age=0';
  384. }
  385. #endregion
  386. }