brave.php 36 KB

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