brave.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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 grep JavaScript object");
  339. }
  340. $data =
  341. $this->fuckhtml
  342. ->parseJsObject(
  343. $grep[1]
  344. );
  345. unset($grep);
  346. if($data === null){
  347. throw new Exception("Failed to decode JavaScript object");
  348. }
  349. $data = $data[1]["data"]["body"]["response"];
  350. /*
  351. Get web results
  352. */
  353. if(!isset($data["web"]["results"])){
  354. return $out;
  355. }
  356. foreach($data["web"]["results"] as $result){
  357. if(
  358. isset($result["thumbnail"]) &&
  359. is_array($result["thumbnail"])
  360. ){
  361. $thumb = [
  362. "ratio" => $result["thumbnail"]["logo"] == "false" ? "16:9" : "1:1",
  363. "url" => $result["thumbnail"]["original"]
  364. ];
  365. }else{
  366. $thumb = [
  367. "ratio" => null,
  368. "url" => null
  369. ];
  370. }
  371. // get sublinks
  372. $sublink = [];
  373. if(
  374. isset($result["cluster"]) &&
  375. is_array($result["cluster"])
  376. ){
  377. foreach($result["cluster"] as $cluster){
  378. $sublink[] = [
  379. "title" => $this->titledots($cluster["title"]),
  380. "description" =>
  381. $this->titledots(
  382. $this->fuckhtml
  383. ->getTextContent(
  384. $cluster["description"]
  385. )
  386. ),
  387. "url" => $cluster["url"],
  388. "date" => null
  389. ];
  390. }
  391. }
  392. // more sublinks
  393. if(
  394. isset($result["deep_results"]) &&
  395. is_array($result["deep_results"])
  396. ){
  397. foreach($result["deep_results"]["buttons"] as $r){
  398. $sublink[] = [
  399. "title" => $this->titledots($r["title"]),
  400. "description" => null,
  401. "url" => $r["url"],
  402. "date" => null
  403. ];
  404. }
  405. }
  406. // parse table elements
  407. $table = [];
  408. /*
  409. [locations] => void 0 Done
  410. [video] => void 0 Done
  411. [movie] => void 0 Done
  412. [faq] => void 0
  413. [recipe] => void 0
  414. [qa] => void 0 Not needed
  415. [book] => void 0
  416. [rating] => void 0
  417. [article] => void 0
  418. [product] => void 0 Done
  419. [product_cluster] => void 0
  420. [cluster_type] => void 0
  421. [cluster] => void 0 Done
  422. [creative_work] => void 0 Done
  423. [music_recording] => void 0
  424. [review] => void 0 Done
  425. [software] => void 0 Done
  426. [content_type] => void 0
  427. [descriptionLength] => 271
  428. */
  429. // product
  430. // creative_work
  431. $ref = null;
  432. if(isset($result["product"])){
  433. $ref = &$result["product"];
  434. }elseif(isset($result["creative_work"])){
  435. $ref = &$result["creative_work"];
  436. }
  437. if($ref !== null){
  438. if(isset($ref["offers"])){
  439. foreach($ref["offers"] as $offer){
  440. $price = null;
  441. if(isset($offer["price"])){
  442. if((float)$offer["price"] == 0){
  443. $price = "Free";
  444. }else{
  445. $price = $offer["price"];
  446. }
  447. }
  448. if($price !== "Free"){
  449. if(isset($offer["priceCurrency"])){
  450. $price .= " " . $offer["priceCurrency"];
  451. }
  452. }
  453. if($price !== null){
  454. $table["Price"] = trim($price);
  455. }
  456. }
  457. }
  458. if(isset($ref["rating"])){
  459. $rating = null;
  460. if(isset($ref["rating"]["ratingValue"])){
  461. $rating = $ref["rating"]["ratingValue"];
  462. if(isset($ref["rating"]["bestRating"])){
  463. $rating .= "/" . $ref["rating"]["bestRating"];
  464. }
  465. }
  466. if(isset($ref["rating"]["reviewCount"])){
  467. $isnull = $rating === null ? false : true;
  468. if($isnull){
  469. $rating .= " (";
  470. }
  471. $rating .= number_format($ref["rating"]["reviewCount"]) . " hits";
  472. if($isnull){
  473. $rating .= ")";
  474. }
  475. }
  476. if($rating !== null){
  477. $table["Rating"] = $rating;
  478. }
  479. }
  480. }
  481. // review
  482. if(
  483. isset($result["review"]) &&
  484. is_array($result["review"])
  485. ){
  486. if(isset($result["review"]["rating"]["ratingValue"])){
  487. $table["Rating"] =
  488. $result["review"]["rating"]["ratingValue"] . "/" .
  489. $result["review"]["rating"]["bestRating"];
  490. }
  491. }
  492. // software
  493. if(
  494. isset($result["software"]) &&
  495. is_array($result["software"])
  496. ){
  497. if(isset($result["software"]["author"])){
  498. $table["Author"] = $result["software"]["author"];
  499. }
  500. if(isset($result["software"]["stars"])){
  501. $table["Stars"] = number_format($result["software"]["stars"]);
  502. }
  503. if(isset($result["software"]["forks"])){
  504. $table["Forks"] = number_format($result["software"]["forks"]);
  505. }
  506. if(
  507. isset($result["software"]["programmingLanguage"]) &&
  508. $result["software"]["programmingLanguage"] != ""
  509. ){
  510. $table["Programming languages"] = $result["software"]["programmingLanguage"];
  511. }
  512. }
  513. // location
  514. if(
  515. isset($result["location"]) &&
  516. is_array($result["location"])
  517. ){
  518. if(isset($result["location"]["postal_address"]["displayAddress"])){
  519. $table["Address"] = $result["location"]["postal_address"]["displayAddress"];
  520. }
  521. if(isset($result["location"]["rating"])){
  522. $table["Rating"] =
  523. $result["location"]["rating"]["ratingValue"] . "/" .
  524. $result["location"]["rating"]["bestRating"] . " (" .
  525. number_format($result["location"]["rating"]["reviewCount"]) . " votes)";
  526. }
  527. if(isset($result["location"]["contact"]["telephone"])){
  528. $table["Phone number"] =
  529. $result["location"]["contact"]["telephone"];
  530. }
  531. if(isset($result["location"]["price_range"])){
  532. $table["Price"] =
  533. $result["location"]["price_range"];
  534. }
  535. }
  536. // video
  537. if(
  538. isset($result["video"]) &&
  539. is_array($result["video"])
  540. ){
  541. foreach($result["video"] as $key => $value){
  542. if(is_string($result["video"][$key]) === false){
  543. continue;
  544. }
  545. $table[ucfirst($key)] = $value;
  546. }
  547. }
  548. // movie
  549. if(
  550. isset($result["video"]) &&
  551. is_array($result["movie"])
  552. ){
  553. if(isset($result["movie"]["release"])){
  554. $table["Release date"] = $result["movie"]["release"];
  555. }
  556. if(isset($result["movie"]["directors"])){
  557. $directors = [];
  558. foreach($result["movie"]["directors"] as $director){
  559. $directors[] = $director["name"];
  560. }
  561. if(count($directors) !== 0){
  562. $table["Directors"] = implode(", ", $directors);
  563. }
  564. }
  565. if(isset($result["movie"]["actors"])){
  566. $actors = [];
  567. foreach($result["movie"]["actors"] as $actor){
  568. $actors[] = $actor["name"];
  569. }
  570. if(count($actors) !== 0){
  571. $table["Actors"] = implode(", ", $actors);
  572. }
  573. }
  574. if(isset($result["movie"]["rating"])){
  575. $table["Rating"] =
  576. $result["movie"]["rating"]["ratingValue"] . "/" .
  577. $result["movie"]["rating"]["bestRating"] . " (" .
  578. number_format($result["movie"]["rating"]["reviewCount"]) . " votes)";
  579. }
  580. if(isset($result["movie"]["duration"])){
  581. $table["Duration"] =
  582. $result["movie"]["duration"];
  583. }
  584. if(isset($result["movie"]["genre"])){
  585. $genres = [];
  586. foreach($result["movie"]["genre"] as $genre){
  587. $genres[] = $genre;
  588. }
  589. if(count($genres) !== 0){
  590. $table["Genre"] = implode(", ", $genres);
  591. }
  592. }
  593. }
  594. if(
  595. isset($result["age"]) &&
  596. $result["age"] != "void 0" &&
  597. $result["age"] != ""
  598. ){
  599. $date = strtotime($result["age"]);
  600. }else{
  601. $date = null;
  602. }
  603. $out["web"][] = [
  604. "title" =>
  605. $this->titledots(
  606. $result["title"]
  607. ),
  608. "description" =>
  609. isset($result["review"]["description"]) ?
  610. $this->limitstrlen(
  611. strip_tags(
  612. $result["review"]["description"]
  613. )
  614. ) :
  615. $this->titledots(
  616. $this->fuckhtml
  617. ->getTextContent(
  618. $result["description"]
  619. )
  620. ),
  621. "url" => $result["url"],
  622. "date" => $date,
  623. "type" => "web",
  624. "thumb" => $thumb,
  625. "sublink" => $sublink,
  626. "table" => $table
  627. ];
  628. }
  629. /*
  630. Get spelling autocorrect
  631. */
  632. if(
  633. isset($data["query"]["bo_altered_diff"][0][0]) &&
  634. $data["query"]["bo_altered_diff"][0][0] == "true"
  635. ){
  636. $using = [];
  637. foreach($data["query"]["bo_altered_diff"] as $diff){
  638. $using[] = $diff[1];
  639. }
  640. $out["spelling"] = [
  641. "type" => "including",
  642. "using" => implode(" ", $using),
  643. "correction" => $get["s"]
  644. ];
  645. }
  646. /*
  647. Get wikipedia heads
  648. */
  649. if(isset($data["infobox"]["results"][0])){
  650. foreach($data["infobox"]["results"] as $info){
  651. if($info["subtype"] == "code"){
  652. $description =
  653. $this->stackoverflow_parse($info["data"]["answer"]["text"]);
  654. if(isset($info["data"]["answer"]["author"])){
  655. $description[] = [
  656. "type" => "quote",
  657. "value" => "Answer from " . $info["data"]["answer"]["author"]
  658. ];
  659. }
  660. }else{
  661. $description = [];
  662. if(
  663. isset($info["description"]) &&
  664. $info["description"] != ""
  665. ){
  666. $description[] = [
  667. "type" => "quote",
  668. "value" => $info["description"]
  669. ];
  670. }
  671. if(
  672. isset($info["long_desc"]) &&
  673. $info["long_desc"] != ""
  674. ){
  675. $description[] = [
  676. "type" => "text",
  677. "value" => $this->titledots($info["long_desc"])
  678. ];
  679. }
  680. // parse ratings
  681. if(
  682. isset($info["ratings"]) &&
  683. $info["ratings"] != "void 0" &&
  684. is_array($info["ratings"]) &&
  685. count($info["ratings"]) !== 0
  686. ){
  687. $description[] = [
  688. "type" => "title",
  689. "value" => "Ratings"
  690. ];
  691. foreach($info["ratings"] as $rating){
  692. $description[] = [
  693. "type" => "link",
  694. "url" => $rating["profile"]["url"],
  695. "value" => $rating["profile"]["name"]
  696. ];
  697. $description[] = [
  698. "type" => "text",
  699. "value" => ": " . $rating["ratingValue"] . "/" . $rating["bestRating"] . "\n"
  700. ];
  701. }
  702. }
  703. }
  704. $table = [];
  705. if(isset($info["attributes"])){
  706. foreach($info["attributes"] as $row){
  707. if(
  708. $row[1] == "null" &&
  709. count($table) !== 0
  710. ){
  711. break;
  712. }
  713. if($row[1] == "null"){
  714. continue;
  715. }
  716. $table[
  717. $this->fuckhtml->getTextContent($row[0])
  718. ] =
  719. $this->fuckhtml->getTextContent($row[1]);
  720. }
  721. }
  722. $sublink = [];
  723. if(isset($info["profiles"])){
  724. foreach($info["profiles"] as $row){
  725. $name = $this->fuckhtml->getTextContent($row["name"]);
  726. if(strtolower($name) == "steampowered"){
  727. $name = "Steam";
  728. }
  729. $sublink[
  730. $this->fuckhtml->getTextContent($name)
  731. ] =
  732. $this->fuckhtml->getTextContent($row["url"]);
  733. }
  734. }
  735. $out["answer"][] = [
  736. "title" => $this->fuckhtml->getTextContent($info["title"]),
  737. "description" => $description,
  738. "url" => $info["url"],
  739. "thumb" => isset($info["images"][0]["original"]) ? $info["images"][0]["original"] : null,
  740. "table" => $table,
  741. "sublink" => $sublink
  742. ];
  743. break; // only iterate once, we get garbage most of the time
  744. }
  745. }
  746. /*
  747. Get videos
  748. */
  749. if(isset($data["videos"]["results"])){
  750. foreach($data["videos"]["results"] as $video){
  751. $out["video"][] = [
  752. "title" => $this->titledots($video["title"]),
  753. "description" => $this->titledots($video["description"]),
  754. "date" => isset($video["age"]) && $video["age"] != "void 0" ? strtotime($video["age"]) : null,
  755. "duration" => isset($video["video"]["duration"]) && $video["video"]["duration"] != "void 0" ? $this->hms2int($video["video"]["duration"]) : null,
  756. "views" => isset($video["video"]["views"]) && $video["video"]["views"] != "void 0" ? (int)$video["video"]["views"] : null,
  757. "thumb" =>
  758. isset($video["thumbnail"]["src"]) ?
  759. [
  760. "ratio" => "16:9",
  761. "url" => $this->unshiturl($video["thumbnail"]["src"])
  762. ] :
  763. [
  764. "ratio" => null,
  765. "url" => null
  766. ],
  767. "url" => $video["url"]
  768. ];
  769. }
  770. }
  771. /*
  772. Get news
  773. */
  774. if(isset($data["news"]["results"])){
  775. foreach($data["news"]["results"] as $news){
  776. $out["news"][] = [
  777. "title" => $this->titledots($news["title"]),
  778. "description" => $this->titledots($news["description"]),
  779. "date" => isset($news["age"]) ? strtotime($news["age"]) : null,
  780. "thumb" =>
  781. isset($video["thumbnail"]["src"]) ?
  782. [
  783. "ratio" => "16:9",
  784. "url" => $this->unshiturl($video["thumbnail"]["src"])
  785. ] :
  786. [
  787. "ratio" => null,
  788. "url" => null
  789. ],
  790. "url" => $news["url"]
  791. ];
  792. }
  793. }
  794. /*
  795. Get discussions
  796. */
  797. $disc_out = [];
  798. if(isset($data["discussions"]["results"])){
  799. foreach($data["discussions"]["results"] as $disc){
  800. $table = [];
  801. if(isset($disc["data"]["num_votes"])){
  802. $table["Votes"] = number_format($disc["data"]["num_votes"]);
  803. }
  804. if(isset($disc["data"]["num_answers"])){
  805. $table["Comments"] = number_format($disc["data"]["num_answers"]);
  806. }
  807. $disc_out[] = [
  808. "title" =>
  809. $this->titledots(
  810. $disc["title"]
  811. ),
  812. "description" =>
  813. $this->limitstrlen(
  814. $this->titledots(
  815. $this->fuckhtml
  816. ->getTextContent(
  817. $disc["description"]
  818. )
  819. )
  820. ),
  821. "url" => $disc["url"],
  822. "date" => isset($disc["age"]) ? strtotime($disc["age"]) : null,
  823. "type" => "web",
  824. "thumb" => [
  825. "ratio" => null,
  826. "url" => null
  827. ],
  828. "sublink" => [],
  829. "table" => $table
  830. ];
  831. }
  832. }
  833. // append discussions at position 2
  834. array_splice($out["web"], 1, 0, $disc_out);
  835. return $out;
  836. }
  837. public function news($get){
  838. if($get["npt"]){
  839. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  840. $req = json_decode($req, true);
  841. $search = $req["q"];
  842. $country = $req["country"];
  843. $nsfw = $req["nsfw"];
  844. $offset = $req["offset"];
  845. $spellcheck = $req["spellcheck"];
  846. try{
  847. $html =
  848. $this->get(
  849. $proxy,
  850. "https://search.brave.com/news",
  851. [
  852. "q" => $search,
  853. "offset" => $offset,
  854. "spellcheck" => $spellcheck
  855. ],
  856. $nsfw,
  857. $country
  858. );
  859. }catch(Exception $error){
  860. throw new Exception("Could not fetch search page");
  861. }
  862. }else{
  863. $search = $get["s"];
  864. if(strlen($search) === 0){
  865. throw new Exception("Search term is empty!");
  866. }
  867. if(strlen($search) > 2048){
  868. throw new Exception("Search term is too long!");
  869. }
  870. $proxy = $this->backend->get_ip();
  871. $nsfw = $get["nsfw"];
  872. $country = $get["country"];
  873. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  874. /*
  875. $handle = fopen("scraper/brave-news.html", "r");
  876. $html = fread($handle, filesize("scraper/brave-news.html"));
  877. fclose($handle);*/
  878. try{
  879. $html =
  880. $this->get(
  881. $proxy,
  882. "https://search.brave.com/news",
  883. [
  884. "q" => $search,
  885. "spellcheck" => $spellcheck
  886. ],
  887. $nsfw,
  888. $country
  889. );
  890. }catch(Exception $error){
  891. throw new Exception("Could not fetch search page");
  892. }
  893. }
  894. $out = [
  895. "status" => "ok",
  896. "npt" => null,
  897. "news" => []
  898. ];
  899. // load html
  900. $this->fuckhtml->load($html);
  901. // get npt
  902. $out["npt"] =
  903. $this->generatenextpagetoken(
  904. $search,
  905. $nsfw,
  906. $country,
  907. $spellcheck,
  908. "news",
  909. $proxy
  910. );
  911. preg_match(
  912. '/const data ?= ?(\[{.*}]);/',
  913. $html,
  914. $json
  915. );
  916. if(!isset($json[1])){
  917. throw new Exception("Failed to grep javascript object");
  918. }
  919. $json = $this->fuckhtml->parseJsObject($json[1], true);
  920. if($json === null){
  921. throw new Exception("Failed to parse javascript object");
  922. }
  923. foreach(
  924. $json[1]["data"]["body"]["response"]["news"]["results"]
  925. as $news
  926. ){
  927. if(
  928. !isset($news["thumbnail"]["src"]) ||
  929. $news["thumbnail"]["src"] == "void 0"
  930. ){
  931. $thumb = [
  932. "url" => null,
  933. "ratio" => null
  934. ];
  935. }else{
  936. $thumb = [
  937. "url" => $this->unshiturl($news["thumbnail"]["src"]),
  938. "ratio" => "16:9"
  939. ];
  940. }
  941. $out["news"][] = [
  942. "title" => $news["title"],
  943. "author" => null,
  944. "description" => $news["description"],
  945. "date" => !isset($news["age"]) || $news["age"] == "void 0" || $news["age"] == "null" ? null : strtotime($news["age"]),
  946. "thumb" => $thumb,
  947. "url" => $news["url"]
  948. ];
  949. }
  950. return $out;
  951. }
  952. public function image($get){
  953. $search = $get["s"];
  954. if(strlen($search) === 0){
  955. throw new Exception("Search term is empty!");
  956. }
  957. if(strlen($search) > 2048){
  958. throw new Exception("Search term is too long!");
  959. }
  960. $country = $get["country"];
  961. $nsfw = $get["nsfw"];
  962. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  963. $out = [
  964. "status" => "ok",
  965. "npt" => null,
  966. "image" => []
  967. ];
  968. try{
  969. $html =
  970. $this->get(
  971. $this->backend->get_ip(), // no nextpage right now, pass proxy directly
  972. "https://search.brave.com/images",
  973. [
  974. "q" => $search,
  975. "spellcheck" => $spellcheck
  976. ],
  977. $nsfw,
  978. $country
  979. );
  980. }catch(Exception $error){
  981. throw new Exception("Could not fetch search page");
  982. }
  983. /*
  984. $handle = fopen("scraper/brave-image.html", "r");
  985. $html = fread($handle, filesize("scraper/brave-image.html"));
  986. fclose($handle);*/
  987. preg_match(
  988. '/const data = (\[{.*}\]);/',
  989. $html,
  990. $json
  991. );
  992. if(!isset($json[1])){
  993. throw new Exception("Failed to get data object");
  994. }
  995. $json =
  996. $this->fuckhtml
  997. ->parseJsObject(
  998. $json[1]
  999. );
  1000. foreach(
  1001. $json[1]
  1002. ["data"]
  1003. ["body"]
  1004. ["response"]
  1005. ["results"]
  1006. as $result
  1007. ){
  1008. $out["image"][] = [
  1009. "title" => $result["title"],
  1010. "source" => [
  1011. [
  1012. "url" => $result["properties"]["url"],
  1013. "width" => null,
  1014. "height" => null
  1015. ],
  1016. [
  1017. "url" => $result["thumbnail"]["src"],
  1018. "width" => null,
  1019. "height" => null
  1020. ]
  1021. ],
  1022. "url" => $result["url"]
  1023. ];
  1024. }
  1025. return $out;
  1026. }
  1027. public function video($get){
  1028. if($get["npt"]){
  1029. [$npt, $proxy] = $this->backend->get($get["npt"], "videos");
  1030. $npt = json_decode($npt, true);
  1031. $search = $npt["q"];
  1032. $offset = $npt["offset"];
  1033. $spellcheck = $npt["spellcheck"];
  1034. $country = $npt["country"];
  1035. $nsfw = $npt["nsfw"];
  1036. try{
  1037. $html =
  1038. $this->get(
  1039. $proxy,
  1040. "https://search.brave.com/videos",
  1041. [
  1042. "q" => $search,
  1043. "offset" => $offset,
  1044. "spellcheck" => $spellcheck
  1045. ],
  1046. $nsfw,
  1047. $country
  1048. );
  1049. }catch(Exception $error){
  1050. throw new Exception("Could not fetch search page");
  1051. }
  1052. }else{
  1053. $search = $get["s"];
  1054. if(strlen($search) === 0){
  1055. throw new Exception("Search term is empty!");
  1056. }
  1057. if(strlen($search) > 2048){
  1058. throw new Exception("Search term is too long!");
  1059. }
  1060. $country = $get["country"];
  1061. $nsfw = $get["nsfw"];
  1062. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  1063. $proxy = $this->backend->get_ip();
  1064. try{
  1065. $html =
  1066. $this->get(
  1067. $proxy,
  1068. "https://search.brave.com/videos",
  1069. [
  1070. "q" => $search,
  1071. "spellcheck" => $spellcheck
  1072. ],
  1073. $nsfw,
  1074. $country
  1075. );
  1076. }catch(Exception $error){
  1077. throw new Exception("Could not fetch search page");
  1078. }
  1079. }
  1080. $this->fuckhtml->load($html);
  1081. $out = [
  1082. "status" => "ok",
  1083. "npt" => null,
  1084. "video" => [],
  1085. "author" => [],
  1086. "livestream" => [],
  1087. "playlist" => [],
  1088. "reel" => []
  1089. ];
  1090. // get npt
  1091. $out["npt"] =
  1092. $this->generatenextpagetoken(
  1093. $search,
  1094. $nsfw,
  1095. $country,
  1096. $spellcheck,
  1097. "videos",
  1098. $proxy
  1099. );
  1100. /*
  1101. $handle = fopen("scraper/brave-video.html", "r");
  1102. $html = fread($handle, filesize("scraper/brave-video.html"));
  1103. fclose($handle);*/
  1104. preg_match(
  1105. '/const data = (\[{.*}\]);/',
  1106. $html,
  1107. $json
  1108. );
  1109. if(!isset($json[1])){
  1110. throw new Exception("Failed to get data object");
  1111. }
  1112. $json =
  1113. $this->fuckhtml
  1114. ->parseJsObject(
  1115. $json[1]
  1116. );
  1117. foreach(
  1118. $json
  1119. [1]
  1120. ["data"]
  1121. ["body"]
  1122. ["response"]
  1123. ["results"]
  1124. as $result
  1125. ){
  1126. if($result["video"]["author"] != "null"){
  1127. $author = [
  1128. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1129. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1130. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1131. ];
  1132. }else{
  1133. $author = [
  1134. "name" => null,
  1135. "url" => null,
  1136. "avatar" => null
  1137. ];
  1138. }
  1139. if($result["thumbnail"] != "null"){
  1140. $thumb = [
  1141. "url" => $result["thumbnail"]["original"],
  1142. "ratio" => "16:9"
  1143. ];
  1144. }else{
  1145. $thumb = [
  1146. "url" => null,
  1147. "ratio" => null
  1148. ];
  1149. }
  1150. $out["video"][] = [
  1151. "title" => $result["title"],
  1152. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1153. "author" => $author,
  1154. "date" => ($result["age"] == "null" || $result["age"] == "void 0") ? null : strtotime($result["age"]),
  1155. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1156. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1157. "thumb" => $thumb,
  1158. "url" => $result["url"]
  1159. ];
  1160. }
  1161. return $out;
  1162. }
  1163. private function stackoverflow_parse($html){
  1164. $i = 0;
  1165. $answer = [];
  1166. $this->fuckhtml->load($html);
  1167. foreach(
  1168. $this->fuckhtml->getElementsByTagName("*")
  1169. as $snippet
  1170. ){
  1171. switch($snippet["tagName"]){
  1172. case "p":
  1173. $this->fuckhtml->load($snippet["innerHTML"]);
  1174. $codetags =
  1175. $this->fuckhtml
  1176. ->getElementsByTagName("*");
  1177. $tmphtml = $snippet["innerHTML"];
  1178. foreach($codetags as $tag){
  1179. if(!isset($tag["outerHTML"])){
  1180. continue;
  1181. }
  1182. $tmphtml =
  1183. explode(
  1184. $tag["outerHTML"],
  1185. $tmphtml,
  1186. 2
  1187. );
  1188. $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
  1189. $this->appendtext($value, $answer, $i);
  1190. $type = null;
  1191. switch($tag["tagName"]){
  1192. case "code": $type = "inline_code"; break;
  1193. case "em": $type = "italic"; break;
  1194. case "blockquote": $type = "quote"; break;
  1195. default: $type = "text";
  1196. }
  1197. if($type !== null){
  1198. $value = $this->fuckhtml->getTextContent($tag, false, true);
  1199. if(trim($value) != ""){
  1200. if(
  1201. $i !== 0 &&
  1202. $type == "title"
  1203. ){
  1204. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1205. }
  1206. $answer[] = [
  1207. "type" => $type,
  1208. "value" => $value
  1209. ];
  1210. $i++;
  1211. }
  1212. }
  1213. if(count($tmphtml) === 2){
  1214. $tmphtml = $tmphtml[1];
  1215. }else{
  1216. break;
  1217. }
  1218. }
  1219. if(is_array($tmphtml)){
  1220. $tmphtml = $tmphtml[0];
  1221. }
  1222. if(strlen($tmphtml) !== 0){
  1223. $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
  1224. $this->appendtext($value, $answer, $i);
  1225. }
  1226. break;
  1227. case "img":
  1228. $answer[] = [
  1229. "type" => "image",
  1230. "url" =>
  1231. $this->fuckhtml
  1232. ->getTextContent(
  1233. $tag["attributes"]["src"]
  1234. )
  1235. ];
  1236. $i++;
  1237. break;
  1238. case "pre":
  1239. switch($answer[$i - 1]["type"]){
  1240. case "text":
  1241. case "italic":
  1242. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1243. break;
  1244. }
  1245. $answer[] =
  1246. [
  1247. "type" => "code",
  1248. "value" =>
  1249. rtrim(
  1250. $this->fuckhtml
  1251. ->getTextContent(
  1252. $snippet,
  1253. true,
  1254. false
  1255. )
  1256. )
  1257. ];
  1258. $i++;
  1259. break;
  1260. case "ol":
  1261. $o = 0;
  1262. $this->fuckhtml->load($snippet);
  1263. $li =
  1264. $this->fuckhtml
  1265. ->getElementsByTagName("li");
  1266. foreach($li as $elem){
  1267. $o++;
  1268. $this->appendtext(
  1269. $o . ". " .
  1270. $this->fuckhtml
  1271. ->getTextContent(
  1272. $elem
  1273. ),
  1274. $answer,
  1275. $i
  1276. );
  1277. }
  1278. break;
  1279. }
  1280. }
  1281. if(
  1282. $i !== 0 &&
  1283. $answer[$i - 1]["type"] == "text"
  1284. ){
  1285. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1286. }
  1287. return $answer;
  1288. }
  1289. private function hms2int($time){
  1290. $parts = explode(":", $time, 3);
  1291. $time = 0;
  1292. if(count($parts) === 3){
  1293. // hours
  1294. $time = $time + ((int)$parts[0] * 3600);
  1295. array_shift($parts);
  1296. }
  1297. if(count($parts) === 2){
  1298. // minutes
  1299. $time = $time + ((int)$parts[0] * 60);
  1300. array_shift($parts);
  1301. }
  1302. // seconds
  1303. $time = $time + (int)$parts[0];
  1304. return $time;
  1305. }
  1306. private function appendtext($payload, &$text, &$index){
  1307. if(trim($payload) == ""){
  1308. return;
  1309. }
  1310. if(
  1311. $index !== 0 &&
  1312. $text[$index - 1]["type"] == "text"
  1313. ){
  1314. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1315. }else{
  1316. $text[] = [
  1317. "type" => "text",
  1318. "value" => preg_replace('/ $/', " ", $payload)
  1319. ];
  1320. $index++;
  1321. }
  1322. }
  1323. private function tablesublink($html_collection, &$data){
  1324. foreach($html_collection as $html){
  1325. $html["innerHTML"] = preg_replace(
  1326. '/<style>[\S\s]*<\/style>/i',
  1327. "",
  1328. $html["innerHTML"]
  1329. );
  1330. $html =
  1331. explode(
  1332. ":",
  1333. $this->fuckhtml->getTextContent($html),
  1334. 2
  1335. );
  1336. if(count($html) === 1){
  1337. $html = ["Rating", $html[0]];
  1338. }
  1339. $data["table"][trim($html[0])] = trim($html[1]);
  1340. }
  1341. }
  1342. /*
  1343. private function getimagelinkfromstyle($thumb){
  1344. $thumb =
  1345. $this->fuckhtml
  1346. ->getElementsByClassName(
  1347. $thumb,
  1348. "div"
  1349. );
  1350. if(count($thumb) === 0){
  1351. return [
  1352. "url" => null,
  1353. "ratio" => null
  1354. ];
  1355. }
  1356. $thumb = $thumb[0]["attributes"]["style"];
  1357. preg_match(
  1358. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1359. $thumb,
  1360. $thumb
  1361. );
  1362. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1363. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1364. return [
  1365. "url" => null,
  1366. "ratio" => null
  1367. ];
  1368. }
  1369. return [
  1370. "url" => $url,
  1371. "ratio" => "16:9"
  1372. ];
  1373. }*/
  1374. private function limitstrlen($text){
  1375. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1376. }
  1377. /*
  1378. private function limitwhitespace($text){
  1379. return
  1380. preg_replace(
  1381. '/[\s]+/',
  1382. " ",
  1383. $text
  1384. );
  1385. }*/
  1386. private function titledots($title){
  1387. $substr = substr($title, -3);
  1388. if(
  1389. $substr == "..." ||
  1390. $substr == "…"
  1391. ){
  1392. return trim(substr($title, 0, -3));
  1393. }
  1394. return trim($title);
  1395. }
  1396. private function generatenextpagetoken($q, $nsfw, $country, $spellcheck, $page, $proxy){
  1397. $nextpage =
  1398. $this->fuckhtml
  1399. ->getElementsByClassName("btn", "a");
  1400. if(count($nextpage) !== 0){
  1401. $nextpage =
  1402. $nextpage[count($nextpage) - 1];
  1403. if(
  1404. strtolower(
  1405. $this->fuckhtml
  1406. ->getTextContent(
  1407. $nextpage
  1408. )
  1409. ) == "next"
  1410. ){
  1411. preg_match(
  1412. '/offset=([0-9]+)/',
  1413. $this->fuckhtml->getTextContent($nextpage["attributes"]["href"]),
  1414. $nextpage
  1415. );
  1416. return
  1417. $this->backend->store(
  1418. json_encode(
  1419. [
  1420. "q" => $q,
  1421. "offset" => (int)$nextpage[1],
  1422. "nsfw" => $nsfw,
  1423. "country" => $country,
  1424. "spellcheck" => $spellcheck
  1425. ]
  1426. ),
  1427. $page,
  1428. $proxy
  1429. );
  1430. }
  1431. }
  1432. return null;
  1433. }
  1434. private function unshiturl($url){
  1435. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1436. $tmp = explode("aHR0", $url);
  1437. if(count($tmp) !== 2){
  1438. // nothing to do
  1439. return $url;
  1440. }
  1441. return
  1442. base64_decode(
  1443. "aHR0" .
  1444. str_replace(["/", "_"], ["", "/"],
  1445. explode(
  1446. ".",
  1447. $tmp[1]
  1448. )[0]
  1449. )
  1450. );
  1451. }
  1452. }