123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937 |
- <?php
- class qwant{
-
- public function __construct(){
-
- include "lib/backend.php";
- $this->backend = new backend("qwant");
- }
-
- public function getfilters($page){
-
- $base = [
- "nsfw" => [
- "display" => "NSFW",
- "option" => [
- "yes" => "Yes",
- "maybe" => "Maybe",
- "no" => "No"
- ]
- ],
- "country" => [
- "display" => "Country",
- "option" => [
- "en_US" => "United States",
- "fr_FR" => "France",
- "en_GB" => "Great Britain",
- "de_DE" => "Germany",
- "it_IT" => "Italy",
- "es_AR" => "Argentina",
- "en_AU" => "Australia",
- "es_ES" => "Spain (es)",
- "ca_ES" => "Spain (ca)",
- "cs_CZ" => "Czech Republic",
- "ro_RO" => "Romania",
- "el_GR" => "Greece",
- "zh_CN" => "China",
- "zh_HK" => "Hong Kong",
- "en_NZ" => "New Zealand",
- "fr_FR" => "France",
- "th_TH" => "Thailand",
- "ko_KR" => "South Korea",
- "sv_SE" => "Sweden",
- "nb_NO" => "Norway",
- "da_DK" => "Denmark",
- "hu_HU" => "Hungary",
- "et_EE" => "Estonia",
- "es_MX" => "Mexico",
- "es_CL" => "Chile",
- "en_CA" => "Canada (en)",
- "fr_CA" => "Canada (fr)",
- "en_MY" => "Malaysia",
- "bg_BG" => "Bulgaria",
- "fi_FI" => "Finland",
- "pl_PL" => "Poland",
- "nl_NL" => "Netherlands",
- "pt_PT" => "Portugal",
- "de_CH" => "Switzerland (de)",
- "fr_CH" => "Switzerland (fr)",
- "it_CH" => "Switzerland (it)",
- "de_AT" => "Austria",
- "fr_BE" => "Belgium (fr)",
- "nl_BE" => "Belgium (nl)",
- "en_IE" => "Ireland",
- "he_IL" => "Israel"
- ]
- ]
- ];
-
- switch($page){
-
- case "web":
- $base = array_merge(
- $base,
- [
- "time" => [
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "day" => "Past 24 hours",
- "week" => "Past week",
- "month" => "Past month"
- ]
- ],
- "extendedsearch" => [
- // no display, wont show in interface
- "option" => [
- "yes" => "Yes",
- "no" => "No"
- ]
- ]
- ]
- );
- break;
-
- case "images":
- $base = array_merge(
- $base,
- [
- "time" => [
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "day" => "Past 24 hours",
- "week" => "Past week",
- "month" => "Past month"
- ]
- ],
- "size" => [
- "display" => "Size",
- "option" => [
- "any" => "Any size",
- "large" => "Large",
- "medium" => "Medium",
- "small" => "Small"
- ]
- ],
- "color" => [
- "display" => "Color",
- "option" => [
- "any" => "Any color",
- "coloronly" => "Color only",
- "monochrome" => "Monochrome",
- "black" => "Black",
- "brown" => "Brown",
- "gray" => "Gray",
- "white" => "White",
- "yellow" => "Yellow",
- "orange" => "Orange",
- "red" => "Red",
- "pink" => "Pink",
- "purple" => "Purple",
- "blue" => "Blue",
- "teal" => "Teal",
- "green" => "Green"
- ]
- ],
- "imagetype" => [
- "display" => "Type",
- "option" => [
- "any" => "Any type",
- "animatedgif" => "Animated GIF",
- "photo" => "Photograph",
- "transparent" => "Transparent"
- ]
- ],
- "license" => [
- "display" => "License",
- "option" => [
- "any" => "Any license",
- "share" => "Non-commercial reproduction and sharing",
- "sharecommercially" => "Reproduction and sharing",
- "modify" => "Non-commercial reproduction, sharing and modification",
- "modifycommercially" => "Reproduction, sharing and modification",
- "public" => "Public domain"
- ]
- ]
- ]
- );
- break;
-
- case "videos":
- $base = array_merge(
- $base,
- [
- "order" => [
- "display" => "Order by",
- "option" => [
- "relevance" => "Relevance",
- "views" => "Views",
- "date" => "Most recent",
- ]
- ],
- "source" => [
- "display" => "Source",
- "option" => [
- "any" => "Any source",
- "youtube" => "YouTube",
- "dailymotion" => "Dailymotion",
- ]
- ]
- ]
- );
- break;
-
- case "news":
- $base = array_merge(
- $base,
- [
- "time" => [
- "display" => "Time posted",
- "option" => [
- "any" => "Any time",
- "hour" => "Less than 1 hour ago",
- "day" => "Past 24 hours",
- "week" => "Past week",
- "month" => "Past month"
- ]
- ],
- "order" => [
- "display" => "Order by",
- "option" => [
- "relevance" => "Relevance",
- "date" => "Most recent"
- ]
- ]
- ]
- );
- break;
- }
-
- return $base;
- }
-
- private function get($proxy, $url, $get = []){
-
- $headers = [
- "User-Agent: " . config::USER_AGENT,
- "Accept: application/json, text/plain, */*",
- "Accept-Language: en-US,en;q=0.5",
- "Accept-Encoding: gzip",
- "DNT: 1",
- "Connection: keep-alive",
- "Origin: https://www.qwant.com",
- "Referer: https://www.qwant.com/",
- "Sec-Fetch-Dest: empty",
- "Sec-Fetch-Mode: cors",
- "Sec-Fetch-Site: same-site",
- "TE: trailers"
- ];
-
- $curlproc = curl_init();
-
- if($get !== []){
- $get = http_build_query($get);
- $url .= "?" . $get;
- }
-
- curl_setopt($curlproc, CURLOPT_URL, $url);
-
- curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
- curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
-
- // Bypass HTTP/2 check
- curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
-
- 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"]){
-
- // get next page data
- [$params, $proxy] = $this->backend->get($get["npt"], "web");
-
- $params = json_decode($params, true);
-
- }else{
-
- // get _GET data instead
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- if(strlen($search) > 2048){
-
- throw new Exception("Search term is too long!");
- }
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "q" => $search,
- "freshness" => $get["time"],
- "count" => 10,
- "locale" => $get["country"],
- "offset" => 0,
- "device" => "desktop",
- "tgp" => 3,
- "safesearch" => 0,
- "displayed" => "true"
- ];
-
- switch($get["nsfw"]){
-
- case "yes": $params["safesearch"] = 0; break;
- case "maybe": $params["safesearch"] = 1; break;
- case "no": $params["safesearch"] = 2; break;
- }
- }
- /*
- $handle = fopen("scraper/qwant_web.json", "r");
- $json = fread($handle, filesize("scraper/qwant_web.json"));
- fclose($handle);*/
-
- try{
- $json =
- $this->get(
- $proxy,
- "https://fdn.qwant.com/v3/search/web",
- $params
- );
-
- }catch(Exception $error){
-
- throw new Exception("Could not fetch JSON");
- }
-
- $json = json_decode($json, true);
-
- if($json === NULL){
-
- throw new Exception("Failed to decode JSON");
- }
-
- if(isset($json["data"]["message"][0])){
-
- throw new Exception("Server returned an error:\n" . $json["data"]["message"][0]);
- }
-
- $out = [
- "status" => "ok",
- "spelling" => [
- "type" => "no_correction",
- "using" => null,
- "correction" => null
- ],
- "npt" => null,
- "answer" => [],
- "web" => [],
- "image" => [],
- "video" => [],
- "news" => [],
- "related" => []
- ];
-
- if(
- $json["status"] != "success" &&
- $json["data"]["error_code"] === 5
- ){
-
- // no results
- return $out;
- }
-
- $this->detect_errors($json);
-
- if(!isset($json["data"]["result"]["items"]["mainline"])){
-
- throw new Exception("Server did not return a result object");
- }
-
- // data is OK, parse
-
- // get instant answer
- if(
- $get["extendedsearch"] == "yes" &&
- isset($json["data"]["result"]["items"]["sidebar"][0]["endpoint"])
- ){
-
- try{
- $answer =
- $this->get(
- $proxy,
- "https://api.qwant.com/v3" .
- $json["data"]["result"]["items"]["sidebar"][0]["endpoint"],
- []
- );
-
- $answer = json_decode($answer, true);
-
- if(
- $answer === null ||
- $answer["status"] != "success" ||
- $answer["data"]["result"] === null
- ){
-
- throw new Exception();
- }
-
- // parse answer
- $out["answer"][] = [
- "title" => $answer["data"]["result"]["title"],
- "description" => [
- [
- "type" => "text",
- "value" => $this->trimdots($answer["data"]["result"]["description"])
- ]
- ],
- "url" => $answer["data"]["result"]["url"],
- "thumb" =>
- $answer["data"]["result"]["thumbnail"]["landscape"] == null ?
- null :
- $this->unshitimage(
- $answer["data"]["result"]["thumbnail"]["landscape"],
- false
- ),
- "table" => [],
- "sublink" => []
- ];
-
- }catch(Exception $error){
-
- // do nothing in case of failure
- }
-
- }
-
- // get word correction
- if(isset($json["data"]["query"]["queryContext"]["alteredQuery"])){
-
- $out["spelling"] = [
- "type" => "including",
- "using" => $json["data"]["query"]["queryContext"]["alteredQuery"],
- "correction" => $json["data"]["query"]["queryContext"]["alterationOverrideQuery"]
- ];
- }
-
- // check for next page
- if($json["data"]["result"]["lastPage"] === false){
-
- $params["offset"] = $params["offset"] + 10;
-
- $out["npt"] =
- $this->backend->store(
- json_encode($params),
- "web",
- $proxy
- );
- }
-
- // parse results
- foreach($json["data"]["result"]["items"]["mainline"] as $item){
-
- switch($item["type"]){ // ignores ads
-
- case "web":
-
- $first_iteration = true;
- foreach($item["items"] as $result){
-
- if(isset($result["thumbnailUrl"])){
-
- $thumb = [
- "url" => $this->unshitimage($result["thumbnailUrl"]),
- "ratio" => "16:9"
- ];
- }else{
-
- $thumb = [
- "url" => null,
- "ratio" => null
- ];
- }
-
- $sublinks = [];
- if(isset($result["links"])){
-
- foreach($result["links"] as $link){
-
- $sublinks[] = [
- "title" => $this->trimdots($link["title"]),
- "date" => null,
- "description" => isset($link["desc"]) ? $this->trimdots($link["desc"]) : null,
- "url" => $link["url"]
- ];
- }
- }
-
- // detect gibberish results
- if(
- $first_iteration &&
- !isset($result["urlPingSuffix"])
- ){
-
- throw new Exception("Qwant returned gibberish results");
- }
-
- $out["web"][] = [
- "title" => $this->trimdots($result["title"]),
- "description" => $this->trimdots($result["desc"]),
- "url" => $result["url"],
- "date" => null,
- "type" => "web",
- "thumb" => $thumb,
- "sublink" => $sublinks,
- "table" => []
- ];
-
- $first_iteration = false;
- }
- break;
-
- case "images":
- foreach($item["items"] as $image){
-
- $out["image"][] = [
- "title" => $image["title"],
- "source" => [
- [
- "url" => $image["media"],
- "width" => (int)$image["width"],
- "height" => (int)$image["height"]
- ],
- [
- "url" => $this->unshitimage($image["thumbnail"]),
- "width" => $image["thumb_width"],
- "height" => $image["thumb_height"]
- ]
- ],
- "url" => $image["url"]
- ];
- }
- break;
-
- case "videos":
- foreach($item["items"] as $video){
-
- $out["video"][] = [
- "title" => $video["title"],
- "description" => null,
- "date" => (int)$video["date"],
- "duration" => $video["duration"] === null ? null : $video["duration"] / 1000,
- "views" => null,
- "thumb" =>
- $video["thumbnail"] === null ?
- [
- "url" => null,
- "ratio" => null,
- ] :
- [
- "url" => $this->unshitimage($video["thumbnail"]),
- "ratio" => "16:9",
- ],
- "url" => $video["url"]
- ];
- }
- break;
-
- case "related_searches":
- foreach($item["items"] as $related){
-
- $out["related"][] = $related["text"];
- }
- break;
- }
- }
-
- return $out;
- }
-
-
- public function image($get){
-
- if($get["npt"]){
-
- [$params, $proxy] =
- $this->backend->get(
- $get["npt"],
- "images"
- );
-
- $params = json_decode($params, true);
- }else{
-
- $search = $get["s"];
-
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $proxy = $this->backend->get_ip();
-
- $params = [
- "t" => "images",
- "q" => $search,
- "count" => 125,
- "locale" => $get["country"],
- "offset" => 0, // increment by 125
- "device" => "desktop",
- "tgp" => 3
- ];
-
- if($get["time"] != "any"){
-
- $params["freshness"] = $get["time"];
- }
-
- foreach(["size", "color", "imagetype", "license"] as $p){
-
- if($get[$p] != "any"){
-
- $params[$p] = $get[$p];
- }
- }
-
- switch($get["nsfw"]){
-
- case "yes": $params["safesearch"] = 0; break;
- case "maybe": $params["safesearch"] = 1; break;
- case "no": $params["safesearch"] = 2; break;
- }
- }
-
- try{
- $json = $this->get(
- $proxy,
- "https://api.qwant.com/v3/search/images",
- $params,
- );
- }catch(Exception $err){
-
- throw new Exception("Failed to get JSON");
- }
-
- /*
- $handle = fopen("scraper/yandex.json", "r");
- $json = fread($handle, filesize("scraper/yandex.json"));
- fclose($handle);*/
-
- $json = json_decode($json, true);
-
- if($json === null){
-
- throw new Exception("Failed to decode JSON");
- }
-
- $this->detect_errors($json);
-
- if(isset($json["data"]["result"]["items"]["mainline"])){
-
- throw new Exception("Qwant returned gibberish results");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "image" => []
- ];
-
- if($json["data"]["result"]["lastPage"] === false){
-
- $params["offset"] = $params["offset"] + 125;
-
- $out["npt"] = $this->backend->store(
- json_encode($params),
- "images",
- $proxy
- );
- }
-
- foreach($json["data"]["result"]["items"] as $image){
-
- $out["image"][] = [
- "title" => $this->trimdots($image["title"]),
- "source" => [
- [
- "url" => $image["media"],
- "width" => $image["width"],
- "height" => $image["height"]
- ],
- [
- "url" => $this->unshitimage($image["thumbnail"]),
- "width" => $image["thumb_width"],
- "height" => $image["thumb_height"]
- ]
- ],
- "url" => $image["url"]
- ];
- }
-
- return $out;
- }
-
- public function video($get){
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $params = [
- "t" => "videos",
- "q" => $search,
- "count" => 50,
- "locale" => $get["country"],
- "offset" => 0, // dont implement pagination
- "device" => "desktop",
- "tgp" => 3
- ];
-
- switch($get["nsfw"]){
-
- case "yes": $params["safesearch"] = 0; break;
- case "maybe": $params["safesearch"] = 1; break;
- case "no": $params["safesearch"] = 2; break;
- }
-
- try{
- $json =
- $this->get(
- $this->backend->get_ip(),
- "https://api.qwant.com/v3/search/videos",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Could not fetch JSON");
- }
-
- /*
- $handle = fopen("scraper/yandex-video.json", "r");
- $json = fread($handle, filesize("scraper/yandex-video.json"));
- fclose($handle);
- */
-
- $json = json_decode($json, true);
-
- if($json === null){
-
- throw new Exception("Could not parse JSON");
- }
-
- $this->detect_errors($json);
-
- if(isset($json["data"]["result"]["items"]["mainline"])){
-
- throw new Exception("Qwant returned gibberish results");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "video" => [],
- "author" => [],
- "livestream" => [],
- "playlist" => [],
- "reel" => []
- ];
-
- foreach($json["data"]["result"]["items"] as $video){
-
- if(empty($video["thumbnail"])){
-
- $thumb = [
- "url" => null,
- "ratio" => null
- ];
- }else{
-
- $thumb = [
- "url" => $this->unshitimage($video["thumbnail"], false),
- "ratio" => "16:9"
- ];
- }
-
- $duration = (int)$video["duration"];
-
- $out["video"][] = [
- "title" => $video["title"],
- "description" => $this->limitstrlen($video["desc"]),
- "author" => [
- "name" => $video["channel"],
- "url" => null,
- "avatar" => null
- ],
- "date" => (int)$video["date"],
- "duration" => $duration === 0 ? null : $duration,
- "views" => null,
- "thumb" => $thumb,
- "url" => preg_replace("/\?syndication=.+/", "", $video["url"])
- ];
- }
-
- return $out;
- }
-
- public function news($get){
-
- $search = $get["s"];
- if(strlen($search) === 0){
-
- throw new Exception("Search term is empty!");
- }
-
- $params = [
- "t" => "news",
- "q" => $search,
- "count" => 50,
- "locale" => $get["country"],
- "offset" => 0, // dont implement pagination
- "device" => "desktop",
- "tgp" => 3
- ];
-
- switch($get["nsfw"]){
-
- case "yes": $params["safesearch"] = 0; break;
- case "maybe": $params["safesearch"] = 1; break;
- case "no": $params["safesearch"] = 2; break;
- }
-
- try{
- $json =
- $this->get(
- $this->backend->get_ip(),
- "https://api.qwant.com/v3/search/news",
- $params
- );
- }catch(Exception $error){
-
- throw new Exception("Could not fetch JSON");
- }
-
- /*
- $handle = fopen("scraper/yandex-video.json", "r");
- $json = fread($handle, filesize("scraper/yandex-video.json"));
- fclose($handle);
- */
-
- $json = json_decode($json, true);
-
- if($json === null){
-
- throw new Exception("Could not parse JSON");
- }
-
- $this->detect_errors($json);
-
- if(isset($json["data"]["result"]["items"]["mainline"])){
-
- throw new Exception("Qwant returned gibberish results");
- }
-
- $out = [
- "status" => "ok",
- "npt" => null,
- "news" => []
- ];
-
- foreach($json["data"]["result"]["items"] as $news){
-
- if(empty($news["media"][0]["pict_big"]["url"])){
-
- $thumb = [
- "url" => null,
- "ratio" => null
- ];
- }else{
-
- $thumb = [
- "url" => $this->unshitimage($news["media"][0]["pict_big"]["url"], false),
- "ratio" => "16:9"
- ];
- }
-
- $out["news"][] = [
- "title" => $news["title"],
- "author" => $news["press_name"],
- "description" => $this->trimdots($news["desc"]),
- "date" => (int)$news["date"],
- "thumb" => $thumb,
- "url" => $news["url"]
- ];
- }
-
- return $out;
- }
-
- private function detect_errors($json){
-
- if(
- isset($json["status"]) &&
- $json["status"] == "error"
- ){
-
- if(isset($json["data"]["error_data"]["captchaUrl"])){
-
- throw new Exception("Qwant returned a captcha");
- }elseif(isset($json["data"]["error_data"]["error_code"])){
-
- throw new Exception(
- "Qwant returned an API error: " .
- $json["data"]["error_data"]["error_code"]
- );
- }
-
- throw new Exception("Qwant returned an API error");
- }
- }
-
- private function limitstrlen($text){
-
- return explode("\n", wordwrap($text, 300, "\n"))[0];
- }
-
- private function trimdots($text){
-
- return trim($text, ". ");
- }
-
- private function unshitimage($url, $is_bing = true){
-
- // https://s1.qwant.com/thumbr/0x0/8/d/f6de4deb2c2b12f55d8bdcaae576f9f62fd58a05ec0feeac117b354d1bf5c2/th.jpg?u=https%3A%2F%2Fwww.bing.com%2Fth%3Fid%3DOIP.vvDWsagzxjoKKP_rOqhwrQAAAA%26w%3D160%26h%3D160%26c%3D7%26pid%3D5.1&q=0&b=1&p=0&a=0
- parse_str(parse_url($url)["query"], $parts);
-
- if($is_bing){
- $parse = parse_url($parts["u"]);
- parse_str($parse["query"], $parts);
-
- return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
- }
-
- return $parts["u"];
- }
- }
|