brave.php 36 KB

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