IsoHuntBridge.php 12 KB

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