brave.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  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 title + url
  965. */
  966. $title =
  967. $this->fuckhtml
  968. ->getElementsByClassName("infobox-title", "a");
  969. if(count($title) !== 0){
  970. $answer["title"] =
  971. $this->fuckhtml
  972. ->getTextContent(
  973. $title[0]
  974. );
  975. $answer["url"] =
  976. $this->fuckhtml
  977. ->getTextContent(
  978. $title[0]["attributes"]["href"]
  979. );
  980. }
  981. /*
  982. Get thumbnail
  983. */
  984. $thumb = $this->getimagelinkfromstyle("thumb");
  985. if($thumb["url"] !== null){
  986. $answer["thumb"] = $thumb["url"];
  987. }
  988. /*
  989. Get table
  990. */
  991. $title =
  992. $this->fuckhtml
  993. ->getElementsByClassName(
  994. "infobox-attr-header",
  995. "div"
  996. );
  997. $rowhtml = $infobox;
  998. if(count($title) >= 2){
  999. $rowhtml =
  1000. explode(
  1001. $title[1]["outerHTML"],
  1002. $infobox["innerHTML"],
  1003. 2
  1004. )[0];
  1005. }
  1006. $this->fuckhtml->load($rowhtml);
  1007. $rows =
  1008. $this->fuckhtml
  1009. ->getElementsByClassName("infobox-attr", "div");
  1010. foreach($rows as $row){
  1011. if(!isset($row["innerHTML"])){
  1012. continue;
  1013. }
  1014. $this->fuckhtml->load($row);
  1015. $span =
  1016. $this->fuckhtml
  1017. ->getElementsByTagName("span");
  1018. if(count($span) === 2){
  1019. $answer["table"][
  1020. $this->fuckhtml->getTextContent($span[0])
  1021. ] = str_replace("\n", ", ", $this->fuckhtml->getTextContent($span[1], true));
  1022. }
  1023. }
  1024. $this->fuckhtml->load($infobox);
  1025. /*
  1026. Parse stackoverflow answers
  1027. */
  1028. $code =
  1029. $this->fuckhtml
  1030. ->getElementById("codebox-answer", $div);
  1031. if($code){
  1032. // this might be standalone text with no paragraphs, check for that
  1033. $author =
  1034. $this->fuckhtml
  1035. ->getElementById("author");
  1036. $desc_tmp =
  1037. str_replace(
  1038. $author["outerHTML"],
  1039. "",
  1040. $code["innerHTML"]
  1041. );
  1042. $this->fuckhtml->load($desc_tmp);
  1043. $code =
  1044. $this->fuckhtml
  1045. ->getElementsByTagName("*");
  1046. if(count($code) === 0){
  1047. $answer["description"] =
  1048. [
  1049. [
  1050. "type" => "text",
  1051. "value" =>
  1052. $this->fuckhtml
  1053. ->getTextContent(
  1054. $desc_tmp
  1055. )
  1056. ],
  1057. [
  1058. "type" => "quote",
  1059. "value" =>
  1060. $this->fuckhtml
  1061. ->getTextContent(
  1062. $author
  1063. )
  1064. ]
  1065. ];
  1066. }else{
  1067. $text = [];
  1068. $i = 0;
  1069. foreach($code as $snippet){
  1070. switch($snippet["tagName"]){
  1071. case "p":
  1072. $this->fuckhtml->load($snippet["innerHTML"]);
  1073. $codetags =
  1074. $this->fuckhtml
  1075. ->getElementsByTagName("*");
  1076. $tmphtml = $snippet["innerHTML"];
  1077. foreach($codetags as $tag){
  1078. if(!isset($tag["outerHTML"])){
  1079. continue;
  1080. }
  1081. $tmphtml =
  1082. explode(
  1083. $tag["outerHTML"],
  1084. $tmphtml,
  1085. 2
  1086. );
  1087. $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
  1088. $this->appendtext($value, $text, $i);
  1089. $type = null;
  1090. switch($tag["tagName"]){
  1091. case "code": $type = "inline_code"; break;
  1092. case "em": $type = "italic"; break;
  1093. case "blockquote": $type = "quote"; break;
  1094. default: $type = "text";
  1095. }
  1096. if($type !== null){
  1097. $value = $this->fuckhtml->getTextContent($tag, false, true);
  1098. if(trim($value) != ""){
  1099. if(
  1100. $i !== 0 &&
  1101. $type == "title"
  1102. ){
  1103. $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
  1104. }
  1105. $text[] = [
  1106. "type" => $type,
  1107. "value" => $value
  1108. ];
  1109. $i++;
  1110. }
  1111. }
  1112. if(count($tmphtml) === 2){
  1113. $tmphtml = $tmphtml[1];
  1114. }else{
  1115. break;
  1116. }
  1117. }
  1118. if(is_array($tmphtml)){
  1119. $tmphtml = $tmphtml[0];
  1120. }
  1121. if(strlen($tmphtml) !== 0){
  1122. $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
  1123. $this->appendtext($value, $text, $i);
  1124. }
  1125. break;
  1126. case "pre":
  1127. switch($text[$i - 1]["type"]){
  1128. case "text":
  1129. case "italic":
  1130. $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
  1131. break;
  1132. }
  1133. $text[] =
  1134. [
  1135. "type" => "code",
  1136. "value" =>
  1137. rtrim(
  1138. $this->fuckhtml
  1139. ->getTextContent(
  1140. $snippet,
  1141. true,
  1142. false
  1143. )
  1144. )
  1145. ];
  1146. $i++;
  1147. break;
  1148. case "ol":
  1149. $o = 0;
  1150. $this->fuckhtml->load($snippet);
  1151. $li =
  1152. $this->fuckhtml
  1153. ->getElementsByTagName("li");
  1154. foreach($li as $elem){
  1155. $o++;
  1156. $this->appendtext(
  1157. $o . ". " .
  1158. $this->fuckhtml
  1159. ->getTextContent(
  1160. $elem
  1161. ),
  1162. $text,
  1163. $i
  1164. );
  1165. }
  1166. break;
  1167. }
  1168. }
  1169. if(
  1170. $i !== 0 &&
  1171. $text[$i - 1]["type"] == "text"
  1172. ){
  1173. $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
  1174. }
  1175. if($author){
  1176. $text[] = [
  1177. "type" => "quote",
  1178. "value" => $this->fuckhtml->getTextContent($author)
  1179. ];
  1180. }
  1181. $answer["description"] = $text;
  1182. }
  1183. }else{
  1184. /*
  1185. Get normal description
  1186. */
  1187. $description =
  1188. $this->fuckhtml
  1189. ->getElementsByClassName(
  1190. "mb-6",
  1191. "div"
  1192. );
  1193. if(count($description) !== 0){
  1194. $description =
  1195. [
  1196. [
  1197. "type" => "text",
  1198. "value" =>
  1199. $this->titledots(
  1200. preg_replace(
  1201. '/ Wikipedia$/',
  1202. "",
  1203. $this->fuckhtml
  1204. ->getTextContent(
  1205. $description[0]
  1206. )
  1207. )
  1208. )
  1209. ]
  1210. ];
  1211. $ratings =
  1212. $this->fuckhtml
  1213. ->getElementById("ratings");
  1214. if($ratings){
  1215. $this->fuckhtml->load($ratings);
  1216. $ratings =
  1217. $this->fuckhtml
  1218. ->getElementsByClassName(
  1219. "flex-hcenter mb-10",
  1220. "div"
  1221. );
  1222. $description[] = [
  1223. "type" => "title",
  1224. "value" => "Ratings"
  1225. ];
  1226. foreach($ratings as $rating){
  1227. $this->fuckhtml->load($rating);
  1228. $num =
  1229. $this->fuckhtml
  1230. ->getTextContent(
  1231. $this->fuckhtml
  1232. ->getElementsByClassName(
  1233. "r-num",
  1234. "div"
  1235. )[0]
  1236. );
  1237. $href =
  1238. $this->fuckhtml
  1239. ->getElementsByClassName(
  1240. "mr-10",
  1241. "a"
  1242. )[0];
  1243. $votes =
  1244. $this->fuckhtml
  1245. ->getTextContent(
  1246. $this->fuckhtml
  1247. ->getElementsByClassName(
  1248. "text-sm",
  1249. "span"
  1250. )[0]
  1251. );
  1252. $c = count($description) - 1;
  1253. if(
  1254. $c !== -1 &&
  1255. $description[$c]["type"] == "text"
  1256. ){
  1257. $description[$c]["value"] .= $num . " ";
  1258. }else{
  1259. $description[] = [
  1260. "type" => "text",
  1261. "value" => $num . " "
  1262. ];
  1263. }
  1264. $description[] = [
  1265. "type" => "link",
  1266. "value" => $this->fuckhtml->getTextContent($href),
  1267. "url" => $this->fuckhtml->getTextContent($href["attributes"]["href"])
  1268. ];
  1269. $description[] = [
  1270. "type" => "text",
  1271. "value" => " (" . $votes . ")\n"
  1272. ];
  1273. }
  1274. }
  1275. $answer["description"] = $description;
  1276. }
  1277. }
  1278. /*
  1279. Get sublinks
  1280. */
  1281. $this->fuckhtml->load($infobox);
  1282. $profiles =
  1283. $this->fuckhtml
  1284. ->getElementById("profiles");
  1285. if($profiles){
  1286. $profiles =
  1287. $this->fuckhtml
  1288. ->getElementsByClassName(
  1289. "chip",
  1290. "a"
  1291. );
  1292. foreach($profiles as $profile){
  1293. $name = $this->fuckhtml->getTextContent($profile["attributes"]["title"]);
  1294. if(strtolower($name) == "steampowered"){
  1295. $name = "Steam";
  1296. }
  1297. $answer["sublink"][$name] =
  1298. $this->fuckhtml->getTextContent($profile["attributes"]["href"]);
  1299. }
  1300. }
  1301. $actors =
  1302. $this->fuckhtml
  1303. ->getElementById("panel-movie-cast");
  1304. if($actors){
  1305. $this->fuckhtml->load($actors);
  1306. $actors =
  1307. $this->fuckhtml
  1308. ->getElementsByClassName("card");
  1309. $answer["description"][] = [
  1310. "type" => "title",
  1311. "value" => "Cast"
  1312. ];
  1313. foreach($actors as $actor){
  1314. $this->fuckhtml->load($actor);
  1315. $answer["description"][] = [
  1316. "type" => "text",
  1317. "value" =>
  1318. $this->fuckhtml
  1319. ->getTextContent(
  1320. $this->fuckhtml
  1321. ->getElementsByClassName("card-body")
  1322. [0]
  1323. )
  1324. ];
  1325. $answer["description"][] = [
  1326. "type" => "image",
  1327. "url" => $this->getimagelinkfromstyle("person-thumb")["url"]
  1328. ];
  1329. }
  1330. }
  1331. $out["answer"][] = $answer;
  1332. }
  1333. /*
  1334. Get actor standalone thingy
  1335. */
  1336. $this->fuckhtml->load($resulthtml);
  1337. $actors =
  1338. $this->fuckhtml
  1339. ->getElementById("predicate-entity");
  1340. if($actors){
  1341. $this->fuckhtml->load($actors);
  1342. $cards =
  1343. $this->fuckhtml
  1344. ->getElementsByClassName("card");
  1345. $url =
  1346. $this->fuckhtml
  1347. ->getElementsByClassName(
  1348. "disclaimer",
  1349. "div"
  1350. )[0];
  1351. $this->fuckhtml->load($url);
  1352. $url =
  1353. $this->fuckhtml
  1354. ->getTextContent(
  1355. $this->fuckhtml
  1356. ->getElementsByTagName("a")
  1357. [0]
  1358. ["attributes"]
  1359. ["href"]
  1360. );
  1361. $this->fuckhtml->load($actors);
  1362. $answer = [
  1363. "title" =>
  1364. $this->fuckhtml
  1365. ->getTextContent(
  1366. $this->fuckhtml
  1367. ->getElementsByClassName(
  1368. "entity",
  1369. "span"
  1370. )[0]
  1371. ) . " (Cast)",
  1372. "description" => [],
  1373. "url" => $url,
  1374. "sublink" => [],
  1375. "thumb" => null,
  1376. "table" => []
  1377. ];
  1378. foreach($cards as $card){
  1379. $this->fuckhtml->load($card);
  1380. $answer["description"][] = [
  1381. "type" => "title",
  1382. "value" =>
  1383. $this->fuckhtml
  1384. ->getTextContent(
  1385. $this->fuckhtml
  1386. ->getElementsByClassName(
  1387. "title"
  1388. )[0]
  1389. )
  1390. ];
  1391. $answer["description"][] = [
  1392. "type" => "text",
  1393. "value" =>
  1394. $this->fuckhtml
  1395. ->getTextContent(
  1396. $this->fuckhtml
  1397. ->getElementsByClassName(
  1398. "text-xs desc"
  1399. )[0]
  1400. )
  1401. ];
  1402. $answer["description"][] = [
  1403. "type" => "image",
  1404. "url" => $this->getimagelinkfromstyle("img-bg")["url"]
  1405. ];
  1406. }
  1407. $out["answer"][] = $answer;
  1408. }
  1409. return $out;
  1410. }
  1411. public function news($get){
  1412. $search = $get["s"];
  1413. if(strlen($search) === 0){
  1414. throw new Exception("Search term is empty!");
  1415. }
  1416. $nsfw = $get["nsfw"];
  1417. $country = $get["country"];
  1418. if(strlen($search) > 2048){
  1419. throw new Exception("Search query is too long!");
  1420. }
  1421. /*
  1422. $handle = fopen("scraper/brave-news.html", "r");
  1423. $html = fread($handle, filesize("scraper/brave-news.html"));
  1424. fclose($handle);*/
  1425. try{
  1426. $html =
  1427. $this->get(
  1428. "https://search.brave.com/news",
  1429. [
  1430. "q" => $search
  1431. ],
  1432. $nsfw,
  1433. $country
  1434. );
  1435. }catch(Exception $error){
  1436. throw new Exception("Could not fetch search page");
  1437. }
  1438. $out = [
  1439. "status" => "ok",
  1440. "npt" => null,
  1441. "news" => []
  1442. ];
  1443. // load html
  1444. $this->fuckhtml->load($html);
  1445. $news =
  1446. $this->fuckhtml
  1447. ->getElementsByClassName(
  1448. "snippet inline gap-standard",
  1449. "div"
  1450. );
  1451. foreach($news as $article){
  1452. $data = [
  1453. "title" => null,
  1454. "author" => null,
  1455. "description" => null,
  1456. "date" => null,
  1457. "thumb" =>
  1458. [
  1459. "url" => null,
  1460. "ratio" => null
  1461. ],
  1462. "url" => null
  1463. ];
  1464. $this->fuckhtml->load($article);
  1465. $elems =
  1466. $this->fuckhtml
  1467. ->getElementsByTagName("*");
  1468. // get title
  1469. $data["title"] =
  1470. $this->fuckhtml
  1471. ->getTextContent(
  1472. $this->fuckhtml
  1473. ->getElementsByClassName(
  1474. "snippet-title",
  1475. $elems
  1476. )
  1477. [0]
  1478. ["innerHTML"]
  1479. );
  1480. // get description
  1481. $data["description"] =
  1482. $this->titledots(
  1483. $this->fuckhtml
  1484. ->getTextContent(
  1485. $this->fuckhtml
  1486. ->getElementsByClassName(
  1487. "snippet-description",
  1488. $elems
  1489. )
  1490. [0]
  1491. ["innerHTML"]
  1492. )
  1493. );
  1494. // get date
  1495. $date =
  1496. explode(
  1497. "•",
  1498. $this->fuckhtml
  1499. ->getTextContent(
  1500. $this->fuckhtml
  1501. ->getElementsByClassName(
  1502. "snippet-url",
  1503. $elems
  1504. )[0]
  1505. )
  1506. );
  1507. if(
  1508. count($date) !== 1 &&
  1509. trim($date[1]) != ""
  1510. ){
  1511. $data["date"] =
  1512. strtotime(
  1513. $date[1]
  1514. );
  1515. }
  1516. // get URL
  1517. $data["url"] =
  1518. $this->fuckhtml->getTextContent(
  1519. $this->unshiturl(
  1520. $this->fuckhtml
  1521. ->getElementsByClassName(
  1522. "result-header",
  1523. $elems
  1524. )
  1525. [0]
  1526. ["attributes"]
  1527. ["href"]
  1528. )
  1529. );
  1530. // get thumbnail
  1531. $thumb =
  1532. $this->fuckhtml
  1533. ->getElementsByTagName(
  1534. "img"
  1535. );
  1536. if(
  1537. count($thumb) === 2 &&
  1538. trim(
  1539. $thumb[1]
  1540. ["attributes"]
  1541. ["src"]
  1542. ) != ""
  1543. ){
  1544. $data["thumb"] = [
  1545. "url" =>
  1546. $this->fuckhtml->getTextContent(
  1547. $this->unshiturl(
  1548. $thumb[1]
  1549. ["attributes"]
  1550. ["src"]
  1551. )
  1552. ),
  1553. "ratio" => "16:9"
  1554. ];
  1555. }
  1556. $out["news"][] = $data;
  1557. }
  1558. return $out;
  1559. }
  1560. public function image($get){
  1561. $search = $get["s"];
  1562. $country = $get["country"];
  1563. $nsfw = $get["nsfw"];
  1564. $out = [
  1565. "status" => "ok",
  1566. "npt" => null,
  1567. "image" => []
  1568. ];
  1569. try{
  1570. $html =
  1571. $this->get(
  1572. "https://search.brave.com/images",
  1573. [
  1574. "q" => $search
  1575. ],
  1576. $nsfw,
  1577. $country
  1578. );
  1579. }catch(Exception $error){
  1580. throw new Exception("Could not fetch search page");
  1581. }
  1582. /*
  1583. $handle = fopen("scraper/brave-image.html", "r");
  1584. $html = fread($handle, filesize("scraper/brave-image.html"));
  1585. fclose($handle);*/
  1586. preg_match(
  1587. '/const data = (\[{.*}\]);/',
  1588. $html,
  1589. $json
  1590. );
  1591. if(!isset($json[1])){
  1592. throw new Exception("Failed to get data object");
  1593. }
  1594. $json =
  1595. $this->fuckhtml
  1596. ->parseJsObject(
  1597. $json[1]
  1598. );
  1599. foreach(
  1600. $json[1]
  1601. ["data"]
  1602. ["body"]
  1603. ["response"]
  1604. ["results"]
  1605. as $result
  1606. ){
  1607. $out["image"][] = [
  1608. "title" => $result["title"],
  1609. "source" => [
  1610. [
  1611. "url" => $result["properties"]["url"],
  1612. "width" => null,
  1613. "height" => null
  1614. ],
  1615. [
  1616. "url" => $result["thumbnail"]["src"],
  1617. "width" => null,
  1618. "height" => null
  1619. ]
  1620. ],
  1621. "url" => $result["url"]
  1622. ];
  1623. }
  1624. return $out;
  1625. }
  1626. public function video($get){
  1627. $search = $get["s"];
  1628. $country = $get["country"];
  1629. $nsfw = $get["nsfw"];
  1630. $out = [
  1631. "status" => "ok",
  1632. "npt" => null,
  1633. "video" => [],
  1634. "author" => [],
  1635. "livestream" => [],
  1636. "playlist" => [],
  1637. "reel" => []
  1638. ];
  1639. try{
  1640. $html =
  1641. $this->get(
  1642. "https://search.brave.com/videos",
  1643. [
  1644. "q" => $search
  1645. ],
  1646. $nsfw,
  1647. $country
  1648. );
  1649. }catch(Exception $error){
  1650. throw new Exception("Could not fetch search page");
  1651. }
  1652. /*
  1653. $handle = fopen("scraper/brave-video.html", "r");
  1654. $html = fread($handle, filesize("scraper/brave-video.html"));
  1655. fclose($handle);*/
  1656. preg_match(
  1657. '/const data = (\[{.*}\]);/',
  1658. $html,
  1659. $json
  1660. );
  1661. if(!isset($json[1])){
  1662. throw new Exception("Failed to get data object");
  1663. }
  1664. $json =
  1665. $this->fuckhtml
  1666. ->parseJsObject(
  1667. $json[1]
  1668. );
  1669. foreach(
  1670. $json
  1671. [1]
  1672. ["data"]
  1673. ["body"]
  1674. ["response"]
  1675. ["results"]
  1676. as $result
  1677. ){
  1678. if($result["video"]["author"] != "null"){
  1679. $author = [
  1680. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1681. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1682. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1683. ];
  1684. }else{
  1685. $author = [
  1686. "name" => null,
  1687. "url" => null,
  1688. "avatar" => null
  1689. ];
  1690. }
  1691. if($result["thumbnail"] != "null"){
  1692. $thumb = [
  1693. "url" => $result["thumbnail"]["original"],
  1694. "ratio" => "16:9"
  1695. ];
  1696. }else{
  1697. $thumb = [
  1698. "url" => null,
  1699. "ratio" => null
  1700. ];
  1701. }
  1702. $out["video"][] = [
  1703. "title" => $result["title"],
  1704. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1705. "author" => $author,
  1706. "date" => $result["age"] == "null" ? null : strtotime($result["age"]),
  1707. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1708. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1709. "thumb" => $thumb,
  1710. "url" => $result["url"]
  1711. ];
  1712. }
  1713. return $out;
  1714. }
  1715. private function hms2int($time){
  1716. $parts = explode(":", $time, 3);
  1717. $time = 0;
  1718. if(count($parts) === 3){
  1719. // hours
  1720. $time = $time + ((int)$parts[0] * 3600);
  1721. array_shift($parts);
  1722. }
  1723. if(count($parts) === 2){
  1724. // minutes
  1725. $time = $time + ((int)$parts[0] * 60);
  1726. array_shift($parts);
  1727. }
  1728. // seconds
  1729. $time = $time + (int)$parts[0];
  1730. return $time;
  1731. }
  1732. private function appendtext($payload, &$text, &$index){
  1733. if(trim($payload) == ""){
  1734. return;
  1735. }
  1736. if(
  1737. $index !== 0 &&
  1738. $text[$index - 1]["type"] == "text"
  1739. ){
  1740. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1741. }else{
  1742. $text[] = [
  1743. "type" => "text",
  1744. "value" => preg_replace('/ $/', " ", $payload)
  1745. ];
  1746. $index++;
  1747. }
  1748. }
  1749. private function tablesublink($html_collection, &$data){
  1750. foreach($html_collection as $html){
  1751. $html["innerHTML"] = preg_replace(
  1752. '/<style>[\S\s]*<\/style>/i',
  1753. "",
  1754. $html["innerHTML"]
  1755. );
  1756. $html =
  1757. explode(
  1758. ":",
  1759. $this->fuckhtml->getTextContent($html),
  1760. 2
  1761. );
  1762. if(count($html) === 1){
  1763. $html = ["Rating", $html[0]];
  1764. }
  1765. $data["table"][trim($html[0])] = trim($html[1]);
  1766. }
  1767. }
  1768. private function getimagelinkfromstyle($thumb){
  1769. $thumb =
  1770. $this->fuckhtml
  1771. ->getElementsByClassName(
  1772. $thumb,
  1773. "div"
  1774. );
  1775. if(count($thumb) === 0){
  1776. return [
  1777. "url" => null,
  1778. "ratio" => null
  1779. ];
  1780. }
  1781. $thumb = $thumb[0]["attributes"]["style"];
  1782. preg_match(
  1783. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1784. $thumb,
  1785. $thumb
  1786. );
  1787. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1788. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1789. return [
  1790. "url" => null,
  1791. "ratio" => null
  1792. ];
  1793. }
  1794. return [
  1795. "url" => $url,
  1796. "ratio" => "16:9"
  1797. ];
  1798. }
  1799. private function limitstrlen($text){
  1800. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1801. }
  1802. private function limitwhitespace($text){
  1803. return
  1804. preg_replace(
  1805. '/[\s]+/',
  1806. " ",
  1807. $text
  1808. );
  1809. }
  1810. private function titledots($title){
  1811. $substr = substr($title, -3);
  1812. if(
  1813. $substr == "..." ||
  1814. $substr == "…"
  1815. ){
  1816. return trim(substr($title, 0, -3));
  1817. }
  1818. return trim($title);
  1819. }
  1820. private function unshiturl($url){
  1821. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1822. $tmp = explode("aHR0", $url);
  1823. if(count($tmp) !== 2){
  1824. // nothing to do
  1825. return $url;
  1826. }
  1827. return
  1828. base64_decode(
  1829. "aHR0" .
  1830. str_replace(["/", "_"], ["", "/"],
  1831. explode(
  1832. ".",
  1833. $tmp[1]
  1834. )[0]
  1835. )
  1836. );
  1837. }
  1838. }