brave.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  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" => (int)$result["properties"]["width"],
  1027. "height" => (int)$result["properties"]["height"]
  1028. ],
  1029. [
  1030. "url" => $result["thumbnail"]["src"],
  1031. "width" => (int)$result["thumbnail"]["width"],
  1032. "height" => (int)$result["thumbnail"]["height"]
  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. }