IsoHuntBridge.php 13 KB

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