brave.php 38 KB

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