| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529 |
- <?php
- class startpage{
-
- public function __construct(){
-
- include "lib/backend.php";
- $this->backend = new backend("startpage");
-
- include "lib/fuckhtml.php";
- $this->fuckhtml = new fuckhtml();
- }
-
- public function getfilters($page){
-
- switch($page){
- case "web":
- return [
- "country" => [
- "display" => "Country",
- "option" => [
- "any" => "All Regions",
- "es_AR" => "Argentina",
- "en_AU" => "Australia",
- "de_AT" => "Austria",
- "ru_BY" => "Belarus",
- "fr_BE" => "Belgium (FR)",
- "nl_BE" => "Belgium (NL)",
- "bg_BG" => "Bulgaria",
- "en_CA" => "Canada (EN)",
- "fr_CA" => "Canada (FR)",
- "es_CL" => "Chile",
- "es_CO" => "Colombia",
- "cs_CZ" => "Czech Republic",
- "da_DK" => "Denmark",
- "ar_EG" => "Egypt",
- "et_EE" => "Estonia",
- "fi_FI" => "Finland",
- "fr_FR" => "France",
- "de_DE" => "Germany",
- "el_GR" => "Greece",
- "hu_HU" => "Hungary",
- "hi_IN" => "India (HI)",
- "en_IN" => "India (EN)",
- "id_ID" => "Indonesia (ID)",
- "en_ID" => "Indonesia (EN)",
- "en_IE" => "Ireland",
- "it_IT" => "Italy",
- "ja_JP" => "Japan",
- "ko_KR" => "Korea",
- "ms_MY" => "Malaysia (MS)",
- "en_MY" => "Malaysia (EN)",
- "es_MX" => "Mexico",
- "nl_NL" => "Netherlands",
- "en_NZ" => "New Zealand",
- "no_NO" => "Norway",
- "es_PE" => "Peru",
- "fil_PH" => "Philippines (FIL)",
- "en_PH" => "Philippines (EN)",
- "pl_PL" => "Poland",
- "pt_PT" => "Portugal",
- "ro_RO" => "Romania",
- "ru_RU" => "Russia",
- "ms_SG" => "Singapore (MS)",
- "en_SG" => "Singapore (EN)",
- "es_ES" => "Spain (ES)",
- "ca_ES" => "Spain (CA)",
- "sv_SE" => "Sweden",
- "de_CH" => "Switzerland (DE)",
- "fr_CH" => "Switzerland (FR)",
- "it_CH" => "Switzerland (IT)",
- "tr_TR" => "Turkey",
- "uk_UA" => "Ukraine",
- "en_US" => "US (EN)",
- "es_US" => "US (ES)",
- "es_UY" => "Uruguay",
- "es_VE" => "Venezuela",
- "vi_VN" => "Vietnam (VI)",
- "en_VN" => "Vietnam (EN)",
- "en_ZA" => "South Africa"
- ]
- ],
- "nsfw" => [ // qadf
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes", // qadf=none
- "no" => "No" // qadf=heavy
- ]
- ],
- "time" => [ // with_date
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "d" => "Past 24 hours",
- "w" => "Past week",
- "m" => "Past month",
- "y" => "Past year",
- ]
- ],
- "extendedsearch" => [
- // undefined display, so it wont show in frontend
- "option" => [
- "yes" => "Yes",
- "no" => "No"
- ]
- ]
- ];
- break;
-
- case "images":
- return [
- "nsfw" => [ // qadf
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes", // qadf=none
- "no" => "No" // qadf=heavy
- ]
- ],
- "size" => [ // flimgsize
- "display" => "Size",
- "option" => [
- "any" => "Any size",
- "Small" => "Small",
- "Medium" => "Medium",
- "Large" => "Large",
- "Wallpaper" => "Wallpaper",
- // from here, image-size-select, var prefix = isz:lt,islt:
- "qsvgs" => "Larger than 400x300",
- "vga" => "Larger than 640x480",
- "svga" => "Larger than 800x600",
- "xga" => "Larger than 1024x768",
- "qsvgs" => "Larger than 400x300",
- "2mp" => "Larger than 2 MP (1600x1200)",
- "4mp" => "Larger than 4 MP (2272x1704)",
- "6mp" => "Larger than 6 MP (2816x2112)",
- "8mp" => "Larger than 8 MP (3264x2448)",
- "10mp" => "Larger than 10 MP (3648x2736)",
- "12mp" => "Larger than 12 MP (4096x3072)",
- "15mp" => "Larger than 15 MP (4480x3360)",
- "20mp" => "Larger than 20 MP (5120x3840)",
- "40mp" => "Larger than 40 MP (7216x5412)",
- "70mp" => "Larger than 70 MP (9600x7200)"
- ]
- ],
- "color" => [ // flimgcolor
- "display" => "Color",
- "option" => [
- "any" => "Any color",
- // from here, var prefix = ic:
- "color" => "Color only",
- "bnw" => "Black & white", // set to "gray"
- // from here, var prefix = ic:specific,isc:
- "red" => "Red",
- "orange" => "Orange",
- "yellow" => "Yellow",
- "green" => "Green",
- "teal" => "Teal",
- "blue" => "Blue",
- "purple" => "Purple",
- "pink" => "Pink",
- "white" => "White",
- "gray" => "Gray",
- "black" => "Black",
- "brown" => "Brown"
- ]
- ],
- "type" => [ // flimgtype
- "display" => "Type",
- "option" => [
- "any" => "Any type",
- "AnimatedGif" => "Animated GIF",
- "Clipart" => "Clip Art",
- "Line" => "Line Drawing",
- "Photo" => "Photograph",
- "Transparent" => "Transparent Background"
- ]
- ],
- "license" => [ // flimglicense
- "display" => "License",
- "option" => [
- "any" => "Any license",
- "p" => "Public domain",
- "s" => "Free to share",
- "sc" => "Free to share commercially",
- "m" => "Free to modify",
- "mc" => "Free to modify commercially"
- ]
- ]
- ];
- break;
-
- case "videos":
- return [
- "nsfw" => [ // qadf
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes", // qadf=none
- "no" => "No" // qadf=heavy
- ]
- ],
- "sort" => [
- "display" => "Sort by",
- "option" => [
- "relevance" => "Most relevant",
- "popular" => "Most popular",
- "recent" => "Most recent"
- ]
- ],
- "duration" => [ // with_duration
- "display" => "Duration",
- "option" => [
- "any" => "Any duration",
- "short" => "Short",
- "medium" => "Medium",
- "long" => "Long"
- ]
- ]
- ];
- break;
-
- case "news":
- return [
- "nsfw" => [ // qadf
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes", // qadf=none
- "no" => "No" // qadf=heavy
- ]
- ],
- "time" => [ // with_date
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "d" => "Past 24 hours",
- "w" => "Past week",
- "m" => "Past month"
- ]
- ]
- ];
- break;
-
- //preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEazerbaijaniN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius; Domain=startpage.com; Expires=Mon, 28 Oct 2024 20:21:58 GMT; Secure; Path=/
- //preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius; Domain=startpage.com; Expires=Mon, 28 Oct 2024 20:22:52 GMT; Secure; Path=/
- }
- }
-
- private function get($proxy, $url, $get = [], $post = false, $is_xhr = false){
-
- $curlproc = curl_init();
-
- if($post === true){
-
- curl_setopt($curlproc, CURLOPT_POST, true);
- curl_setopt($curlproc, CURLOPT_POSTFIELDS, $get);
-
- }elseif($get !== []){
-
- $get = http_build_query($get);
- $url .= "?" . $get;
- }
-
- curl_setopt($curlproc, CURLOPT_URL, $url);
-
- // http2 bypass
- curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
-
- curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
-
- if($is_xhr === true){
-
- curl_setopt($curlproc, CURLOPT_HTTPHEADER,
- ["User-Agent: " . config::USER_AGENT,
- "Accept: application/json",
- "Accept-Language: en-US,en;q=0.5",
- "Accept-Encoding: gzip",
- "Referer: https://www.startpage.com/",
- "Content-Type: application/json",
- "Content-Length: " . strlen($get),
- "Origin: https://www.startpage.com/",
- "DNT: 1",
- "Connection: keep-alive",
- "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
- "Sec-Fetch-Dest: empty",
- "Sec-Fetch-Mode: cors",
- "Sec-Fetch-Site: same-origin",
- "TE: trailers"]
- );
-
- }elseif($post === true){
-
- curl_setopt($curlproc, CURLOPT_HTTPHEADER,
- ["User-Agent: " . config::USER_AGENT,
- "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",
- "Referer: https://www.startpage.com/",
- "Content-Type: application/x-www-form-urlencoded",
- "Content-Length: " . strlen($get),
- "DNT: 1",
- "Connection: keep-alive",
- "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
- "Upgrade-Insecure-Requests: 1",
- "Sec-Fetch-Dest: document",
- "Sec-Fetch-Mode: navigate",
- "Sec-Fetch-Site: none",
- "Sec-Fetch-User: ?1",
- "Priority: u=0, i",
- "TE: trailers"]
- );
- }else{
-
- curl_setopt($curlproc, CURLOPT_HTTPHEADER,
- ["User-Agent: " . config::USER_AGENT,
- "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",
- "DNT: 1",
- "Connection: keep-alive",
- "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
- "Sec-Fetch-Dest: document",
- "Sec-Fetch-Mode: navigate",
- "Sec-Fetch-Site: none",
- "Sec-Fetch-User: ?1",
- "Priority: u=0, i",
- "TE: trailers"]
- );
- }
-
- 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);
-
- $this->backend->assign_proxy($curlproc, $proxy);
-
- $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"]){
-
- [$post, $proxy] = $this->backend->get($get["npt"], "web");
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $post,
- true
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
-
- $get_instant_answer = false;
-
- }else{
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "query" => $get["s"],
- "cat" => "web",
- "pl" => "opensearch"
- ];
-
- if($get["nsfw"] == "no"){
-
- $params["qadf"] = "heavy";
- $get_instant_answer = false;
- }else{
-
- $get_instant_answer = true;
- }
-
- if($get["country"] !== "any"){
-
- $params["qsr"] = $get["country"];
- }
-
- if($get["time"] !== "any"){
-
- $params["with_date"] = $get["time"];
- }
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
-
- //$html = file_get_contents("scraper/startpage.html");
- }
-
- if(
- preg_match(
- '/React\.createElement\(UIStartpage\.AppSerpWeb, ?(.+)\),$/m',
- $html,
- $matches
- ) === 0
- ){
-
- throw new Exception("Failed to grep JSON object");
- }
-
- $json = json_decode($matches[1], true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON");
- }
-
- //print_r($json);
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- // get npt
- $out["npt"] = $this->parse_npt($json, "web", $proxy);
-
- foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
-
- if(!isset($category["display_type"])){
-
- continue;
- }
-
- switch($category["display_type"]){
-
- case "web-google":
- foreach($category["results"] as $result){
-
- $sublinks = [];
-
- foreach($result["siteLinks"] as $sublink){
-
- $sublinks[] = [
- "title" => $sublink["title"],
- "description" => null,
- "url" => $sublink["clickUrl"]
- ];
- }
-
- $description =
- explode(
- "...",
- $this->titledots(
- html_entity_decode(
- $this->fuckhtml
- ->getTextContent(
- $result["description"]
- )
- )
- ),
- 2
- );
-
- $date = strtotime(trim($description[0]));
-
- if(
- $date === false ||
- count($description) !== 2 ||
- strlen($description[0]) > 14
- ){
-
- // no date found
- $description =
- implode(
- " ... ",
- $description
- );
-
- $date = null;
- }else{
-
- // date found
- $description = ltrim($description[1]);
- }
-
- $out["web"][] = [
- "title" =>
- $this->titledots(
- html_entity_decode(
- $this->fuckhtml
- ->getTextContent(
- $result["title"]
- )
- )
- ),
- "description" => $description,
- "url" => $result["clickUrl"],
- "date" => $date,
- "type" => "web",
- "thumb" => [
- "url" => null,
- "ratio" => null
- ],
- "sublink" => $sublinks,
- "table" => []
- ];
- }
- break;
-
- case "images-qi-top":
- foreach($category["results"] as $result){
-
- $out["image"][] = [
- "title" =>
- $this->titledots(
- html_entity_decode(
- $this->fuckhtml
- ->getTextContent(
- $result["title"]
- )
- )
- ),
- "source" => [
- [
- "url" => $result["rawImageUrl"],
- "width" => (int)$result["width"],
- "height" => (int)$result["height"]
- ],
- [
- "url" => $this->unshitimage($result["mdThumbnailUrl"]),
- "width" => (int)$result["mdThumbnailWidth"],
- "height" => (int)$result["mdThumbnailHeight"]
- ]
- ],
- "url" =>
- $result["altClickUrl"]
- ];
- }
- break;
-
- case "spellsuggest-google":
- $out["spelling"] =
- [
- "type" => "including",
- "using" => $json["render"]["query"],
- "correction" => $category["results"][0]["query"]
- ];
- break;
-
- case "dictionary-qi":
- foreach($category["results"] as $result){
-
- $answer = [
- "title" => $result["word"],
- "description" => [],
- "url" => null,
- "thumb" => null,
- "table" => [],
- "sublink" => []
- ];
-
- foreach($result["lexical_categories"] as $lexic_type => $definitions){
-
- $answer["description"][] = [
- "type" => "title",
- "value" => $lexic_type
- ];
-
- $i = 0;
-
- foreach($definitions as $definition){
-
- $text_definition = trim($definition["definition"]);
- $text_example = trim($definition["example"]);
- $text_synonyms = implode(", ", $definition["synonyms"]);
-
- if($text_definition != ""){
-
- $i++;
-
- $c = count($answer["description"]) - 1;
- if(
- $c !== 0 &&
- $answer["description"][$c]["type"] == "text"
- ){
-
- $answer["description"][$c]["value"] .=
- "\n\n" . $i . ". " . $text_definition;
-
- }else{
-
- $answer["description"][] = [
- "type" => "text",
- "value" => $i . ". " . $text_definition
- ];
- }
- }
-
- if($text_example != ""){
-
- $answer["description"][] = [
- "type" => "quote",
- "value" => $text_example
- ];
- }
-
- if($text_synonyms != ""){
-
- $answer["description"][] = [
- "type" => "text",
- "value" => "Synonyms: " . $text_synonyms
- ];
- }
- }
- }
-
- $out["answer"][] = $answer;
- }
- break;
- }
- }
-
- // parse instant answers
- if(
- $get["extendedsearch"] == "yes" &&
- $get_instant_answer === true
- ){
-
- // https://www.startpage.com/sp/qi?qimsn=ex&sxap=%2Fv1%2Fquery&sc=BqZ3inqrAgF701&sr=1
- try{
- $post = [
- "se" => "n0vze2y9dqwy",
- "q" => $json["render"]["query"],
- "results" => [], // populate
- "enableKnowledgePanel" => true,
- "enableMediaThumbBar" => false,
- "enableSearchSuggestions" => false,
- "enableTripadvisorProperties" => [],
- "enableTripadvisorPlaces" => [],
- "enableTripadvisorPlacesForLocations" => [],
- "enableWebProducts" => false,
- "tripadvisorPartnerId" => null,
- "tripadvisorMapColorMode" => "light",
- "tripadvisorDisablesKnowledgePanel" => false,
- "instantAnswers" => [
- "smartAnswers",
- "youtube",
- "tripadvisor"
- ],
- "iaType" => null,
- "forceEnhancedKnowledgePanel" => false,
- "shoppingOnly" => false,
- "allowAdultProducts" => true,
- "lang" => "en",
- "browserLang" => "en-US",
- "browserTimezone" => "America/New_York",
- "market" => null,
- "userLocation" => null,
- "userDate" => date("Y-m-d"),
- "userAgentType" => "unknown"
- ];
-
- foreach($out["web"] as $result){
-
- $post["results"][] = [
- "url" => $result["url"],
- "title" => $result["title"]
- ];
- }
-
- $post = json_encode($post, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE);
-
- $additional_data =
- $this->get(
- $proxy,
- "https://www.startpage.com/sp/qi?qimsn=ex&sxap=%2Fv1%2Fquery&sc=" . $json["render"]["callback_sc"] . "&sr=1",
- $post,
- true,
- true
- );
-
- $additional_data = json_decode($additional_data, true);
-
- if($additional_data === null){
-
- throw new Exception("Failed to decode JSON"); // just break out, dont fail completely
- }
-
- if(!isset($additional_data["knowledgePanel"])){
-
- throw new Exception("Response has missing data (knowledgePanel)");
- }
-
- $additional_data = $additional_data["knowledgePanel"];
-
- $answer = [
- "title" => $additional_data["meta"]["title"],
- "description" => [
- [
- "type" => "quote",
- "value" => $additional_data["meta"]["description"]
- ]
- ],
- "url" => $additional_data["meta"]["origWikiUrl"],
- "thumb" => $additional_data["meta"]["image"],
- "table" => [],
- "sublink" => []
- ];
-
- // parse html for instant answer
- $this->fuckhtml->load($additional_data["html"]);
-
- $div =
- $this->fuckhtml
- ->getElementsByTagName(
- "div"
- );
-
- // get description
- $description =
- $this->fuckhtml
- ->getElementsByClassName(
- "sx-kp-short-extract sx-kp-short-extract-complete",
- $div
- );
-
- if(count($description) !== 0){
-
- $answer["description"][] = [
- "type" => "text",
- "value" =>
- html_entity_decode(
- $this->fuckhtml
- ->getTextContent(
- $description[0]
- )
- )
- ];
- }
-
- // get socials
- $socials =
- $this->fuckhtml
- ->getElementsByClassName(
- "sx-wiki-social-link",
- "a"
- );
-
- foreach($socials as $social){
-
- $title =
- $this->fuckhtml
- ->getTextContent(
- $social["attributes"]["title"]
- );
-
- $url =
- $this->fuckhtml
- ->getTextContent(
- $social["attributes"]["href"]
- );
-
- switch($title){
-
- case "Official Website":
- $title = "Website";
- break;
- }
-
- $answer["sublink"][$title] = $url;
- }
-
- // get videos
- $videos =
- $this->fuckhtml
- ->getElementsByClassName(
- "sx-kp-video-grid-item",
- $div
- );
-
- foreach($videos as $video){
-
- $this->fuckhtml->load($video);
-
- $as =
- $this->fuckhtml
- ->getElementsByTagName(
- "a"
- );
-
- if(count($as) === 0){
-
- // ?? invalid
- continue;
- }
-
- $image =
- $this->fuckhtml
- ->getElementsByAttributeName(
- "data-sx-src",
- "img"
- );
-
- if(count($image) !== 0){
-
- $thumb = [
- "ratio" => "16:9",
- "url" =>
- $this->fuckhtml
- ->getTextContent(
- $image[0]["attributes"]["data-sx-src"]
- )
- ];
- }else{
- $thumb = [
- "ratio" => null,
- "url" => null
- ];
- }
-
- $out["video"][] = [
- "title" =>
- $this->fuckhtml
- ->getTextContent(
- $as[0]["attributes"]["title"]
- ),
- "description" => null,
- "date" => null,
- "duration" => null,
- "views" => null,
- "thumb" => $thumb,
- "url" =>
- $this->fuckhtml
- ->getTextContent(
- $as[0]["attributes"]["href"]
- )
- ];
- }
-
- // reset
- $this->fuckhtml->load($additional_data["html"]);
-
- // get table elements
- $table =
- $this->fuckhtml
- ->getElementsByClassName(
- "sx-infobox",
- "table"
- );
-
- if(count($table) !== 0){
-
- $trs =
- $this->fuckhtml
- ->getElementsByTagName(
- "tr"
- );
-
- foreach($trs as $tr){
-
- $this->fuckhtml->load($tr);
-
- // ok so startpage devs cant fucking code a table
- // td = content
- // th (AAAHH) = title
- $tds =
- $this->fuckhtml
- ->getElementsByTagName(
- "td"
- );
-
- $ths =
- $this->fuckhtml
- ->getElementsByTagName(
- "th"
- );
-
- if(
- count($ths) === 1 &&
- count($tds) === 1
- ){
-
- $title =
- $this->fuckhtml
- ->getTextContent(
- $ths[0]
- );
-
- $description = [];
-
- $this->fuckhtml->load($tds[0]);
-
- $lis =
- $this->fuckhtml
- ->getElementsByTagName(
- "li"
- );
-
- if(count($lis) !== 0){
-
- foreach($lis as $li){
-
- $description[] =
- $this->fuckhtml
- ->getTextContent(
- $li
- );
- }
-
- $description = implode(", ", $description);
- }else{
-
- $description =
- $this->fuckhtml
- ->getTextContent(
- $tds[0]
- );
- }
-
- $answer["table"][$title] = $description;
- }
- }
- }
-
- $out["answer"][] = $answer;
-
- }catch(Exception $error){
-
- // do nothing
- //echo "error!";
- }
- }
-
- return $out;
- }
-
- public function image($get){
-
- if($get["npt"]){
-
- [$post, $proxy] = $this->backend->get($get["npt"], "images");
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $post,
- true
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
-
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- try{
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "query" => $get["s"],
- "cat" => "images",
- "pl" => "opensearch"
- ];
-
- if($get["nsfw"] == "no"){
-
- $params["qadf"] = "heavy";
- }
-
- if($get["size"] != "any"){
-
- if(
- $get["size"] == "Small" ||
- $get["size"] == "Medium" ||
- $get["size"] == "Large" ||
- $get["size"] == "Wallpaper"
- ){
-
- $params["flimgsize"] = $get["size"];
- }else{
-
- $params["image-size-select"] = "isz:lt,islt:" . $get["size"];
- }
- }
-
- if($get["color"] != "any"){
-
- if($get["color"] == "color"){
-
- $params["flimgcolor"] = "ic:color";
- }elseif($get["color"] == "bnw"){
-
- $params["flimgcolor"] = "ic:gray";
- }else{
-
- $params["flimgcolor"] = "ic:specific,isc:" . $get["color"];
- }
- }
-
- if($get["type"] != "any"){
-
- $params["flimgtype"] = $get["type"];
- }
-
- if($get["license"] != "any"){
-
- $params["flimglicense"] = $get["license"];
- }
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- //$html = file_get_contents("scraper/startpage.html");
-
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "image" => []
- ];
-
- if(
- preg_match(
- '/React\.createElement\(UIStartpage\.AppSerpImages, ?(.+)\),$/m',
- $html,
- $matches
- ) === 0
- ){
-
- throw new Exception("Failed to grep JSON object");
- }
-
- $json = json_decode($matches[1], true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON object");
- }
-
- // get npt
- $out["npt"] = $this->parse_npt($json, "images", $proxy);
-
- // get images
- foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
-
- if($category["display_type"] != "images-bing"){
-
- // ignore ads and !! suggestions !! @todo
- continue;
- }
-
- foreach($category["results"] as $image){
-
- $out["image"][] = [
- "title" => $this->titledots($image["title"]),
- "source" => [
- [
- "url" => $this->unshitimage($image["clickUrl"]),
- "width" => (int)$image["width"],
- "height" => (int)$image["height"]
- ],
- [
- "url" => $this->unshitimage($image["thumbnailUrl"]),
- "width" => (int)$image["thumbnailWidth"],
- "height" => (int)$image["thumbnailHeight"]
- ]
- ],
- "url" => $image["altClickUrl"]
- ];
- }
- }
-
- return $out;
- }
-
- public function video($get){
-
- if($get["npt"]){
-
- [$post, $proxy] = $this->backend->get($get["npt"], "videos");
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $post,
- true
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
-
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- try{
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "query" => $get["s"],
- "cat" => "video",
- "pl" => "opensearch"
- ];
-
- if($get["nsfw"] == "no"){
-
- $params["qadf"] = "heavy";
- }
-
- if($get["sort"] != "relevance"){
-
- $params["sort_by"] = $get["sort"];
- }
-
- if($get["duration"] != "any"){
-
- $params["with_duration"] = $get["duration"];
- }
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- //$html = file_get_contents("scraper/startpage.html");
-
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- }
-
- if(
- preg_match(
- '/React\.createElement\(UIStartpage\.AppSerpVideos, ?(.+)\),$/m',
- $html,
- $matches
- ) === 0
- ){
-
- throw new Exception("Failed to get JSON object");
- }
-
- $json = json_decode($matches[1], true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON object");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "video" => [],
- "author" => [],
- "livestream" => [],
- "playlist" => [],
- "reel" => []
- ];
-
- // get npt
- $out["npt"] = $this->parse_npt($json, "video", $proxy);
-
- // get results
- foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
-
- if($category["display_type"] == "video-youtube"){
-
- foreach($category["results"] as $video){
-
- if(
- isset($video["thumbnailUrl"]) &&
- $video["thumbnailUrl"] !== null
- ){
-
- $thumb = [
- "ratio" => "16:9",
- "url" => $this->unshitimage($video["thumbnailUrl"])
- ];
- }else{
-
- $thumb = [
- "ratio" => null,
- "url" => null
- ];
- }
-
- $out["video"][] = [
- "title" => $video["title"],
- "description" => $this->limitstrlen($video["description"]),
- "author" => [
- "name" => $video["channelTitle"],
- "url" => null,
- "avatar" => null
- ],
- "date" => strtotime($video["publishDate"]),
- "duration" => $this->hms2int($video["duration"]),
- "views" => (int)$video["viewCount"],
- "thumb" => $thumb,
- "url" => $video["clickUrl"]
- ];
- }
- }
- }
-
- return $out;
- }
-
- public function news($get){
-
- if($get["npt"]){
-
- [$post, $proxy] = $this->backend->get($get["npt"], "news");
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $post,
- true
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
-
- }else{
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- try{
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "query" => $get["s"],
- "cat" => "news",
- "pl" => "opensearch"
- ];
-
- if($get["nsfw"] == "no"){
-
- $params["qadf"] = "heavy";
- }
-
- if($get["time"] != "any"){
-
- $params["with_date"] = $get["time"];
- }
-
- try{
- $html = $this->get(
- $proxy,
- "https://www.startpage.com/sp/search",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- //$html = file_get_contents("scraper/startpage.html");
-
- }catch(Exception $error){
-
- throw new Exception("Failed to fetch search page");
- }
- }
-
- if(
- preg_match(
- '/React\.createElement\(UIStartpage\.AppSerpNews, ?(.+)\),$/m',
- $html,
- $matches
- ) === 0
- ){
-
- throw new Exception("Failed to get JSON object");
- }
-
- $json = json_decode($matches[1], true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON object");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "news" => []
- ];
-
- // get npt
- $out["npt"] = $this->parse_npt($json, "news", $proxy);
-
- foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
-
- if($category["display_type"] != "news-bing"){
-
- // unsupported category
- continue;
- }
-
- foreach($category["results"] as $news){
-
- if(
- isset($news["thumbnailUrl"]) &&
- $news["thumbnailUrl"] !== null
- ){
-
- $thumb = [
- "ratio" => "16:9",
- "url" => $this->unshitimage($news["thumbnailUrl"])
- ];
- }else{
-
- $thumb = [
- "ratio" => null,
- "url" => null
- ];
- }
-
- $out["news"][] = [
- "title" => $this->titledots($this->remove_penguins($news["title"])),
- "author" => $news["source"],
- "description" => $this->titledots($this->remove_penguins($news["description"])),
- "date" => (int)substr((string)$news["date"], 0, -3),
- "thumb" => $thumb,
- "url" => $news["clickUrl"]
- ];
- }
- }
-
- return $out;
- }
-
- private function parse_npt($json, $pagetype, $proxy){
-
- foreach($json["render"]["presenter"]["pagination"]["pages"] as $page){
-
- if($page["name"] == "Next"){
-
- parse_str(
- explode(
- "?",
- $page["url"],
- 2
- )[1],
- $str
- );
-
- return
- $this->backend->store(
- http_build_query(
- [
- "lui" => "english",
- "language" => "english",
- "query" => $str["q"],
- "cat" => $pagetype,
- "sc" => $str["sc"],
- "t" => "device",
- "segment" => "startpage.udog",
- "page" => $str["page"]
- ]
- ),
- $pagetype,
- $proxy
- );
-
- break;
- }
- }
-
- return null;
- }
-
- private function unshitimage($url){
-
- $query = parse_url($url, PHP_URL_QUERY);
- parse_str($query, $query);
-
- if(isset($query["piurl"])){
-
- if(strpos($query["piurl"], "gstatic.com/")){
-
- return
- explode(
- "&",
- $query["piurl"],
- 2
- )[0];
- }
-
- if(
- strpos($query["piurl"], "bing.net/") ||
- strpos($query["piurl"], "bing.com/")
- ){
-
- return
- explode(
- "&",
- $query["piurl"],
- 2
- )[0];
- }
-
- return $query["piurl"];
- }
-
- return $url;
- }
-
- private function limitstrlen($text){
-
- return
- explode(
- "\n",
- wordwrap(
- str_replace(
- ["\n\r", "\r\n", "\n", "\r"],
- " ",
- $text
- ),
- 300,
- "\n"
- ),
- 2
- )[0];
- }
-
- private function titledots($title){
-
- return trim($title, " .\t\n\r\0\x0B…");
- }
-
- private function hms2int($time){
-
- $parts = explode(":", $time, 3);
- $time = 0;
-
- if(count($parts) === 3){
-
- // hours
- $time = $time + ((int)$parts[0] * 3600);
- array_shift($parts);
- }
-
- if(count($parts) === 2){
-
- // minutes
- $time = $time + ((int)$parts[0] * 60);
- array_shift($parts);
- }
-
- // seconds
- $time = $time + (int)$parts[0];
-
- return $time;
- }
-
- private function remove_penguins($text){
-
- return str_replace(
- ["", ""],
- "",
- $text
- );
- }
- }
|