2023-07-22 13:41:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ddg{
|
|
|
|
|
|
|
|
public function __construct(){
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
include "lib/backend.php";
|
|
|
|
$this->backend = new backend("ddg");
|
|
|
|
|
|
|
|
include "lib/fuckhtml.php";
|
|
|
|
$this->fuckhtml = new fuckhtml();
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
curl functions
|
|
|
|
*/
|
|
|
|
private const req_web = 0;
|
|
|
|
private const req_xhr = 1;
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
private function get($proxy, $url, $get = [], $reqtype = self::req_web){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$curlproc = curl_init();
|
|
|
|
|
|
|
|
if($get !== []){
|
|
|
|
$get = http_build_query($get);
|
|
|
|
$url .= "?" . $get;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($curlproc, CURLOPT_URL, $url);
|
|
|
|
|
2024-11-07 23:37:43 -05:00
|
|
|
// http2 bypass
|
|
|
|
curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
switch($reqtype){
|
|
|
|
case self::req_web:
|
|
|
|
$headers =
|
2023-11-07 08:04:56 -05:00
|
|
|
["User-Agent: " . config::USER_AGENT,
|
2023-07-22 13:41:14 -05:00
|
|
|
"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",
|
2024-12-17 00:31:15 -05:00
|
|
|
"Accept-Encoding: gzip",
|
2023-07-22 13:41:14 -05:00
|
|
|
"DNT: 1",
|
2024-11-07 23:37:43 -05:00
|
|
|
"Sec-GPC: 1",
|
2023-07-22 13:41:14 -05:00
|
|
|
"Connection: keep-alive",
|
|
|
|
"Upgrade-Insecure-Requests: 1",
|
|
|
|
"Sec-Fetch-Dest: document",
|
|
|
|
"Sec-Fetch-Mode: navigate",
|
2024-11-07 23:37:43 -05:00
|
|
|
"Sec-Fetch-Site: same-origin",
|
|
|
|
"Sec-Fetch-User: ?1",
|
|
|
|
"Priority: u=0, i",
|
|
|
|
"TE: trailers"];
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::req_xhr:
|
|
|
|
$headers =
|
2023-11-07 08:04:56 -05:00
|
|
|
["User-Agent: " . config::USER_AGENT,
|
2024-12-17 00:31:15 -05:00
|
|
|
"Accept: */*",
|
2023-07-22 13:41:14 -05:00
|
|
|
"Accept-Language: en-US,en;q=0.5",
|
2024-12-17 00:31:15 -05:00
|
|
|
"Accept-Encoding: gzip",
|
2023-07-22 13:41:14 -05:00
|
|
|
"Referer: https://duckduckgo.com/",
|
|
|
|
"DNT: 1",
|
2024-11-07 23:37:43 -05:00
|
|
|
"Sec-GPC: 1",
|
|
|
|
"Connection: keep-alive",
|
2024-12-17 00:31:15 -05:00
|
|
|
"Sec-Fetch-Dest: script",
|
|
|
|
"Sec-Fetch-Mode: no-cors",
|
|
|
|
"Sec-Fetch-Site: same-site",
|
|
|
|
"Priority: u=1"];
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
$this->backend->assign_proxy($curlproc, $proxy);
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
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 getfilters($pagetype){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$base = [
|
|
|
|
"country" => [
|
|
|
|
"display" => "Country",
|
|
|
|
"option" => [
|
|
|
|
"us-en" => "US (English)",
|
|
|
|
"ar-es" => "Argentina",
|
|
|
|
"au-en" => "Australia",
|
|
|
|
"at-de" => "Austria",
|
|
|
|
"be-fr" => "Belgium (fr)",
|
|
|
|
"be-nl" => "Belgium (nl)",
|
|
|
|
"br-pt" => "Brazil",
|
|
|
|
"bg-bg" => "Bulgaria",
|
|
|
|
"ca-en" => "Canada (en)",
|
|
|
|
"ca-fr" => "Canada (fr)",
|
|
|
|
"ct-ca" => "Catalonia",
|
|
|
|
"cl-es" => "Chile",
|
|
|
|
"cn-zh" => "China",
|
|
|
|
"co-es" => "Colombia",
|
|
|
|
"hr-hr" => "Croatia",
|
|
|
|
"cz-cs" => "Czech Republic",
|
|
|
|
"dk-da" => "Denmark",
|
|
|
|
"ee-et" => "Estonia",
|
|
|
|
"fi-fi" => "Finland",
|
|
|
|
"fr-fr" => "France",
|
|
|
|
"de-de" => "Germany",
|
|
|
|
"gr-el" => "Greece",
|
|
|
|
"hk-tzh" => "Hong Kong",
|
|
|
|
"hu-hu" => "Hungary",
|
|
|
|
"in-en" => "India (en)",
|
|
|
|
"id-en" => "Indonesia (en)",
|
|
|
|
"ie-en" => "Ireland",
|
|
|
|
"il-en" => "Israel (en)",
|
|
|
|
"it-it" => "Italy",
|
|
|
|
"jp-jp" => "Japan",
|
|
|
|
"kr-kr" => "Korea",
|
|
|
|
"lv-lv" => "Latvia",
|
|
|
|
"lt-lt" => "Lithuania",
|
|
|
|
"my-en" => "Malaysia (en)",
|
|
|
|
"mx-es" => "Mexico",
|
|
|
|
"nl-nl" => "Netherlands",
|
|
|
|
"nz-en" => "New Zealand",
|
|
|
|
"no-no" => "Norway",
|
|
|
|
"pk-en" => "Pakistan (en)",
|
|
|
|
"pe-es" => "Peru",
|
|
|
|
"ph-en" => "Philippines (en)",
|
|
|
|
"pl-pl" => "Poland",
|
|
|
|
"pt-pt" => "Portugal",
|
|
|
|
"ro-ro" => "Romania",
|
|
|
|
"ru-ru" => "Russia",
|
|
|
|
"xa-ar" => "Saudi Arabia",
|
|
|
|
"sg-en" => "Singapore",
|
|
|
|
"sk-sk" => "Slovakia",
|
|
|
|
"sl-sl" => "Slovenia",
|
|
|
|
"za-en" => "South Africa",
|
|
|
|
"es-ca" => "Spain (ca)",
|
|
|
|
"es-es" => "Spain (es)",
|
|
|
|
"se-sv" => "Sweden",
|
|
|
|
"ch-de" => "Switzerland (de)",
|
|
|
|
"ch-fr" => "Switzerland (fr)",
|
|
|
|
"tw-tzh" => "Taiwan",
|
|
|
|
"th-en" => "Thailand (en)",
|
|
|
|
"tr-tr" => "Turkey",
|
|
|
|
"us-es" => "US (Spanish)",
|
|
|
|
"ua-uk" => "Ukraine",
|
|
|
|
"uk-en" => "United Kingdom",
|
|
|
|
"vn-en" => "Vietnam (en)"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
switch($pagetype){
|
|
|
|
|
|
|
|
case "web":
|
2024-12-17 00:31:15 -05:00
|
|
|
$base["country"]["option"] =
|
|
|
|
array_merge(["any" => "All Regions"], $base["country"]["option"]);
|
|
|
|
|
|
|
|
return array_merge($base,
|
|
|
|
[
|
|
|
|
"nsfw" => [
|
|
|
|
"display" => "NSFW",
|
|
|
|
"option" => [
|
|
|
|
"yes" => "Yes",
|
|
|
|
"maybe" => "Maybe",
|
|
|
|
"no" => "No"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"newer" => [
|
|
|
|
"display" => "Newer than",
|
|
|
|
"option" => "_DATE"
|
|
|
|
],
|
|
|
|
"older" => [
|
|
|
|
"display" => "Older than",
|
|
|
|
"option" => "_DATE"
|
|
|
|
],
|
|
|
|
"extendedsearch" => [
|
|
|
|
// undefined display
|
|
|
|
"option" => [
|
|
|
|
"yes" => "Yes",
|
|
|
|
"no" => "No",
|
|
|
|
]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
]
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "images":
|
2024-12-17 00:31:15 -05:00
|
|
|
return array_merge($base,
|
|
|
|
[
|
|
|
|
"nsfw" => [
|
|
|
|
"display" => "NSFW",
|
|
|
|
"option" => [
|
|
|
|
"yes" => "Yes",
|
|
|
|
"no" => "No"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"date" => [
|
|
|
|
"display" => "Time posted",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any time",
|
|
|
|
"Day" => "Past day",
|
|
|
|
"Week" => "Past week",
|
|
|
|
"Month" => "Past month"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"size" => [
|
|
|
|
"display" => "Size",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any size",
|
|
|
|
"Small" => "Small",
|
|
|
|
"Medium" => "Medium",
|
|
|
|
"Large" => "Large",
|
|
|
|
"Wallpaper" => "Wallpaper"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"color" => [
|
|
|
|
"display" => "Colors",
|
|
|
|
"option" => [
|
|
|
|
"any" => "All colors",
|
|
|
|
"Monochrome" => "Black and white",
|
|
|
|
"Red" => "Red",
|
|
|
|
"Orange" => "Orange",
|
|
|
|
"Yellow" => "Yellow",
|
|
|
|
"Green" => "Green",
|
|
|
|
"Blue" => "Blue",
|
|
|
|
"Purple" => "Purple",
|
|
|
|
"Pink" => "Pink",
|
|
|
|
"Brown" => "Brown",
|
|
|
|
"Black" => "Black",
|
|
|
|
"Gray" => "Gray",
|
|
|
|
"Teal" => "Teal",
|
|
|
|
"White" => "White"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"type" => [
|
|
|
|
"display" => "Type",
|
|
|
|
"option" => [
|
|
|
|
"any" => "All types",
|
|
|
|
"photo" => "Photograph",
|
|
|
|
"clipart" => "Clipart",
|
|
|
|
"gif" => "Animated GIF",
|
|
|
|
"transparent" => "Transparent"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"layout" => [
|
|
|
|
"display" => "Layout",
|
|
|
|
"option" => [
|
|
|
|
"any" => "All layouts",
|
|
|
|
"Square" => "Square",
|
|
|
|
"Tall" => "Tall",
|
|
|
|
"Wide" => "Wide"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"license" => [
|
|
|
|
"display" => "License",
|
|
|
|
"option" => [
|
|
|
|
"any" => "All licenses",
|
|
|
|
"Any" => "All Creative Commons",
|
|
|
|
"Public" => "Public domain",
|
|
|
|
"Share" => "Free to Share and Use",
|
|
|
|
"ShareCommercially" => "Free to Share and Use Commercially",
|
|
|
|
"Modify" => "Free to Modify, Share, and Use",
|
|
|
|
"ModifyCommercially" => "Free to Modify, Share, and Use Commercially"
|
|
|
|
]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
]
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "videos":
|
2024-12-17 00:31:15 -05:00
|
|
|
return array_merge($base,
|
|
|
|
[
|
|
|
|
"nsfw" => [
|
|
|
|
"display" => "NSFW",
|
|
|
|
"option" => [
|
|
|
|
"yes" => "Yes",
|
|
|
|
"no" => "No"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"date" => [
|
|
|
|
"display" => "Time fetched",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any time",
|
|
|
|
"d" => "Past day",
|
|
|
|
"w" => "Past week",
|
|
|
|
"m" => "Past month"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"resolution" => [ //videoDefinition
|
|
|
|
"display" => "Resolution",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any resolution",
|
|
|
|
"high" => "High definition",
|
|
|
|
"standard" => "Standard definition"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"duration" => [ // videoDuration
|
|
|
|
"display" => "Duration",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any duration",
|
|
|
|
"short" => "Short (>5min)",
|
|
|
|
"medium" => "Medium (5-20min)",
|
|
|
|
"long" => "Long (<20min)"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"license" => [
|
|
|
|
"display" => "License",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any license",
|
|
|
|
"creativeCommon" => "Creative Commons",
|
|
|
|
"youtube" => "YouTube Standard"
|
|
|
|
]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
]
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "news":
|
2024-12-17 00:31:15 -05:00
|
|
|
return array_merge($base,
|
|
|
|
[
|
|
|
|
"nsfw" => [
|
|
|
|
"display" => "NSFW",
|
|
|
|
"option" => [
|
|
|
|
"yes" => "Yes",
|
|
|
|
"maybe" => "Maybe",
|
|
|
|
"no" => "No"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"date" => [
|
|
|
|
"display" => "Time posted",
|
|
|
|
"option" => [
|
|
|
|
"any" => "Any time",
|
|
|
|
"d" => "Past day",
|
|
|
|
"w" => "Past week",
|
|
|
|
"m" => "Past month"
|
|
|
|
]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
]
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function web($get){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"spelling" => [
|
|
|
|
"type" => "no_correction",
|
|
|
|
"using" => null,
|
|
|
|
"correction" => null
|
|
|
|
],
|
|
|
|
"npt" => null,
|
|
|
|
"answer" => [],
|
|
|
|
"web" => [],
|
|
|
|
"image" => [],
|
|
|
|
"video" => [],
|
|
|
|
"news" => [],
|
|
|
|
"related" => []
|
|
|
|
];
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
if($get["npt"]){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
[$js_link, $proxy] = $this->backend->get($get["npt"], "web");
|
|
|
|
$js_link = "https://links.duckduckgo.com" . $js_link;
|
|
|
|
|
|
|
|
$html = "";
|
|
|
|
$get["extendedsearch"] = "no";
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
}else{
|
2024-12-17 00:31:15 -05:00
|
|
|
if(strlen($get["s"]) === 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
throw new Exception("Search term is empty!");
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy = $this->backend->get_ip();
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
// generate filters
|
|
|
|
$get_filters = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"q" => $get["s"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["country"] == "any"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$get_filters["kl"] = "wt-wt";
|
|
|
|
}else{
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$get_filters["kl"] = $get["country"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
switch($get["nsfw"]){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
case "yes": $get_filters["kp"] = "-2"; break;
|
|
|
|
case "maybe": $get_filters["kp"] = "-1"; break;
|
|
|
|
case "no": $get_filters["kp"] = "1"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$df = true;
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["newer"] === false){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["older"] !== false){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$start = 36000;
|
2024-12-17 00:31:15 -05:00
|
|
|
$end = $get["older"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}else{
|
|
|
|
|
|
|
|
$df = false;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$start = $get["newer"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["older"] !== false){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$end = $get["older"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}else{
|
|
|
|
|
|
|
|
$end = time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($df === true){
|
|
|
|
$get_filters["df"] = date("Y-m-d", $start) . ".." . date("Y-m-d", $end);
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get HTML
|
|
|
|
//
|
2023-07-22 13:41:14 -05:00
|
|
|
try{
|
2024-12-17 00:31:15 -05:00
|
|
|
$html = $this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2023-07-22 13:41:14 -05:00
|
|
|
"https://duckduckgo.com/",
|
|
|
|
$get_filters
|
|
|
|
);
|
|
|
|
}catch(Exception $e){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$this->fuckhtml->load($html);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$script =
|
|
|
|
$this->fuckhtml
|
|
|
|
->getElementById(
|
|
|
|
"deep_preload_link",
|
|
|
|
"link"
|
|
|
|
);
|
|
|
|
|
|
|
|
if(
|
|
|
|
$script === null ||
|
|
|
|
!isset($script["attributes"]["href"])
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to grep d.js");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_link =
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$script["attributes"]["href"]
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get d.js
|
|
|
|
//
|
2023-07-22 13:41:14 -05:00
|
|
|
try{
|
|
|
|
$js = $this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_link,
|
2023-07-22 13:41:14 -05:00
|
|
|
[],
|
|
|
|
ddg::req_xhr
|
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
}catch(Exception $e){
|
|
|
|
|
|
|
|
throw new Exception("Failed to fetch d.js");
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//echo htmlspecialchars($js);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.pageLayout\.load\(\s*\'d\'\s*,\s*/',
|
|
|
|
$js,
|
|
|
|
2
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($js_tmp) <= 1){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to grep pageLayout(d)");
|
|
|
|
}
|
|
|
|
|
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
if($json === null){
|
|
|
|
|
|
|
|
throw new Exception("Failed to decode JSON");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get search results + NPT token
|
|
|
|
//
|
|
|
|
foreach($json as $item){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($item["c"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$table = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// get youtube video information
|
|
|
|
if(isset($item["video"]["thumbnail_url_template"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb =
|
|
|
|
[
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $this->bingimg($item["video"]["thumbnail_url_template"])
|
|
|
|
];
|
|
|
|
}else{
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb =
|
|
|
|
[
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get table items
|
|
|
|
if(isset($item["rf"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($item["rf"] as $hint){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(
|
|
|
|
!isset($hint["label"]["text"]) ||
|
|
|
|
!isset($hint["items"][0]["text"])
|
|
|
|
){
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$text = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($hint["items"] as $text_part){
|
|
|
|
|
|
|
|
$text[] = $text_part["text"];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$text = implode(", ", $text);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(is_numeric($text)){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$text = number_format((string)$text);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$table[$hint["label"]["text"]] = $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get ratings
|
|
|
|
if(isset($item["ar"])){
|
|
|
|
|
|
|
|
foreach($item["ar"] as $rating){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(
|
|
|
|
isset($rating["aggregateRating"]["bestRating"]) &&
|
|
|
|
isset($rating["aggregateRating"]["ratingValue"])
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$text = $rating["aggregateRating"]["ratingValue"] . "/" . $rating["aggregateRating"]["bestRating"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($rating["aggregateRating"]["reviewCount"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$text .= " (" . number_format($rating["aggregateRating"]["reviewCount"]) . " votes)";
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$table["Rating"] = $text;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get sublinks
|
|
|
|
$sublinks = [];
|
|
|
|
|
|
|
|
if(isset($item["l"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($item["l"] as $sublink){
|
|
|
|
|
|
|
|
$sublinks[] = [
|
|
|
|
"title" => $this->titledots($sublink["text"]),
|
|
|
|
"description" => $this->titledots($sublink["snippet"]),
|
|
|
|
"url" => $sublink["targetUrl"],
|
|
|
|
"date" => null
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
// parse search result
|
|
|
|
$out["web"][] = [
|
|
|
|
"title" =>
|
|
|
|
$this->titledots(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$item["t"]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"description" =>
|
|
|
|
isset($item["a"]) ?
|
|
|
|
$this->titledots(
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$item["a"]
|
|
|
|
)
|
|
|
|
) : null,
|
|
|
|
"url" => $this->unshiturl($item["c"]),
|
|
|
|
"date" =>
|
|
|
|
isset($item["e"]) ?
|
|
|
|
strtotime($item["e"]) : null,
|
|
|
|
"type" => "web",
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"sublink" => $sublinks,
|
|
|
|
"table" => $table
|
|
|
|
];
|
|
|
|
continue;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($item["n"])){
|
|
|
|
|
|
|
|
// get NPT
|
|
|
|
$out["npt"] =
|
|
|
|
$this->backend->store(
|
|
|
|
$item["n"],
|
|
|
|
"web",
|
|
|
|
$proxy
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get spelling
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.page\.showMessage\(\s*\'spelling\'\s*,\s*/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$js,
|
2024-12-17 00:31:15 -05:00
|
|
|
2
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
if(count($js_tmp) > 1){
|
|
|
|
|
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($json !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// parse spelling
|
|
|
|
// qc=2: including
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
switch((int)$json["qc"]){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
case 2:
|
|
|
|
$type = "including";
|
|
|
|
break;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
default:
|
|
|
|
$type = "not_many";
|
|
|
|
break;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
$out["spelling"] = [
|
|
|
|
"type" => $type,
|
|
|
|
"using" => $json["suggestion"],
|
|
|
|
"correction" => $json["recourseText"]
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get images
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.duckbar\.load\(\s*\'images\'\s*,\s*/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$js,
|
2024-12-17 00:31:15 -05:00
|
|
|
2
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
if(count($js_tmp) > 1){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if($json !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($json["results"] as $image){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$ratio = $this->bingratio((int)$image["width"], (int)$image["height"]);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$out["image"][] = [
|
|
|
|
"title" => $image["title"],
|
|
|
|
"source" => [
|
2023-07-22 13:41:14 -05:00
|
|
|
[
|
2024-12-17 00:31:15 -05:00
|
|
|
"url" => $image["image"],
|
|
|
|
"width" => (int)$image["width"],
|
|
|
|
"height" => (int)$image["height"]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"url" => $this->bingimg($image["thumbnail"]),
|
|
|
|
"width" => $ratio[0],
|
|
|
|
"height" => $ratio[1]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
2024-12-17 00:31:15 -05:00
|
|
|
],
|
|
|
|
"url" => $this->unshiturl($image["url"])
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get videos
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.duckbar\.load\(\s*\'videos\'\s*,\s*/',
|
|
|
|
$js,
|
|
|
|
2
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($js_tmp) > 1){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if($json !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($json["results"] as $video){
|
|
|
|
|
|
|
|
$thumb = [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach(["large", "medium", "small"] as $contender){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($video["images"][$contender])){
|
|
|
|
|
|
|
|
$thumb = [
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $this->bingimg($video["images"][$contender])
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
$out["video"][] = [
|
|
|
|
"title" => $this->titledots($video["title"]),
|
|
|
|
"description" =>
|
|
|
|
$video["description"] != "" ?
|
|
|
|
$this->titledots($video["description"]) : null,
|
|
|
|
"date" =>
|
|
|
|
isset($video["published"]) ?
|
|
|
|
strtotime($video["published"]) : null,
|
|
|
|
"duration" =>
|
|
|
|
$video["duration"] != "" ?
|
|
|
|
$this->hms2int($video["duration"]) : null,
|
|
|
|
"views" =>
|
|
|
|
isset($video["statistics"]["viewCount"]) ?
|
|
|
|
(int)$video["statistics"]["viewCount"] : null,
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"url" => $this->unshiturl($video["content"])
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get news
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.duckbar\.load\(\s*\'news\'\s*,\s*/',
|
|
|
|
$js,
|
|
|
|
2
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($js_tmp) > 1){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if($json !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($json["results"] as $news){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($news["image"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb = [
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $news["image"]
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}else{
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb = [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$out["news"][] = [
|
|
|
|
"title" => $news["title"],
|
|
|
|
"description" =>
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$news["excerpt"]
|
|
|
|
),
|
|
|
|
"date" => (int)$news["date"],
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"url" => $news["url"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get related searches
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.duckbar\.loadModule\(\s*\'related_searches\'\s*,\s*/',
|
|
|
|
$js,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
if(count($js_tmp) > 1){
|
|
|
|
|
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($json !== null){
|
|
|
|
|
|
|
|
foreach($json["results"] as $related){
|
|
|
|
|
|
|
|
$out["related"][] = $related["text"];
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get instant answers
|
|
|
|
//
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/DDG\.duckbar\.add\(\s*/',
|
|
|
|
$html . $js,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
if(count($js_tmp) > 1){
|
|
|
|
|
|
|
|
$json =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
if($json !== null){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json = $json["data"];
|
|
|
|
$table = [];
|
|
|
|
$sublinks = [];
|
|
|
|
$description = [];
|
|
|
|
|
|
|
|
// get official website
|
|
|
|
if(
|
|
|
|
isset($json["OfficialWebsite"]) &&
|
|
|
|
$json["OfficialWebsite"] !== null
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$sublinks["Website"] = $json["OfficialWebsite"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// get sublinks & table elements
|
|
|
|
if(isset($json["Infobox"]["content"])){
|
|
|
|
foreach($json["Infobox"]["content"] as $info){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($info["data_type"] == "string"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// add table element
|
|
|
|
$table[$info["label"]] = $info["value"];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($info["data_type"] == "wd_description"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$description[] = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"type" => "quote",
|
|
|
|
"value" => $info["value"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
2024-12-17 00:31:15 -05:00
|
|
|
continue;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// add sublink
|
|
|
|
switch($info["data_type"]){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
case "official_site":
|
|
|
|
case "official_website":
|
|
|
|
$type = "Website";
|
|
|
|
break;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
case "wikipedia": $type = "Wikipedia"; break;
|
|
|
|
case "itunes": $type = "iTunes"; break;
|
|
|
|
case "amazon": $type = "Amazon"; break;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
case "imdb_title_id":
|
|
|
|
case "imdb_id":
|
|
|
|
case "imdb_name_id":
|
|
|
|
$type = "IMDb";
|
|
|
|
$delim = substr($info["value"], 0, 2);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($delim == "nm"){
|
|
|
|
|
|
|
|
$prefix = "https://www.imdb.com/name/";
|
|
|
|
}elseif($delim == "tt"){
|
|
|
|
|
|
|
|
$prefix = "https://www.imdb.com/title/";
|
|
|
|
}elseif($delim == "co"){
|
|
|
|
|
|
|
|
$prefix = "https://www.imdb.com/search/title/?companies=";
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$prefix = "https://www.imdb.com/title/";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "imdb_name_id": $prefix = "https://www.imdb.com/name/"; $type = "IMDb"; break;
|
|
|
|
case "twitter_profile": $prefix = "https://twitter.com/"; $type = "Twitter"; break;
|
|
|
|
case "instagram_profile": $prefix = "https://instagram.com/"; $type = "Instagram"; break;
|
|
|
|
case "facebook_profile": $prefix = "https://facebook.com/"; $type = "Facebook"; break;
|
|
|
|
case "spotify_artist_id": $prefix = "https://open.spotify.com/artist/"; $type = "Spotify"; break;
|
|
|
|
case "itunes_artist_id": $prefix = "https://music.apple.com/us/artist/"; $type = "iTunes"; break;
|
|
|
|
case "rotten_tomatoes": $prefix = "https://rottentomatoes.com/"; $type = "Rotten Tomatoes"; break;
|
|
|
|
case "youtube_channel": $prefix = "https://youtube.com/channel/"; $type = "YouTube"; break;
|
|
|
|
case "soundcloud_id": $prefix = "https://soundcloud.com/"; $type = "SoundCloud"; break;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
default:
|
|
|
|
$prefix = null;
|
|
|
|
$type = false;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($type !== false){
|
|
|
|
|
|
|
|
if($prefix === null){
|
|
|
|
|
|
|
|
$sublinks[$type] = $info["value"];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$sublinks[$type] = $prefix . $info["value"];
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($json["Abstract"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$description[] =
|
2023-07-22 13:41:14 -05:00
|
|
|
[
|
|
|
|
"type" => "text",
|
2024-12-17 00:31:15 -05:00
|
|
|
"value" => $json["Abstract"]
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
$out["answer"][] = [
|
|
|
|
"title" => $json["Heading"],
|
|
|
|
"description" => $description,
|
|
|
|
"url" => $json["AbstractURL"],
|
|
|
|
"thumb" =>
|
|
|
|
(isset($json["Image"]) && $json["Image"]) !== null ?
|
|
|
|
"https://duckduckgo.com" . $json["Image"] : null,
|
|
|
|
"table" => $table,
|
|
|
|
"sublink" => $sublinks
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["extendedsearch"] == "no"){
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
//
|
|
|
|
// Get wordnik definition
|
|
|
|
//
|
|
|
|
//nrj('/js/spice/dictionary/definition/create', null, null, null, null, 'dictionary_definition');
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
preg_match(
|
2024-12-17 00:31:15 -05:00
|
|
|
'/nrj\(\s*\'([^\']+)\'/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$js,
|
2024-12-17 00:31:15 -05:00
|
|
|
$nrj
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($nrj[1])){
|
|
|
|
|
|
|
|
$nrj = $nrj[1];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
preg_match(
|
|
|
|
'/\/js\/spice\/dictionary\/definition\/([^\/]+)/',
|
|
|
|
$nrj,
|
|
|
|
$word
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($word[1])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$word = $word[1];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// found wordnik definition & word
|
|
|
|
try{
|
|
|
|
$nik =
|
|
|
|
$this->get(
|
|
|
|
$proxy,
|
|
|
|
"https://duckduckgo.com/js/spice/dictionary/definition/" . $word,
|
|
|
|
[],
|
|
|
|
ddg::req_xhr
|
|
|
|
);
|
|
|
|
|
|
|
|
}catch(Exception $e){
|
|
|
|
|
|
|
|
// fail gracefully
|
|
|
|
return $out;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// remove javascript
|
|
|
|
$js_tmp =
|
|
|
|
preg_split(
|
|
|
|
'/ddg_spice_dictionary_definition\(\s*/',
|
|
|
|
$nik,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
if(count($js_tmp) > 1){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$nik =
|
|
|
|
json_decode(
|
|
|
|
$this->fuckhtml
|
|
|
|
->extract_json(
|
|
|
|
$js_tmp[1]
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($nik === null){
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
$answer_cat = [];
|
|
|
|
$answer = [];
|
|
|
|
|
|
|
|
foreach($nik as $snippet){
|
|
|
|
|
|
|
|
if(!isset($snippet["partOfSpeech"])){ continue; }
|
|
|
|
|
|
|
|
$push = [];
|
|
|
|
|
|
|
|
// add text snippet
|
|
|
|
if(isset($snippet["text"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$push[] = [
|
|
|
|
"type" => "text",
|
|
|
|
"value" =>
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$snippet["text"]
|
|
|
|
)
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// add example uses
|
|
|
|
if(isset($snippet["exampleUses"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($snippet["exampleUses"] as $example){
|
|
|
|
|
|
|
|
$push[] = [
|
|
|
|
"type" => "quote",
|
|
|
|
"value" => "\"" .
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$example["text"]
|
|
|
|
) . "\""
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// add citations
|
|
|
|
if(isset($snippet["citations"])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($snippet["citations"] as $citation){
|
|
|
|
|
|
|
|
$push[] = [
|
|
|
|
"type" => "quote",
|
|
|
|
"value" =>
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$citation["cite"]
|
|
|
|
) . " - " .
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$citation["source"]
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// add related words
|
|
|
|
if(isset($snippet["relatedWords"])){
|
|
|
|
|
|
|
|
$relations = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($snippet["relatedWords"] as $related){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$words = [];
|
|
|
|
foreach($related["words"] as $wrd){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$words[] =
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$wrd
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($words) !== 0){
|
|
|
|
|
|
|
|
$relations[ucfirst($related["relationshipType"]) . "s"] =
|
|
|
|
implode(", ", $words);
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($relations as $relation_title => $relation_content){
|
|
|
|
|
|
|
|
$push[] = [
|
|
|
|
"type" => "quote",
|
|
|
|
"value" => $relation_title . ": " . $relation_content
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
if(count($push) !== 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// push data to answer_cat
|
|
|
|
if(!isset($answer_cat[$snippet["partOfSpeech"]])){
|
|
|
|
|
|
|
|
$answer_cat[$snippet["partOfSpeech"]] = [];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$answer_cat[$snippet["partOfSpeech"]] =
|
|
|
|
array_merge(
|
|
|
|
$answer_cat[$snippet["partOfSpeech"]],
|
|
|
|
$push
|
|
|
|
);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($answer_cat as $answer_title => $answer_content){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$i = 0;
|
|
|
|
$answer[] = [
|
|
|
|
"type" => "title",
|
|
|
|
"value" => $answer_title
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$old_type = $answer[count($answer) - 1]["type"];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($answer_content as $ans){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(
|
|
|
|
$ans["type"] == "text" &&
|
|
|
|
$old_type == "text"
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$i++;
|
|
|
|
$c = count($answer) - 1;
|
|
|
|
|
|
|
|
// append text to existing textfield
|
|
|
|
$answer[$c] = [
|
|
|
|
"type" => "text",
|
|
|
|
"value" => $answer[$c]["value"] . "\n" . $i . ". " . $ans["value"]
|
|
|
|
];
|
|
|
|
|
|
|
|
}elseif($ans["type"] == "text"){
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
$answer[] = [
|
|
|
|
"type" => "text",
|
|
|
|
"value" => $i . ". " . $ans["value"]
|
|
|
|
];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// append normally
|
|
|
|
$answer[] = $ans;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$old_type = $ans["type"];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// yeah.. sometimes duckduckgo doesnt give us a definition back
|
|
|
|
if(count($answer) !== 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$out["answer"][] = [
|
|
|
|
"title" => ucfirst($word),
|
|
|
|
"description" => $answer,
|
|
|
|
"url" => "https://www.wordnik.com/words/" . $word,
|
|
|
|
"thumb" => null,
|
|
|
|
"table" => [],
|
|
|
|
"sublink" => []
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function image($get){
|
|
|
|
|
|
|
|
if($get["npt"]){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
[$js_link, $proxy] = $this->backend->get($get["npt"], "images");
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
}else{
|
2024-12-17 00:31:15 -05:00
|
|
|
if(strlen($get["s"]) === 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
throw new Exception("Search term is empty!");
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy = $this->backend->get_ip();
|
2024-12-17 00:31:15 -05:00
|
|
|
|
|
|
|
$filters = [];
|
|
|
|
|
|
|
|
if($get["date"] != "any"){ $filters[] = "time:{$get["date"]}"; }
|
|
|
|
if($get["size"] != "any"){ $filters[] = "size:{$get["size"]}"; }
|
|
|
|
if($get["color"] != "any"){ $filters[] = "color:{$get["color"]}"; }
|
|
|
|
if($get["type"] != "any"){ $filters[] = "type:{$get["type"]}"; }
|
|
|
|
if($get["layout"] != "any"){ $filters[] = "layout:{$get["layout"]}"; }
|
|
|
|
if($get["license"] != "any"){ $filters[] = "license:{$get["license"]}"; }
|
|
|
|
|
|
|
|
$filters = implode(",", $filters);
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
$get_filters = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"q" => $get["s"],
|
2023-07-22 13:41:14 -05:00
|
|
|
"iax" => "images",
|
|
|
|
"ia" => "images"
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($filters != ""){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$get_filters["iaf"] = $filters;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
|
|
|
|
$get_filters["kp"] = $nsfw;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
$html = $this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2023-07-22 13:41:14 -05:00
|
|
|
"https://duckduckgo.com",
|
|
|
|
$get_filters,
|
|
|
|
ddg::req_web
|
|
|
|
);
|
|
|
|
}catch(Exception $err){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
preg_match(
|
2024-12-17 00:31:15 -05:00
|
|
|
'/vqd="([0-9-]+)"/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$html,
|
|
|
|
$vqd
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!isset($vqd[1])){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to grep VQD token");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_link =
|
|
|
|
"i.js?" .
|
|
|
|
http_build_query([
|
|
|
|
"l" => $get["country"],
|
|
|
|
"o" => "json",
|
|
|
|
"q" => $get["s"],
|
|
|
|
"vqd" => $vqd[1],
|
|
|
|
"f" => $filters,
|
|
|
|
"p" => $nsfw
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json =
|
|
|
|
$this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2024-12-17 00:31:15 -05:00
|
|
|
"https://duckduckgo.com/" . $js_link,
|
|
|
|
[],
|
2023-07-22 13:41:14 -05:00
|
|
|
ddg::req_xhr
|
2024-11-07 23:37:43 -05:00
|
|
|
);
|
2024-12-17 00:31:15 -05:00
|
|
|
}catch(Exception $error){
|
|
|
|
|
|
|
|
throw new Exception("Failed to get i.js");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-11-07 23:37:43 -05:00
|
|
|
$json = json_decode($json, true);
|
|
|
|
|
|
|
|
if($json === null){
|
|
|
|
|
|
|
|
throw new Exception("Failed to decode JSON");
|
|
|
|
}
|
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"npt" => null,
|
|
|
|
"image" => []
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(!isset($json["results"])){
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get npt
|
|
|
|
if(
|
|
|
|
isset($json["next"]) &&
|
|
|
|
$json["next"] !== null
|
|
|
|
){
|
|
|
|
|
|
|
|
$vqd = null;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($vqd[1])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$vqd = $vqd[1];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$vqd = array_values($json["vqd"]);
|
|
|
|
|
|
|
|
if(count($vqd) > 0){
|
|
|
|
|
|
|
|
$vqd = $vqd[0];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($vqd !== null){
|
|
|
|
|
|
|
|
$out["npt"] =
|
|
|
|
$this->backend->store(
|
|
|
|
$json["next"] . "&vqd=" . $vqd,
|
|
|
|
"images",
|
|
|
|
$proxy
|
|
|
|
);
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// get images
|
|
|
|
foreach($json["results"] as $image){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$ratio =
|
|
|
|
$this->bingratio(
|
2024-12-17 00:31:15 -05:00
|
|
|
(int)$image["width"],
|
|
|
|
(int)$image["height"]
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$out["image"][] = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"title" => $this->titledots($image["title"]),
|
2023-07-22 13:41:14 -05:00
|
|
|
"source" => [
|
|
|
|
[
|
2024-12-17 00:31:15 -05:00
|
|
|
"url" => $image["image"],
|
|
|
|
"width" => (int)$image["width"],
|
|
|
|
"height" => (int)$image["height"]
|
2023-07-22 13:41:14 -05:00
|
|
|
],
|
|
|
|
[
|
2024-12-17 00:31:15 -05:00
|
|
|
"url" => $this->bingimg($image["thumbnail"]),
|
2023-07-22 13:41:14 -05:00
|
|
|
"width" => $ratio[0],
|
2024-12-17 00:31:15 -05:00
|
|
|
"height" => $ratio[1]
|
2023-07-22 13:41:14 -05:00
|
|
|
]
|
|
|
|
],
|
2024-12-17 00:31:15 -05:00
|
|
|
"url" => $this->unshiturl($image["url"])
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function video($get){
|
|
|
|
|
|
|
|
if($get["npt"]){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
[$js_link, $proxy] = $this->backend->get($get["npt"], "videos");
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
}else{
|
2024-12-17 00:31:15 -05:00
|
|
|
if(strlen($get["s"]) === 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
throw new Exception("Search term is empty!");
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy = $this->backend->get_ip();
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$get_filters = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"q" => $get["s"],
|
2023-07-22 13:41:14 -05:00
|
|
|
"iax" => "videos",
|
|
|
|
"ia" => "videos"
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
|
|
|
|
$get_filters["kp"] = $nsfw;
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$filters = [];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["date"] != "any"){ $filters[] = "publishedAfter:{$date}"; }
|
|
|
|
if($get["resolution"] != "any"){ $filters[] = "videoDefinition:{$resolution}"; }
|
|
|
|
if($get["duration"] != "any"){ $filters[] = "videoDuration:{$duration}"; }
|
|
|
|
if($get["license"] != "any"){ $filters[] = "videoLicense:{$license}"; }
|
|
|
|
|
|
|
|
$filters = implode(",", $filters);
|
|
|
|
|
|
|
|
if($filters != ""){
|
|
|
|
|
|
|
|
$get_filters["iaf"] = $filters;
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
try{
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$html =
|
|
|
|
$this->get(
|
|
|
|
$proxy,
|
|
|
|
"https://duckduckgo.com/",
|
|
|
|
$get_filters,
|
|
|
|
ddg::req_web
|
|
|
|
);
|
|
|
|
}catch(Exception $error){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
preg_match(
|
2024-12-17 00:31:15 -05:00
|
|
|
'/vqd="([0-9-]+)"/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$html,
|
|
|
|
$vqd
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!isset($vqd[1])){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to grep VQD token");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_link =
|
|
|
|
"v.js?" .
|
|
|
|
http_build_query([
|
|
|
|
"l" => $get["country"],
|
|
|
|
"o" => "json",
|
|
|
|
"sr" => "1",
|
|
|
|
"q" => $get["s"],
|
|
|
|
"vqd" => $vqd[1],
|
|
|
|
"f" => $filters,
|
|
|
|
"p" => $nsfw
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$json =
|
|
|
|
$this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2024-12-17 00:31:15 -05:00
|
|
|
"https://duckduckgo.com/" . $js_link,
|
|
|
|
[],
|
2023-07-22 13:41:14 -05:00
|
|
|
ddg::req_xhr
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
|
|
|
}catch(Exception $error){
|
|
|
|
|
|
|
|
throw new Exception("Failed to fetch JSON");
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode($json, true);
|
|
|
|
|
|
|
|
if($json === null){
|
|
|
|
|
|
|
|
throw new Exception("Failed to decode JSON");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"npt" => null,
|
|
|
|
"video" => [],
|
|
|
|
"author" => [],
|
|
|
|
"livestream" => [],
|
|
|
|
"playlist" => [],
|
|
|
|
"reel" => []
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(!isset($json["results"])){
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get NPT
|
|
|
|
if(
|
|
|
|
isset($json["next"]) &&
|
|
|
|
$json["next"] !== null
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$out["npt"] =
|
2023-11-07 08:04:56 -05:00
|
|
|
$this->backend->store(
|
2024-12-17 00:31:15 -05:00
|
|
|
$json["next"],
|
2023-11-07 08:04:56 -05:00
|
|
|
"videos",
|
|
|
|
$proxy
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($json["results"] as $video){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb = [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach(["large", "medium", "small"] as $contender){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(isset($video["images"][$contender])){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$thumb = [
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $this->bingimg($video["images"][$contender])
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$out["video"][] = [
|
|
|
|
"title" => $this->titledots($video["title"]),
|
|
|
|
"description" => $this->titledots($video["description"]),
|
|
|
|
"author" => [
|
|
|
|
"name" =>
|
|
|
|
(
|
|
|
|
isset($video["uploader"]) &&
|
|
|
|
$video["uploader"] != ""
|
|
|
|
) ?
|
|
|
|
$video["uploader"] : null,
|
|
|
|
"url" => null,
|
|
|
|
"avatar" => null
|
|
|
|
],
|
|
|
|
"date" =>
|
|
|
|
(
|
|
|
|
isset($video["published"]) &&
|
|
|
|
$video["published"] != ""
|
|
|
|
) ?
|
|
|
|
strtotime($video["published"]) : null,
|
|
|
|
"duration" =>
|
|
|
|
(
|
|
|
|
isset($video["duration"]) &&
|
|
|
|
$video["duration"] != ""
|
|
|
|
) ?
|
|
|
|
$this->hms2int($video["duration"]) : null,
|
|
|
|
"views" =>
|
|
|
|
isset($video["statistics"]["viewCount"]) ?
|
|
|
|
(int)$video["statistics"]["viewCount"] : null,
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"url" => $this->unshiturl($video["content"])
|
|
|
|
];
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function news($get){
|
|
|
|
|
|
|
|
if($get["npt"]){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
[$js_link, $proxy] = $this->backend->get($get["npt"], "news");
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
}else{
|
2024-12-17 00:31:15 -05:00
|
|
|
if(strlen($get["s"]) === 0){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
throw new Exception("Search term is empty!");
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy = $this->backend->get_ip();
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$get_filters = [
|
|
|
|
"q" => $get["s"],
|
2023-07-22 13:41:14 -05:00
|
|
|
"iar" => "news",
|
|
|
|
"ia" => "news"
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if($get["date"] != "any"){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$date = $get["date"];
|
|
|
|
$get_filters["df"] = $date;
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$date = "";
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
switch($get["nsfw"]){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
case "yes": $get_filters["kp"] = "-2"; break;
|
|
|
|
case "maybe": $get_filters["kp"] = "-1"; break;
|
|
|
|
case "no": $get_filters["kp"] = "1"; break;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$html =
|
|
|
|
$this->get(
|
|
|
|
$proxy,
|
|
|
|
"https://duckduckgo.com/",
|
|
|
|
$get_filters,
|
|
|
|
ddg::req_web
|
|
|
|
);
|
|
|
|
}catch(Exception $error){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to fetch search page");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
preg_match(
|
2024-12-17 00:31:15 -05:00
|
|
|
'/vqd="([0-9-]+)"/',
|
2023-07-22 13:41:14 -05:00
|
|
|
$html,
|
|
|
|
$vqd
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!isset($vqd[1])){
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
throw new Exception("Failed to grep VQD token");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$js_link =
|
|
|
|
"news.js?" .
|
|
|
|
http_build_query([
|
|
|
|
"l" => $get["country"],
|
2023-07-22 13:41:14 -05:00
|
|
|
"o" => "json",
|
|
|
|
"noamp" => "1",
|
2024-12-17 00:31:15 -05:00
|
|
|
"m" => "30",
|
|
|
|
"q" => $get["s"],
|
|
|
|
"vqd" => $vqd[1],
|
|
|
|
"p" => $get_filters["kp"],
|
|
|
|
"df" => $date,
|
|
|
|
"u" => "bing"
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
$json =
|
|
|
|
$this->get(
|
2023-11-07 08:04:56 -05:00
|
|
|
$proxy,
|
2024-12-17 00:31:15 -05:00
|
|
|
"https://duckduckgo.com/" . $js_link,
|
|
|
|
[],
|
2023-07-22 13:41:14 -05:00
|
|
|
ddg::req_xhr
|
2024-12-17 00:31:15 -05:00
|
|
|
);
|
|
|
|
}catch(Exception $error){
|
|
|
|
|
|
|
|
throw new Exception("Failed to fetch JSON");
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode($json, true);
|
|
|
|
|
|
|
|
if($json === null){
|
|
|
|
|
|
|
|
throw new Exception("Failed to decode JSON");
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$out = [
|
|
|
|
"status" => "ok",
|
|
|
|
"npt" => null,
|
|
|
|
"news" => []
|
|
|
|
];
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(!isset($json["results"])){
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get NPT
|
|
|
|
if(
|
|
|
|
isset($json["next"]) &&
|
|
|
|
$json["next"] !== null
|
|
|
|
){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$out["npt"] =
|
2023-11-07 08:04:56 -05:00
|
|
|
$this->backend->store(
|
2024-12-17 00:31:15 -05:00
|
|
|
$json["next"],
|
2023-11-07 08:04:56 -05:00
|
|
|
"news",
|
|
|
|
$proxy
|
2023-07-22 13:41:14 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
foreach($json["results"] as $news){
|
|
|
|
|
|
|
|
if(
|
|
|
|
isset($news["image"]) &&
|
|
|
|
$news["image"] != ""
|
|
|
|
){
|
|
|
|
|
|
|
|
$thumb = [
|
|
|
|
"ratio" => "16:9",
|
|
|
|
"url" => $news["image"]
|
|
|
|
];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$thumb = [
|
|
|
|
"ratio" => null,
|
|
|
|
"url" => null
|
|
|
|
];
|
|
|
|
}
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
$out["news"][] = [
|
2024-12-17 00:31:15 -05:00
|
|
|
"title" => $news["title"],
|
|
|
|
"author" =>
|
|
|
|
(
|
|
|
|
isset($news["source"]) &&
|
|
|
|
$news["source"] != ""
|
|
|
|
) ?
|
|
|
|
$news["source"] : null,
|
|
|
|
"description" =>
|
|
|
|
(
|
|
|
|
isset($news["excerpt"]) &&
|
|
|
|
$news["excerpt"] != ""
|
|
|
|
) ?
|
|
|
|
$this->fuckhtml
|
|
|
|
->getTextContent(
|
|
|
|
$news["excerpt"]
|
|
|
|
) : null,
|
|
|
|
"date" =>
|
|
|
|
isset($news["date"]) ?
|
|
|
|
(int)$news["date"] : null,
|
|
|
|
"thumb" => $thumb,
|
|
|
|
"url" => $this->unshiturl($news["url"])
|
2023-07-22 13:41:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function titledots($title){
|
|
|
|
|
|
|
|
$substr = substr($title, -3);
|
|
|
|
|
|
|
|
if(
|
|
|
|
$substr == "..." ||
|
|
|
|
$substr == "…"
|
|
|
|
){
|
|
|
|
|
|
|
|
return trim(substr($title, 0, -3));
|
|
|
|
}
|
|
|
|
|
|
|
|
return trim($title);
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
private function hms2int($time){
|
2023-11-07 08:04:56 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$parts = explode(":", $time, 3);
|
|
|
|
$time = 0;
|
2023-11-07 08:04:56 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($parts) === 3){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// hours
|
|
|
|
$time = $time + ((int)$parts[0] * 3600);
|
|
|
|
array_shift($parts);
|
2023-11-07 08:04:56 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
if(count($parts) === 2){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// minutes
|
|
|
|
$time = $time + ((int)$parts[0] * 60);
|
|
|
|
array_shift($parts);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
// seconds
|
|
|
|
$time = $time + (int)$parts[0];
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
return $time;
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
2024-12-17 00:31:15 -05:00
|
|
|
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
private function unshiturl($url){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
|
|
|
// check for domains w/out first short subdomain (ex: www.)
|
|
|
|
|
|
|
|
$domain = parse_url($url, PHP_URL_HOST);
|
|
|
|
|
|
|
|
$subdomain = preg_replace(
|
|
|
|
'/^[A-z0-9]{1,3}\./',
|
|
|
|
"",
|
|
|
|
$domain
|
|
|
|
);
|
|
|
|
|
|
|
|
switch($subdomain){
|
|
|
|
case "ebay.com.au":
|
|
|
|
case "ebay.at":
|
|
|
|
case "ebay.ca":
|
|
|
|
case "ebay.fr":
|
|
|
|
case "ebay.de":
|
|
|
|
case "ebay.com.hk":
|
|
|
|
case "ebay.ie":
|
|
|
|
case "ebay.it":
|
|
|
|
case "ebay.com.my":
|
|
|
|
case "ebay.nl":
|
|
|
|
case "ebay.ph":
|
|
|
|
case "ebay.pl":
|
|
|
|
case "ebay.com.sg":
|
|
|
|
case "ebay.es":
|
|
|
|
case "ebay.ch":
|
|
|
|
case "ebay.co.uk":
|
|
|
|
case "cafr.ebay.ca":
|
|
|
|
case "ebay.com":
|
|
|
|
case "community.ebay.com":
|
|
|
|
case "pages.ebay.com":
|
|
|
|
|
|
|
|
// remove ebay tracking elements
|
|
|
|
$old_params = parse_url($url, PHP_URL_QUERY);
|
|
|
|
parse_str($old_params, $params);
|
|
|
|
|
|
|
|
if(isset($params["mkevt"])){ unset($params["mkevt"]); }
|
|
|
|
if(isset($params["mkcid"])){ unset($params["mkcid"]); }
|
|
|
|
if(isset($params["mkrid"])){ unset($params["mkrid"]); }
|
|
|
|
if(isset($params["campid"])){ unset($params["campid"]); }
|
|
|
|
if(isset($params["customid"])){ unset($params["customid"]); }
|
|
|
|
if(isset($params["toolid"])){ unset($params["toolid"]); }
|
|
|
|
if(isset($params["_sop"])){ unset($params["_sop"]); }
|
|
|
|
if(isset($params["_dcat"])){ unset($params["_dcat"]); }
|
|
|
|
if(isset($params["epid"])){ unset($params["epid"]); }
|
|
|
|
if(isset($params["epid"])){ unset($params["oid"]); }
|
|
|
|
|
|
|
|
$params = http_build_query($params);
|
|
|
|
|
|
|
|
if(strlen($params) === 0){
|
|
|
|
$replace = "\?";
|
|
|
|
}else{
|
|
|
|
$replace = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = preg_replace(
|
|
|
|
"/" . $replace . preg_quote($old_params, "/") . "$/",
|
|
|
|
$params,
|
|
|
|
$url
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
private function bingimg($url){
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
$parse = parse_url($url);
|
|
|
|
parse_str($parse["query"], $parts);
|
2023-07-22 13:41:14 -05:00
|
|
|
|
2024-12-17 00:31:15 -05:00
|
|
|
return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
|
2023-07-22 13:41:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private function bingratio($width, $height){
|
|
|
|
|
|
|
|
$ratio = [
|
|
|
|
474 / $width,
|
|
|
|
474 / $height
|
|
|
|
];
|
|
|
|
|
|
|
|
if($ratio[0] < $ratio[1]){
|
|
|
|
|
|
|
|
$ratio = $ratio[0];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$ratio = $ratio[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
floor($width * $ratio),
|
|
|
|
floor($height * $ratio)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|