brave.php 37 KB

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