brave.php 37 KB

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