GoogleEngine.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /* $Id: GoogleEngine.php 2506 2007-02-04 15:12:30Z b4rt $ */
  3. /*************************************************************
  4. * TorrentFlux PHP Torrent Manager
  5. * www.torrentflux.com
  6. **************************************************************/
  7. /*
  8. This file is part of TorrentFlux.
  9. TorrentFlux is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. TorrentFlux is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with TorrentFlux; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. include_once("GoogleFunctions.php");
  22. include_once("inc/classes/BDecode.php");
  23. class SearchEngine extends SearchEngineBase
  24. {
  25. function SearchEngine($cfg)
  26. {
  27. $this->mainURL = "google.com";
  28. $this->altURL = "www.google.com";
  29. $this->mainTitle = "Google";
  30. $this->engineName = "Google";
  31. $this->author = "kboy";
  32. $this->version = "1.00-tfb";
  33. $this->updateURL = "http://www.torrentflux.com/forum/index.php/topic,1690.0.html";
  34. $this->Initialize($cfg);
  35. }
  36. //----------------------------------------------------------------
  37. // Function to get Latest..
  38. function getLatest()
  39. {
  40. $this->msg = "Get Latest Not available on this Engine";
  41. return $this->msg;
  42. }
  43. //----------------------------------------------------------------
  44. // Function to perform Search.
  45. function performSearch($searchTerm)
  46. {
  47. //http://www.google.com/search?as_q=test+d8&num=3&as_qdr=m3&as_filetype=torrent
  48. // as_q -> advanced search query term (NOTE: we must add +d8 to scrub down to just true torrent files)
  49. // num -> is the number of results per page.
  50. // lr -> language (lang_en) for english
  51. // as_qdr -> advanced search query days returned. (m3) is updated in the past 1 months
  52. // m3 ... and y (year) are valid values.
  53. // as_filetype -> to search for torrents :-)
  54. $searchTerm = str_replace(" ", "+", $searchTerm);
  55. $request = '/search?as_q=' . $searchTerm . '+d8&num=100&lr=lang_en&as_qdr=m3&as_filetype=torrent';
  56. if ($this->makeRequest($request))
  57. {
  58. return $this->parseResponse();
  59. }
  60. else
  61. {
  62. return $this->msg;
  63. }
  64. }
  65. //----------------------------------------------------------------
  66. // Function to parse the response.
  67. function parseResponse()
  68. {
  69. $output = $this->tableHeader();
  70. $output .= "\n<!-- Begin Response --> \n";
  71. $thing = $this->htmlPage;
  72. // We got a response so display it.
  73. // Chop the front end off.
  74. while (is_integer(strpos($thing,"Results")))
  75. {
  76. $thing = substr($thing,strpos($thing,"Results"));
  77. $thing = substr($thing,strpos($thing,"</table>")+strlen("</table>"));
  78. if (is_integer(strpos($thing,"Sponsored Links"))) {
  79. $thing = substr($thing,strpos($thing,"Sponsored Links"));
  80. $thing = substr($thing,strpos($thing,"</table>")+strlen("</table>"));
  81. }
  82. //$thing = substr($thing,strpos($thing,"<div>")-strlen("<div>");
  83. $tmpList = substr($thing,0,strpos($thing,"</div>"));
  84. if (is_integer(strpos($tmpList,"In order to show you the most relevant results"))) {
  85. $tmpList = substr($thing,0,strpos($thing,"In order to show you the most relevant results")) . "</div>";
  86. }
  87. // ok so now we have the listing.
  88. //$tmpListArr = explode("</p>",$tmpList);
  89. $allowedTags = '<a><b><i><br>';
  90. // ok so now we have the listing.
  91. $tmpListArr = explode("</a>",strip_tags($tmpList,$allowedTags));
  92. $langFile = $this->cfg['_FILE'];
  93. $bg = $this->cfg["bgLight"];
  94. foreach($tmpListArr as $key =>$value)
  95. {
  96. if (strpos($value,"Similar&nbsp;pages"))
  97. {
  98. }
  99. elseif (strpos($value, "ile Format:"))
  100. {
  101. }
  102. elseif (strpos($value, "Cached"))
  103. {
  104. }
  105. elseif (strpos($value, "More results from"))
  106. {
  107. }
  108. elseif (strpos($value, "Translate this page"))
  109. {
  110. }
  111. else
  112. {
  113. $goo = new gOOGLE($value);
  114. if (!empty($goo->torrentFile)) {
  115. $output .= trim($goo->BuildOutput($bg,$langFile,$this->searchURL()));
  116. // ok switch colors.
  117. if ($bg == $this->cfg["bgLight"])
  118. {
  119. $bg = $this->cfg["bgDark"];
  120. }
  121. else
  122. {
  123. $bg = $this->cfg["bgLight"];
  124. }
  125. }
  126. }
  127. }
  128. // set thing to end of this table.
  129. $thing = substr($thing,strpos($thing,"</table>"));
  130. }
  131. $output .= "</table>";
  132. $output .= "\n<!-- End Response --> \n";
  133. return $output;
  134. }
  135. }
  136. // This is a worker class that takes in a row in a table and parses it.
  137. class gOOGLE
  138. {
  139. var $torrentName = "";
  140. var $torrentDisplayName = "";
  141. var $torrentFile = "";
  142. var $torrentStatus = "";
  143. var $torrentSize = "";
  144. var $fileCount = "";
  145. var $Seeds = "";
  146. var $Peers = "";
  147. var $Data = "";
  148. function gOOGLE( $htmlLine )
  149. {
  150. $tmpVal = substr($htmlLine,strpos($htmlLine,"<a"));
  151. $tmpVal = trim($tmpVal);
  152. if (strlen($tmpVal) > 0)
  153. {
  154. $this->Data = $htmlLine;
  155. $tmpVal2 = substr($tmpVal,strpos($tmpVal,"href=\"")+strlen("href=\""));
  156. $this->torrentFile = substr($tmpVal2,0,strpos($tmpVal2,"\""));
  157. $html = FetchHTMLNoWaitNoFollow( $this->torrentFile );
  158. // Make sure we have a torrent file
  159. if( strpos( $html, "d8:" ) === false )
  160. {
  161. // We don't have a Torrent File... it is something else
  162. $this->torrentFile = "";
  163. }
  164. else
  165. {
  166. $array = BDecode($html);
  167. $this->torrentSize = formatBytesTokBMBGBTB($array["info"]["piece length"] * (strlen($array["info"]["pieces"]) / 20));
  168. $this->torrentName = $array['info']['name'];
  169. $this->fileCount = count($array['info']['files']);
  170. $this->torrentDisplayName = $array['info']['name'];
  171. if(array_key_exists('comment',$array))
  172. {
  173. $this->torrentDisplayName .= " [". $array['comment']."]";
  174. }
  175. }
  176. /*
  177. $this->Seeds = $this->cleanLine($tmpListArr["4"]); // Seeds
  178. $this->Peers = $this->cleanLine($tmpListArr["5"]); // Peers
  179. */
  180. if ($this->Peers == '')
  181. {
  182. $this->Peers = "N/A";
  183. if (empty($this->Seeds)) $this->Seeds = "N/A";
  184. }
  185. if ($this->Seeds == '') $this->Seeds = "N/A";
  186. if(strlen($this->torrentDisplayName) > 50)
  187. {
  188. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  189. }
  190. }
  191. }
  192. function cleanLine($stringIn,$tags='')
  193. {
  194. if(empty($tags))
  195. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn)));
  196. else
  197. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn,$tags)));
  198. }
  199. //----------------------------------------------------------------
  200. // Function to build output for the table.
  201. function BuildOutput($bg,$langFILE, $searchURL = '')
  202. {
  203. $output = "<tr>\n";
  204. $output .= " <td width=16 bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\"><img src=\"".getImagesPath()."download_owner.gif\" width=\"16\" height=\"16\" title=\"".$this->torrentName."\" border=0></a></td>\n";
  205. $output .= " <td bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\" title=\"".$this->torrentName."\">".$this->torrentDisplayName."</a></td>\n";
  206. $output .= "</td>\n";
  207. $output .= " <td bgcolor=\"".$bg."\">&nbsp;</td>\n";
  208. $output .= " <td bgcolor=\"".$bg."\" align=right>".$this->torrentSize."</td>\n";
  209. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Seeds."</td>\n";
  210. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Peers."</td>\n";
  211. $output .= "</tr>\n";
  212. return $output;
  213. }
  214. }
  215. ?>