brave.php 36 KB

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