1
0

brave.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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. print_r($result);
  1608. $out["image"][] = [
  1609. "title" => $result["title"],
  1610. "source" => [
  1611. [
  1612. "url" => $result["properties"]["url"],
  1613. "width" => null,
  1614. "height" => null
  1615. ],
  1616. [
  1617. "url" => $result["thumbnail"]["src"],
  1618. "width" => null,
  1619. "height" => null
  1620. ]
  1621. ],
  1622. "url" => $result["url"]
  1623. ];
  1624. }
  1625. return $out;
  1626. }
  1627. public function video($get){
  1628. $search = $get["s"];
  1629. $country = $get["country"];
  1630. $nsfw = $get["nsfw"];
  1631. $out = [
  1632. "status" => "ok",
  1633. "npt" => null,
  1634. "video" => [],
  1635. "author" => [],
  1636. "livestream" => [],
  1637. "playlist" => [],
  1638. "reel" => []
  1639. ];
  1640. try{
  1641. $html =
  1642. $this->get(
  1643. "https://search.brave.com/videos",
  1644. [
  1645. "q" => $search
  1646. ],
  1647. $nsfw,
  1648. $country
  1649. );
  1650. }catch(Exception $error){
  1651. throw new Exception("Could not fetch search page");
  1652. }
  1653. /*
  1654. $handle = fopen("scraper/brave-video.html", "r");
  1655. $html = fread($handle, filesize("scraper/brave-video.html"));
  1656. fclose($handle);*/
  1657. preg_match(
  1658. '/const data = (\[{.*}\]);/',
  1659. $html,
  1660. $json
  1661. );
  1662. if(!isset($json[1])){
  1663. throw new Exception("Failed to get data object");
  1664. }
  1665. $json =
  1666. $this->fuckhtml
  1667. ->parseJsObject(
  1668. $json[1]
  1669. );
  1670. foreach(
  1671. $json
  1672. [1]
  1673. ["data"]
  1674. ["body"]
  1675. ["response"]
  1676. ["results"]
  1677. as $result
  1678. ){
  1679. if($result["video"]["author"] != "null"){
  1680. $author = [
  1681. "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
  1682. "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
  1683. "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
  1684. ];
  1685. }else{
  1686. $author = [
  1687. "name" => null,
  1688. "url" => null,
  1689. "avatar" => null
  1690. ];
  1691. }
  1692. if($result["thumbnail"] != "null"){
  1693. $thumb = [
  1694. "url" => $result["thumbnail"]["original"],
  1695. "ratio" => "16:9"
  1696. ];
  1697. }else{
  1698. $thumb = [
  1699. "url" => null,
  1700. "ratio" => null
  1701. ];
  1702. }
  1703. $out["video"][] = [
  1704. "title" => $result["title"],
  1705. "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
  1706. "author" => $author,
  1707. "date" => $result["age"] == "null" ? null : strtotime($result["age"]),
  1708. "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
  1709. "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
  1710. "thumb" => $thumb,
  1711. "url" => $result["url"]
  1712. ];
  1713. }
  1714. return $out;
  1715. }
  1716. private function hms2int($time){
  1717. $parts = explode(":", $time, 3);
  1718. $time = 0;
  1719. if(count($parts) === 3){
  1720. // hours
  1721. $time = $time + ((int)$parts[0] * 3600);
  1722. array_shift($parts);
  1723. }
  1724. if(count($parts) === 2){
  1725. // minutes
  1726. $time = $time + ((int)$parts[0] * 60);
  1727. array_shift($parts);
  1728. }
  1729. // seconds
  1730. $time = $time + (int)$parts[0];
  1731. return $time;
  1732. }
  1733. private function appendtext($payload, &$text, &$index){
  1734. if(trim($payload) == ""){
  1735. return;
  1736. }
  1737. if(
  1738. $index !== 0 &&
  1739. $text[$index - 1]["type"] == "text"
  1740. ){
  1741. $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
  1742. }else{
  1743. $text[] = [
  1744. "type" => "text",
  1745. "value" => preg_replace('/ $/', " ", $payload)
  1746. ];
  1747. $index++;
  1748. }
  1749. }
  1750. private function tablesublink($html_collection, &$data){
  1751. foreach($html_collection as $html){
  1752. $html["innerHTML"] = preg_replace(
  1753. '/<style>[\S\s]*<\/style>/i',
  1754. "",
  1755. $html["innerHTML"]
  1756. );
  1757. $html =
  1758. explode(
  1759. ":",
  1760. $this->fuckhtml->getTextContent($html),
  1761. 2
  1762. );
  1763. if(count($html) === 1){
  1764. $html = ["Rating", $html[0]];
  1765. }
  1766. $data["table"][trim($html[0])] = trim($html[1]);
  1767. }
  1768. }
  1769. private function getimagelinkfromstyle($thumb){
  1770. $thumb =
  1771. $this->fuckhtml
  1772. ->getElementsByClassName(
  1773. $thumb,
  1774. "div"
  1775. );
  1776. if(count($thumb) === 0){
  1777. return [
  1778. "url" => null,
  1779. "ratio" => null
  1780. ];
  1781. }
  1782. $thumb = $thumb[0]["attributes"]["style"];
  1783. preg_match(
  1784. '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
  1785. $thumb,
  1786. $thumb
  1787. );
  1788. $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
  1789. if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
  1790. return [
  1791. "url" => null,
  1792. "ratio" => null
  1793. ];
  1794. }
  1795. return [
  1796. "url" => $url,
  1797. "ratio" => "16:9"
  1798. ];
  1799. }
  1800. private function limitstrlen($text){
  1801. return explode("\n", wordwrap($text, 300, "\n"))[0];
  1802. }
  1803. private function limitwhitespace($text){
  1804. return
  1805. preg_replace(
  1806. '/[\s]+/',
  1807. " ",
  1808. $text
  1809. );
  1810. }
  1811. private function titledots($title){
  1812. $substr = substr($title, -3);
  1813. if(
  1814. $substr == "..." ||
  1815. $substr == "…"
  1816. ){
  1817. return trim(substr($title, 0, -3));
  1818. }
  1819. return trim($title);
  1820. }
  1821. private function unshiturl($url){
  1822. // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
  1823. $tmp = explode("aHR0", $url);
  1824. if(count($tmp) !== 2){
  1825. // nothing to do
  1826. return $url;
  1827. }
  1828. return
  1829. base64_decode(
  1830. "aHR0" .
  1831. str_replace(["/", "_"], ["", "/"],
  1832. explode(
  1833. ".",
  1834. $tmp[1]
  1835. )[0]
  1836. )
  1837. );
  1838. }
  1839. }