brave.php 36 KB

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