2023-07-22 13:41:14 -05:00
|
|
|
<?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"
|
2023-09-06 07:43:22 -05:00
|
|
|
],
|
|
|
|
"spellcheck" => [
|
|
|
|
"display" => "Spellcheck",
|
|
|
|
"option" => [
|
|
|
|
"no" => "No",
|
|
|
|
"yes" => "Yes"
|
|
|
|
]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
case "images":
|
|
|
|
case "videos":
|
2023-07-22 13:41:14 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
private function get($url, $get = [], $nsfw, $country){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
switch($nsfw){
|
|
|
|
|
|
|
|
case "yes": $nsfw = "off"; break;
|
|
|
|
case "maybe": $nsfw = "moderate"; break;
|
|
|
|
case "no": $nsfw = "strict"; break;
|
|
|
|
}
|
|
|
|
|
2023-08-10 21:54:37 -05:00
|
|
|
if($country == "any"){
|
|
|
|
|
|
|
|
$country = "all";
|
|
|
|
}
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
$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",
|
2023-08-08 02:09:47 -05:00
|
|
|
"Sec-Fetch-User: ?1"
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$curlproc = curl_init();
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
if($get !== []){
|
|
|
|
$get = http_build_query($get);
|
|
|
|
$url .= "?" . $get;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
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"];
|
2023-09-06 07:43:22 -05:00
|
|
|
$q["spellcheck"] = "0";
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$nsfw = $q["nsfw"];
|
|
|
|
unset($q["nsfw"]);
|
|
|
|
|
|
|
|
$country = $q["country"];
|
|
|
|
unset($q["country"]);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// get _GET data instead
|
|
|
|
$search = $get["s"];
|
2023-09-06 07:43:22 -05:00
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
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"];
|
2023-09-06 07:43:22 -05:00
|
|
|
$spellcheck = $get["spellcheck"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$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}";
|
|
|
|
}
|
2023-09-06 07:43:22 -05:00
|
|
|
|
|
|
|
// spellcheck
|
|
|
|
if($spellcheck == "no"){
|
|
|
|
|
|
|
|
$q["spellcheck"] = "0";
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-09-06 07:49:11 -05:00
|
|
|
/*
|
2023-07-22 13:41:14 -05:00
|
|
|
$handle = fopen("scraper/brave.html", "r");
|
|
|
|
$html = fread($handle, filesize("scraper/brave.html"));
|
|
|
|
fclose($handle);
|
2023-09-06 07:49:11 -05:00
|
|
|
*/
|
2023-07-22 13:41:14 -05:00
|
|
|
try{
|
|
|
|
$html =
|
|
|
|
$this->get(
|
|
|
|
"https://search.brave.com/search",
|
|
|
|
$q,
|
|
|
|
$nsfw,
|
|
|
|
$country
|
|
|
|
);
|
|
|
|
|
|
|
|
}catch(Exception $error){
|
|
|
|
|
|
|
|
throw new Exception("Could not fetch search page");
|
|
|
|
}
|
2023-09-06 07:49:11 -05:00
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
$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
|
|
|
|
->getElementById(
|
2023-09-06 07:43:22 -05:00
|
|
|
"pagination",
|
|
|
|
"div"
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($nextpage){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$this->fuckhtml->load($nextpage);
|
|
|
|
|
|
|
|
$nextpage =
|
|
|
|
$this->fuckhtml
|
|
|
|
->getElementsByClassName("btn", "a");
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(count($nextpage) !== 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$nextpage =
|
|
|
|
$nextpage[count($nextpage) - 1];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(
|
|
|
|
strtolower(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$nextpage
|
2023-07-22 13:41:14 -05:00
|
|
|
)
|
2023-09-06 07:43:22 -05:00
|
|
|
) == "next"
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
preg_match(
|
|
|
|
'/offset=([0-9]+)/',
|
|
|
|
$this->fuckhtml->getTextContent($nextpage["attributes"]["href"]),
|
|
|
|
$nextpage
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$q["offset"] = (int)$nextpage[1];
|
|
|
|
$q["nsfw"] = $nsfw;
|
|
|
|
$q["country"] = $country;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$out["npt"] =
|
|
|
|
$this->nextpage->store(
|
|
|
|
json_encode($q),
|
|
|
|
"web"
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$this->fuckhtml->load($html);
|
|
|
|
|
|
|
|
$script_disc =
|
2023-07-22 13:41:14 -05:00
|
|
|
$this->fuckhtml
|
2023-09-06 07:43:22 -05:00
|
|
|
->getElementsByTagName(
|
|
|
|
"script"
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$grep = [];
|
|
|
|
foreach($script_disc as $discs){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
preg_match(
|
|
|
|
'/const data ?= ?(\[{.*}]);/',
|
|
|
|
$discs["innerHTML"],
|
|
|
|
$grep
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($grep[1])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
break;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(!isset($grep[1])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
throw new Exception("Could not get data JS");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-09-06 07:43:22 -05:00
|
|
|
|
|
|
|
$data =
|
|
|
|
$this->fuckhtml
|
|
|
|
->parseJsObject(
|
|
|
|
$grep[1]
|
|
|
|
);
|
|
|
|
unset($grep);
|
|
|
|
|
|
|
|
$data = $data[1]["data"]["body"]["response"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get web results
|
|
|
|
*/
|
2023-09-06 07:43:22 -05:00
|
|
|
if(!isset($data["web"]["results"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//$has_so_answer = false;
|
|
|
|
|
|
|
|
foreach($data["web"]["results"] as $result){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($result["thumbnail"])){
|
|
|
|
|
|
|
|
$thumb = [
|
|
|
|
"ratio" => $result["thumbnail"]["logo"] == "false" ? "16:9" : "1:1",
|
|
|
|
"url" => $result["thumbnail"]["original"]
|
|
|
|
];
|
|
|
|
}else{
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$thumb = [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$sublink = [];
|
|
|
|
if(isset($result["cluster"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($result["cluster"] as $cluster){
|
|
|
|
|
|
|
|
$sublink[] = [
|
|
|
|
"title" => $this->titledots($cluster["title"]),
|
|
|
|
"description" =>
|
|
|
|
$this->titledots(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$cluster["description"]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"url" => $cluster["url"],
|
|
|
|
"date" => null
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
// parse table elements
|
|
|
|
$table = [];
|
|
|
|
|
|
|
|
// product
|
|
|
|
$ref = null;
|
|
|
|
|
|
|
|
if(isset($result["product"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$ref = &$result["product"];
|
|
|
|
}elseif(isset($result["creative_work"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$ref = &$result["creative_work"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($ref !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($ref["offers"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($ref["offers"] as $offer){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$price = null;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($offer["price"])){
|
|
|
|
|
|
|
|
if((float)$offer["price"] == 0){
|
|
|
|
|
|
|
|
$price = "Free";
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$price = $offer["price"];
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($price !== "Free"){
|
|
|
|
if(isset($offer["priceCurrency"])){
|
|
|
|
|
|
|
|
$price .= " " . $offer["priceCurrency"];
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($price !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$table["Price"] = trim($price);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($ref["rating"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$rating = null;
|
|
|
|
if(isset($ref["rating"]["ratingValue"])){
|
|
|
|
|
|
|
|
$rating = $ref["rating"]["ratingValue"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($ref["rating"]["bestRating"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$rating .= "/" . $ref["rating"]["bestRating"];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($ref["rating"]["reviewCount"])){
|
|
|
|
|
|
|
|
$isnull = $rating === null ? false : true;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($isnull){
|
|
|
|
|
|
|
|
$rating .= " (";
|
|
|
|
}
|
|
|
|
|
|
|
|
$rating .= number_format($ref["rating"]["reviewCount"]) . " hits";
|
|
|
|
|
|
|
|
if($isnull){
|
|
|
|
|
|
|
|
$rating .= ")";
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if($rating !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$table["Rating"] = $rating;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
// video
|
|
|
|
if(isset($result["video"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($result["video"] as $key => $value){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(is_string($result["video"][$key]) === false){
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$table[ucfirst($key)] = $value;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-09-06 07:43:22 -05:00
|
|
|
Get StackOverflow answers
|
2023-07-22 13:41:14 -05:00
|
|
|
*/
|
2023-09-06 07:43:22 -05:00
|
|
|
// commented out since it also returns alot of garbage
|
2023-07-22 13:41:14 -05:00
|
|
|
/*
|
2023-09-06 07:43:22 -05:00
|
|
|
if(
|
|
|
|
$has_so_answer === false &&
|
|
|
|
isset($result["qa"])
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$has_so_answer = true;
|
|
|
|
$answer = $this->stackoverflow_parse($result["qa"]["answer"]["text"]);
|
|
|
|
|
|
|
|
if(isset($result["qa"]["answer"]["author"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$answer[] = [
|
|
|
|
"type" => "quote",
|
|
|
|
"value" => "Answer from " . $result["qa"]["answer"]["author"]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$out["answer"][] = [
|
|
|
|
"title" =>
|
2023-07-22 13:41:14 -05:00
|
|
|
$this->fuckhtml
|
2023-09-06 07:43:22 -05:00
|
|
|
->getTextContent(
|
|
|
|
$result["qa"]["question"]
|
|
|
|
),
|
|
|
|
"description" => $answer,
|
|
|
|
"url" => $result["url"],
|
|
|
|
"thumb" => null,
|
|
|
|
"table" => [],
|
|
|
|
"sublink" => []
|
|
|
|
];
|
|
|
|
}*/
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$out["web"][] = [
|
|
|
|
"title" =>
|
|
|
|
$this->titledots(
|
|
|
|
$result["title"]
|
|
|
|
),
|
|
|
|
"description" =>
|
2023-07-22 13:41:14 -05:00
|
|
|
$this->titledots(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
2023-09-06 07:43:22 -05:00
|
|
|
$result["description"]
|
2023-07-22 13:41:14 -05:00
|
|
|
)
|
2023-09-06 07:43:22 -05:00
|
|
|
),
|
|
|
|
"url" => $result["url"],
|
|
|
|
"date" => isset($result["age"]) ? strtotime($result["age"]) : null,
|
|
|
|
"type" => "web",
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"sublink" => $sublink,
|
|
|
|
"table" => $table
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-09-06 07:43:22 -05:00
|
|
|
Get spelling autocorrect
|
2023-07-22 13:41:14 -05:00
|
|
|
*/
|
2023-09-06 07:43:22 -05:00
|
|
|
if(
|
|
|
|
isset($data["query"]["bo_altered_diff"][0][0]) &&
|
|
|
|
$data["query"]["bo_altered_diff"][0][0] == "true"
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$out["spelling"] = [
|
|
|
|
"type" => "including",
|
|
|
|
"using" => $data["query"]["bo_altered_diff"][0][1],
|
|
|
|
"correction" => $get["s"]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get wikipedia heads
|
|
|
|
*/
|
|
|
|
if(isset($data["infobox"]["results"][0])){
|
|
|
|
|
|
|
|
foreach($data["infobox"]["results"] as $info){
|
|
|
|
|
|
|
|
if($info["subtype"] == "code"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$description =
|
|
|
|
$this->stackoverflow_parse($info["data"]["answer"]["text"]);
|
|
|
|
|
|
|
|
if(isset($info["data"]["answer"]["author"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$description[] = [
|
|
|
|
"type" => "quote",
|
|
|
|
"value" => "Answer from " . $info["data"]["answer"]["author"]
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-09-06 07:43:22 -05:00
|
|
|
}else{
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$description = [];
|
|
|
|
|
|
|
|
if(
|
|
|
|
isset($info["description"]) &&
|
|
|
|
$info["description"] != ""
|
|
|
|
){
|
|
|
|
$description[] = [
|
2023-07-22 13:41:14 -05:00
|
|
|
"type" => "quote",
|
2023-09-06 07:43:22 -05:00
|
|
|
"value" => $info["description"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(
|
|
|
|
isset($info["long_desc"]) &&
|
|
|
|
$info["long_desc"] != ""
|
|
|
|
){
|
|
|
|
$description[] = [
|
2023-08-27 00:45:59 -05:00
|
|
|
"type" => "text",
|
2023-09-06 07:43:22 -05:00
|
|
|
"value" => $this->titledots($info["long_desc"])
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
2023-09-06 07:43:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$table = [];
|
|
|
|
if(isset($info["attributes"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($info["attributes"] as $row){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(
|
|
|
|
$row[1] == "null" &&
|
|
|
|
count($table) !== 0
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($row[1] == "null"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
continue;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-09-06 07:43:22 -05:00
|
|
|
|
|
|
|
$table[
|
|
|
|
$this->fuckhtml->getTextContent($row[0])
|
|
|
|
] =
|
|
|
|
$this->fuckhtml->getTextContent($row[1]);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$sublink = [];
|
|
|
|
if(isset($info["profiles"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($info["profiles"] as $row){
|
|
|
|
|
|
|
|
$name = $this->fuckhtml->getTextContent($row["name"]);
|
|
|
|
|
|
|
|
if(strtolower($name) == "steampowered"){
|
|
|
|
|
|
|
|
$name = "Steam";
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$sublink[
|
|
|
|
$this->fuckhtml->getTextContent($name)
|
|
|
|
] =
|
|
|
|
$this->fuckhtml->getTextContent($row["url"]);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$out["answer"][] = [
|
|
|
|
"title" => $this->fuckhtml->getTextContent($info["title"]),
|
|
|
|
"description" => $description,
|
|
|
|
"url" => $info["url"],
|
|
|
|
"thumb" => isset($info["images"][0]["original"]) ? $info["images"][0]["original"] : null,
|
|
|
|
"table" => $table,
|
|
|
|
"sublink" => $sublink
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
break; // only iterate once, we get garbage most of the time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get videos
|
|
|
|
*/
|
|
|
|
if(isset($data["videos"]["results"])){
|
|
|
|
|
|
|
|
foreach($data["videos"]["results"] as $video){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$out["video"][] = [
|
|
|
|
"title" => $this->titledots($video["title"]),
|
|
|
|
"description" => $this->titledots($video["description"]),
|
|
|
|
"date" => isset($video["age"]) ? strtotime($video["age"]) : null,
|
|
|
|
"duration" => isset($video["video"]["duration"]) ? $this->hms2int($video["video"]["duration"]) : null,
|
|
|
|
"views" => null,
|
|
|
|
"thumb" =>
|
|
|
|
isset($video["thumbnail"]["src"]) ?
|
|
|
|
[
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $this->unshiturl($video["thumbnail"]["src"])
|
|
|
|
] :
|
|
|
|
[
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
],
|
|
|
|
"url" => $video["url"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
2023-09-06 07:43:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get news
|
|
|
|
*/
|
|
|
|
if(isset($data["news"]["results"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($data["news"]["results"] as $news){
|
|
|
|
|
|
|
|
$out["news"][] = [
|
|
|
|
"title" => $this->titledots($news["title"]),
|
|
|
|
"description" => $this->titledots($news["description"]),
|
|
|
|
"date" => isset($news["age"]) ? strtotime($news["age"]) : null,
|
|
|
|
"thumb" =>
|
|
|
|
isset($video["thumbnail"]["src"]) ?
|
|
|
|
[
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $this->unshiturl($video["thumbnail"]["src"])
|
|
|
|
] :
|
|
|
|
[
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
],
|
|
|
|
"url" => $news["url"]
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-09-06 07:43:22 -05:00
|
|
|
Get discussions
|
2023-07-22 13:41:14 -05:00
|
|
|
*/
|
2023-09-06 07:43:22 -05:00
|
|
|
$disc_out = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($data["discussions"]["results"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
foreach($data["discussions"]["results"] as $disc){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$table = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($disc["data"]["num_votes"])){
|
|
|
|
|
|
|
|
$table["Votes"] = (int)$disc["data"]["num_votes"];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
if(isset($disc["data"]["num_answers"])){
|
|
|
|
|
|
|
|
$table["Comments"] = (int)$disc["data"]["num_answers"];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
$disc_out[] = [
|
|
|
|
"title" =>
|
|
|
|
$this->titledots(
|
|
|
|
$disc["title"]
|
|
|
|
),
|
|
|
|
"description" =>
|
|
|
|
$this->limitstrlen(
|
|
|
|
$this->titledots(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$disc["description"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"url" => $disc["url"],
|
|
|
|
"date" => isset($disc["age"]) ? strtotime($disc["age"]) : null,
|
|
|
|
"type" => "web",
|
|
|
|
"thumb" => [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
],
|
|
|
|
"sublink" => [],
|
|
|
|
"table" => $table
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
// append discussions at position 2
|
|
|
|
array_splice($out["web"], 1, 0, $disc_out);
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
public function image($get){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$search = $get["s"];
|
|
|
|
$country = $get["country"];
|
|
|
|
$nsfw = $get["nsfw"];
|
|
|
|
|
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"npt" => null,
|
|
|
|
"image" => []
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
try{
|
|
|
|
$html =
|
|
|
|
$this->get(
|
2023-08-08 02:09:47 -05:00
|
|
|
"https://search.brave.com/images",
|
2023-07-22 13:41:14 -05:00
|
|
|
[
|
2023-08-08 02:09:47 -05:00
|
|
|
"q" => $search
|
2023-07-22 13:41:14 -05:00
|
|
|
],
|
|
|
|
$nsfw,
|
|
|
|
$country
|
|
|
|
);
|
|
|
|
|
|
|
|
}catch(Exception $error){
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
throw new Exception("Could not fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-08-08 02:09:47 -05:00
|
|
|
/*
|
|
|
|
$handle = fopen("scraper/brave-image.html", "r");
|
|
|
|
$html = fread($handle, filesize("scraper/brave-image.html"));
|
|
|
|
fclose($handle);*/
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
preg_match(
|
2023-08-08 02:09:47 -05:00
|
|
|
'/const data = (\[{.*}\]);/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$html,
|
2023-08-08 02:09:47 -05:00
|
|
|
$json
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
if(!isset($json[1])){
|
|
|
|
|
|
|
|
throw new Exception("Failed to get data object");
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$json =
|
|
|
|
$this->fuckhtml
|
|
|
|
->parseJsObject(
|
|
|
|
$json[1]
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
foreach(
|
2023-08-08 02:09:47 -05:00
|
|
|
$json[1]
|
|
|
|
["data"]
|
|
|
|
["body"]
|
|
|
|
["response"]
|
|
|
|
["results"]
|
|
|
|
as $result
|
2023-07-22 13:41:14 -05:00
|
|
|
){
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$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"]
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function video($get){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$search = $get["s"];
|
|
|
|
$country = $get["country"];
|
|
|
|
$nsfw = $get["nsfw"];
|
|
|
|
|
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"npt" => null,
|
|
|
|
"video" => [],
|
|
|
|
"author" => [],
|
|
|
|
"livestream" => [],
|
|
|
|
"playlist" => [],
|
|
|
|
"reel" => []
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
try{
|
|
|
|
$html =
|
|
|
|
$this->get(
|
|
|
|
"https://search.brave.com/videos",
|
|
|
|
[
|
|
|
|
"q" => $search
|
|
|
|
],
|
|
|
|
$nsfw,
|
|
|
|
$country
|
|
|
|
);
|
|
|
|
|
|
|
|
}catch(Exception $error){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
throw new Exception("Could not fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2023-08-08 02:09:47 -05:00
|
|
|
/*
|
|
|
|
$handle = fopen("scraper/brave-video.html", "r");
|
|
|
|
$html = fread($handle, filesize("scraper/brave-video.html"));
|
|
|
|
fclose($handle);*/
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
preg_match(
|
2023-08-08 02:09:47 -05:00
|
|
|
'/const data = (\[{.*}\]);/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$html,
|
2023-08-08 02:09:47 -05:00
|
|
|
$json
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
if(!isset($json[1])){
|
|
|
|
|
|
|
|
throw new Exception("Failed to get data object");
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$json =
|
|
|
|
$this->fuckhtml
|
|
|
|
->parseJsObject(
|
|
|
|
$json[1]
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:43:22 -05:00
|
|
|
private function stackoverflow_parse($html){
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$answer = [];
|
|
|
|
|
|
|
|
$this->fuckhtml->load($html);
|
|
|
|
|
|
|
|
foreach(
|
|
|
|
$this->fuckhtml->getElementsByTagName("*")
|
|
|
|
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, $answer, $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"
|
|
|
|
){
|
|
|
|
|
|
|
|
$answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$answer[] = [
|
|
|
|
"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, $answer, $i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "img":
|
|
|
|
$answer[] = [
|
|
|
|
"type" => "image",
|
|
|
|
"url" =>
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$tag["attributes"]["src"]
|
|
|
|
)
|
|
|
|
];
|
|
|
|
$i++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "pre":
|
|
|
|
|
|
|
|
switch($answer[$i - 1]["type"]){
|
|
|
|
|
|
|
|
case "text":
|
|
|
|
case "italic":
|
|
|
|
$answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$answer[] =
|
|
|
|
[
|
|
|
|
"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
|
|
|
|
),
|
|
|
|
$answer,
|
|
|
|
$i
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
|
|
|
$i !== 0 &&
|
|
|
|
$answer[$i - 1]["type"] == "text"
|
|
|
|
){
|
|
|
|
|
|
|
|
$answer[$i - 1]["value"] = rtrim($answer[$i - 1]["value"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
private function hms2int($time){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
$parts = explode(":", $time, 3);
|
|
|
|
$time = 0;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
if(count($parts) === 3){
|
|
|
|
|
|
|
|
// hours
|
|
|
|
$time = $time + ((int)$parts[0] * 3600);
|
|
|
|
array_shift($parts);
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
if(count($parts) === 2){
|
|
|
|
|
|
|
|
// minutes
|
|
|
|
$time = $time + ((int)$parts[0] * 60);
|
|
|
|
array_shift($parts);
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
// seconds
|
|
|
|
$time = $time + (int)$parts[0];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2023-08-08 02:09:47 -05:00
|
|
|
return $time;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|