brave.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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" => (isset($result["thumbnail"]["logo"]) && $result["thumbnail"]["logo"] == "true") ? "1:1" : "16:9",
  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. if(
  783. $video["title"] == "" &&
  784. $video["description"] == ""
  785. ){
  786. // ignore result
  787. continue;
  788. }
  789. // get thumbnails
  790. $thumbs = [];
  791. if(isset($video["video"]["thumbnail"]["src"])){ $thumbs[] = $video["video"]["thumbnail"]["src"]; }
  792. if(isset($video["video"]["thumbnail"]["original"])){ $thumbs[] = $video["video"]["thumbnail"]["original"]; }
  793. if(isset($video["thumbnail"]["src"])){ $thumbs[] = $video["thumbnail"]["src"]; }
  794. if(isset($video["thumbnail"]["original"])){ $thumbs[] = $video["thumbnail"]["original"]; }
  795. $thumb = $this->check_thumbs($thumbs);
  796. $out["video"][] = [
  797. "title" => $this->titledots($video["title"]),
  798. "description" => $this->titledots($video["description"]),
  799. "date" => isset($video["age"]) && $video["age"] != "void 0" ? strtotime($video["age"]) : null,
  800. "duration" => isset($video["video"]["duration"]) && $video["video"]["duration"] != "void 0" ? $this->hms2int($video["video"]["duration"]) : null,
  801. "views" => isset($video["video"]["views"]) && $video["video"]["views"] != "void 0" ? (int)$video["video"]["views"] : null,
  802. "thumb" =>
  803. $thumb !== false ?
  804. [
  805. "ratio" => "16:9",
  806. "url" => $this->unshiturl($thumb)
  807. ] :
  808. [
  809. "ratio" => null,
  810. "url" => null
  811. ],
  812. "url" => $video["url"]
  813. ];
  814. }
  815. }
  816. /*
  817. Get news
  818. */
  819. if(isset($data["news"]["results"])){
  820. foreach($data["news"]["results"] as $news){
  821. $out["news"][] = [
  822. "title" => $this->titledots($news["title"]),
  823. "description" => $this->titledots($news["description"]),
  824. "date" => isset($news["age"]) ? strtotime($news["age"]) : null,
  825. "thumb" =>
  826. isset($video["thumbnail"]["src"]) ?
  827. [
  828. "ratio" => "16:9",
  829. "url" => $this->unshiturl($video["thumbnail"]["src"])
  830. ] :
  831. [
  832. "ratio" => null,
  833. "url" => null
  834. ],
  835. "url" => $news["url"]
  836. ];
  837. }
  838. }
  839. /*
  840. Get discussions
  841. */
  842. $disc_out = [];
  843. if(isset($data["discussions"]["results"])){
  844. foreach($data["discussions"]["results"] as $disc){
  845. $table = [];
  846. if(isset($disc["data"]["num_votes"])){
  847. $table["Votes"] = number_format($disc["data"]["num_votes"]);
  848. }
  849. if(isset($disc["data"]["num_answers"])){
  850. $table["Comments"] = number_format($disc["data"]["num_answers"]);
  851. }
  852. $disc_out[] = [
  853. "title" =>
  854. $this->titledots(
  855. $disc["title"]
  856. ),
  857. "description" =>
  858. $this->limitstrlen(
  859. $this->titledots(
  860. $this->fuckhtml
  861. ->getTextContent(
  862. $disc["description"]
  863. )
  864. )
  865. ),
  866. "url" => $disc["url"],
  867. "date" => isset($disc["age"]) ? strtotime($disc["age"]) : null,
  868. "type" => "web",
  869. "thumb" => [
  870. "ratio" => null,
  871. "url" => null
  872. ],
  873. "sublink" => [],
  874. "table" => $table
  875. ];
  876. }
  877. }
  878. // append discussions at position 2
  879. array_splice($out["web"], 1, 0, $disc_out);
  880. }
  881. if($has_found_body === false){
  882. throw new Exception("Brave did not return a search result object");
  883. }
  884. return $out;
  885. }
  886. public function image($get){
  887. $search = $get["s"];
  888. if(strlen($search) === 0){
  889. throw new Exception("Search term is empty!");
  890. }
  891. if(strlen($search) > 2048){
  892. throw new Exception("Search term is too long!");
  893. }
  894. $country = $get["country"];
  895. $nsfw = $get["nsfw"];
  896. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  897. $out = [
  898. "status" => "ok",
  899. "npt" => null,
  900. "image" => []
  901. ];
  902. try{
  903. $html =
  904. $this->get(
  905. $this->backend->get_ip(), // no nextpage right now, pass proxy directly
  906. "https://search.brave.com/images",
  907. [
  908. "q" => $search,
  909. "spellcheck" => $spellcheck
  910. ],
  911. $nsfw,
  912. $country
  913. );
  914. }catch(Exception $error){
  915. throw new Exception("Could not fetch search page");
  916. }
  917. /*
  918. $handle = fopen("scraper/brave-image.html", "r");
  919. $html = fread($handle, filesize("scraper/brave-image.html"));
  920. fclose($handle);*/
  921. $this->fuckhtml->load($html);
  922. $this->detect_captcha($html);
  923. $jsons = $this->get_js();
  924. foreach($jsons as $json){
  925. if(!isset($json["data"]["body"]["response"]["results"])){
  926. continue;
  927. }
  928. foreach(
  929. $json
  930. ["data"]
  931. ["body"]
  932. ["response"]
  933. ["results"]
  934. as $result
  935. ){
  936. $out["image"][] = [
  937. "title" => $result["title"],
  938. "source" => [
  939. [
  940. "url" => $result["properties"]["url"],
  941. "width" => (int)$result["properties"]["width"],
  942. "height" => (int)$result["properties"]["height"]
  943. ],
  944. [
  945. "url" => $result["thumbnail"]["src"],
  946. "width" => (int)$result["thumbnail"]["width"],
  947. "height" => (int)$result["thumbnail"]["height"]
  948. ]
  949. ],
  950. "url" => $result["url"]
  951. ];
  952. }
  953. }
  954. return $out;
  955. }
  956. public function video($get){
  957. if($get["npt"]){
  958. [$npt, $proxy] = $this->backend->get($get["npt"], "videos");
  959. $npt = json_decode($npt, true);
  960. $search = $npt["q"];
  961. $offset = $npt["offset"];
  962. $spellcheck = $npt["spellcheck"];
  963. $country = $npt["country"];
  964. $nsfw = $npt["nsfw"];
  965. try{
  966. $html =
  967. $this->get(
  968. $proxy,
  969. "https://search.brave.com/videos",
  970. [
  971. "q" => $search,
  972. "offset" => $offset,
  973. "spellcheck" => $spellcheck
  974. ],
  975. $nsfw,
  976. $country
  977. );
  978. }catch(Exception $error){
  979. throw new Exception("Could not fetch search page");
  980. }
  981. }else{
  982. $search = $get["s"];
  983. if(strlen($search) === 0){
  984. throw new Exception("Search term is empty!");
  985. }
  986. if(strlen($search) > 2048){
  987. throw new Exception("Search term is too long!");
  988. }
  989. $country = $get["country"];
  990. $nsfw = $get["nsfw"];
  991. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  992. $proxy = $this->backend->get_ip();
  993. try{
  994. $html =
  995. $this->get(
  996. $proxy,
  997. "https://search.brave.com/videos",
  998. [
  999. "q" => $search,
  1000. "spellcheck" => $spellcheck
  1001. ],
  1002. $nsfw,
  1003. $country
  1004. );
  1005. }catch(Exception $error){
  1006. throw new Exception("Could not fetch search page");
  1007. }
  1008. }
  1009. $this->fuckhtml->load($html);
  1010. $this->detect_captcha($html);
  1011. $out = [
  1012. "status" => "ok",
  1013. "npt" => null,
  1014. "video" => [],
  1015. "author" => [],
  1016. "livestream" => [],
  1017. "playlist" => [],
  1018. "reel" => []
  1019. ];
  1020. // get npt
  1021. $out["npt"] =
  1022. $this->generatenextpagetoken(
  1023. $search,
  1024. $nsfw,
  1025. $country,
  1026. $spellcheck,
  1027. "videos",
  1028. $proxy
  1029. );
  1030. /*
  1031. $handle = fopen("scraper/brave-video.html", "r");
  1032. $html = fread($handle, filesize("scraper/brave-video.html"));
  1033. fclose($handle);*/
  1034. $this->fuckhtml->load($html);
  1035. $jsons = $this->get_js();
  1036. $error = false;
  1037. foreach($jsons as $json){
  1038. if(!isset($json["data"]["body"]["response"]["results"])){
  1039. continue;
  1040. }
  1041. foreach(
  1042. $json
  1043. ["data"]
  1044. ["body"]
  1045. ["response"]
  1046. ["results"]
  1047. as $result
  1048. ){
  1049. if($result["video"]["author"] != "null"){
  1050. $author = [
  1051. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1052. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1053. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1054. ];
  1055. }else{
  1056. $author = [
  1057. "name" => null,
  1058. "url" => null,
  1059. "avatar" => null
  1060. ];
  1061. }
  1062. if($result["thumbnail"] != "null"){
  1063. $thumb = [
  1064. "url" => $result["thumbnail"]["original"],
  1065. "ratio" => "16:9"
  1066. ];
  1067. }else{
  1068. $thumb = [
  1069. "url" => null,
  1070. "ratio" => null
  1071. ];
  1072. }
  1073. $out["video"][] = [
  1074. "title" => $result["title"],
  1075. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1076. "author" => $author,
  1077. "date" => ($result["age"] == "null" || $result["age"] == "void 0") ? null : strtotime($result["age"]),
  1078. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1079. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1080. "thumb" => $thumb,
  1081. "url" => $result["url"]
  1082. ];
  1083. }
  1084. }
  1085. return $out;
  1086. }
  1087. public function news($get){
  1088. if($get["npt"]){
  1089. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1090. $req = json_decode($req, true);
  1091. $search = $req["q"];
  1092. $country = $req["country"];
  1093. $nsfw = $req["nsfw"];
  1094. $offset = $req["offset"];
  1095. $spellcheck = $req["spellcheck"];
  1096. try{
  1097. $html =
  1098. $this->get(
  1099. $proxy,
  1100. "https://search.brave.com/news",
  1101. [
  1102. "q" => $search,
  1103. "offset" => $offset,
  1104. "spellcheck" => $spellcheck
  1105. ],
  1106. $nsfw,
  1107. $country
  1108. );
  1109. }catch(Exception $error){
  1110. throw new Exception("Could not fetch search page");
  1111. }
  1112. }else{
  1113. $search = $get["s"];
  1114. if(strlen($search) === 0){
  1115. throw new Exception("Search term is empty!");
  1116. }
  1117. if(strlen($search) > 2048){
  1118. throw new Exception("Search term is too long!");
  1119. }
  1120. $proxy = $this->backend->get_ip();
  1121. $nsfw = $get["nsfw"];
  1122. $country = $get["country"];
  1123. $spellcheck = $get["spellcheck"] == "yes" ? "1" : "0";
  1124. /*
  1125. $handle = fopen("scraper/brave-news.html", "r");
  1126. $html = fread($handle, filesize("scraper/brave-news.html"));
  1127. fclose($handle);*/
  1128. try{
  1129. $html =
  1130. $this->get(
  1131. $proxy,
  1132. "https://search.brave.com/news",
  1133. [
  1134. "q" => $search,
  1135. "spellcheck" => $spellcheck
  1136. ],
  1137. $nsfw,
  1138. $country
  1139. );
  1140. }catch(Exception $error){
  1141. throw new Exception("Could not fetch search page");
  1142. }
  1143. }
  1144. $out = [
  1145. "status" => "ok",
  1146. "npt" => null,
  1147. "news" => []
  1148. ];
  1149. // load html
  1150. $this->fuckhtml->load($html);
  1151. $this->detect_captcha($html);
  1152. // get npt
  1153. $out["npt"] =
  1154. $this->generatenextpagetoken(
  1155. $search,
  1156. $nsfw,
  1157. $country,
  1158. $spellcheck,
  1159. "news",
  1160. $proxy
  1161. );
  1162. $this->fuckhtml->load($html);
  1163. $jsons = $this->get_js();
  1164. foreach($jsons as $json){
  1165. if(!isset($json["data"]["response"]["news"]["results"])){
  1166. continue;
  1167. }
  1168. foreach(
  1169. $json["data"]["response"]["news"]["results"]
  1170. as $news
  1171. ){
  1172. if(
  1173. !isset($news["thumbnail"]["src"]) ||
  1174. $news["thumbnail"]["src"] == "void 0"
  1175. ){
  1176. $thumb = [
  1177. "url" => null,
  1178. "ratio" => null
  1179. ];
  1180. }else{
  1181. $thumb = [
  1182. "url" => $this->unshiturl($news["thumbnail"]["src"]),
  1183. "ratio" => "16:9"
  1184. ];
  1185. }
  1186. $out["news"][] = [
  1187. "title" => $news["title"],
  1188. "author" => null,
  1189. "description" => $news["description"],
  1190. "date" => !isset($news["age"]) || $news["age"] == "void 0" || $news["age"] == "null" ? null : strtotime($news["age"]),
  1191. "thumb" => $thumb,
  1192. "url" => $news["url"]
  1193. ];
  1194. }
  1195. }
  1196. return $out;
  1197. }
  1198. private function stackoverflow_parse($html){
  1199. $i = 0;
  1200. $answer = [];
  1201. $this->fuckhtml->load($html);
  1202. foreach(
  1203. $this->fuckhtml->getElementsByTagName("*")
  1204. as $snippet
  1205. ){
  1206. switch($snippet["tagName"]){
  1207. case "p":
  1208. $this->fuckhtml->load($snippet["innerHTML"]);
  1209. $codetags =
  1210. $this->fuckhtml
  1211. ->getElementsByTagName("*");
  1212. $tmphtml = $snippet["innerHTML"];
  1213. foreach($codetags as $tag){
  1214. if(!isset($tag["outerHTML"])){
  1215. continue;
  1216. }
  1217. $tmphtml =
  1218. explode(
  1219. $tag["outerHTML"],
  1220. $tmphtml,
  1221. 2
  1222. );
  1223. $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
  1224. $this->appendtext($value, $answer, $i);
  1225. $type = null;
  1226. switch($tag["tagName"]){
  1227. case "code": $type = "inline_code"; break;
  1228. case "em": $type = "italic"; break;
  1229. case "blockquote": $type = "quote"; break;
  1230. default: $type = "text";
  1231. }
  1232. if($type !== null){
  1233. $value = $this->fuckhtml->getTextContent($tag, false, true);
  1234. if(trim($value) != ""){
  1235. if(
  1236. $i !== 0 &&
  1237. $type == "title"
  1238. ){
  1239. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1240. }
  1241. $answer[] = [
  1242. "type" => $type,
  1243. "value" => $value
  1244. ];
  1245. $i++;
  1246. }
  1247. }
  1248. if(count($tmphtml) === 2){
  1249. $tmphtml = $tmphtml[1];
  1250. }else{
  1251. break;
  1252. }
  1253. }
  1254. if(is_array($tmphtml)){
  1255. $tmphtml = $tmphtml[0];
  1256. }
  1257. if(strlen($tmphtml) !== 0){
  1258. $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
  1259. $this->appendtext($value, $answer, $i);
  1260. }
  1261. break;
  1262. case "img":
  1263. $answer[] = [
  1264. "type" => "image",
  1265. "url" =>
  1266. $this->fuckhtml
  1267. ->getTextContent(
  1268. $tag["attributes"]["src"]
  1269. )
  1270. ];
  1271. $i++;
  1272. break;
  1273. case "pre":
  1274. switch($answer[$i - 1]["type"]){
  1275. case "text":
  1276. case "italic":
  1277. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1278. break;
  1279. }
  1280. $answer[] =
  1281. [
  1282. "type" => "code",
  1283. "value" =>
  1284. rtrim(
  1285. $this->fuckhtml
  1286. ->getTextContent(
  1287. $snippet,
  1288. true,
  1289. false
  1290. )
  1291. )
  1292. ];
  1293. $i++;
  1294. break;
  1295. case "ol":
  1296. $o = 0;
  1297. $this->fuckhtml->load($snippet);
  1298. $li =
  1299. $this->fuckhtml
  1300. ->getElementsByTagName("li");
  1301. foreach($li as $elem){
  1302. $o++;
  1303. $this->appendtext(
  1304. $o . ". " .
  1305. $this->fuckhtml
  1306. ->getTextContent(
  1307. $elem
  1308. ),
  1309. $answer,
  1310. $i
  1311. );
  1312. }
  1313. break;
  1314. }
  1315. }
  1316. if(
  1317. $i !== 0 &&
  1318. $answer[$i - 1]["type"] == "text"
  1319. ){
  1320. $answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
  1321. }
  1322. return $answer;
  1323. }
  1324. private function hms2int($time){
  1325. $parts = explode(":", $time, 3);
  1326. $time = 0;
  1327. if(count($parts) === 3){
  1328. // hours
  1329. $time = $time + ((int)$parts[0] * 3600);
  1330. array_shift($parts);
  1331. }
  1332. if(count($parts) === 2){
  1333. // minutes
  1334. $time = $time + ((int)$parts[0] * 60);
  1335. array_shift($parts);
  1336. }
  1337. // seconds
  1338. $time = $time + (int)$parts[0];
  1339. return $time;
  1340. }
  1341. private function appendtext($payload, &$text, &$index){
  1342. if(trim($payload) == ""){
  1343. return;
  1344. }
  1345. if(
  1346. $index !== 0 &&
  1347. $text[$index - 1]["type"] == "text"
  1348. ){
  1349. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1350. }else{
  1351. $text[] = [
  1352. "type" => "text",
  1353. "value" => preg_replace('/ $/', " ", $payload)
  1354. ];
  1355. $index++;
  1356. }
  1357. }
  1358. private function tablesublink($html_collection, &$data){
  1359. foreach($html_collection as $html){
  1360. $html["innerHTML"] = preg_replace(
  1361. '/<style>[\S\s]*<\/style>/i',
  1362. "",
  1363. $html["innerHTML"]
  1364. );
  1365. $html =
  1366. explode(
  1367. ":",
  1368. $this->fuckhtml->getTextContent($html),
  1369. 2
  1370. );
  1371. if(count($html) === 1){
  1372. $html = ["Rating", $html[0]];
  1373. }
  1374. $data["table"][trim($html[0])] = trim($html[1]);
  1375. }
  1376. }
  1377. /*
  1378. private function getimagelinkfromstyle($thumb){
  1379. $thumb =
  1380. $this->fuckhtml
  1381. ->getElementsByClassName(
  1382. $thumb,
  1383. "div"
  1384. );
  1385. if(count($thumb) === 0){
  1386. return [
  1387. "url" => null,
  1388. "ratio" => null
  1389. ];
  1390. }
  1391. $thumb = $thumb[0]["attributes"]["style"];
  1392. preg_match(
  1393. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1394. $thumb,
  1395. $thumb
  1396. );
  1397. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1398. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1399. return [
  1400. "url" => null,
  1401. "ratio" => null
  1402. ];
  1403. }
  1404. return [
  1405. "url" => $url,
  1406. "ratio" => "16:9"
  1407. ];
  1408. }*/
  1409. private function limitstrlen($text){
  1410. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1411. }
  1412. /*
  1413. private function limitwhitespace($text){
  1414. return
  1415. preg_replace(
  1416. '/[\s]+/',
  1417. " ",
  1418. $text
  1419. );
  1420. }*/
  1421. private function titledots($title){
  1422. $substr = substr($title, -3);
  1423. if(
  1424. $substr == "..." ||
  1425. $substr == "…"
  1426. ){
  1427. return trim(substr($title, 0, -3));
  1428. }
  1429. return trim($title);
  1430. }
  1431. private function generatenextpagetoken($q, $nsfw, $country, $spellcheck, $page, $proxy){
  1432. $nextpage =
  1433. $this->fuckhtml
  1434. ->getElementsByClassName(
  1435. "pagination",
  1436. "div"
  1437. );
  1438. if(count($nextpage) !== 0){
  1439. $this->fuckhtml->load($nextpage[0]);
  1440. $nextpage =
  1441. $this->fuckhtml
  1442. ->getElementsByClassName(
  1443. "button",
  1444. "a"
  1445. );
  1446. if(count($nextpage) !== 0){
  1447. $nextpage =
  1448. $nextpage[count($nextpage) - 1];
  1449. if(
  1450. strtolower(
  1451. $this->fuckhtml
  1452. ->getTextContent(
  1453. $nextpage
  1454. )
  1455. ) == "next"
  1456. ){
  1457. preg_match(
  1458. '/offset=([0-9]+)/',
  1459. $this->fuckhtml->getTextContent($nextpage["attributes"]["href"]),
  1460. $nextpage
  1461. );
  1462. return
  1463. $this->backend->store(
  1464. json_encode(
  1465. [
  1466. "q" => $q,
  1467. "offset" => (int)$nextpage[1],
  1468. "nsfw" => $nsfw,
  1469. "country" => $country,
  1470. "spellcheck" => $spellcheck
  1471. ]
  1472. ),
  1473. $page,
  1474. $proxy
  1475. );
  1476. }
  1477. }
  1478. }
  1479. return null;
  1480. }
  1481. private function unshiturl($url){
  1482. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1483. $tmp = explode("aHR0", $url);
  1484. if(count($tmp) !== 2){
  1485. // nothing to do
  1486. return $url;
  1487. }
  1488. return
  1489. base64_decode(
  1490. "aHR0" .
  1491. str_replace(["/", "_"], ["", "/"],
  1492. explode(
  1493. ".",
  1494. $tmp[1]
  1495. )[0]
  1496. )
  1497. );
  1498. }
  1499. private function check_thumbs($thumbs){
  1500. foreach($thumbs as $thumb){
  1501. if(
  1502. preg_match(
  1503. '/^http/',
  1504. $thumb
  1505. ) === 1
  1506. ){
  1507. return $this->unshiturl($thumb);
  1508. }
  1509. }
  1510. return false;
  1511. }
  1512. private function detect_captcha(){
  1513. $title =
  1514. $this->fuckhtml
  1515. ->getElementsByTagName(
  1516. "title"
  1517. );
  1518. if(count($title) === 0){
  1519. if(strtolower($this->fuckhtml->getloadedhtml()) == "this service is not available in your region"){
  1520. throw new Exception("Brave rangebanned the IP range");
  1521. }
  1522. throw new Exception("Brave returned a malformed page");
  1523. }
  1524. $title =
  1525. strtolower(
  1526. $this->fuckhtml
  1527. ->getTextContent($title[0])
  1528. );
  1529. if($title == "human verification"){
  1530. throw new Exception("Brave returned a Captcha");
  1531. }
  1532. }
  1533. }