brave.php 36 KB

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