1
0

brave.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. <?php
  2. class brave{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("brave");
  8. }
  9. public function getfilters($page){
  10. switch($page){
  11. case "web":
  12. return [
  13. "country" => [
  14. "display" => "Country",
  15. "option" => [
  16. "all" => "All Regions",
  17. "ar" => "Argentina",
  18. "au" => "Australia",
  19. "at" => "Austria",
  20. "be" => "Belgium",
  21. "br" => "Brazil",
  22. "ca" => "Canada",
  23. "cl" => "Chile",
  24. "cn" => "China",
  25. "dk" => "Denmark",
  26. "fi" => "Finland",
  27. "fr" => "France",
  28. "de" => "Germany",
  29. "hk" => "Hong Kong",
  30. "in" => "India",
  31. "id" => "Indonesia",
  32. "it" => "Italy",
  33. "jp" => "Japan",
  34. "kr" => "Korea",
  35. "my" => "Malaysia",
  36. "mx" => "Mexico",
  37. "nl" => "Netherlands",
  38. "nz" => "New Zealand",
  39. "no" => "Norway",
  40. "pl" => "Poland",
  41. "pt" => "Portugal",
  42. "ph" => "Philippines",
  43. "ru" => "Russia",
  44. "sa" => "Saudi Arabia",
  45. "za" => "South Africa",
  46. "es" => "Spain",
  47. "se" => "Sweden",
  48. "ch" => "Switzerland",
  49. "tw" => "Taiwan",
  50. "tr" => "Turkey",
  51. "gb" => "United Kingdom",
  52. "us" => "United States"
  53. ]
  54. ],
  55. "nsfw" => [
  56. "display" => "NSFW",
  57. "option" => [
  58. "yes" => "Yes",
  59. "maybe" => "Maybe",
  60. "no" => "No"
  61. ]
  62. ],
  63. "newer" => [
  64. "display" => "Newer than",
  65. "option" => "_DATE"
  66. ],
  67. "older" => [
  68. "display" => "Older than",
  69. "option" => "_DATE"
  70. ],
  71. "spellcheck" => [
  72. "display" => "Spellcheck",
  73. "option" => [
  74. "yes" => "Yes",
  75. "no" => "No"
  76. ]
  77. ]
  78. ];
  79. break;
  80. case "images":
  81. case "videos":
  82. case "news":
  83. return [
  84. "country" => [
  85. "display" => "Country",
  86. "option" => [
  87. "all" => "All regions",
  88. "ar" => "Argentina",
  89. "au" => "Australia",
  90. "at" => "Austria",
  91. "be" => "Belgium",
  92. "br" => "Brazil",
  93. "ca" => "Canada",
  94. "cl" => "Chile",
  95. "cn" => "China",
  96. "dk" => "Denmark",
  97. "fi" => "Finland",
  98. "fr" => "France",
  99. "de" => "Germany",
  100. "hk" => "Hong Kong",
  101. "in" => "India",
  102. "id" => "Indonesia",
  103. "it" => "Italy",
  104. "jp" => "Japan",
  105. "kr" => "Korea",
  106. "my" => "Malaysia",
  107. "mx" => "Mexico",
  108. "nl" => "Netherlands",
  109. "nz" => "New Zealand",
  110. "no" => "Norway",
  111. "pl" => "Poland",
  112. "pt" => "Portugal",
  113. "ph" => "Philippines",
  114. "ru" => "Russia",
  115. "sa" => "Saudi Arabia",
  116. "za" => "South Africa",
  117. "es" => "Spain",
  118. "se" => "Sweden",
  119. "ch" => "Switzerland",
  120. "tw" => "Taiwan",
  121. "tr" => "Turkey",
  122. "gb" => "United Kingdom",
  123. "us" => "United States"
  124. ]
  125. ],
  126. "nsfw" => [
  127. "display" => "NSFW",
  128. "option" => [
  129. "yes" => "Yes",
  130. "maybe" => "Maybe",
  131. "no" => "No"
  132. ]
  133. ],
  134. "spellcheck" => [
  135. "display" => "Spellcheck",
  136. "option" => [
  137. "yes" => "Yes",
  138. "no" => "No"
  139. ]
  140. ]
  141. ];
  142. break;
  143. }
  144. }
  145. private function get($proxy, $url, $get = [], $nsfw, $country){
  146. switch($nsfw){
  147. case "yes": $nsfw = "off"; break;
  148. case "maybe": $nsfw = "moderate"; break;
  149. case "no": $nsfw = "strict"; break;
  150. }
  151. if($country == "any"){
  152. $country = "all";
  153. }
  154. $headers = [
  155. "User-Agent: " . config::USER_AGENT,
  156. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  157. "Accept-Language: en-US,en;q=0.5",
  158. "Accept-Encoding: gzip",
  159. "Cookie: safesearch={$nsfw}; country={$country}; useLocation=0; summarizer=0",
  160. "DNT: 1",
  161. "Connection: keep-alive",
  162. "Upgrade-Insecure-Requests: 1",
  163. "Sec-Fetch-Dest: document",
  164. "Sec-Fetch-Mode: navigate",
  165. "Sec-Fetch-Site: none",
  166. "Sec-Fetch-User: ?1"
  167. ];
  168. $curlproc = curl_init();
  169. if($get !== []){
  170. $get = http_build_query($get);
  171. $url .= "?" . $get;
  172. }
  173. curl_setopt($curlproc, CURLOPT_URL, $url);
  174. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  175. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  176. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  177. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  178. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  179. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  180. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  181. $this->backend->assign_proxy($curlproc, $proxy);
  182. $data = curl_exec($curlproc);
  183. if(curl_errno($curlproc)){
  184. throw new Exception(curl_error($curlproc));
  185. }
  186. curl_close($curlproc);
  187. return $data;
  188. }
  189. public function web($get){
  190. if($get["npt"]){
  191. // get next page data
  192. [$q, $proxy] = $this->backend->get($get["npt"], "web");
  193. $q = json_decode($q, true);
  194. $search = $q["q"];
  195. $q["spellcheck"] = "0";
  196. $nsfw = $q["nsfw"];
  197. unset($q["nsfw"]);
  198. $country = $q["country"];
  199. unset($q["country"]);
  200. }else{
  201. // get _GET data instead
  202. $search = $get["s"];
  203. if(strlen($search) === 0){
  204. throw new Exception("Search term is empty!");
  205. }
  206. if(strlen($search) > 2048){
  207. throw new Exception("Search term is too long!");
  208. }
  209. $proxy = $this->backend->get_ip();
  210. $nsfw = $get["nsfw"];
  211. $country = $get["country"];
  212. $older = $get["older"];
  213. $newer = $get["newer"];
  214. $spellcheck = $get["spellcheck"];
  215. $q = [
  216. "q" => $search
  217. ];
  218. /*
  219. Pass older/newer filters to brave
  220. */
  221. if($newer !== false){
  222. $newer = date("Y-m-d", $newer);
  223. if($older === false){
  224. $older = date("Y-m-d", time());
  225. }
  226. }
  227. if(
  228. is_string($older) === false &&
  229. $older !== false
  230. ){
  231. $older = date("Y-m-d", $older);
  232. if($newer === false){
  233. $newer = "1970-01-02";
  234. }
  235. }
  236. if($older !== false){
  237. $q["tf"] = "{$newer}to{$older}";
  238. }
  239. // spellcheck
  240. if($spellcheck == "no"){
  241. $q["spellcheck"] = "0";
  242. }
  243. }
  244. /*
  245. $handle = fopen("scraper/brave.html", "r");
  246. $html = fread($handle, filesize("scraper/brave.html"));
  247. fclose($handle);
  248. */
  249. try{
  250. $html =
  251. $this->get(
  252. $proxy,
  253. "https://search.brave.com/search",
  254. $q,
  255. $nsfw,
  256. $country
  257. );
  258. }catch(Exception $error){
  259. throw new Exception("Could not fetch search page");
  260. }
  261. $out = [
  262. "status" => "ok",
  263. "spelling" => [
  264. "type" => "no_correction",
  265. "using" => null,
  266. "correction" => null
  267. ],
  268. "npt" => null,
  269. "answer" => [],
  270. "web" => [],
  271. "image" => [],
  272. "video" => [],
  273. "news" => [],
  274. "related" => []
  275. ];
  276. // load html
  277. $this->fuckhtml->load($html);
  278. /*
  279. Get next page "token"
  280. */
  281. $nextpage =
  282. $this->fuckhtml
  283. ->getElementById(
  284. "pagination",
  285. "div"
  286. );
  287. if($nextpage){
  288. $this->fuckhtml->load($nextpage);
  289. $nextpage =
  290. $this->fuckhtml
  291. ->getElementsByClassName("btn", "a");
  292. if(count($nextpage) !== 0){
  293. $nextpage =
  294. $nextpage[count($nextpage) - 1];
  295. if(
  296. strtolower(
  297. $this->fuckhtml
  298. ->getTextContent(
  299. $nextpage
  300. )
  301. ) == "next"
  302. ){
  303. preg_match(
  304. '/offset=([0-9]+)/',
  305. $this->fuckhtml->getTextContent($nextpage["attributes"]["href"]),
  306. $nextpage
  307. );
  308. $q["offset"] = (int)$nextpage[1];
  309. $q["nsfw"] = $nsfw;
  310. $q["country"] = $country;
  311. $out["npt"] =
  312. $this->backend->store(
  313. json_encode($q),
  314. "web",
  315. $proxy
  316. );
  317. }
  318. }
  319. }
  320. $this->fuckhtml->load($html);
  321. $script_disc =
  322. $this->fuckhtml
  323. ->getElementsByTagName(
  324. "script"
  325. );
  326. $grep = [];
  327. foreach($script_disc as $discs){
  328. preg_match(
  329. '/const data ?= ?(\[{.*}]);/',
  330. $discs["innerHTML"],
  331. $grep
  332. );
  333. if(isset($grep[1])){
  334. break;
  335. }
  336. }
  337. if(!isset($grep[1])){
  338. throw new Exception("Could not get data JS");
  339. }
  340. $data =
  341. $this->fuckhtml
  342. ->parseJsObject(
  343. $grep[1]
  344. );
  345. unset($grep);
  346. $data = $data[1]["data"]["body"]["response"];
  347. /*
  348. Get web results
  349. */
  350. if(!isset($data["web"]["results"])){
  351. return $out;
  352. }
  353. foreach($data["web"]["results"] as $result){
  354. if(
  355. isset($result["thumbnail"]) &&
  356. is_array($result["thumbnail"])
  357. ){
  358. $thumb = [
  359. "ratio" => $result["thumbnail"]["logo"] == "false" ? "16:9" : "1:1",
  360. "url" => $result["thumbnail"]["original"]
  361. ];
  362. }else{
  363. $thumb = [
  364. "ratio" => null,
  365. "url" => null
  366. ];
  367. }
  368. // get sublinks
  369. $sublink = [];
  370. if(
  371. isset($result["cluster"]) &&
  372. is_array($result["cluster"])
  373. ){
  374. foreach($result["cluster"] as $cluster){
  375. $sublink[] = [
  376. "title" => $this->titledots($cluster["title"]),
  377. "description" =>
  378. $this->titledots(
  379. $this->fuckhtml
  380. ->getTextContent(
  381. $cluster["description"]
  382. )
  383. ),
  384. "url" => $cluster["url"],
  385. "date" => null
  386. ];
  387. }
  388. }
  389. // more sublinks
  390. if(
  391. isset($result["deep_results"]) &&
  392. is_array($result["deep_results"])
  393. ){
  394. foreach($result["deep_results"]["buttons"] as $r){
  395. $sublink[] = [
  396. "title" => $this->titledots($r["title"]),
  397. "description" => null,
  398. "url" => $r["url"],
  399. "date" => null
  400. ];
  401. }
  402. }
  403. // parse table elements
  404. $table = [];
  405. /*
  406. [locations] => void 0 Done
  407. [video] => void 0 Done
  408. [movie] => void 0 Done
  409. [faq] => void 0
  410. [recipe] => void 0
  411. [qa] => void 0 Not needed
  412. [book] => void 0
  413. [rating] => void 0
  414. [article] => void 0
  415. [product] => void 0 Done
  416. [product_cluster] => void 0
  417. [cluster_type] => void 0
  418. [cluster] => void 0 Done
  419. [creative_work] => void 0 Done
  420. [music_recording] => void 0
  421. [review] => void 0 Done
  422. [software] => void 0 Done
  423. [content_type] => void 0
  424. [descriptionLength] => 271
  425. */
  426. // product
  427. // creative_work
  428. $ref = null;
  429. if(isset($result["product"])){
  430. $ref = &$result["product"];
  431. }elseif(isset($result["creative_work"])){
  432. $ref = &$result["creative_work"];
  433. }
  434. if($ref !== null){
  435. if(isset($ref["offers"])){
  436. foreach($ref["offers"] as $offer){
  437. $price = null;
  438. if(isset($offer["price"])){
  439. if((float)$offer["price"] == 0){
  440. $price = "Free";
  441. }else{
  442. $price = $offer["price"];
  443. }
  444. }
  445. if($price !== "Free"){
  446. if(isset($offer["priceCurrency"])){
  447. $price .= " " . $offer["priceCurrency"];
  448. }
  449. }
  450. if($price !== null){
  451. $table["Price"] = trim($price);
  452. }
  453. }
  454. }
  455. if(isset($ref["rating"])){
  456. $rating = null;
  457. if(isset($ref["rating"]["ratingValue"])){
  458. $rating = $ref["rating"]["ratingValue"];
  459. if(isset($ref["rating"]["bestRating"])){
  460. $rating .= "/" . $ref["rating"]["bestRating"];
  461. }
  462. }
  463. if(isset($ref["rating"]["reviewCount"])){
  464. $isnull = $rating === null ? false : true;
  465. if($isnull){
  466. $rating .= " (";
  467. }
  468. $rating .= number_format($ref["rating"]["reviewCount"]) . " hits";
  469. if($isnull){
  470. $rating .= ")";
  471. }
  472. }
  473. if($rating !== null){
  474. $table["Rating"] = $rating;
  475. }
  476. }
  477. }
  478. // review
  479. if(
  480. isset($result["review"]) &&
  481. is_array($result["review"])
  482. ){
  483. if(isset($result["review"]["rating"]["ratingValue"])){
  484. $table["Rating"] =
  485. $result["review"]["rating"]["ratingValue"] . "/" .
  486. $result["review"]["rating"]["bestRating"];
  487. }
  488. }
  489. // software
  490. if(
  491. isset($result["software"]) &&
  492. is_array($result["software"])
  493. ){
  494. if(isset($result["software"]["author"])){
  495. $table["Author"] = $result["software"]["author"];
  496. }
  497. if(isset($result["software"]["stars"])){
  498. $table["Stars"] = number_format($result["software"]["stars"]);
  499. }
  500. if(isset($result["software"]["forks"])){
  501. $table["Forks"] = number_format($result["software"]["forks"]);
  502. }
  503. if(
  504. isset($result["software"]["programmingLanguage"]) &&
  505. $result["software"]["programmingLanguage"] != ""
  506. ){
  507. $table["Programming languages"] = $result["software"]["programmingLanguage"];
  508. }
  509. }
  510. // location
  511. if(
  512. isset($result["location"]) &&
  513. is_array($result["location"])
  514. ){
  515. if(isset($result["location"]["postal_address"]["displayAddress"])){
  516. $table["Address"] = $result["location"]["postal_address"]["displayAddress"];
  517. }
  518. if(isset($result["location"]["rating"])){
  519. $table["Rating"] =
  520. $result["location"]["rating"]["ratingValue"] . "/" .
  521. $result["location"]["rating"]["bestRating"] . " (" .
  522. number_format($result["location"]["rating"]["reviewCount"]) . " votes)";
  523. }
  524. if(isset($result["location"]["contact"]["telephone"])){
  525. $table["Phone number"] =
  526. $result["location"]["contact"]["telephone"];
  527. }
  528. if(isset($result["location"]["price_range"])){
  529. $table["Price"] =
  530. $result["location"]["price_range"];
  531. }
  532. }
  533. // video
  534. if(
  535. isset($result["video"]) &&
  536. is_array($result["video"])
  537. ){
  538. foreach($result["video"] as $key => $value){
  539. if(is_string($result["video"][$key]) === false){
  540. continue;
  541. }
  542. $table[ucfirst($key)] = $value;
  543. }
  544. }
  545. // movie
  546. if(
  547. isset($result["video"]) &&
  548. is_array($result["movie"])
  549. ){
  550. if(isset($result["movie"]["release"])){
  551. $table["Release date"] = $result["movie"]["release"];
  552. }
  553. if(isset($result["movie"]["directors"])){
  554. $directors = [];
  555. foreach($result["movie"]["directors"] as $director){
  556. $directors[] = $director["name"];
  557. }
  558. if(count($directors) !== 0){
  559. $table["Directors"] = implode(", ", $directors);
  560. }
  561. }
  562. if(isset($result["movie"]["actors"])){
  563. $actors = [];
  564. foreach($result["movie"]["actors"] as $actor){
  565. $actors[] = $actor["name"];
  566. }
  567. if(count($actors) !== 0){
  568. $table["Actors"] = implode(", ", $actors);
  569. }
  570. }
  571. if(isset($result["movie"]["rating"])){
  572. $table["Rating"] =
  573. $result["movie"]["rating"]["ratingValue"] . "/" .
  574. $result["movie"]["rating"]["bestRating"] . " (" .
  575. number_format($result["movie"]["rating"]["reviewCount"]) . " votes)";
  576. }
  577. if(isset($result["movie"]["duration"])){
  578. $table["Duration"] =
  579. $result["movie"]["duration"];
  580. }
  581. if(isset($result["movie"]["genre"])){
  582. $genres = [];
  583. foreach($result["movie"]["genre"] as $genre){
  584. $genres[] = $genre;
  585. }
  586. if(count($genres) !== 0){
  587. $table["Genre"] = implode(", ", $genres);
  588. }
  589. }
  590. }
  591. if(
  592. isset($result["age"]) &&
  593. $result["age"] != "void 0" &&
  594. $result["age"] != ""
  595. ){
  596. $date = strtotime($result["age"]);
  597. }else{
  598. $date = null;
  599. }
  600. $out["web"][] = [
  601. "title" =>
  602. $this->titledots(
  603. $result["title"]
  604. ),
  605. "description" =>
  606. isset($result["review"]["description"]) ?
  607. $this->limitstrlen(
  608. strip_tags(
  609. $result["review"]["description"]
  610. )
  611. ) :
  612. $this->titledots(
  613. $this->fuckhtml
  614. ->getTextContent(
  615. $result["description"]
  616. )
  617. ),
  618. "url" => $result["url"],
  619. "date" => $date,
  620. "type" => "web",
  621. "thumb" => $thumb,
  622. "sublink" => $sublink,
  623. "table" => $table
  624. ];
  625. }
  626. /*
  627. Get spelling autocorrect
  628. */
  629. if(
  630. isset($data["query"]["bo_altered_diff"][0][0]) &&
  631. $data["query"]["bo_altered_diff"][0][0] == "true"
  632. ){
  633. $using = [];
  634. foreach($data["query"]["bo_altered_diff"] as $diff){
  635. $using[] = $diff[1];
  636. }
  637. $out["spelling"] = [
  638. "type" => "including",
  639. "using" => implode(" ", $using),
  640. "correction" => $get["s"]
  641. ];
  642. }
  643. /*
  644. Get wikipedia heads
  645. */
  646. if(isset($data["infobox"]["results"][0])){
  647. foreach($data["infobox"]["results"] as $info){
  648. if($info["subtype"] == "code"){
  649. $description =
  650. $this->stackoverflow_parse($info["data"]["answer"]["text"]);
  651. if(isset($info["data"]["answer"]["author"])){
  652. $description[] = [
  653. "type" => "quote",
  654. "value" => "Answer from " . $info["data"]["answer"]["author"]
  655. ];
  656. }
  657. }else{
  658. $description = [];
  659. if(
  660. isset($info["description"]) &&
  661. $info["description"] != ""
  662. ){
  663. $description[] = [
  664. "type" => "quote",
  665. "value" => $info["description"]
  666. ];
  667. }
  668. if(
  669. isset($info["long_desc"]) &&
  670. $info["long_desc"] != ""
  671. ){
  672. $description[] = [
  673. "type" => "text",
  674. "value" => $this->titledots($info["long_desc"])
  675. ];
  676. }
  677. // parse ratings
  678. if(
  679. isset($info["ratings"]) &&
  680. $info["ratings"] != "void 0" &&
  681. is_array($info["ratings"]) &&
  682. count($info["ratings"]) !== 0
  683. ){
  684. $description[] = [
  685. "type" => "title",
  686. "value" => "Ratings"
  687. ];
  688. foreach($info["ratings"] as $rating){
  689. $description[] = [
  690. "type" => "link",
  691. "url" => $rating["profile"]["url"],
  692. "value" => $rating["profile"]["name"]
  693. ];
  694. $description[] = [
  695. "type" => "text",
  696. "value" => ": " . $rating["ratingValue"] . "/" . $rating["bestRating"] . "\n"
  697. ];
  698. }
  699. }
  700. }
  701. $table = [];
  702. if(isset($info["attributes"])){
  703. foreach($info["attributes"] as $row){
  704. if(
  705. $row[1] == "null" &&
  706. count($table) !== 0
  707. ){
  708. break;
  709. }
  710. if($row[1] == "null"){
  711. continue;
  712. }
  713. $table[
  714. $this->fuckhtml->getTextContent($row[0])
  715. ] =
  716. $this->fuckhtml->getTextContent($row[1]);
  717. }
  718. }
  719. $sublink = [];
  720. if(isset($info["profiles"])){
  721. foreach($info["profiles"] as $row){
  722. $name = $this->fuckhtml->getTextContent($row["name"]);
  723. if(strtolower($name) == "steampowered"){
  724. $name = "Steam";
  725. }
  726. $sublink[
  727. $this->fuckhtml->getTextContent($name)
  728. ] =
  729. $this->fuckhtml->getTextContent($row["url"]);
  730. }
  731. }
  732. $out["answer"][] = [
  733. "title" => $this->fuckhtml->getTextContent($info["title"]),
  734. "description" => $description,
  735. "url" => $info["url"],
  736. "thumb" => isset($info["images"][0]["original"]) ? $info["images"][0]["original"] : null,
  737. "table" => $table,
  738. "sublink" => $sublink
  739. ];
  740. break; // only iterate once, we get garbage most of the time
  741. }
  742. }
  743. /*
  744. Get videos
  745. */
  746. if(isset($data["videos"]["results"])){
  747. foreach($data["videos"]["results"] as $video){
  748. $out["video"][] = [
  749. "title" => $this->titledots($video["title"]),
  750. "description" => $this->titledots($video["description"]),
  751. "date" => isset($video["age"]) && $video["age"] != "void 0" ? strtotime($video["age"]) : null,
  752. "duration" => isset($video["video"]["duration"]) && $video["video"]["duration"] != "void 0" ? $this->hms2int($video["video"]["duration"]) : null,
  753. "views" => isset($video["video"]["views"]) && $video["video"]["views"] != "void 0" ? (int)$video["video"]["views"] : null,
  754. "thumb" =>
  755. isset($video["thumbnail"]["src"]) ?
  756. [
  757. "ratio" => "16:9",
  758. "url" => $this->unshiturl($video["thumbnail"]["src"])
  759. ] :
  760. [
  761. "ratio" => null,
  762. "url" => null
  763. ],
  764. "url" => $video["url"]
  765. ];
  766. }
  767. }
  768. /*
  769. Get news
  770. */
  771. if(isset($data["news"]["results"])){
  772. foreach($data["news"]["results"] as $news){
  773. $out["news"][] = [
  774. "title" => $this->titledots($news["title"]),
  775. "description" => $this->titledots($news["description"]),
  776. "date" => isset($news["age"]) ? strtotime($news["age"]) : null,
  777. "thumb" =>
  778. isset($video["thumbnail"]["src"]) ?
  779. [
  780. "ratio" => "16:9",
  781. "url" => $this->unshiturl($video["thumbnail"]["src"])
  782. ] :
  783. [
  784. "ratio" => null,
  785. "url" => null
  786. ],
  787. "url" => $news["url"]
  788. ];
  789. }
  790. }
  791. /*
  792. Get discussions
  793. */
  794. $disc_out = [];
  795. if(isset($data["discussions"]["results"])){
  796. foreach($data["discussions"]["results"] as $disc){
  797. $table = [];
  798. if(isset($disc["data"]["num_votes"])){
  799. $table["Votes"] = number_format($disc["data"]["num_votes"]);
  800. }
  801. if(isset($disc["data"]["num_answers"])){
  802. $table["Comments"] = number_format($disc["data"]["num_answers"]);
  803. }
  804. $disc_out[] = [
  805. "title" =>
  806. $this->titledots(
  807. $disc["title"]
  808. ),
  809. "description" =>
  810. $this->limitstrlen(
  811. $this->titledots(
  812. $this->fuckhtml
  813. ->getTextContent(
  814. $disc["description"]
  815. )
  816. )
  817. ),
  818. "url" => $disc["url"],
  819. "date" => isset($disc["age"]) ? strtotime($disc["age"]) : null,
  820. "type" => "web",
  821. "thumb" => [
  822. "ratio" => null,
  823. "url" => null
  824. ],
  825. "sublink" => [],
  826. "table" => $table
  827. ];
  828. }
  829. }
  830. // append discussions at position 2
  831. array_splice($out["web"], 1, 0, $disc_out);
  832. return $out;
  833. }
  834. public function news($get){
  835. if($get["npt"]){
  836. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  837. $req = json_decode($req, true);
  838. $search = $req["q"];
  839. $country = $req["country"];
  840. $nsfw = $req["nsfw"];
  841. $offset = $req["offset"];
  842. $spellcheck = $req["spellcheck"];
  843. try{
  844. $html =
  845. $this->get(
  846. $proxy,
  847. "https://search.brave.com/news",
  848. [
  849. "q" => $search,
  850. "offset" => $offset,
  851. "spellcheck" => $spellcheck
  852. ],
  853. $nsfw,
  854. $country
  855. );
  856. }catch(Exception $error){
  857. throw new Exception("Could not fetch search page");
  858. }
  859. }else{
  860. $search = $get["s"];
  861. if(strlen($search) === 0){
  862. throw new Exception("Search term is empty!");
  863. }
  864. if(strlen($search) > 2048){
  865. throw new Exception("Search term is too long!");
  866. }
  867. $proxy = $this->backend->get_ip();
  868. $nsfw = $get["nsfw"];
  869. $country = $get["country"];
  870. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  871. /*
  872. $handle = fopen("scraper/brave-news.html", "r");
  873. $html = fread($handle, filesize("scraper/brave-news.html"));
  874. fclose($handle);*/
  875. try{
  876. $html =
  877. $this->get(
  878. $proxy,
  879. "https://search.brave.com/news",
  880. [
  881. "q" => $search,
  882. "spellcheck" => $spellcheck
  883. ],
  884. $nsfw,
  885. $country
  886. );
  887. }catch(Exception $error){
  888. throw new Exception("Could not fetch search page");
  889. }
  890. }
  891. $out = [
  892. "status" => "ok",
  893. "npt" => null,
  894. "news" => []
  895. ];
  896. // load html
  897. $this->fuckhtml->load($html);
  898. // get npt
  899. $out["npt"] =
  900. $this->generatenextpagetoken(
  901. $search,
  902. $nsfw,
  903. $country,
  904. $spellcheck,
  905. "news",
  906. $proxy
  907. );
  908. preg_match(
  909. '/const data ?= ?(\[{.*}]);/',
  910. $html,
  911. $json
  912. );
  913. if(!isset($json[1])){
  914. throw new Exception("Failed to grep javascript object");
  915. }
  916. $json = $this->fuckhtml->parseJsObject($json[1], true);
  917. if($json === null){
  918. throw new Exception("Failed to parse javascript object");
  919. }
  920. foreach(
  921. $json[1]["data"]["body"]["response"]["news"]["results"]
  922. as $news
  923. ){
  924. if(
  925. !isset($news["thumbnail"]["src"]) ||
  926. $news["thumbnail"]["src"] == "void 0"
  927. ){
  928. $thumb = [
  929. "url" => null,
  930. "ratio" => null
  931. ];
  932. }else{
  933. $thumb = [
  934. "url" => $this->unshiturl($news["thumbnail"]["src"]),
  935. "ratio" => "16:9"
  936. ];
  937. }
  938. $out["news"][] = [
  939. "title" => $news["title"],
  940. "author" => null,
  941. "description" => $news["description"],
  942. "date" => !isset($news["age"]) || $news["age"] == "void 0" || $news["age"] == "null" ? null : strtotime($news["age"]),
  943. "thumb" => $thumb,
  944. "url" => $news["url"]
  945. ];
  946. }
  947. return $out;
  948. }
  949. public function image($get){
  950. $search = $get["s"];
  951. if(strlen($search) === 0){
  952. throw new Exception("Search term is empty!");
  953. }
  954. if(strlen($search) > 2048){
  955. throw new Exception("Search term is too long!");
  956. }
  957. $country = $get["country"];
  958. $nsfw = $get["nsfw"];
  959. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  960. $out = [
  961. "status" => "ok",
  962. "npt" => null,
  963. "image" => []
  964. ];
  965. try{
  966. $html =
  967. $this->get(
  968. $this->backend->get_ip(), // no nextpage right now, pass proxy directly
  969. "https://search.brave.com/images",
  970. [
  971. "q" => $search,
  972. "spellcheck" => $spellcheck
  973. ],
  974. $nsfw,
  975. $country
  976. );
  977. }catch(Exception $error){
  978. throw new Exception("Could not fetch search page");
  979. }
  980. /*
  981. $handle = fopen("scraper/brave-image.html", "r");
  982. $html = fread($handle, filesize("scraper/brave-image.html"));
  983. fclose($handle);*/
  984. preg_match(
  985. '/const data = (\[{.*}\]);/',
  986. $html,
  987. $json
  988. );
  989. if(!isset($json[1])){
  990. throw new Exception("Failed to get data object");
  991. }
  992. $json =
  993. $this->fuckhtml
  994. ->parseJsObject(
  995. $json[1]
  996. );
  997. foreach(
  998. $json[1]
  999. ["data"]
  1000. ["body"]
  1001. ["response"]
  1002. ["results"]
  1003. as $result
  1004. ){
  1005. $out["image"][] = [
  1006. "title" => $result["title"],
  1007. "source" => [
  1008. [
  1009. "url" => $result["properties"]["url"],
  1010. "width" => null,
  1011. "height" => null
  1012. ],
  1013. [
  1014. "url" => $result["thumbnail"]["src"],
  1015. "width" => null,
  1016. "height" => null
  1017. ]
  1018. ],
  1019. "url" => $result["url"]
  1020. ];
  1021. }
  1022. return $out;
  1023. }
  1024. public function video($get){
  1025. if($get["npt"]){
  1026. [$npt, $proxy] = $this->backend->get($get["npt"], "videos");
  1027. $npt = json_decode($npt, true);
  1028. $search = $npt["q"];
  1029. $offset = $npt["offset"];
  1030. $spellcheck = $npt["spellcheck"];
  1031. $country = $npt["country"];
  1032. $nsfw = $npt["nsfw"];
  1033. try{
  1034. $html =
  1035. $this->get(
  1036. $proxy,
  1037. "https://search.brave.com/videos",
  1038. [
  1039. "q" => $search,
  1040. "offset" => $offset,
  1041. "spellcheck" => $spellcheck
  1042. ],
  1043. $nsfw,
  1044. $country
  1045. );
  1046. }catch(Exception $error){
  1047. throw new Exception("Could not fetch search page");
  1048. }
  1049. }else{
  1050. $search = $get["s"];
  1051. if(strlen($search) === 0){
  1052. throw new Exception("Search term is empty!");
  1053. }
  1054. if(strlen($search) > 2048){
  1055. throw new Exception("Search term is too long!");
  1056. }
  1057. $country = $get["country"];
  1058. $nsfw = $get["nsfw"];
  1059. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  1060. $proxy = $this->backend->get_ip();
  1061. try{
  1062. $html =
  1063. $this->get(
  1064. $proxy,
  1065. "https://search.brave.com/videos",
  1066. [
  1067. "q" => $search,
  1068. "spellcheck" => $spellcheck
  1069. ],
  1070. $nsfw,
  1071. $country
  1072. );
  1073. }catch(Exception $error){
  1074. throw new Exception("Could not fetch search page");
  1075. }
  1076. }
  1077. $this->fuckhtml->load($html);
  1078. $out = [
  1079. "status" => "ok",
  1080. "npt" => null,
  1081. "video" => [],
  1082. "author" => [],
  1083. "livestream" => [],
  1084. "playlist" => [],
  1085. "reel" => []
  1086. ];
  1087. // get npt
  1088. $out["npt"] =
  1089. $this->generatenextpagetoken(
  1090. $search,
  1091. $nsfw,
  1092. $country,
  1093. $spellcheck,
  1094. "videos",
  1095. $proxy
  1096. );
  1097. /*
  1098. $handle = fopen("scraper/brave-video.html", "r");
  1099. $html = fread($handle, filesize("scraper/brave-video.html"));
  1100. fclose($handle);*/
  1101. preg_match(
  1102. '/const data = (\[{.*}\]);/',
  1103. $html,
  1104. $json
  1105. );
  1106. if(!isset($json[1])){
  1107. throw new Exception("Failed to get data object");
  1108. }
  1109. $json =
  1110. $this->fuckhtml
  1111. ->parseJsObject(
  1112. $json[1]
  1113. );
  1114. foreach(
  1115. $json
  1116. [1]
  1117. ["data"]
  1118. ["body"]
  1119. ["response"]
  1120. ["results"]
  1121. as $result
  1122. ){
  1123. if($result["video"]["author"] != "null"){
  1124. $author = [
  1125. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1126. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1127. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1128. ];
  1129. }else{
  1130. $author = [
  1131. "name" => null,
  1132. "url" => null,
  1133. "avatar" => null
  1134. ];
  1135. }
  1136. if($result["thumbnail"] != "null"){
  1137. $thumb = [
  1138. "url" => $result["thumbnail"]["original"],
  1139. "ratio" => "16:9"
  1140. ];
  1141. }else{
  1142. $thumb = [
  1143. "url" => null,
  1144. "ratio" => null
  1145. ];
  1146. }
  1147. $out["video"][] = [
  1148. "title" => $result["title"],
  1149. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1150. "author" => $author,
  1151. "date" => ($result["age"] == "null" || $result["age"] == "void 0") ? null : strtotime($result["age"]),
  1152. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1153. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1154. "thumb" => $thumb,
  1155. "url" => $result["url"]
  1156. ];
  1157. }
  1158. return $out;
  1159. }
  1160. private function stackoverflow_parse($html){
  1161. $i = 0;
  1162. $answer = [];
  1163. $this->fuckhtml->load($html);
  1164. foreach(
  1165. $this->fuckhtml->getElementsByTagName("*")
  1166. as $snippet
  1167. ){
  1168. switch($snippet["tagName"]){
  1169. case "p":
  1170. $this->fuckhtml->load($snippet["innerHTML"]);
  1171. $codetags =
  1172. $this->fuckhtml
  1173. ->getElementsByTagName("*");
  1174. $tmphtml = $snippet["innerHTML"];
  1175. foreach($codetags as $tag){
  1176. if(!isset($tag["outerHTML"])){
  1177. continue;
  1178. }
  1179. $tmphtml =
  1180. explode(
  1181. $tag["outerHTML"],
  1182. $tmphtml,
  1183. 2
  1184. );
  1185. $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
  1186. $this->appendtext($value, $answer, $i);
  1187. $type = null;
  1188. switch($tag["tagName"]){
  1189. case "code": $type = "inline_code"; break;
  1190. case "em": $type = "italic"; break;
  1191. case "blockquote": $type = "quote"; break;
  1192. default: $type = "text";
  1193. }
  1194. if($type !== null){
  1195. $value = $this->fuckhtml->getTextContent($tag, false, true);
  1196. if(trim($value) != ""){
  1197. if(
  1198. $i !== 0 &&
  1199. $type == "title"
  1200. ){
  1201. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1202. }
  1203. $answer[] = [
  1204. "type" => $type,
  1205. "value" => $value
  1206. ];
  1207. $i++;
  1208. }
  1209. }
  1210. if(count($tmphtml) === 2){
  1211. $tmphtml = $tmphtml[1];
  1212. }else{
  1213. break;
  1214. }
  1215. }
  1216. if(is_array($tmphtml)){
  1217. $tmphtml = $tmphtml[0];
  1218. }
  1219. if(strlen($tmphtml) !== 0){
  1220. $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
  1221. $this->appendtext($value, $answer, $i);
  1222. }
  1223. break;
  1224. case "img":
  1225. $answer[] = [
  1226. "type" => "image",
  1227. "url" =>
  1228. $this->fuckhtml
  1229. ->getTextContent(
  1230. $tag["attributes"]["src"]
  1231. )
  1232. ];
  1233. $i++;
  1234. break;
  1235. case "pre":
  1236. switch($answer[$i - 1]["type"]){
  1237. case "text":
  1238. case "italic":
  1239. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1240. break;
  1241. }
  1242. $answer[] =
  1243. [
  1244. "type" => "code",
  1245. "value" =>
  1246. rtrim(
  1247. $this->fuckhtml
  1248. ->getTextContent(
  1249. $snippet,
  1250. true,
  1251. false
  1252. )
  1253. )
  1254. ];
  1255. $i++;
  1256. break;
  1257. case "ol":
  1258. $o = 0;
  1259. $this->fuckhtml->load($snippet);
  1260. $li =
  1261. $this->fuckhtml
  1262. ->getElementsByTagName("li");
  1263. foreach($li as $elem){
  1264. $o++;
  1265. $this->appendtext(
  1266. $o . ". " .
  1267. $this->fuckhtml
  1268. ->getTextContent(
  1269. $elem
  1270. ),
  1271. $answer,
  1272. $i
  1273. );
  1274. }
  1275. break;
  1276. }
  1277. }
  1278. if(
  1279. $i !== 0 &&
  1280. $answer[$i - 1]["type"] == "text"
  1281. ){
  1282. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1283. }
  1284. return $answer;
  1285. }
  1286. private function hms2int($time){
  1287. $parts = explode(":", $time, 3);
  1288. $time = 0;
  1289. if(count($parts) === 3){
  1290. // hours
  1291. $time = $time + ((int)$parts[0] * 3600);
  1292. array_shift($parts);
  1293. }
  1294. if(count($parts) === 2){
  1295. // minutes
  1296. $time = $time + ((int)$parts[0] * 60);
  1297. array_shift($parts);
  1298. }
  1299. // seconds
  1300. $time = $time + (int)$parts[0];
  1301. return $time;
  1302. }
  1303. private function appendtext($payload, &$text, &$index){
  1304. if(trim($payload) == ""){
  1305. return;
  1306. }
  1307. if(
  1308. $index !== 0 &&
  1309. $text[$index - 1]["type"] == "text"
  1310. ){
  1311. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1312. }else{
  1313. $text[] = [
  1314. "type" => "text",
  1315. "value" => preg_replace('/ $/', " ", $payload)
  1316. ];
  1317. $index++;
  1318. }
  1319. }
  1320. private function tablesublink($html_collection, &$data){
  1321. foreach($html_collection as $html){
  1322. $html["innerHTML"] = preg_replace(
  1323. '/<style>[\S\s]*<\/style>/i',
  1324. "",
  1325. $html["innerHTML"]
  1326. );
  1327. $html =
  1328. explode(
  1329. ":",
  1330. $this->fuckhtml->getTextContent($html),
  1331. 2
  1332. );
  1333. if(count($html) === 1){
  1334. $html = ["Rating", $html[0]];
  1335. }
  1336. $data["table"][trim($html[0])] = trim($html[1]);
  1337. }
  1338. }
  1339. /*
  1340. private function getimagelinkfromstyle($thumb){
  1341. $thumb =
  1342. $this->fuckhtml
  1343. ->getElementsByClassName(
  1344. $thumb,
  1345. "div"
  1346. );
  1347. if(count($thumb) === 0){
  1348. return [
  1349. "url" => null,
  1350. "ratio" => null
  1351. ];
  1352. }
  1353. $thumb = $thumb[0]["attributes"]["style"];
  1354. preg_match(
  1355. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1356. $thumb,
  1357. $thumb
  1358. );
  1359. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1360. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1361. return [
  1362. "url" => null,
  1363. "ratio" => null
  1364. ];
  1365. }
  1366. return [
  1367. "url" => $url,
  1368. "ratio" => "16:9"
  1369. ];
  1370. }*/
  1371. private function limitstrlen($text){
  1372. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1373. }
  1374. /*
  1375. private function limitwhitespace($text){
  1376. return
  1377. preg_replace(
  1378. '/[\s]+/',
  1379. " ",
  1380. $text
  1381. );
  1382. }*/
  1383. private function titledots($title){
  1384. $substr = substr($title, -3);
  1385. if(
  1386. $substr == "..." ||
  1387. $substr == "…"
  1388. ){
  1389. return trim(substr($title, 0, -3));
  1390. }
  1391. return trim($title);
  1392. }
  1393. private function generatenextpagetoken($q, $nsfw, $country, $spellcheck, $page, $proxy){
  1394. $nextpage =
  1395. $this->fuckhtml
  1396. ->getElementsByClassName("btn", "a");
  1397. if(count($nextpage) !== 0){
  1398. $nextpage =
  1399. $nextpage[count($nextpage) - 1];
  1400. if(
  1401. strtolower(
  1402. $this->fuckhtml
  1403. ->getTextContent(
  1404. $nextpage
  1405. )
  1406. ) == "next"
  1407. ){
  1408. preg_match(
  1409. '/offset=([0-9]+)/',
  1410. $this->fuckhtml->getTextContent($nextpage["attributes"]["href"]),
  1411. $nextpage
  1412. );
  1413. return
  1414. $this->backend->store(
  1415. json_encode(
  1416. [
  1417. "q" => $q,
  1418. "offset" => (int)$nextpage[1],
  1419. "nsfw" => $nsfw,
  1420. "country" => $country,
  1421. "spellcheck" => $spellcheck
  1422. ]
  1423. ),
  1424. $page,
  1425. $proxy
  1426. );
  1427. }
  1428. }
  1429. return null;
  1430. }
  1431. private function unshiturl($url){
  1432. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1433. $tmp = explode("aHR0", $url);
  1434. if(count($tmp) !== 2){
  1435. // nothing to do
  1436. return $url;
  1437. }
  1438. return
  1439. base64_decode(
  1440. "aHR0" .
  1441. str_replace(["/", "_"], ["", "/"],
  1442. explode(
  1443. ".",
  1444. $tmp[1]
  1445. )[0]
  1446. )
  1447. );
  1448. }
  1449. }