brave.php 39 KB

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