wiby.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class wiby{
  3. public function __construct(){
  4. include "lib/nextpage.php";
  5. $this->nextpage = new nextpage("wiby");
  6. }
  7. public function getfilters($page){
  8. if($page != "web"){
  9. return [];
  10. }
  11. return [
  12. "nsfw" => [
  13. "display" => "NSFW",
  14. "option" => [
  15. "yes" => "Yes",
  16. "no" => "No"
  17. ]
  18. ],
  19. "date" => [
  20. "display" => "Time posted",
  21. "option" => [
  22. "any" => "Any time",
  23. "day" => "Past day",
  24. "week" => "Past week",
  25. "month" => "Past month",
  26. "year" => "Past year",
  27. ]
  28. ]
  29. ];
  30. }
  31. private function get($url, $get = [], $nsfw){
  32. $curlproc = curl_init();
  33. if($get !== []){
  34. $get = http_build_query($get);
  35. $url .= "?" . $get;
  36. }
  37. curl_setopt($curlproc, CURLOPT_URL, $url);
  38. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  39. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  40. ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/110.0",
  41. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  42. "Accept-Language: en-US,en;q=0.5",
  43. "Accept-Encoding: gzip",
  44. "Cookie: ws={$nsfw}",
  45. "DNT: 1",
  46. "Connection: keep-alive",
  47. "Upgrade-Insecure-Requests: 1",
  48. "Sec-Fetch-Dest: document",
  49. "Sec-Fetch-Mode: navigate",
  50. "Sec-Fetch-Site: none",
  51. "Sec-Fetch-User: ?1"]
  52. );
  53. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  54. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  55. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  56. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  57. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  58. $data = curl_exec($curlproc);
  59. if(curl_errno($curlproc)){
  60. throw new Exception(curl_error($curlproc));
  61. }
  62. curl_close($curlproc);
  63. return $data;
  64. }
  65. public function web($get){
  66. if($get["npt"]){
  67. $q =
  68. json_decode(
  69. $this->nextpage->get($get["npt"], "web"),
  70. true
  71. );
  72. $nsfw = $q["nsfw"];
  73. unset($q["nsfw"]);
  74. }else{
  75. $search = $get["s"];
  76. if(strlen($search) === 0){
  77. throw new Exception("Search term is empty!");
  78. }
  79. $date = $get["date"];
  80. $nsfw = $get["nsfw"] == "yes" ? "0" : "1";
  81. $search =
  82. str_replace(
  83. [
  84. "!g",
  85. "!gi",
  86. "!gv",
  87. "!gm",
  88. "!b",
  89. "!bi",
  90. "!bv",
  91. "!bm",
  92. "!td",
  93. "!tw",
  94. "!tm",
  95. "!ty",
  96. "&g",
  97. "&gi",
  98. "&gv",
  99. "&gm",
  100. "&b",
  101. "&bi",
  102. "&bv",
  103. "&bm",
  104. "&td",
  105. "&tw",
  106. "&tm",
  107. "&ty",
  108. ],
  109. "",
  110. $search
  111. );
  112. switch($date){
  113. case "day": $search = "!td " . $search; break;
  114. case "week": $search = "!tw " . $search; break;
  115. case "month": $search = "!tm " . $search; break;
  116. case "year": $search = "!ty " . $search; break;
  117. }
  118. $q = [
  119. "q" => $search
  120. ];
  121. }
  122. try{
  123. $html = $this->get(
  124. "https://wiby.me/",
  125. $q,
  126. $nsfw
  127. );
  128. }catch(Exception $error){
  129. throw new Exception("Failed to fetch search page");
  130. }
  131. preg_match(
  132. '/<p class="pin"><blockquote>(?:<\/p>)?<br><a class="more" href="\/\?q=[^"]+&p=([0-9]+)">Find more\.\.\.<\/a><\/blockquote>/',
  133. $html,
  134. $nextpage
  135. );
  136. if(count($nextpage) === 0){
  137. $nextpage = null;
  138. }else{
  139. $nextpage =
  140. $this->nextpage->store(
  141. json_encode([
  142. "q" => $q["q"],
  143. "p" => (int)$nextpage[1],
  144. "nsfw" => $nsfw
  145. ]),
  146. "web"
  147. );
  148. }
  149. $out = [
  150. "status" => "ok",
  151. "spelling" => [
  152. "type" => "no_correction",
  153. "using" => null,
  154. "correction" => null
  155. ],
  156. "npt" => $nextpage,
  157. "answer" => [],
  158. "web" => [],
  159. "image" => [],
  160. "video" => [],
  161. "news" => [],
  162. "related" => []
  163. ];
  164. preg_match_all(
  165. '/<blockquote>[\s]*<a .* href="(.*)">(.*)<\/a>.*<p>(.*)<\/p>[\s]*<\/blockquote>/Ui',
  166. $html,
  167. $links
  168. );
  169. for($i=0; $i<count($links[0]); $i++){
  170. $out["web"][] = [
  171. "title" => $this->unescapehtml(trim($links[2][$i])),
  172. "description" => $this->unescapehtml(trim(strip_tags($links[3][$i]))),
  173. "url" => trim($links[1][$i]),
  174. "date" => null,
  175. "type" => "web",
  176. "thumb" => [
  177. "url" => null,
  178. "ratio" => null
  179. ],
  180. "sublink" => [],
  181. "table" => []
  182. ];
  183. }
  184. return $out;
  185. }
  186. private function unescapehtml($str){
  187. return html_entity_decode(
  188. str_replace(
  189. [
  190. "<br>",
  191. "<br/>",
  192. "</br>",
  193. "<BR>",
  194. "<BR/>",
  195. "</BR>",
  196. ],
  197. "\n",
  198. $str
  199. ),
  200. ENT_QUOTES | ENT_XML1, 'UTF-8'
  201. );
  202. }
  203. }