brave.php 44 KB

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