brave.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. <?php
  2. class brave{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/nextpage.php";
  7. $this->nextpage = new nextpage("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. ];
  72. break;
  73. case "images":
  74. case "videos":
  75. case "news":
  76. return [
  77. "country" => [
  78. "display" => "Country",
  79. "option" => [
  80. "all" => "All regions",
  81. "ar" => "Argentina",
  82. "au" => "Australia",
  83. "at" => "Austria",
  84. "be" => "Belgium",
  85. "br" => "Brazil",
  86. "ca" => "Canada",
  87. "cl" => "Chile",
  88. "cn" => "China",
  89. "dk" => "Denmark",
  90. "fi" => "Finland",
  91. "fr" => "France",
  92. "de" => "Germany",
  93. "hk" => "Hong Kong",
  94. "in" => "India",
  95. "id" => "Indonesia",
  96. "it" => "Italy",
  97. "jp" => "Japan",
  98. "kr" => "Korea",
  99. "my" => "Malaysia",
  100. "mx" => "Mexico",
  101. "nl" => "Netherlands",
  102. "nz" => "New Zealand",
  103. "no" => "Norway",
  104. "pl" => "Poland",
  105. "pt" => "Portugal",
  106. "ph" => "Philippines",
  107. "ru" => "Russia",
  108. "sa" => "Saudi Arabia",
  109. "za" => "South Africa",
  110. "es" => "Spain",
  111. "se" => "Sweden",
  112. "ch" => "Switzerland",
  113. "tw" => "Taiwan",
  114. "tr" => "Turkey",
  115. "gb" => "United Kingdom",
  116. "us" => "United States"
  117. ]
  118. ],
  119. "nsfw" => [
  120. "display" => "NSFW",
  121. "option" => [
  122. "yes" => "Yes",
  123. "maybe" => "Maybe",
  124. "no" => "No"
  125. ]
  126. ]
  127. ];
  128. break;
  129. }
  130. }
  131. private function get($url, $get = [], $nsfw, $country){
  132. switch($nsfw){
  133. case "yes": $nsfw = "off"; break;
  134. case "maybe": $nsfw = "moderate"; break;
  135. case "no": $nsfw = "strict"; break;
  136. }
  137. if($country == "any"){
  138. $country = "all";
  139. }
  140. $headers = [
  141. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/110.0",
  142. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  143. "Accept-Language: en-US,en;q=0.5",
  144. "Accept-Encoding: gzip",
  145. "Cookie: safesearch={$nsfw}; country={$country}; useLocation=0; summarizer=0",
  146. "DNT: 1",
  147. "Connection: keep-alive",
  148. "Upgrade-Insecure-Requests: 1",
  149. "Sec-Fetch-Dest: document",
  150. "Sec-Fetch-Mode: navigate",
  151. "Sec-Fetch-Site: none",
  152. "Sec-Fetch-User: ?1"
  153. ];
  154. $curlproc = curl_init();
  155. if($get !== []){
  156. $get = http_build_query($get);
  157. $url .= "?" . $get;
  158. }
  159. curl_setopt($curlproc, CURLOPT_URL, $url);
  160. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  161. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  162. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  163. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  164. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  165. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  166. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  167. $data = curl_exec($curlproc);
  168. if(curl_errno($curlproc)){
  169. throw new Exception(curl_error($curlproc));
  170. }
  171. curl_close($curlproc);
  172. return $data;
  173. }
  174. public function web($get){
  175. if($get["npt"]){
  176. // get next page data
  177. $q = json_decode($this->nextpage->get($get["npt"], "web"), true);
  178. $search = $q["q"];
  179. $q["spellcheck"] = 0;
  180. $nsfw = $q["nsfw"];
  181. unset($q["nsfw"]);
  182. $country = $q["country"];
  183. unset($q["country"]);
  184. }else{
  185. // get _GET data instead
  186. $search = $get["s"];
  187. if(strlen($search) === 0){
  188. throw new Exception("Search term is empty!");
  189. }
  190. if(strlen($search) > 2048){
  191. throw new Exception("Search query is too long!");
  192. }
  193. $nsfw = $get["nsfw"];
  194. $country = $get["country"];
  195. $older = $get["older"];
  196. $newer = $get["newer"];
  197. $q = [
  198. "q" => $search
  199. ];
  200. /*
  201. Pass older/newer filters to brave
  202. */
  203. if($newer !== false){
  204. $newer = date("Y-m-d", $newer);
  205. if($older === false){
  206. $older = date("Y-m-d", time());
  207. }
  208. }
  209. if(
  210. is_string($older) === false &&
  211. $older !== false
  212. ){
  213. $older = date("Y-m-d", $older);
  214. if($newer === false){
  215. $newer = "1970-01-02";
  216. }
  217. }
  218. if($older !== false){
  219. $q["tf"] = "{$newer}to{$older}";
  220. }
  221. }
  222. /*
  223. $handle = fopen("scraper/brave.html", "r");
  224. $html = fread($handle, filesize("scraper/brave.html"));
  225. fclose($handle);
  226. */
  227. try{
  228. $html =
  229. $this->get(
  230. "https://search.brave.com/search",
  231. $q,
  232. $nsfw,
  233. $country
  234. );
  235. }catch(Exception $error){
  236. throw new Exception("Could not fetch search page");
  237. }
  238. $out = [
  239. "status" => "ok",
  240. "spelling" => [
  241. "type" => "no_correction",
  242. "using" => null,
  243. "correction" => null
  244. ],
  245. "npt" => null,
  246. "answer" => [],
  247. "web" => [],
  248. "image" => [],
  249. "video" => [],
  250. "news" => [],
  251. "related" => []
  252. ];
  253. // load html
  254. $this->fuckhtml->load($html);
  255. /*
  256. Get next page "token"
  257. */
  258. $nextpage =
  259. $this->fuckhtml
  260. ->getElementsByClassName(
  261. "btn ml-15",
  262. "a"
  263. );
  264. if(count($nextpage) !== 0){
  265. preg_match(
  266. '/offset=([0-9]+)/',
  267. $this->fuckhtml->getTextContent($nextpage[0]["attributes"]["href"]),
  268. $nextpage
  269. );
  270. $q["offset"] = (int)$nextpage[1];
  271. $q["nsfw"] = $nsfw;
  272. $q["country"] = $country;
  273. $out["npt"] =
  274. $this->nextpage->store(
  275. json_encode($q),
  276. "web"
  277. );
  278. }
  279. /*
  280. Get discussions (and append them to web results)
  281. */
  282. // they're loaded using javascript!!
  283. $discussion =
  284. $this->fuckhtml
  285. ->getElementById(
  286. "js-discussions",
  287. "script"
  288. );
  289. if(
  290. $discussion &&
  291. isset($discussion["attributes"]["data"])
  292. ){
  293. $discussion =
  294. json_decode(
  295. $this->fuckhtml
  296. ->getTextContent(
  297. $discussion["attributes"]["data"]
  298. ),
  299. true
  300. );
  301. foreach($discussion["results"] as $result){
  302. $data = [
  303. "title" => $this->titledots($result["title"]),
  304. "description" => null,
  305. "url" => $result["url"],
  306. "date" => null,
  307. "type" => "web",
  308. "thumb" => [
  309. "url" => null,
  310. "ratio" => null
  311. ],
  312. "sublink" => [],
  313. "table" => []
  314. ];
  315. // description
  316. $data["description"] =
  317. $this->limitstrlen(
  318. $this->limitwhitespace(
  319. $this->titledots(
  320. $this->fuckhtml->getTextContent(
  321. $result["description"]
  322. )
  323. )
  324. )
  325. );
  326. if($result["age"] != ""){
  327. $data["date"] = strtotime($result["age"]);
  328. }
  329. // populate table
  330. if($result["data"]["num_answers"] != ""){
  331. $data["table"]["Replies"] = (int)$result["data"]["num_answers"];
  332. }
  333. if($result["data"]["score"] != ""){
  334. $score = explode("|", $result["data"]["score"]);
  335. if(count($score) === 2){
  336. $score = ((int)$score[1]) . " (" . trim($score[0]) . ")";
  337. }else{
  338. $score = (int)$score[0];
  339. }
  340. $data["table"]["Votes"] = $score;
  341. }
  342. if($result["thumbnail"] != ""){
  343. $data["thumb"]["url"] = $result["thumbnail"];
  344. $data["thumb"]["ratio"] = "16:9";
  345. }
  346. $out["web"][] = $data;
  347. }
  348. }
  349. /*
  350. Get related searches
  351. */
  352. $faq =
  353. $this->fuckhtml
  354. ->getElementById("js-faq", "script");
  355. if(
  356. $faq &&
  357. isset($faq["attributes"]["data"])
  358. ){
  359. $faq =
  360. json_decode(
  361. $this->fuckhtml
  362. ->getTextContent(
  363. $faq["attributes"]["data"]
  364. ),
  365. true
  366. );
  367. foreach($faq["items"] as $related){
  368. $out["related"][] = $related["question"];
  369. }
  370. }
  371. /*
  372. Get spelling autocorrect
  373. */
  374. $altered =
  375. $this->fuckhtml
  376. ->getElementById("altered-query", "div");
  377. if($altered){
  378. $this->fuckhtml->load($altered);
  379. $altered =
  380. $this->fuckhtml
  381. ->getElementsByTagName("a");
  382. if(count($altered) === 2){
  383. $out["spelling"] = [
  384. "type" => "including",
  385. "using" =>
  386. $this->fuckhtml
  387. ->getTextContent($altered[0]),
  388. "correction" =>
  389. $this->fuckhtml
  390. ->getTextContent($altered[1])
  391. ];
  392. }
  393. $this->fuckhtml->load($html);
  394. }
  395. /*
  396. Get web results
  397. */
  398. $resulthtml =
  399. $this->fuckhtml
  400. ->getElementById(
  401. "results",
  402. "div"
  403. );
  404. $this->fuckhtml->load($resulthtml);
  405. $items = 0;
  406. foreach(
  407. $this->fuckhtml
  408. ->getElementsByClassName("snippet fdb")
  409. as $result
  410. ){
  411. $data = [
  412. "title" => null,
  413. "description" => null,
  414. "url" => null,
  415. "date" => null,
  416. "type" => "web",
  417. "thumb" => [
  418. "url" => null,
  419. "ratio" => null
  420. ],
  421. "sublink" => [],
  422. "table" => []
  423. ];
  424. if(
  425. isset($result["attributes"]["data-type"]) &&
  426. $result["attributes"]["data-type"] == "ad"
  427. ){
  428. // is an ad, skip
  429. continue;
  430. }
  431. $this->fuckhtml->load($result);
  432. /*
  433. Get title
  434. */
  435. $title =
  436. $this->fuckhtml
  437. ->getElementsByClassName(
  438. "snippet-title",
  439. "span"
  440. );
  441. if(count($title) === 0){
  442. // encountered AI summarizer
  443. // or misspelling indicator @TODO
  444. continue;
  445. }
  446. if(isset($title[0]["attributes"]["title"])){
  447. $data["title"] =
  448. $this->titledots(
  449. $this->fuckhtml
  450. ->getTextContent(
  451. $title[0]["attributes"]["title"]
  452. )
  453. );
  454. }else{
  455. $data["title"] =
  456. $this->titledots(
  457. $this->fuckhtml
  458. ->getTextContent(
  459. $title[0]
  460. )
  461. );
  462. }
  463. /*
  464. Get description
  465. */
  466. $description =
  467. $this->fuckhtml
  468. ->getElementsByClassName(
  469. "snippet-description",
  470. "p"
  471. );
  472. if(count($description) !== 0){
  473. $data["description"] =
  474. $this->titledots(
  475. $this->fuckhtml
  476. ->getTextContent(
  477. $description[0]
  478. )
  479. );
  480. // also check for thumbnail in here
  481. $img =
  482. $this->fuckhtml
  483. ->getElementsByClassName(
  484. "thumb",
  485. "img"
  486. );
  487. if(count($img) !== 0){
  488. $data["thumb"] = [
  489. "url" => $this->unshiturl($img[0]["attributes"]["src"]),
  490. "ratio" => "16:9"
  491. ];
  492. }else{
  493. // might be a video thumbnail wrapper?
  494. $wrapper =
  495. $this->fuckhtml
  496. ->getElementsByClassName(
  497. "video-thumb",
  498. "a"
  499. );
  500. if(count($wrapper) !== 0){
  501. // we found a video
  502. $this->fuckhtml->load($wrapper[0]);
  503. $img =
  504. $this->fuckhtml
  505. ->getElementsByTagName("img");
  506. $data["thumb"] = [
  507. "url" => $this->unshiturl($img[0]["attributes"]["src"]),
  508. "ratio" => "16:9"
  509. ];
  510. // get the video length, if its there
  511. $duration =
  512. $this->fuckhtml
  513. ->getElementsByClassName(
  514. "duration",
  515. "div"
  516. );
  517. if(count($duration) !== 0){
  518. $data["table"]["Duration"] = $duration[0]["innerHTML"];
  519. }
  520. // reset html load
  521. $this->fuckhtml->load($result);
  522. }
  523. }
  524. }else{
  525. // is a steam/shop listing
  526. $description_alt =
  527. $this->fuckhtml
  528. ->getElementsByClassName(
  529. "text-sm",
  530. "div"
  531. );
  532. if(count($description_alt) !== 0){
  533. switch($description_alt[0]["attributes"]["class"]){
  534. case "text-sm text-gray":
  535. case "description text-sm":
  536. $data["description"] =
  537. $this->titledots(
  538. $this->fuckhtml
  539. ->getTextContent(
  540. $description_alt[0]
  541. )
  542. );
  543. break;
  544. }
  545. // get table sublink
  546. $sublink =
  547. $this->fuckhtml
  548. ->getElementsByClassName(
  549. "r-attr text-sm",
  550. "div"
  551. );
  552. if(count($sublink) !== 0){
  553. $this->tablesublink($sublink, $data);
  554. }
  555. // check for thumb element
  556. $data["thumb"] = $this->getimagelinkfromstyle("thumb");
  557. }else{
  558. // ok... finally...
  559. // maybe its the instant answer thingy
  560. $answer =
  561. $this->fuckhtml
  562. ->getElementsByClassName("answer");
  563. if(count($answer) !== 0){
  564. $data["description"] =
  565. $this->titledots(
  566. $this->fuckhtml
  567. ->getTextContent($answer[0])
  568. );
  569. }
  570. }
  571. }
  572. // finally, fix brave's date format sucking balls
  573. $data["description"] = explode(" - ", $data["description"], 2);
  574. if(count($data["description"]) === 0){
  575. // nothing to do
  576. $data["description"] = $data["description"][0];
  577. }else{
  578. // attempt to parse
  579. $time = strtotime($data["description"][0]);
  580. if($time !== false){
  581. // got response
  582. $data["date"] = $time;
  583. array_shift($data["description"]);
  584. }
  585. // merge back
  586. $data["description"] =
  587. implode(" - ", $data["description"]);
  588. }
  589. /*
  590. Check content type
  591. */
  592. $content_type =
  593. $this->fuckhtml
  594. ->getElementsByClassName(
  595. "content-type",
  596. "span"
  597. );
  598. if(count($content_type) !== 0){
  599. $data["type"] =
  600. strtolower($this->fuckhtml->getTextContent($content_type[0]));
  601. }
  602. /*
  603. Check subtext table thingy
  604. */
  605. $table_items =
  606. array_merge(
  607. $this->fuckhtml
  608. ->getElementsByClassName(
  609. "item-attributes",
  610. "div"
  611. ),
  612. $this->fuckhtml
  613. ->getElementsByClassName(
  614. "r",
  615. "div"
  616. )
  617. );
  618. /*
  619. DIV: item-attributes
  620. */
  621. if(count($table_items) !== 0){
  622. foreach($table_items as $table){
  623. $this->fuckhtml->load($table);
  624. $span =
  625. $this->fuckhtml
  626. ->getElementsByClassName(
  627. "text-sm",
  628. "*"
  629. );
  630. foreach($span as $item){
  631. $item =
  632. explode(
  633. ":",
  634. $this->fuckhtml->getTextContent(preg_replace('/\n/', " ", $item["innerHTML"])),
  635. 2
  636. );
  637. if(count($item) === 2){
  638. $data["table"][trim($item[0])] = trim($this->limitwhitespace($item[1]));
  639. }
  640. }
  641. }
  642. $this->fuckhtml->load($result);
  643. }
  644. // get video sublinks
  645. $table_items =
  646. $this->fuckhtml
  647. ->getElementsByClassName(
  648. "snippet-description published-time",
  649. "p"
  650. );
  651. if(count($table_items) !== 0){
  652. $table_items =
  653. explode(
  654. '<span class="mr-15"></span>',
  655. $table_items[0]["innerHTML"],
  656. 2
  657. );
  658. if(count($table_items) === 2){
  659. $item2 = [];
  660. $item2[] = explode(":", $this->fuckhtml->getTextContent($table_items[0]));
  661. if(trim($table_items[1]) != ""){
  662. $item2[] = explode(":", $this->fuckhtml->getTextContent($table_items[1]));
  663. }
  664. foreach($item2 as $it){
  665. $data["table"][trim($it[0])] = trim($it[1]);
  666. }
  667. }
  668. }
  669. /*
  670. Get URL
  671. */
  672. $data["url"] =
  673. $this->fuckhtml->getTextContent(
  674. $this->fuckhtml
  675. ->getElementsByTagName("a")
  676. [0]
  677. ["attributes"]
  678. ["href"]
  679. );
  680. /*
  681. Get sublinks
  682. */
  683. $sublinks_elems =
  684. $this->fuckhtml
  685. ->getElementsByClassName(
  686. "snippet",
  687. "div"
  688. );
  689. $sublinks = [];
  690. foreach($sublinks_elems as $sublink){
  691. $this->fuckhtml->load($sublink);
  692. $a =
  693. $this->fuckhtml
  694. ->getElementsByTagName("a")[0];
  695. $title =
  696. $this->fuckhtml
  697. ->getTextContent($a);
  698. $url = $a["attributes"]["href"];
  699. $description =
  700. $this->titledots(
  701. $this->fuckhtml
  702. ->getTextContent(
  703. $this->fuckhtml
  704. ->getElementsByTagName("p")[0]
  705. )
  706. );
  707. $sublinks[] = [
  708. "title" => $title,
  709. "date" => null,
  710. "description" => $description,
  711. "url" => $url
  712. ];
  713. }
  714. /*
  715. Get smaller sublinks
  716. */
  717. $sublinks_elems =
  718. $this->fuckhtml
  719. ->getElementsByClassName(
  720. "deep-link",
  721. "a"
  722. );
  723. foreach($sublinks_elems as $sublink){
  724. $sublinks[] = [
  725. "title" => $this->fuckhtml->getTextContent($sublink),
  726. "date" => null,
  727. "description" => null,
  728. "url" => $sublink["attributes"]["href"]
  729. ];
  730. }
  731. // append sublinks to $data !!
  732. $data["sublink"] = $sublinks;
  733. // append first result to start of $out["web"]
  734. // other results are after
  735. if($items === 0){
  736. $out["web"] = [$data, ...$out["web"]];
  737. }else{
  738. $out["web"][] = $data;
  739. }
  740. $items++;
  741. }
  742. /*
  743. Get news
  744. */
  745. $this->fuckhtml->load($resulthtml);
  746. $news_carousel = $this->fuckhtml->getElementById("news-carousel");
  747. $this->fuckhtml->load($news_carousel);
  748. if($news_carousel){
  749. $a =
  750. $this->fuckhtml
  751. ->getElementsByClassName(
  752. "card fdb",
  753. "a"
  754. );
  755. foreach($a as $news){
  756. $this->fuckhtml->load($news);
  757. $out["news"][] = [
  758. "title" =>
  759. $this->titledots(
  760. $this->fuckhtml
  761. ->getTextContent(
  762. $this->fuckhtml
  763. ->getElementsByClassName(
  764. "title",
  765. "div"
  766. )[0]
  767. )
  768. ),
  769. "description" => null,
  770. "date" =>
  771. strtotime(
  772. $this->fuckhtml
  773. ->getTextContent(
  774. $this->fuckhtml
  775. ->getElementsByClassName(
  776. "card-footer__timestamp",
  777. "span"
  778. )[0]
  779. )
  780. ),
  781. "thumb" => $this->getimagelinkfromstyle("img-bg"),
  782. "url" => $this->fuckhtml->getTextContent($news["attributes"]["href"])
  783. ];
  784. }
  785. }
  786. /*
  787. Get videos
  788. */
  789. $this->fuckhtml->load($resulthtml);
  790. $news_carousel = $this->fuckhtml->getElementById("video-carousel");
  791. $this->fuckhtml->load($news_carousel);
  792. if($news_carousel){
  793. $a =
  794. $this->fuckhtml
  795. ->getElementsByClassName(
  796. "card fdb",
  797. "a"
  798. );
  799. foreach($a as $video){
  800. $this->fuckhtml->load($video);
  801. $date = null;
  802. $date_o =
  803. $this->fuckhtml
  804. ->getElementsByClassName(
  805. "text-gray text-xs",
  806. "span"
  807. );
  808. if(count($date_o) !== 0){
  809. $date =
  810. strtotime(
  811. $this->fuckhtml
  812. ->getTextContent(
  813. $date_o[0]
  814. )
  815. );
  816. }
  817. $out["video"][] = [
  818. "title" =>
  819. $this->titledots(
  820. $this->fuckhtml
  821. ->getTextContent(
  822. $this->fuckhtml
  823. ->getElementsByClassName(
  824. "title",
  825. "div"
  826. )[0]
  827. )
  828. ),
  829. "description" => null,
  830. "date" => $date,
  831. "duration" => null,
  832. "views" => null,
  833. "thumb" => $this->getimagelinkfromstyle("img-bg"),
  834. "url" => $this->fuckhtml->getTextContent($video["attributes"]["href"])
  835. ];
  836. }
  837. }
  838. /*
  839. Get DEFINITION snippet
  840. */
  841. $this->fuckhtml->load($html);
  842. $infobox = $this->fuckhtml->getElementById("rh-definitions", "div");
  843. if($infobox !== false){
  844. $answer = [
  845. "title" => null,
  846. "description" => [],
  847. "url" => null,
  848. "thumb" => null,
  849. "table" => [],
  850. "sublink" => []
  851. ];
  852. $this->fuckhtml->load($infobox);
  853. $answer["title"] =
  854. $this->fuckhtml
  855. ->getTextContent(
  856. $this->fuckhtml
  857. ->getElementsByClassName(
  858. "header",
  859. "h5"
  860. )[0]
  861. );
  862. $sections =
  863. $this->fuckhtml
  864. ->getElementsByTagName("section");
  865. $i = -1;
  866. foreach($sections as $section){
  867. $this->fuckhtml->load($section);
  868. $items =
  869. $this->fuckhtml
  870. ->getElementsByTagName("*");
  871. $li = 1;
  872. $pronounce = false;
  873. foreach($items as $item){
  874. switch($item["tagName"]){
  875. case "h6":
  876. if(
  877. isset($item["attributes"]["class"]) &&
  878. $item["attributes"]["class"] == "h6 pronunciation"
  879. ){
  880. if($pronounce){
  881. break;
  882. }
  883. $answer["description"][] = [
  884. "type" => "quote",
  885. "value" =>
  886. $this->fuckhtml
  887. ->getTextContent(
  888. $item
  889. )
  890. ];
  891. $answer["description"][] =
  892. [
  893. "type" => "audio",
  894. "url" => "https://search.brave.com/api/rhfetch?rhtype=definitions&word={$answer["title"]}&source=ahd-5"
  895. ];
  896. $pronounce = true;
  897. $i = $i + 2;
  898. break;
  899. }
  900. $answer["description"][] = [
  901. "type" => "title",
  902. "value" =>
  903. $this->fuckhtml
  904. ->getTextContent(
  905. $item
  906. )
  907. ];
  908. $i++;
  909. break;
  910. case "li":
  911. if(
  912. $i !== -1 &&
  913. $answer["description"][$i]["type"] == "text"
  914. ){
  915. $answer["description"][$i]["value"] .=
  916. "\n" . $li . ". " .
  917. $this->fuckhtml
  918. ->getTextContent(
  919. $item
  920. );
  921. }else{
  922. $answer["description"][] = [
  923. "type" => "text",
  924. "value" =>
  925. $li . ". " .
  926. $this->fuckhtml
  927. ->getTextContent(
  928. $item
  929. )
  930. ];
  931. $i++;
  932. }
  933. $li++;
  934. break;
  935. case "a":
  936. $answer["url"] =
  937. $this->fuckhtml
  938. ->getTextContent(
  939. $item["attributes"]["href"]
  940. );
  941. break;
  942. }
  943. }
  944. }
  945. $out["answer"][] = $answer;
  946. }
  947. /*
  948. Get instant answer
  949. */
  950. $this->fuckhtml->load($html);
  951. $infobox = $this->fuckhtml->getElementById("infobox", "div");
  952. if($infobox !== false){
  953. $answer = [
  954. "title" => null,
  955. "description" => [],
  956. "url" => null,
  957. "thumb" => null,
  958. "table" => [],
  959. "sublink" => []
  960. ];
  961. $this->fuckhtml->load($infobox);
  962. $div = $this->fuckhtml->getElementsByTagName("div");
  963. /*
  964. Get small description
  965. */
  966. $small_desc =
  967. $this->fuckhtml
  968. ->getElementsByClassName(
  969. "infobox-description",
  970. $div
  971. );
  972. if(count($small_desc) !== 0){
  973. $answer["description"][] = [
  974. "type" => "quote",
  975. "value" =>
  976. $this->fuckhtml
  977. ->getTextContent(
  978. $small_desc[0]
  979. )
  980. ];
  981. }
  982. /*
  983. Get title + url
  984. */
  985. $title =
  986. $this->fuckhtml
  987. ->getElementsByClassName("infobox-title", "a");
  988. if(count($title) !== 0){
  989. $answer["title"] =
  990. $this->fuckhtml
  991. ->getTextContent(
  992. $title[0]
  993. );
  994. $answer["url"] =
  995. $this->fuckhtml
  996. ->getTextContent(
  997. $title[0]["attributes"]["href"]
  998. );
  999. }
  1000. /*
  1001. Get thumbnail
  1002. */
  1003. $thumb = $this->getimagelinkfromstyle("thumb");
  1004. if($thumb["url"] !== null){
  1005. $answer["thumb"] = $thumb["url"];
  1006. }
  1007. /*
  1008. Get table
  1009. */
  1010. $title =
  1011. $this->fuckhtml
  1012. ->getElementsByClassName(
  1013. "infobox-attr-header",
  1014. "div"
  1015. );
  1016. $rowhtml = $infobox;
  1017. if(count($title) >= 2){
  1018. $rowhtml =
  1019. explode(
  1020. $title[1]["outerHTML"],
  1021. $infobox["innerHTML"],
  1022. 2
  1023. )[0];
  1024. }
  1025. $this->fuckhtml->load($rowhtml);
  1026. $rows =
  1027. $this->fuckhtml
  1028. ->getElementsByClassName("infobox-attr", "div");
  1029. foreach($rows as $row){
  1030. if(!isset($row["innerHTML"])){
  1031. continue;
  1032. }
  1033. $this->fuckhtml->load($row);
  1034. $span =
  1035. $this->fuckhtml
  1036. ->getElementsByTagName("span");
  1037. if(count($span) === 2){
  1038. $answer["table"][
  1039. $this->fuckhtml->getTextContent($span[0])
  1040. ] = str_replace("\n", ", ", $this->fuckhtml->getTextContent($span[1], true));
  1041. }
  1042. }
  1043. $this->fuckhtml->load($infobox);
  1044. /*
  1045. Parse stackoverflow answers
  1046. */
  1047. $code =
  1048. $this->fuckhtml
  1049. ->getElementById("codebox-answer", $div);
  1050. if($code){
  1051. // this might be standalone text with no paragraphs, check for that
  1052. $author =
  1053. $this->fuckhtml
  1054. ->getElementById("author");
  1055. $desc_tmp =
  1056. str_replace(
  1057. $author["outerHTML"],
  1058. "",
  1059. $code["innerHTML"]
  1060. );
  1061. $this->fuckhtml->load($desc_tmp);
  1062. $code =
  1063. $this->fuckhtml
  1064. ->getElementsByTagName("*");
  1065. if(count($code) === 0){
  1066. $answer["description"][] = [
  1067. "type" => "text",
  1068. "value" =>
  1069. $this->fuckhtml
  1070. ->getTextContent(
  1071. $desc_tmp
  1072. )
  1073. ];
  1074. $answer["description"][] = [
  1075. "type" => "quote",
  1076. "value" =>
  1077. $this->fuckhtml
  1078. ->getTextContent(
  1079. $author
  1080. )
  1081. ];
  1082. }else{
  1083. $i = 0;
  1084. foreach($code as $snippet){
  1085. switch($snippet["tagName"]){
  1086. case "p":
  1087. $this->fuckhtml->load($snippet["innerHTML"]);
  1088. $codetags =
  1089. $this->fuckhtml
  1090. ->getElementsByTagName("*");
  1091. $tmphtml = $snippet["innerHTML"];
  1092. foreach($codetags as $tag){
  1093. if(!isset($tag["outerHTML"])){
  1094. continue;
  1095. }
  1096. $tmphtml =
  1097. explode(
  1098. $tag["outerHTML"],
  1099. $tmphtml,
  1100. 2
  1101. );
  1102. $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
  1103. $this->appendtext($value, $answer["description"], $i);
  1104. $type = null;
  1105. switch($tag["tagName"]){
  1106. case "code": $type = "inline_code"; break;
  1107. case "em": $type = "italic"; break;
  1108. case "blockquote": $type = "quote"; break;
  1109. default: $type = "text";
  1110. }
  1111. if($type !== null){
  1112. $value = $this->fuckhtml->getTextContent($tag, false, true);
  1113. if(trim($value) != ""){
  1114. if(
  1115. $i !== 0 &&
  1116. $type == "title"
  1117. ){
  1118. $answer["description"][$i - 1]["value"] = rtrim($answer["description"][$i - 1]["value"]);
  1119. }
  1120. $answer["description"][] = [
  1121. "type" => $type,
  1122. "value" => $value
  1123. ];
  1124. $i++;
  1125. }
  1126. }
  1127. if(count($tmphtml) === 2){
  1128. $tmphtml = $tmphtml[1];
  1129. }else{
  1130. break;
  1131. }
  1132. }
  1133. if(is_array($tmphtml)){
  1134. $tmphtml = $tmphtml[0];
  1135. }
  1136. if(strlen($tmphtml) !== 0){
  1137. $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
  1138. $this->appendtext($value, $answer["description"], $i);
  1139. }
  1140. break;
  1141. case "pre":
  1142. switch($answer["description"][$i - 1]["type"]){
  1143. case "text":
  1144. case "italic":
  1145. $answer["description"][$i - 1]["value"] = rtrim($answer["description"][$i - 1]["value"]);
  1146. break;
  1147. }
  1148. $answer["description"][] =
  1149. [
  1150. "type" => "code",
  1151. "value" =>
  1152. rtrim(
  1153. $this->fuckhtml
  1154. ->getTextContent(
  1155. $snippet,
  1156. true,
  1157. false
  1158. )
  1159. )
  1160. ];
  1161. $i++;
  1162. break;
  1163. case "ol":
  1164. $o = 0;
  1165. $this->fuckhtml->load($snippet);
  1166. $li =
  1167. $this->fuckhtml
  1168. ->getElementsByTagName("li");
  1169. foreach($li as $elem){
  1170. $o++;
  1171. $this->appendtext(
  1172. $o . ". " .
  1173. $this->fuckhtml
  1174. ->getTextContent(
  1175. $elem
  1176. ),
  1177. $answer["description"],
  1178. $i
  1179. );
  1180. }
  1181. break;
  1182. }
  1183. }
  1184. if(
  1185. $i !== 0 &&
  1186. $answer["description"][$i - 1]["type"] == "text"
  1187. ){
  1188. $answer["description"][$i - 1]["value"] = rtrim($answer["description"][$i - 1]["value"]);
  1189. }
  1190. if($author){
  1191. $answer["description"][] = [
  1192. "type" => "quote",
  1193. "value" => $this->fuckhtml->getTextContent($author)
  1194. ];
  1195. }
  1196. }
  1197. }else{
  1198. /*
  1199. Get normal description
  1200. */
  1201. $description =
  1202. $this->fuckhtml
  1203. ->getElementsByClassName(
  1204. "mb-6",
  1205. "div"
  1206. );
  1207. if(count($description) !== 0){
  1208. $answer["description"][] =
  1209. [
  1210. "type" => "text",
  1211. "value" =>
  1212. $this->titledots(
  1213. preg_replace(
  1214. '/ Wikipedia$/',
  1215. "",
  1216. $this->fuckhtml
  1217. ->getTextContent(
  1218. $description[0]
  1219. )
  1220. )
  1221. )
  1222. ];
  1223. $ratings =
  1224. $this->fuckhtml
  1225. ->getElementById("ratings");
  1226. if($ratings){
  1227. $this->fuckhtml->load($ratings);
  1228. $ratings =
  1229. $this->fuckhtml
  1230. ->getElementsByClassName(
  1231. "flex-hcenter mb-10",
  1232. "div"
  1233. );
  1234. $answer["description"][] = [
  1235. "type" => "title",
  1236. "value" => "Ratings"
  1237. ];
  1238. foreach($ratings as $rating){
  1239. $this->fuckhtml->load($rating);
  1240. $num =
  1241. $this->fuckhtml
  1242. ->getTextContent(
  1243. $this->fuckhtml
  1244. ->getElementsByClassName(
  1245. "r-num",
  1246. "div"
  1247. )[0]
  1248. );
  1249. $href =
  1250. $this->fuckhtml
  1251. ->getElementsByClassName(
  1252. "mr-10",
  1253. "a"
  1254. )[0];
  1255. $votes =
  1256. $this->fuckhtml
  1257. ->getTextContent(
  1258. $this->fuckhtml
  1259. ->getElementsByClassName(
  1260. "text-sm",
  1261. "span"
  1262. )[0]
  1263. );
  1264. $c = count($answer["description"]) - 1;
  1265. if(
  1266. $c !== -1 &&
  1267. $answer["description"][$c]["type"] == "text"
  1268. ){
  1269. $answer["description"][$c]["value"] .= $num . " ";
  1270. }else{
  1271. $answer["description"][] = [
  1272. "type" => "text",
  1273. "value" => $num . " "
  1274. ];
  1275. }
  1276. $answer["description"][] = [
  1277. "type" => "link",
  1278. "value" => $this->fuckhtml->getTextContent($href),
  1279. "url" => $this->fuckhtml->getTextContent($href["attributes"]["href"])
  1280. ];
  1281. $answer["description"][] = [
  1282. "type" => "text",
  1283. "value" => " (" . $votes . ")\n"
  1284. ];
  1285. }
  1286. }
  1287. }
  1288. }
  1289. /*
  1290. Get sublinks
  1291. */
  1292. $this->fuckhtml->load($infobox);
  1293. $profiles =
  1294. $this->fuckhtml
  1295. ->getElementById("profiles");
  1296. if($profiles){
  1297. $profiles =
  1298. $this->fuckhtml
  1299. ->getElementsByClassName(
  1300. "chip",
  1301. "a"
  1302. );
  1303. foreach($profiles as $profile){
  1304. $name = $this->fuckhtml->getTextContent($profile["attributes"]["title"]);
  1305. if(strtolower($name) == "steampowered"){
  1306. $name = "Steam";
  1307. }
  1308. $answer["sublink"][$name] =
  1309. $this->fuckhtml->getTextContent($profile["attributes"]["href"]);
  1310. }
  1311. }
  1312. $actors =
  1313. $this->fuckhtml
  1314. ->getElementById("panel-movie-cast");
  1315. if($actors){
  1316. $this->fuckhtml->load($actors);
  1317. $actors =
  1318. $this->fuckhtml
  1319. ->getElementsByClassName("card");
  1320. $answer["description"][] = [
  1321. "type" => "title",
  1322. "value" => "Cast"
  1323. ];
  1324. foreach($actors as $actor){
  1325. $this->fuckhtml->load($actor);
  1326. $answer["description"][] = [
  1327. "type" => "text",
  1328. "value" =>
  1329. $this->fuckhtml
  1330. ->getTextContent(
  1331. $this->fuckhtml
  1332. ->getElementsByClassName("card-body")
  1333. [0]
  1334. )
  1335. ];
  1336. $answer["description"][] = [
  1337. "type" => "image",
  1338. "url" => $this->getimagelinkfromstyle("person-thumb")["url"]
  1339. ];
  1340. }
  1341. }
  1342. $out["answer"][] = $answer;
  1343. }
  1344. /*
  1345. Get actor standalone thingy
  1346. */
  1347. $this->fuckhtml->load($resulthtml);
  1348. $actors =
  1349. $this->fuckhtml
  1350. ->getElementById("predicate-entity");
  1351. if($actors){
  1352. $this->fuckhtml->load($actors);
  1353. $cards =
  1354. $this->fuckhtml
  1355. ->getElementsByClassName("card");
  1356. $url =
  1357. $this->fuckhtml
  1358. ->getElementsByClassName(
  1359. "disclaimer",
  1360. "div"
  1361. )[0];
  1362. $this->fuckhtml->load($url);
  1363. $url =
  1364. $this->fuckhtml
  1365. ->getTextContent(
  1366. $this->fuckhtml
  1367. ->getElementsByTagName("a")
  1368. [0]
  1369. ["attributes"]
  1370. ["href"]
  1371. );
  1372. $this->fuckhtml->load($actors);
  1373. $answer = [
  1374. "title" =>
  1375. $this->fuckhtml
  1376. ->getTextContent(
  1377. $this->fuckhtml
  1378. ->getElementsByClassName(
  1379. "entity",
  1380. "span"
  1381. )[0]
  1382. ) . " (Cast)",
  1383. "description" => [],
  1384. "url" => $url,
  1385. "sublink" => [],
  1386. "thumb" => null,
  1387. "table" => []
  1388. ];
  1389. foreach($cards as $card){
  1390. $this->fuckhtml->load($card);
  1391. $answer["description"][] = [
  1392. "type" => "title",
  1393. "value" =>
  1394. $this->fuckhtml
  1395. ->getTextContent(
  1396. $this->fuckhtml
  1397. ->getElementsByClassName(
  1398. "title"
  1399. )[0]
  1400. )
  1401. ];
  1402. $answer["description"][] = [
  1403. "type" => "text",
  1404. "value" =>
  1405. $this->fuckhtml
  1406. ->getTextContent(
  1407. $this->fuckhtml
  1408. ->getElementsByClassName(
  1409. "text-xs desc"
  1410. )[0]
  1411. )
  1412. ];
  1413. $answer["description"][] = [
  1414. "type" => "image",
  1415. "url" => $this->getimagelinkfromstyle("img-bg")["url"]
  1416. ];
  1417. }
  1418. $out["answer"][] = $answer;
  1419. }
  1420. return $out;
  1421. }
  1422. public function news($get){
  1423. $search = $get["s"];
  1424. if(strlen($search) === 0){
  1425. throw new Exception("Search term is empty!");
  1426. }
  1427. $nsfw = $get["nsfw"];
  1428. $country = $get["country"];
  1429. if(strlen($search) > 2048){
  1430. throw new Exception("Search query is too long!");
  1431. }
  1432. /*
  1433. $handle = fopen("scraper/brave-news.html", "r");
  1434. $html = fread($handle, filesize("scraper/brave-news.html"));
  1435. fclose($handle);*/
  1436. try{
  1437. $html =
  1438. $this->get(
  1439. "https://search.brave.com/news",
  1440. [
  1441. "q" => $search
  1442. ],
  1443. $nsfw,
  1444. $country
  1445. );
  1446. }catch(Exception $error){
  1447. throw new Exception("Could not fetch search page");
  1448. }
  1449. $out = [
  1450. "status" => "ok",
  1451. "npt" => null,
  1452. "news" => []
  1453. ];
  1454. // load html
  1455. $this->fuckhtml->load($html);
  1456. $news =
  1457. $this->fuckhtml
  1458. ->getElementsByClassName(
  1459. "snippet inline gap-standard",
  1460. "div"
  1461. );
  1462. foreach($news as $article){
  1463. $data = [
  1464. "title" => null,
  1465. "author" => null,
  1466. "description" => null,
  1467. "date" => null,
  1468. "thumb" =>
  1469. [
  1470. "url" => null,
  1471. "ratio" => null
  1472. ],
  1473. "url" => null
  1474. ];
  1475. $this->fuckhtml->load($article);
  1476. $elems =
  1477. $this->fuckhtml
  1478. ->getElementsByTagName("*");
  1479. // get title
  1480. $data["title"] =
  1481. $this->fuckhtml
  1482. ->getTextContent(
  1483. $this->fuckhtml
  1484. ->getElementsByClassName(
  1485. "snippet-title",
  1486. $elems
  1487. )
  1488. [0]
  1489. ["innerHTML"]
  1490. );
  1491. // get description
  1492. $data["description"] =
  1493. $this->titledots(
  1494. $this->fuckhtml
  1495. ->getTextContent(
  1496. $this->fuckhtml
  1497. ->getElementsByClassName(
  1498. "snippet-description",
  1499. $elems
  1500. )
  1501. [0]
  1502. ["innerHTML"]
  1503. )
  1504. );
  1505. // get date
  1506. $date =
  1507. explode(
  1508. "•",
  1509. $this->fuckhtml
  1510. ->getTextContent(
  1511. $this->fuckhtml
  1512. ->getElementsByClassName(
  1513. "snippet-url",
  1514. $elems
  1515. )[0]
  1516. )
  1517. );
  1518. if(
  1519. count($date) !== 1 &&
  1520. trim($date[1]) != ""
  1521. ){
  1522. $data["date"] =
  1523. strtotime(
  1524. $date[1]
  1525. );
  1526. }
  1527. // get URL
  1528. $data["url"] =
  1529. $this->fuckhtml->getTextContent(
  1530. $this->unshiturl(
  1531. $this->fuckhtml
  1532. ->getElementsByClassName(
  1533. "result-header",
  1534. $elems
  1535. )
  1536. [0]
  1537. ["attributes"]
  1538. ["href"]
  1539. )
  1540. );
  1541. // get thumbnail
  1542. $thumb =
  1543. $this->fuckhtml
  1544. ->getElementsByTagName(
  1545. "img"
  1546. );
  1547. if(
  1548. count($thumb) === 2 &&
  1549. trim(
  1550. $thumb[1]
  1551. ["attributes"]
  1552. ["src"]
  1553. ) != ""
  1554. ){
  1555. $data["thumb"] = [
  1556. "url" =>
  1557. $this->fuckhtml->getTextContent(
  1558. $this->unshiturl(
  1559. $thumb[1]
  1560. ["attributes"]
  1561. ["src"]
  1562. )
  1563. ),
  1564. "ratio" => "16:9"
  1565. ];
  1566. }
  1567. $out["news"][] = $data;
  1568. }
  1569. return $out;
  1570. }
  1571. public function image($get){
  1572. $search = $get["s"];
  1573. $country = $get["country"];
  1574. $nsfw = $get["nsfw"];
  1575. $out = [
  1576. "status" => "ok",
  1577. "npt" => null,
  1578. "image" => []
  1579. ];
  1580. try{
  1581. $html =
  1582. $this->get(
  1583. "https://search.brave.com/images",
  1584. [
  1585. "q" => $search
  1586. ],
  1587. $nsfw,
  1588. $country
  1589. );
  1590. }catch(Exception $error){
  1591. throw new Exception("Could not fetch search page");
  1592. }
  1593. /*
  1594. $handle = fopen("scraper/brave-image.html", "r");
  1595. $html = fread($handle, filesize("scraper/brave-image.html"));
  1596. fclose($handle);*/
  1597. preg_match(
  1598. '/const data = (\[{.*}\]);/',
  1599. $html,
  1600. $json
  1601. );
  1602. if(!isset($json[1])){
  1603. throw new Exception("Failed to get data object");
  1604. }
  1605. $json =
  1606. $this->fuckhtml
  1607. ->parseJsObject(
  1608. $json[1]
  1609. );
  1610. foreach(
  1611. $json[1]
  1612. ["data"]
  1613. ["body"]
  1614. ["response"]
  1615. ["results"]
  1616. as $result
  1617. ){
  1618. $out["image"][] = [
  1619. "title" => $result["title"],
  1620. "source" => [
  1621. [
  1622. "url" => $result["properties"]["url"],
  1623. "width" => null,
  1624. "height" => null
  1625. ],
  1626. [
  1627. "url" => $result["thumbnail"]["src"],
  1628. "width" => null,
  1629. "height" => null
  1630. ]
  1631. ],
  1632. "url" => $result["url"]
  1633. ];
  1634. }
  1635. return $out;
  1636. }
  1637. public function video($get){
  1638. $search = $get["s"];
  1639. $country = $get["country"];
  1640. $nsfw = $get["nsfw"];
  1641. $out = [
  1642. "status" => "ok",
  1643. "npt" => null,
  1644. "video" => [],
  1645. "author" => [],
  1646. "livestream" => [],
  1647. "playlist" => [],
  1648. "reel" => []
  1649. ];
  1650. try{
  1651. $html =
  1652. $this->get(
  1653. "https://search.brave.com/videos",
  1654. [
  1655. "q" => $search
  1656. ],
  1657. $nsfw,
  1658. $country
  1659. );
  1660. }catch(Exception $error){
  1661. throw new Exception("Could not fetch search page");
  1662. }
  1663. /*
  1664. $handle = fopen("scraper/brave-video.html", "r");
  1665. $html = fread($handle, filesize("scraper/brave-video.html"));
  1666. fclose($handle);*/
  1667. preg_match(
  1668. '/const data = (\[{.*}\]);/',
  1669. $html,
  1670. $json
  1671. );
  1672. if(!isset($json[1])){
  1673. throw new Exception("Failed to get data object");
  1674. }
  1675. $json =
  1676. $this->fuckhtml
  1677. ->parseJsObject(
  1678. $json[1]
  1679. );
  1680. foreach(
  1681. $json
  1682. [1]
  1683. ["data"]
  1684. ["body"]
  1685. ["response"]
  1686. ["results"]
  1687. as $result
  1688. ){
  1689. if($result["video"]["author"] != "null"){
  1690. $author = [
  1691. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1692. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1693. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1694. ];
  1695. }else{
  1696. $author = [
  1697. "name" => null,
  1698. "url" => null,
  1699. "avatar" => null
  1700. ];
  1701. }
  1702. if($result["thumbnail"] != "null"){
  1703. $thumb = [
  1704. "url" => $result["thumbnail"]["original"],
  1705. "ratio" => "16:9"
  1706. ];
  1707. }else{
  1708. $thumb = [
  1709. "url" => null,
  1710. "ratio" => null
  1711. ];
  1712. }
  1713. $out["video"][] = [
  1714. "title" => $result["title"],
  1715. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1716. "author" => $author,
  1717. "date" => $result["age"] == "null" ? null : strtotime($result["age"]),
  1718. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1719. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1720. "thumb" => $thumb,
  1721. "url" => $result["url"]
  1722. ];
  1723. }
  1724. return $out;
  1725. }
  1726. private function hms2int($time){
  1727. $parts = explode(":", $time, 3);
  1728. $time = 0;
  1729. if(count($parts) === 3){
  1730. // hours
  1731. $time = $time + ((int)$parts[0] * 3600);
  1732. array_shift($parts);
  1733. }
  1734. if(count($parts) === 2){
  1735. // minutes
  1736. $time = $time + ((int)$parts[0] * 60);
  1737. array_shift($parts);
  1738. }
  1739. // seconds
  1740. $time = $time + (int)$parts[0];
  1741. return $time;
  1742. }
  1743. private function appendtext($payload, &$text, &$index){
  1744. if(trim($payload) == ""){
  1745. return;
  1746. }
  1747. if(
  1748. $index !== 0 &&
  1749. $text[$index - 1]["type"] == "text"
  1750. ){
  1751. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1752. }else{
  1753. $text[] = [
  1754. "type" => "text",
  1755. "value" => preg_replace('/ $/', " ", $payload)
  1756. ];
  1757. $index++;
  1758. }
  1759. }
  1760. private function tablesublink($html_collection, &$data){
  1761. foreach($html_collection as $html){
  1762. $html["innerHTML"] = preg_replace(
  1763. '/<style>[\S\s]*<\/style>/i',
  1764. "",
  1765. $html["innerHTML"]
  1766. );
  1767. $html =
  1768. explode(
  1769. ":",
  1770. $this->fuckhtml->getTextContent($html),
  1771. 2
  1772. );
  1773. if(count($html) === 1){
  1774. $html = ["Rating", $html[0]];
  1775. }
  1776. $data["table"][trim($html[0])] = trim($html[1]);
  1777. }
  1778. }
  1779. private function getimagelinkfromstyle($thumb){
  1780. $thumb =
  1781. $this->fuckhtml
  1782. ->getElementsByClassName(
  1783. $thumb,
  1784. "div"
  1785. );
  1786. if(count($thumb) === 0){
  1787. return [
  1788. "url" => null,
  1789. "ratio" => null
  1790. ];
  1791. }
  1792. $thumb = $thumb[0]["attributes"]["style"];
  1793. preg_match(
  1794. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1795. $thumb,
  1796. $thumb
  1797. );
  1798. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1799. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1800. return [
  1801. "url" => null,
  1802. "ratio" => null
  1803. ];
  1804. }
  1805. return [
  1806. "url" => $url,
  1807. "ratio" => "16:9"
  1808. ];
  1809. }
  1810. private function limitstrlen($text){
  1811. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1812. }
  1813. private function limitwhitespace($text){
  1814. return
  1815. preg_replace(
  1816. '/[\s]+/',
  1817. " ",
  1818. $text
  1819. );
  1820. }
  1821. private function titledots($title){
  1822. $substr = substr($title, -3);
  1823. if(
  1824. $substr == "..." ||
  1825. $substr == "…"
  1826. ){
  1827. return trim(substr($title, 0, -3));
  1828. }
  1829. return trim($title);
  1830. }
  1831. private function unshiturl($url){
  1832. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1833. $tmp = explode("aHR0", $url);
  1834. if(count($tmp) !== 2){
  1835. // nothing to do
  1836. return $url;
  1837. }
  1838. return
  1839. base64_decode(
  1840. "aHR0" .
  1841. str_replace(["/", "_"], ["", "/"],
  1842. explode(
  1843. ".",
  1844. $tmp[1]
  1845. )[0]
  1846. )
  1847. );
  1848. }
  1849. }