brave.php 42 KB

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