| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284 |
- <?php
- class brave{
-
- public function __construct(){
-
- include "lib/fuckhtml.php";
- $this->fuckhtml = new fuckhtml();
-
- include "lib/nextpage.php";
- $this->nextpage = new nextpage("brave");
- }
-
- public function getfilters($page){
-
- switch($page){
-
- case "web":
- return [
- "country" => [
- "display" => "Country",
- "option" => [
- "all" => "All Regions",
- "ar" => "Argentina",
- "au" => "Australia",
- "at" => "Austria",
- "be" => "Belgium",
- "br" => "Brazil",
- "ca" => "Canada",
- "cl" => "Chile",
- "cn" => "China",
- "dk" => "Denmark",
- "fi" => "Finland",
- "fr" => "France",
- "de" => "Germany",
- "hk" => "Hong Kong",
- "in" => "India",
- "id" => "Indonesia",
- "it" => "Italy",
- "jp" => "Japan",
- "kr" => "Korea",
- "my" => "Malaysia",
- "mx" => "Mexico",
- "nl" => "Netherlands",
- "nz" => "New Zealand",
- "no" => "Norway",
- "pl" => "Poland",
- "pt" => "Portugal",
- "ph" => "Philippines",
- "ru" => "Russia",
- "sa" => "Saudi Arabia",
- "za" => "South Africa",
- "es" => "Spain",
- "se" => "Sweden",
- "ch" => "Switzerland",
- "tw" => "Taiwan",
- "tr" => "Turkey",
- "gb" => "United Kingdom",
- "us" => "United States"
- ]
- ],
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "maybe" => "Maybe",
- "no" => "No"
- ]
- ],
- "newer" => [
- "display" => "Newer than",
- "option" => "_DATE"
- ],
- "older" => [
- "display" => "Older than",
- "option" => "_DATE"
- ]
- ];
- break;
-
- case "images":
- case "videos":
- case "news":
- return [
- "country" => [
- "display" => "Country",
- "option" => [
- "all" => "All regions",
- "ar" => "Argentina",
- "au" => "Australia",
- "at" => "Austria",
- "be" => "Belgium",
- "br" => "Brazil",
- "ca" => "Canada",
- "cl" => "Chile",
- "cn" => "China",
- "dk" => "Denmark",
- "fi" => "Finland",
- "fr" => "France",
- "de" => "Germany",
- "hk" => "Hong Kong",
- "in" => "India",
- "id" => "Indonesia",
- "it" => "Italy",
- "jp" => "Japan",
- "kr" => "Korea",
- "my" => "Malaysia",
- "mx" => "Mexico",
- "nl" => "Netherlands",
- "nz" => "New Zealand",
- "no" => "Norway",
- "pl" => "Poland",
- "pt" => "Portugal",
- "ph" => "Philippines",
- "ru" => "Russia",
- "sa" => "Saudi Arabia",
- "za" => "South Africa",
- "es" => "Spain",
- "se" => "Sweden",
- "ch" => "Switzerland",
- "tw" => "Taiwan",
- "tr" => "Turkey",
- "gb" => "United Kingdom",
- "us" => "United States"
- ]
- ],
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "maybe" => "Maybe",
- "no" => "No"
- ]
- ]
- ];
- break;
- }
- }
-
- private function get($url, $get = [], $nsfw, $country){
-
- switch($nsfw){
-
- case "yes": $nsfw = "off"; break;
- case "maybe": $nsfw = "moderate"; break;
- case "no": $nsfw = "strict"; break;
- }
-
- if($country == "any"){
-
- $country = "all";
- }
-
- $headers = [
- "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/110.0",
- "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
- "Accept-Language: en-US,en;q=0.5",
- "Accept-Encoding: gzip",
- "Cookie: safesearch={$nsfw}; country={$country}; useLocation=0; summarizer=0",
- "DNT: 1",
- "Connection: keep-alive",
- "Upgrade-Insecure-Requests: 1",
- "Sec-Fetch-Dest: document",
- "Sec-Fetch-Mode: navigate",
- "Sec-Fetch-Site: none",
- "Sec-Fetch-User: ?1"
- ];
-
- $curlproc = curl_init();
-
- if($get !== []){
- $get = http_build_query($get);
- $url .= "?" . $get;
- }
-
- curl_setopt($curlproc, CURLOPT_URL, $url);
-
- curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
- curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
-
- curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
-
- $data = curl_exec($curlproc);
-
- if(curl_errno($curlproc)){
-
- throw new Exception(curl_error($curlproc));
- }
-
- curl_close($curlproc);
- return $data;
- }
-
- public function web($get){
-
- if($get["npt"]){
-
- // get next page data
- $q = json_decode($this->nextpage->get($get["npt"], "web"), true);
-
- $search = $q["q"];
- $q["spellcheck"] = 0;
-
- $nsfw = $q["nsfw"];
- unset($q["nsfw"]);
-
- $country = $q["country"];
- unset($q["country"]);
-
- }else{
-
- // get _GET data instead
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- if(strlen($search) > 2048){
-
- throw new Exception("Search query is too long!");
- }
-
- $nsfw = $get["nsfw"];
- $country = $get["country"];
- $older = $get["older"];
- $newer = $get["newer"];
-
- $q = [
- "q" => $search
- ];
-
- /*
- Pass older/newer filters to brave
- */
- if($newer !== false){
-
- $newer = date("Y-m-d", $newer);
-
- if($older === false){
-
- $older = date("Y-m-d", time());
- }
- }
-
- if(
- is_string($older) === false &&
- $older !== false
- ){
-
- $older = date("Y-m-d", $older);
-
- if($newer === false){
-
- $newer = "1970-01-02";
- }
- }
-
- if($older !== false){
-
- $q["tf"] = "{$newer}to{$older}";
- }
- }
- /*
- $handle = fopen("scraper/brave.html", "r");
- $html = fread($handle, filesize("scraper/brave.html"));
- fclose($handle);
- */
- try{
- $html =
- $this->get(
- "https://search.brave.com/search",
- $q,
- $nsfw,
- $country
- );
-
- }catch(Exception $error){
-
- throw new Exception("Could not fetch search page");
- }
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- // load html
- $this->fuckhtml->load($html);
-
- /*
- Get next page "token"
- */
- $nextpage =
- $this->fuckhtml
- ->getElementsByClassName(
- "btn ml-15",
- "a"
- );
-
- if(count($nextpage) !== 0){
-
- preg_match(
- '/offset=([0-9]+)/',
- $this->fuckhtml->getTextContent($nextpage[0]["attributes"]["href"]),
- $nextpage
- );
-
- $q["offset"] = (int)$nextpage[1];
- $q["nsfw"] = $nsfw;
- $q["country"] = $country;
-
- $out["npt"] =
- $this->nextpage->store(
- json_encode($q),
- "web"
- );
- }
-
- /*
- Get discussions (and append them to web results)
- */
-
- // they're loaded using javascript!!
- $discussion =
- $this->fuckhtml
- ->getElementById(
- "js-discussions",
- "script"
- );
-
- if(
- $discussion &&
- isset($discussion["attributes"]["data"])
- ){
-
- $discussion =
- json_decode(
- $this->fuckhtml
- ->getTextContent(
- $discussion["attributes"]["data"]
- ),
- true
- );
-
- foreach($discussion["results"] as $result){
-
- $data = [
- "title" => $this->titledots($result["title"]),
- "description" => null,
- "url" => $result["url"],
- "date" => null,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => [],
- "table" => []
- ];
-
- // description
- $data["description"] =
- $this->limitstrlen(
- $this->limitwhitespace(
- $this->titledots(
- $this->fuckhtml->getTextContent(
- $result["description"]
- )
- )
- )
- );
-
- if($result["age"] != ""){
- $data["date"] = strtotime($result["age"]);
- }
-
- // populate table
-
- if($result["data"]["num_answers"] != ""){
- $data["table"]["Replies"] = (int)$result["data"]["num_answers"];
- }
-
- if($result["data"]["score"] != ""){
-
- $score = explode("|", $result["data"]["score"]);
-
- if(count($score) === 2){
-
- $score = ((int)$score[1]) . " (" . trim($score[0]) . ")";
- }else{
-
- $score = (int)$score[0];
- }
-
- $data["table"]["Votes"] = $score;
- }
-
- if($result["thumbnail"] != ""){
-
- $data["thumb"]["url"] = $result["thumbnail"];
- $data["thumb"]["ratio"] = "16:9";
- }
-
- $out["web"][] = $data;
- }
- }
-
- /*
- Get related searches
- */
- $faq =
- $this->fuckhtml
- ->getElementById("js-faq", "script");
-
- if(
- $faq &&
- isset($faq["attributes"]["data"])
- ){
-
- $faq =
- json_decode(
- $this->fuckhtml
- ->getTextContent(
- $faq["attributes"]["data"]
- ),
- true
- );
-
- foreach($faq["items"] as $related){
-
- $out["related"][] = $related["question"];
- }
- }
-
- /*
- Get spelling autocorrect
- */
- $altered =
- $this->fuckhtml
- ->getElementById("altered-query", "div");
-
- if($altered){
-
- $this->fuckhtml->load($altered);
-
- $altered =
- $this->fuckhtml
- ->getElementsByTagName("a");
-
- if(count($altered) === 2){
-
- $out["spelling"] = [
- "type" => "including",
- "using" =>
- $this->fuckhtml
- ->getTextContent($altered[0]),
- "correction" =>
- $this->fuckhtml
- ->getTextContent($altered[1])
- ];
- }
-
- $this->fuckhtml->load($html);
- }
-
- /*
- Get web results
- */
- $resulthtml =
- $this->fuckhtml
- ->getElementById(
- "results",
- "div"
- );
-
- $this->fuckhtml->load($resulthtml);
- $items = 0;
- foreach(
- $this->fuckhtml
- ->getElementsByClassName("snippet fdb")
- as $result
- ){
-
- $data = [
- "title" => null,
- "description" => null,
- "url" => null,
- "date" => null,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => [],
- "table" => []
- ];
-
- if(
- isset($result["attributes"]["data-type"]) &&
- $result["attributes"]["data-type"] == "ad"
- ){
-
- // is an ad, skip
- continue;
- }
-
- $this->fuckhtml->load($result);
-
- /*
- Get title
- */
- $title =
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-title",
- "span"
- );
-
- if(count($title) === 0){
-
- // encountered AI summarizer
- // or misspelling indicator @TODO
- continue;
- }
-
- if(isset($title[0]["attributes"]["title"])){
-
- $data["title"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $title[0]["attributes"]["title"]
- )
- );
- }else{
-
- $data["title"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $title[0]
- )
- );
- }
-
- /*
- Get description
- */
- $description =
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-description",
- "p"
- );
-
- if(count($description) !== 0){
- $data["description"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $description[0]
- )
- );
-
- // also check for thumbnail in here
- $img =
- $this->fuckhtml
- ->getElementsByClassName(
- "thumb",
- "img"
- );
-
- if(count($img) !== 0){
-
- $data["thumb"] = [
- "url" => $this->unshiturl($img[0]["attributes"]["src"]),
- "ratio" => "16:9"
- ];
- }else{
-
- // might be a video thumbnail wrapper?
- $wrapper =
- $this->fuckhtml
- ->getElementsByClassName(
- "video-thumb",
- "a"
- );
-
- if(count($wrapper) !== 0){
-
- // we found a video
- $this->fuckhtml->load($wrapper[0]);
-
- $img =
- $this->fuckhtml
- ->getElementsByTagName("img");
-
- $data["thumb"] = [
- "url" => $this->unshiturl($img[0]["attributes"]["src"]),
- "ratio" => "16:9"
- ];
-
- // get the video length, if its there
- $duration =
- $this->fuckhtml
- ->getElementsByClassName(
- "duration",
- "div"
- );
-
- if(count($duration) !== 0){
-
- $data["table"]["Duration"] = $duration[0]["innerHTML"];
- }
-
- // reset html load
- $this->fuckhtml->load($result);
- }
- }
-
- }else{
-
- // is a steam/shop listing
- $description_alt =
- $this->fuckhtml
- ->getElementsByClassName(
- "text-sm",
- "div"
- );
-
- if(count($description_alt) !== 0){
-
- switch($description_alt[0]["attributes"]["class"]){
-
- case "text-sm text-gray":
- case "description text-sm":
-
- $data["description"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $description_alt[0]
- )
- );
- break;
- }
-
- // get table sublink
- $sublink =
- $this->fuckhtml
- ->getElementsByClassName(
- "r-attr text-sm",
- "div"
- );
-
- if(count($sublink) !== 0){
-
- $this->tablesublink($sublink, $data);
- }
-
- // check for thumb element
- $data["thumb"] = $this->getimagelinkfromstyle("thumb");
- }else{
-
- // ok... finally...
- // maybe its the instant answer thingy
- $answer =
- $this->fuckhtml
- ->getElementsByClassName("answer");
-
- if(count($answer) !== 0){
-
- $data["description"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent($answer[0])
- );
- }
- }
- }
-
- // finally, fix brave's date format sucking balls
- $data["description"] = explode(" - ", $data["description"], 2);
-
- if(count($data["description"]) === 0){
-
- // nothing to do
- $data["description"] = $data["description"][0];
- }else{
-
- // attempt to parse
- $time = strtotime($data["description"][0]);
-
- if($time !== false){
-
- // got response
- $data["date"] = $time;
-
- array_shift($data["description"]);
- }
-
- // merge back
- $data["description"] =
- implode(" - ", $data["description"]);
- }
-
- /*
- Check content type
- */
- $content_type =
- $this->fuckhtml
- ->getElementsByClassName(
- "content-type",
- "span"
- );
-
- if(count($content_type) !== 0){
-
- $data["type"] =
- strtolower($this->fuckhtml->getTextContent($content_type[0]));
- }
-
- /*
- Check subtext table thingy
- */
- $table_items =
- array_merge(
- $this->fuckhtml
- ->getElementsByClassName(
- "item-attributes",
- "div"
- ),
- $this->fuckhtml
- ->getElementsByClassName(
- "r",
- "div"
- )
- );
-
- /*
- DIV: item-attributes
- */
- if(count($table_items) !== 0){
-
- foreach($table_items as $table){
-
- $this->fuckhtml->load($table);
-
- $span =
- $this->fuckhtml
- ->getElementsByClassName(
- "text-sm",
- "*"
- );
-
- foreach($span as $item){
-
- $item =
- explode(
- ":",
- $this->fuckhtml->getTextContent(preg_replace('/\n/', " ", $item["innerHTML"])),
- 2
- );
-
- if(count($item) === 2){
-
- $data["table"][trim($item[0])] = trim($this->limitwhitespace($item[1]));
- }
- }
- }
-
- $this->fuckhtml->load($result);
- }
-
- // get video sublinks
- $table_items =
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-description published-time",
- "p"
- );
-
- if(count($table_items) !== 0){
-
- $table_items =
- explode(
- '<span class="mr-15"></span>',
- $table_items[0]["innerHTML"],
- 2
- );
- if(count($table_items) === 2){
-
- $item2 = [];
-
- $item2[] = explode(":", $this->fuckhtml->getTextContent($table_items[0]));
-
- if(trim($table_items[1]) != ""){
- $item2[] = explode(":", $this->fuckhtml->getTextContent($table_items[1]));
- }
-
- foreach($item2 as $it){
-
- $data["table"][trim($it[0])] = trim($it[1]);
- }
- }
- }
-
- /*
- Get URL
- */
- $data["url"] =
- $this->fuckhtml->getTextContent(
- $this->fuckhtml
- ->getElementsByTagName("a")
- [0]
- ["attributes"]
- ["href"]
- );
-
- /*
- Get sublinks
- */
- $sublinks_elems =
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet",
- "div"
- );
-
- $sublinks = [];
-
- foreach($sublinks_elems as $sublink){
-
- $this->fuckhtml->load($sublink);
-
- $a =
- $this->fuckhtml
- ->getElementsByTagName("a")[0];
-
- $title =
- $this->fuckhtml
- ->getTextContent($a);
-
- $url = $a["attributes"]["href"];
-
- $description =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByTagName("p")[0]
- )
- );
-
- $sublinks[] = [
- "title" => $title,
- "date" => null,
- "description" => $description,
- "url" => $url
- ];
- }
-
- /*
- Get smaller sublinks
- */
- $sublinks_elems =
- $this->fuckhtml
- ->getElementsByClassName(
- "deep-link",
- "a"
- );
-
- foreach($sublinks_elems as $sublink){
-
- $sublinks[] = [
- "title" => $this->fuckhtml->getTextContent($sublink),
- "date" => null,
- "description" => null,
- "url" => $sublink["attributes"]["href"]
- ];
- }
-
- // append sublinks to $data !!
- $data["sublink"] = $sublinks;
-
- // append first result to start of $out["web"]
- // other results are after
- if($items === 0){
-
- $out["web"] = [$data, ...$out["web"]];
- }else{
-
- $out["web"][] = $data;
- }
- $items++;
- }
-
- /*
- Get news
- */
- $this->fuckhtml->load($resulthtml);
- $news_carousel = $this->fuckhtml->getElementById("news-carousel");
-
- $this->fuckhtml->load($news_carousel);
-
- if($news_carousel){
-
- $a =
- $this->fuckhtml
- ->getElementsByClassName(
- "card fdb",
- "a"
- );
-
- foreach($a as $news){
-
- $this->fuckhtml->load($news);
-
- $out["news"][] = [
- "title" =>
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "title",
- "div"
- )[0]
- )
- ),
- "description" => null,
- "date" =>
- strtotime(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "card-footer__timestamp",
- "span"
- )[0]
- )
- ),
- "thumb" => $this->getimagelinkfromstyle("img-bg"),
- "url" => $this->fuckhtml->getTextContent($news["attributes"]["href"])
- ];
- }
- }
-
-
-
- /*
- Get videos
- */
- $this->fuckhtml->load($resulthtml);
- $news_carousel = $this->fuckhtml->getElementById("video-carousel");
-
- $this->fuckhtml->load($news_carousel);
-
- if($news_carousel){
-
- $a =
- $this->fuckhtml
- ->getElementsByClassName(
- "card fdb",
- "a"
- );
-
- foreach($a as $video){
-
- $this->fuckhtml->load($video);
-
- $date = null;
-
- $date_o =
- $this->fuckhtml
- ->getElementsByClassName(
- "text-gray text-xs",
- "span"
- );
-
- if(count($date_o) !== 0){
-
- $date =
- strtotime(
- $this->fuckhtml
- ->getTextContent(
- $date_o[0]
- )
- );
- }
-
- $out["video"][] = [
- "title" =>
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "title",
- "div"
- )[0]
- )
- ),
- "description" => null,
- "date" => $date,
- "duration" => null,
- "views" => null,
- "thumb" => $this->getimagelinkfromstyle("img-bg"),
- "url" => $this->fuckhtml->getTextContent($video["attributes"]["href"])
- ];
- }
- }
-
-
- /*
- Get DEFINITION snippet
- */
- $this->fuckhtml->load($html);
- $infobox = $this->fuckhtml->getElementById("rh-definitions", "div");
-
- if($infobox !== false){
-
- $answer = [
- "title" => null,
- "description" => [],
- "url" => null,
- "thumb" => null,
- "table" => [],
- "sublink" => []
- ];
-
- $this->fuckhtml->load($infobox);
-
- $answer["title"] =
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "header",
- "h5"
- )[0]
- );
-
- $sections =
- $this->fuckhtml
- ->getElementsByTagName("section");
-
- $i = -1;
- foreach($sections as $section){
-
- $this->fuckhtml->load($section);
- $items =
- $this->fuckhtml
- ->getElementsByTagName("*");
-
- $li = 1;
- $pronounce = false;
- foreach($items as $item){
-
- switch($item["tagName"]){
-
- case "h6":
-
- if(
- isset($item["attributes"]["class"]) &&
- $item["attributes"]["class"] == "h6 pronunciation"
- ){
-
- if($pronounce){
-
- break;
- }
-
- $answer["description"][] = [
- "type" => "quote",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $item
- )
- ];
-
- $answer["description"][] =
- [
- "type" => "audio",
- "url" => "https://search.brave.com/api/rhfetch?rhtype=definitions&word={$answer["title"]}&source=ahd-5"
- ];
-
- $pronounce = true;
- $i = $i + 2;
- break;
- }
-
- $answer["description"][] = [
- "type" => "title",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $item
- )
- ];
- $i++;
- break;
-
- case "li":
-
- if(
- $i !== -1 &&
- $answer["description"][$i]["type"] == "text"
- ){
-
- $answer["description"][$i]["value"] .=
- "\n" . $li . ". " .
- $this->fuckhtml
- ->getTextContent(
- $item
- );
-
- }else{
- $answer["description"][] = [
- "type" => "text",
- "value" =>
- $li . ". " .
- $this->fuckhtml
- ->getTextContent(
- $item
- )
- ];
- $i++;
- }
- $li++;
- break;
-
- case "a":
- $answer["url"] =
- $this->fuckhtml
- ->getTextContent(
- $item["attributes"]["href"]
- );
- break;
- }
- }
- }
-
- $out["answer"][] = $answer;
- }
-
-
- /*
- Get instant answer
- */
- $this->fuckhtml->load($html);
- $infobox = $this->fuckhtml->getElementById("infobox", "div");
-
- if($infobox !== false){
-
- $answer = [
- "title" => null,
- "description" => [],
- "url" => null,
- "thumb" => null,
- "table" => [],
- "sublink" => []
- ];
-
- $this->fuckhtml->load($infobox);
- $div = $this->fuckhtml->getElementsByTagName("div");
-
- /*
- Get title + url
- */
- $title =
- $this->fuckhtml
- ->getElementsByClassName("infobox-title", "a");
-
- if(count($title) !== 0){
-
- $answer["title"] =
- $this->fuckhtml
- ->getTextContent(
- $title[0]
- );
-
- $answer["url"] =
- $this->fuckhtml
- ->getTextContent(
- $title[0]["attributes"]["href"]
- );
- }
-
- /*
- Get thumbnail
- */
- $thumb = $this->getimagelinkfromstyle("thumb");
-
- if($thumb["url"] !== null){
-
- $answer["thumb"] = $thumb["url"];
- }
-
- /*
- Get table
- */
- $title =
- $this->fuckhtml
- ->getElementsByClassName(
- "infobox-attr-header",
- "div"
- );
-
- $rowhtml = $infobox;
-
- if(count($title) >= 2){
-
- $rowhtml =
- explode(
- $title[1]["outerHTML"],
- $infobox["innerHTML"],
- 2
- )[0];
- }
-
- $this->fuckhtml->load($rowhtml);
-
- $rows =
- $this->fuckhtml
- ->getElementsByClassName("infobox-attr", "div");
-
- foreach($rows as $row){
-
- if(!isset($row["innerHTML"])){
-
- continue;
- }
-
- $this->fuckhtml->load($row);
- $span =
- $this->fuckhtml
- ->getElementsByTagName("span");
-
- if(count($span) === 2){
-
- $answer["table"][
- $this->fuckhtml->getTextContent($span[0])
- ] = str_replace("\n", ", ", $this->fuckhtml->getTextContent($span[1], true));
- }
- }
-
- $this->fuckhtml->load($infobox);
-
- /*
- Parse stackoverflow answers
- */
- $code =
- $this->fuckhtml
- ->getElementById("codebox-answer", $div);
-
- if($code){
-
- // this might be standalone text with no paragraphs, check for that
- $author =
- $this->fuckhtml
- ->getElementById("author");
-
- $desc_tmp =
- str_replace(
- $author["outerHTML"],
- "",
- $code["innerHTML"]
- );
-
- $this->fuckhtml->load($desc_tmp);
- $code =
- $this->fuckhtml
- ->getElementsByTagName("*");
-
- if(count($code) === 0){
-
- $answer["description"] =
- [
- [
- "type" => "text",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $desc_tmp
- )
- ],
- [
- "type" => "quote",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $author
- )
- ]
- ];
- }else{
-
- $text = [];
- $i = 0;
-
- foreach($code as $snippet){
-
- switch($snippet["tagName"]){
-
- case "p":
- $this->fuckhtml->load($snippet["innerHTML"]);
-
- $codetags =
- $this->fuckhtml
- ->getElementsByTagName("*");
-
- $tmphtml = $snippet["innerHTML"];
-
- foreach($codetags as $tag){
-
- if(!isset($tag["outerHTML"])){
-
- continue;
- }
-
- $tmphtml =
- explode(
- $tag["outerHTML"],
- $tmphtml,
- 2
- );
-
- $value = $this->fuckhtml->getTextContent($tmphtml[0], false, false);
- $this->appendtext($value, $text, $i);
-
- $type = null;
- switch($tag["tagName"]){
-
- case "code": $type = "inline_code"; break;
- case "em": $type = "italic"; break;
- case "blockquote": $type = "quote"; break;
- default: $type = "text";
- }
-
- if($type !== null){
- $value = $this->fuckhtml->getTextContent($tag, false, true);
-
- if(trim($value) != ""){
-
- if(
- $i !== 0 &&
- $type == "title"
- ){
-
- $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
- }
-
- $text[] = [
- "type" => $type,
- "value" => $value
- ];
- $i++;
- }
- }
-
- if(count($tmphtml) === 2){
-
- $tmphtml = $tmphtml[1];
- }else{
-
- break;
- }
- }
-
- if(is_array($tmphtml)){
-
- $tmphtml = $tmphtml[0];
- }
-
- if(strlen($tmphtml) !== 0){
-
- $value = $this->fuckhtml->getTextContent($tmphtml, false, false);
- $this->appendtext($value, $text, $i);
- }
- break;
-
- case "pre":
-
- switch($text[$i - 1]["type"]){
-
- case "text":
- case "italic":
- $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
- break;
- }
-
- $text[] =
- [
- "type" => "code",
- "value" =>
- rtrim(
- $this->fuckhtml
- ->getTextContent(
- $snippet,
- true,
- false
- )
- )
- ];
- $i++;
-
- break;
-
- case "ol":
- $o = 0;
-
- $this->fuckhtml->load($snippet);
- $li =
- $this->fuckhtml
- ->getElementsByTagName("li");
-
- foreach($li as $elem){
- $o++;
-
- $this->appendtext(
- $o . ". " .
- $this->fuckhtml
- ->getTextContent(
- $elem
- ),
- $text,
- $i
- );
- }
- break;
- }
- }
-
- if(
- $i !== 0 &&
- $text[$i - 1]["type"] == "text"
- ){
-
- $text[$i - 1]["value"] = rtrim($text[$i - 1]["value"]);
- }
-
- if($author){
-
- $text[] = [
- "type" => "quote",
- "value" => $this->fuckhtml->getTextContent($author)
- ];
- }
-
- $answer["description"] = $text;
- }
- }else{
-
- /*
- Get normal description
- */
- $description =
- $this->fuckhtml
- ->getElementsByClassName(
- "mb-6",
- "div"
- );
-
- if(count($description) !== 0){
-
- $description =
- [
- [
- "type" => "text",
- "value" =>
- $this->titledots(
- preg_replace(
- '/ Wikipedia$/',
- "",
- $this->fuckhtml
- ->getTextContent(
- $description[0]
- )
- )
- )
- ]
- ];
-
- $ratings =
- $this->fuckhtml
- ->getElementById("ratings");
-
- if($ratings){
-
- $this->fuckhtml->load($ratings);
-
- $ratings =
- $this->fuckhtml
- ->getElementsByClassName(
- "flex-hcenter mb-10",
- "div"
- );
-
- $description[] = [
- "type" => "title",
- "value" => "Ratings"
- ];
-
- foreach($ratings as $rating){
-
- $this->fuckhtml->load($rating);
-
- $num =
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "r-num",
- "div"
- )[0]
- );
-
- $href =
- $this->fuckhtml
- ->getElementsByClassName(
- "mr-10",
- "a"
- )[0];
-
- $votes =
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "text-sm",
- "span"
- )[0]
- );
-
- $c = count($description) - 1;
-
- if(
- $c !== -1 &&
- $description[$c]["type"] == "text"
- ){
-
- $description[$c]["value"] .= $num . " ";
- }else{
-
- $description[] = [
- "type" => "text",
- "value" => $num . " "
- ];
- }
-
- $description[] = [
- "type" => "link",
- "value" => $this->fuckhtml->getTextContent($href),
- "url" => $this->fuckhtml->getTextContent($href["attributes"]["href"])
- ];
-
- $description[] = [
- "type" => "text",
- "value" => " (" . $votes . ")\n"
- ];
- }
- }
-
- $answer["description"] = $description;
- }
- }
-
- /*
- Get sublinks
- */
- $this->fuckhtml->load($infobox);
-
- $profiles =
- $this->fuckhtml
- ->getElementById("profiles");
-
- if($profiles){
- $profiles =
- $this->fuckhtml
- ->getElementsByClassName(
- "chip",
- "a"
- );
-
- foreach($profiles as $profile){
-
- $name = $this->fuckhtml->getTextContent($profile["attributes"]["title"]);
-
- if(strtolower($name) == "steampowered"){
-
- $name = "Steam";
- }
-
- $answer["sublink"][$name] =
- $this->fuckhtml->getTextContent($profile["attributes"]["href"]);
- }
- }
-
- $actors =
- $this->fuckhtml
- ->getElementById("panel-movie-cast");
-
- if($actors){
-
- $this->fuckhtml->load($actors);
-
- $actors =
- $this->fuckhtml
- ->getElementsByClassName("card");
-
- $answer["description"][] = [
- "type" => "title",
- "value" => "Cast"
- ];
-
- foreach($actors as $actor){
-
- $this->fuckhtml->load($actor);
-
- $answer["description"][] = [
- "type" => "text",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName("card-body")
- [0]
- )
- ];
-
- $answer["description"][] = [
- "type" => "image",
- "url" => $this->getimagelinkfromstyle("person-thumb")["url"]
- ];
- }
- }
-
- $out["answer"][] = $answer;
- }
-
- /*
- Get actor standalone thingy
- */
- $this->fuckhtml->load($resulthtml);
- $actors =
- $this->fuckhtml
- ->getElementById("predicate-entity");
-
- if($actors){
-
- $this->fuckhtml->load($actors);
-
- $cards =
- $this->fuckhtml
- ->getElementsByClassName("card");
-
- $url =
- $this->fuckhtml
- ->getElementsByClassName(
- "disclaimer",
- "div"
- )[0];
-
- $this->fuckhtml->load($url);
-
- $url =
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByTagName("a")
- [0]
- ["attributes"]
- ["href"]
- );
-
- $this->fuckhtml->load($actors);
-
- $answer = [
- "title" =>
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "entity",
- "span"
- )[0]
- ) . " (Cast)",
- "description" => [],
- "url" => $url,
- "sublink" => [],
- "thumb" => null,
- "table" => []
- ];
-
- foreach($cards as $card){
-
- $this->fuckhtml->load($card);
-
- $answer["description"][] = [
- "type" => "title",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "title"
- )[0]
- )
- ];
-
- $answer["description"][] = [
- "type" => "text",
- "value" =>
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "text-xs desc"
- )[0]
- )
- ];
-
- $answer["description"][] = [
- "type" => "image",
- "url" => $this->getimagelinkfromstyle("img-bg")["url"]
- ];
- }
-
- $out["answer"][] = $answer;
- }
-
- return $out;
- }
-
- public function news($get){
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $nsfw = $get["nsfw"];
- $country = $get["country"];
-
- if(strlen($search) > 2048){
-
- throw new Exception("Search query is too long!");
- }
- /*
- $handle = fopen("scraper/brave-news.html", "r");
- $html = fread($handle, filesize("scraper/brave-news.html"));
- fclose($handle);*/
- try{
- $html =
- $this->get(
- "https://search.brave.com/news",
- [
- "q" => $search
- ],
- $nsfw,
- $country
- );
-
- }catch(Exception $error){
-
- throw new Exception("Could not fetch search page");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "news" => []
- ];
-
- // load html
- $this->fuckhtml->load($html);
-
- $news =
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet inline gap-standard",
- "div"
- );
-
- foreach($news as $article){
-
- $data = [
- "title" => null,
- "author" => null,
- "description" => null,
- "date" => null,
- "thumb" =>
- [
- "url" => null,
- "ratio" => null
- ],
- "url" => null
- ];
-
- $this->fuckhtml->load($article);
- $elems =
- $this->fuckhtml
- ->getElementsByTagName("*");
-
- // get title
- $data["title"] =
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-title",
- $elems
- )
- [0]
- ["innerHTML"]
- );
-
- // get description
- $data["description"] =
- $this->titledots(
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-description",
- $elems
- )
- [0]
- ["innerHTML"]
- )
- );
-
- // get date
- $date =
- explode(
- "•",
- $this->fuckhtml
- ->getTextContent(
- $this->fuckhtml
- ->getElementsByClassName(
- "snippet-url",
- $elems
- )[0]
- )
- );
-
- if(
- count($date) !== 1 &&
- trim($date[1]) != ""
- ){
-
- $data["date"] =
- strtotime(
- $date[1]
- );
- }
-
- // get URL
- $data["url"] =
- $this->fuckhtml->getTextContent(
- $this->unshiturl(
- $this->fuckhtml
- ->getElementsByClassName(
- "result-header",
- $elems
- )
- [0]
- ["attributes"]
- ["href"]
- )
- );
-
- // get thumbnail
- $thumb =
- $this->fuckhtml
- ->getElementsByTagName(
- "img"
- );
-
- if(
- count($thumb) === 2 &&
- trim(
- $thumb[1]
- ["attributes"]
- ["src"]
- ) != ""
- ){
-
- $data["thumb"] = [
- "url" =>
- $this->fuckhtml->getTextContent(
- $this->unshiturl(
- $thumb[1]
- ["attributes"]
- ["src"]
- )
- ),
- "ratio" => "16:9"
- ];
- }
-
- $out["news"][] = $data;
- }
-
- return $out;
- }
-
- public function image($get){
-
- $search = $get["s"];
- $country = $get["country"];
- $nsfw = $get["nsfw"];
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "image" => []
- ];
-
- try{
- $html =
- $this->get(
- "https://search.brave.com/images",
- [
- "q" => $search
- ],
- $nsfw,
- $country
- );
-
- }catch(Exception $error){
-
- throw new Exception("Could not fetch search page");
- }
- /*
- $handle = fopen("scraper/brave-image.html", "r");
- $html = fread($handle, filesize("scraper/brave-image.html"));
- fclose($handle);*/
-
- preg_match(
- '/const data = (\[{.*}\]);/',
- $html,
- $json
- );
-
- if(!isset($json[1])){
-
- throw new Exception("Failed to get data object");
- }
-
- $json =
- $this->fuckhtml
- ->parseJsObject(
- $json[1]
- );
-
- foreach(
- $json[1]
- ["data"]
- ["body"]
- ["response"]
- ["results"]
- as $result
- ){
-
- print_r($result);
-
- $out["image"][] = [
- "title" => $result["title"],
- "source" => [
- [
- "url" => $result["properties"]["url"],
- "width" => null,
- "height" => null
- ],
- [
- "url" => $result["thumbnail"]["src"],
- "width" => null,
- "height" => null
- ]
- ],
- "url" => $result["url"]
- ];
- }
-
- return $out;
- }
-
- public function video($get){
-
- $search = $get["s"];
- $country = $get["country"];
- $nsfw = $get["nsfw"];
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "video" => [],
- "author" => [],
- "livestream" => [],
- "playlist" => [],
- "reel" => []
- ];
-
- try{
- $html =
- $this->get(
- "https://search.brave.com/videos",
- [
- "q" => $search
- ],
- $nsfw,
- $country
- );
-
- }catch(Exception $error){
-
- throw new Exception("Could not fetch search page");
- }
- /*
- $handle = fopen("scraper/brave-video.html", "r");
- $html = fread($handle, filesize("scraper/brave-video.html"));
- fclose($handle);*/
-
- preg_match(
- '/const data = (\[{.*}\]);/',
- $html,
- $json
- );
-
- if(!isset($json[1])){
-
- throw new Exception("Failed to get data object");
- }
-
- $json =
- $this->fuckhtml
- ->parseJsObject(
- $json[1]
- );
-
- foreach(
- $json
- [1]
- ["data"]
- ["body"]
- ["response"]
- ["results"]
- as $result
- ){
-
- if($result["video"]["author"] != "null"){
-
- $author = [
- "name" => $result["video"]["author"]["name"] == "null" ? null : $result["video"]["author"]["name"],
- "url" => $result["video"]["author"]["url"] == "null" ? null : $result["video"]["author"]["url"],
- "avatar" => $result["video"]["author"]["img"] == "null" ? null : $result["video"]["author"]["img"]
- ];
- }else{
-
- $author = [
- "name" => null,
- "url" => null,
- "avatar" => null
- ];
- }
-
- if($result["thumbnail"] != "null"){
-
- $thumb = [
- "url" => $result["thumbnail"]["original"],
- "ratio" => "16:9"
- ];
- }else{
-
- $thumb = [
- "url" => null,
- "ratio" => null
- ];
- }
-
- $out["video"][] = [
- "title" => $result["title"],
- "description" => $result["description"] == "null" ? null : $this->titledots($result["description"]),
- "author" => $author,
- "date" => $result["age"] == "null" ? null : strtotime($result["age"]),
- "duration" => $result["video"]["duration"] == "null" ? null : $this->hms2int($result["video"]["duration"]),
- "views" => $result["video"]["views"] == "null" ? null : (int)$result["video"]["views"],
- "thumb" => $thumb,
- "url" => $result["url"]
- ];
- }
- return $out;
- }
-
- private function hms2int($time){
-
- $parts = explode(":", $time, 3);
- $time = 0;
-
- if(count($parts) === 3){
-
- // hours
- $time = $time + ((int)$parts[0] * 3600);
- array_shift($parts);
- }
-
- if(count($parts) === 2){
-
- // minutes
- $time = $time + ((int)$parts[0] * 60);
- array_shift($parts);
- }
-
- // seconds
- $time = $time + (int)$parts[0];
-
- return $time;
- }
-
- private function appendtext($payload, &$text, &$index){
-
- if(trim($payload) == ""){
-
- return;
- }
-
- if(
- $index !== 0 &&
- $text[$index - 1]["type"] == "text"
- ){
-
- $text[$index - 1]["value"] .= "\n\n" . preg_replace('/ $/', " ", $payload);
- }else{
-
- $text[] = [
- "type" => "text",
- "value" => preg_replace('/ $/', " ", $payload)
- ];
- $index++;
- }
- }
-
- private function tablesublink($html_collection, &$data){
-
- foreach($html_collection as $html){
-
- $html["innerHTML"] = preg_replace(
- '/<style>[\S\s]*<\/style>/i',
- "",
- $html["innerHTML"]
- );
-
- $html =
- explode(
- ":",
- $this->fuckhtml->getTextContent($html),
- 2
- );
-
- if(count($html) === 1){
-
- $html = ["Rating", $html[0]];
- }
-
- $data["table"][trim($html[0])] = trim($html[1]);
- }
- }
-
- private function getimagelinkfromstyle($thumb){
-
- $thumb =
- $this->fuckhtml
- ->getElementsByClassName(
- $thumb,
- "div"
- );
-
- if(count($thumb) === 0){
-
- return [
- "url" => null,
- "ratio" => null
- ];
- }
-
- $thumb = $thumb[0]["attributes"]["style"];
-
- preg_match(
- '/background-image: ?url\((\'[^\']+\'|"[^"]+"|[^\)]+)\)/',
- $thumb,
- $thumb
- );
-
- $url = $this->fuckhtml->getTextContent($this->unshiturl(trim($thumb[1], '"\' ')));
-
- if(parse_url($url, PHP_URL_HOST) == "cdn.search.brave.com"){
-
- return [
- "url" => null,
- "ratio" => null
- ];
- }
-
- return [
- "url" => $url,
- "ratio" => "16:9"
- ];
- }
-
- private function limitstrlen($text){
-
- return explode("\n", wordwrap($text, 300, "\n"))[0];
- }
-
- private function limitwhitespace($text){
-
- return
- preg_replace(
- '/[\s]+/',
- " ",
- $text
- );
- }
-
- private function titledots($title){
-
- $substr = substr($title, -3);
-
- if(
- $substr == "..." ||
- $substr == "…"
- ){
-
- return trim(substr($title, 0, -3));
- }
-
- return trim($title);
- }
-
- private function unshiturl($url){
-
- // https://imgs.search.brave.com/XFnbR8Sl7ge82MBDEH7ju0UHImRovMVmQ2qnDvgNTuA/rs:fit:844:225:1/g:ce/aHR0cHM6Ly90c2U0/Lm1tLmJpbmcubmV0/L3RoP2lkPU9JUC54/UWotQXU5N2ozVndT/RDJnNG9BNVhnSGFF/SyZwaWQ9QXBp.jpeg
-
- $tmp = explode("aHR0", $url);
-
- if(count($tmp) !== 2){
-
- // nothing to do
- return $url;
- }
-
- return
- base64_decode(
- "aHR0" .
- str_replace(["/", "_"], ["", "/"],
- explode(
- ".",
- $tmp[1]
- )[0]
- )
- );
- }
- }
|