IsoHuntBridge.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. class IsoHuntBridge extends BridgeAbstract{
  3. const MAINTAINER = 'logmanoriginal';
  4. const NAME = 'isoHunt Bridge';
  5. const URI = 'https://isohunt.to/';
  6. const DESCRIPTION = 'Returns the latest results by category or search result';
  7. const PARAMETERS = array(
  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. '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. '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. '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 getURI(){
  89. $uri=self::URI;
  90. switch($this->queriedContext){
  91. case 'By "Latest" category':
  92. switch($this->getInput('latest_category')){
  93. case 'hot_torrents':
  94. $uri .= 'statistic/hot/torrents';
  95. break;
  96. case 'news':
  97. break;
  98. case 'releases':
  99. $uri .= 'releases.php';
  100. break;
  101. case 'torrents':
  102. $uri .= 'latest.php';
  103. break;
  104. }
  105. break;
  106. case 'By "Torrent" category':
  107. $uri .= $this->build_category_uri(
  108. $this->getInput('torrent_category'),
  109. $this->getInput('torrent_popularity')
  110. );
  111. break;
  112. case 'Search torrent by name':
  113. $category=$this->getInput('search_category');
  114. $uri .= $this->build_category_uri($category);
  115. if($category!=='movies')
  116. $uri .= '&ihq=' . urlencode($this->getInput('search_name'));
  117. break;
  118. }
  119. return $uri;
  120. }
  121. public function getName(){
  122. switch($this->queriedContext){
  123. case 'By "Latest" category':
  124. $categoryName =
  125. array_search(
  126. $this->getInput('latest_category'),
  127. self::PARAMETERS['By "Latest" category']['latest_category']['values']
  128. );
  129. $name = 'Latest '.$categoryName.' - ' . self::NAME;
  130. break;
  131. case 'By "Torrent" category':
  132. $categoryName =
  133. 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 =
  141. array_search(
  142. $this->getInput('search_category'),
  143. self::PARAMETERS['Search torrent by name']['search_category']['values']
  144. );
  145. $name = 'Search: "' . $this->getInput('search_name') . '" in category: ' . $categoryName . ' - ' . self::NAME;
  146. break;
  147. }
  148. return $name;
  149. }
  150. public function collectData(){
  151. $html = $this->load_html($this->getURI());
  152. switch($this->queriedContext){
  153. case 'By "Latest" category':
  154. switch($this->getInput('latest_category')){
  155. case 'hot_torrents':
  156. $this->get_latest_hot_torrents($html);
  157. break;
  158. case 'news':
  159. $this->get_latest_news($html);
  160. break;
  161. case 'releases':
  162. case 'torrents':
  163. $this->get_latest_torrents($html);
  164. break;
  165. }
  166. break;
  167. case 'By "Torrent" category':
  168. if($this->getInput('torrent_category') === 'movies'){
  169. // This one is special (content wise)
  170. $this->get_movie_torrents($html);
  171. }else{
  172. $this->get_latest_torrents($html);
  173. }
  174. break;
  175. case 'Search torrent by name':
  176. if( $this->getInput('search_category') === 'movies'){
  177. // This one is special (content wise)
  178. $this->get_movie_torrents($html);
  179. } else {
  180. $this->get_latest_torrents($html);
  181. }
  182. break;
  183. }
  184. }
  185. public function getCacheDuration(){
  186. return 300; // 5 minutes
  187. }
  188. #region Helper functions for "Movie Torrents"
  189. private function get_movie_torrents($html){
  190. $container = $html->find('div#w0', 0);
  191. if(!$container)
  192. returnServerError('Unable to find torrent container!');
  193. $torrents = $container->find('article');
  194. if(!$torrents)
  195. returnServerError('Unable to find torrents!');
  196. foreach($torrents as $torrent){
  197. $anchor = $torrent->find('a', 0);
  198. if(!$anchor)
  199. returnServerError('Unable to find anchor!');
  200. $date = $torrent->find('small', 0);
  201. if(!$date)
  202. returnServerError('Unable to find date!');
  203. $item = array();
  204. $item['uri'] = $this->fix_relative_uri($anchor->href);
  205. $item['title'] = $anchor->title;
  206. // $item['author'] =
  207. $item['timestamp'] = strtotime($date->plaintext);
  208. $item['content'] = $this->fix_relative_uri($torrent->innertext);
  209. $this->items[] = $item;
  210. }
  211. }
  212. #endregion
  213. #region Helper functions for "Latest Hot Torrents"
  214. private function get_latest_hot_torrents($html){
  215. $container = $html->find('div#serps', 0);
  216. if(!$container)
  217. returnServerError('Unable to find torrent container!');
  218. $torrents = $container->find('tr');
  219. if(!$torrents)
  220. returnServerError('Unable to find torrents!');
  221. // Remove first element (header row)
  222. $torrents = array_slice($torrents, 1);
  223. foreach($torrents as $torrent){
  224. $cell = $torrent->find('td', 0);
  225. if(!$cell)
  226. returnServerError('Unable to find cell!');
  227. $element = $cell->find('a', 0);
  228. if(!$element)
  229. returnServerError('Unable to find element!');
  230. $item = array();
  231. $item['uri'] = $element->href;
  232. $item['title'] = $element->plaintext;
  233. // $item['author'] =
  234. // $item['timestamp'] =
  235. // $item['content'] =
  236. $this->items[] = $item;
  237. }
  238. }
  239. #endregion
  240. #region Helper functions for "Latest News"
  241. private function get_latest_news($html){
  242. $container = $html->find('div#postcontainer', 0);
  243. if(!$container)
  244. returnServerError('Unable to find post container!');
  245. $posts = $container->find('div.index-post');
  246. if(!$posts)
  247. returnServerError('Unable to find posts!');
  248. foreach($posts as $post){
  249. $item = array();
  250. $item['uri'] = $this->latest_news_extract_uri($post);
  251. $item['title'] = $this->latest_news_extract_title($post);
  252. $item['author'] = $this->latest_news_extract_author($post);
  253. $item['timestamp'] = $this->latest_news_extract_timestamp($post);
  254. $item['content'] = $this->latest_news_extract_content($post);
  255. $this->items[] = $item;
  256. }
  257. }
  258. private function latest_news_extract_author($post){
  259. $author = $post->find('small', 0);
  260. if(!$author)
  261. returnServerError('Unable to find author!');
  262. // The author is hidden within a string like: 'Posted by {author} on {date}'
  263. preg_match('/Posted\sby\s(.*)\son/i', $author->innertext, $matches);
  264. return $matches[1];
  265. }
  266. private function latest_news_extract_timestamp($post){
  267. $date = $post->find('small', 0);
  268. if(!$date)
  269. returnServerError('Unable to find date!');
  270. // The date is hidden within a string like: 'Posted by {author} on {date}'
  271. preg_match('/Posted\sby\s.*\son\s(.*)/i', $date->innertext, $matches);
  272. $timestamp = strtotime($matches[1]);
  273. // Make sure date is not in the future (dates are given like 'Nov. 20' without year)
  274. if($timestamp > time()){
  275. $timestamp = strtotime('-1 year', $timestamp);
  276. }
  277. return $timestamp;
  278. }
  279. private function latest_news_extract_title($post){
  280. $title = $post->find('a', 0);
  281. if(!$title)
  282. returnServerError('Unable to find title!');
  283. return $title->plaintext;
  284. }
  285. private function latest_news_extract_uri($post){
  286. $uri = $post->find('a', 0);
  287. if(!$uri)
  288. returnServerError('Unable to find uri!');
  289. return $uri->href;
  290. }
  291. private function latest_news_extract_content($post){
  292. $content = $post->find('div', 0);
  293. if(!$content)
  294. returnServerError('Unable to find content!');
  295. // Remove <h2>...</h2> (title)
  296. foreach($content->find('h2') as $element){
  297. $element->outertext = '';
  298. }
  299. // Remove <small>...</small> (author)
  300. foreach($content->find('small') as $element){
  301. $element->outertext = '';
  302. }
  303. return $content->innertext;
  304. }
  305. #endregion
  306. #region Helper functions for "Latest Torrents", "Latest Releases" and "Torrent Category"
  307. private function get_latest_torrents($html){
  308. $container = $html->find('div#serps', 0);
  309. if(!$container)
  310. returnServerError('Unable to find torrent container!');
  311. $torrents = $container->find('tr[data-key]');
  312. if(!$torrents)
  313. returnServerError('Unable to find torrents!');
  314. foreach($torrents as $torrent){
  315. $item = array();
  316. $item['uri'] = $this->latest_torrents_extract_uri($torrent);
  317. $item['title'] = $this->latest_torrents_extract_title($torrent);
  318. $item['author'] = $this->latest_torrents_extract_author($torrent);
  319. $item['timestamp'] = $this->latest_torrents_extract_timestamp($torrent);
  320. $item['content'] = ''; // There is no valuable content
  321. $this->items[] = $item;
  322. }
  323. }
  324. private function latest_torrents_extract_title($torrent){
  325. $cell = $torrent->find('td.title-row', 0);
  326. if(!$cell)
  327. returnServerError('Unable to find title cell!');
  328. $title = $cell->find('span', 0);
  329. if(!$title)
  330. returnServerError('Unable to find title!');
  331. return $title->plaintext;
  332. }
  333. private function latest_torrents_extract_uri($torrent){
  334. $cell = $torrent->find('td.title-row', 0);
  335. if(!$cell)
  336. returnServerError('Unable to find title cell!');
  337. $uri = $cell->find('a', 0);
  338. if(!$uri)
  339. returnServerError('Unable to find uri!');
  340. return $this->fix_relative_uri($uri->href);
  341. }
  342. private function latest_torrents_extract_author($torrent){
  343. $cell = $torrent->find('td.user-row', 0);
  344. if(!$cell)
  345. return; // No author
  346. $user = $cell->find('a', 0);
  347. if(!$user)
  348. returnServerError('Unable to find user!');
  349. return $user->plaintext;
  350. }
  351. private function latest_torrents_extract_timestamp($torrent){
  352. $cell = $torrent->find('td.date-row', 0);
  353. if(!$cell)
  354. returnServerError('Unable to find date cell!');
  355. return strtotime('-' . $cell->plaintext, time());
  356. }
  357. #endregion
  358. #region Generic helper functions
  359. private function load_html($uri){
  360. $html = getSimpleHTMLDOM($uri);
  361. if(!$html)
  362. returnServerError('Unable to load ' . $uri . '!');
  363. return $html;
  364. }
  365. private function fix_relative_uri($uri){
  366. return preg_replace('/\//i', self::URI, $uri, 1);
  367. }
  368. private function build_category_uri($category, $order_popularity = false){
  369. switch($category){
  370. case 'anime': $index = 1; break;
  371. case 'software' : $index = 2; break;
  372. case 'games' : $index = 3; break;
  373. case 'adult' : $index = 4; break;
  374. case 'movies' : $index = 5; break;
  375. case 'music' : $index = 6; break;
  376. case 'other' : $index = 7; break;
  377. case 'series_tv' : $index = 8; break;
  378. case 'books': $index = 9; break;
  379. case 'all':
  380. default: $index = 0; break;
  381. }
  382. return 'torrents/?iht=' . $index . '&ihs=' . ($order_popularity ? 1 : 0) . '&age=0';
  383. }
  384. #endregion
  385. }