qwant.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. <?php
  2. class qwant{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("qwant");
  6. }
  7. public function getfilters($page){
  8. $base = [
  9. "nsfw" => [
  10. "display" => "NSFW",
  11. "option" => [
  12. "yes" => "Yes",
  13. "maybe" => "Maybe",
  14. "no" => "No"
  15. ]
  16. ],
  17. "country" => [
  18. "display" => "Country",
  19. "option" => [
  20. "en_US" => "United States",
  21. "fr_FR" => "France",
  22. "en_GB" => "Great Britain",
  23. "de_DE" => "Germany",
  24. "it_IT" => "Italy",
  25. "es_AR" => "Argentina",
  26. "en_AU" => "Australia",
  27. "es_ES" => "Spain (es)",
  28. "ca_ES" => "Spain (ca)",
  29. "cs_CZ" => "Czech Republic",
  30. "ro_RO" => "Romania",
  31. "el_GR" => "Greece",
  32. "zh_CN" => "China",
  33. "zh_HK" => "Hong Kong",
  34. "en_NZ" => "New Zealand",
  35. "fr_FR" => "France",
  36. "th_TH" => "Thailand",
  37. "ko_KR" => "South Korea",
  38. "sv_SE" => "Sweden",
  39. "nb_NO" => "Norway",
  40. "da_DK" => "Denmark",
  41. "hu_HU" => "Hungary",
  42. "et_EE" => "Estonia",
  43. "es_MX" => "Mexico",
  44. "es_CL" => "Chile",
  45. "en_CA" => "Canada (en)",
  46. "fr_CA" => "Canada (fr)",
  47. "en_MY" => "Malaysia",
  48. "bg_BG" => "Bulgaria",
  49. "fi_FI" => "Finland",
  50. "pl_PL" => "Poland",
  51. "nl_NL" => "Netherlands",
  52. "pt_PT" => "Portugal",
  53. "de_CH" => "Switzerland (de)",
  54. "fr_CH" => "Switzerland (fr)",
  55. "it_CH" => "Switzerland (it)",
  56. "de_AT" => "Austria",
  57. "fr_BE" => "Belgium (fr)",
  58. "nl_BE" => "Belgium (nl)",
  59. "en_IE" => "Ireland",
  60. "he_IL" => "Israel"
  61. ]
  62. ]
  63. ];
  64. switch($page){
  65. case "web":
  66. $base = array_merge(
  67. $base,
  68. [
  69. "time" => [
  70. "display" => "Time posted",
  71. "option" => [
  72. "any" => "Any time",
  73. "day" => "Past 24 hours",
  74. "week" => "Past week",
  75. "month" => "Past month"
  76. ]
  77. ],
  78. "extendedsearch" => [
  79. // no display, wont show in interface
  80. "option" => [
  81. "yes" => "Yes",
  82. "no" => "No"
  83. ]
  84. ]
  85. ]
  86. );
  87. break;
  88. case "images":
  89. $base = array_merge(
  90. $base,
  91. [
  92. "time" => [
  93. "display" => "Time posted",
  94. "option" => [
  95. "any" => "Any time",
  96. "day" => "Past 24 hours",
  97. "week" => "Past week",
  98. "month" => "Past month"
  99. ]
  100. ],
  101. "size" => [
  102. "display" => "Size",
  103. "option" => [
  104. "any" => "Any size",
  105. "large" => "Large",
  106. "medium" => "Medium",
  107. "small" => "Small"
  108. ]
  109. ],
  110. "color" => [
  111. "display" => "Color",
  112. "option" => [
  113. "any" => "Any color",
  114. "coloronly" => "Color only",
  115. "monochrome" => "Monochrome",
  116. "black" => "Black",
  117. "brown" => "Brown",
  118. "gray" => "Gray",
  119. "white" => "White",
  120. "yellow" => "Yellow",
  121. "orange" => "Orange",
  122. "red" => "Red",
  123. "pink" => "Pink",
  124. "purple" => "Purple",
  125. "blue" => "Blue",
  126. "teal" => "Teal",
  127. "green" => "Green"
  128. ]
  129. ],
  130. "imagetype" => [
  131. "display" => "Type",
  132. "option" => [
  133. "any" => "Any type",
  134. "animatedgif" => "Animated GIF",
  135. "photo" => "Photograph",
  136. "transparent" => "Transparent"
  137. ]
  138. ],
  139. "license" => [
  140. "display" => "License",
  141. "option" => [
  142. "any" => "Any license",
  143. "share" => "Non-commercial reproduction and sharing",
  144. "sharecommercially" => "Reproduction and sharing",
  145. "modify" => "Non-commercial reproduction, sharing and modification",
  146. "modifycommercially" => "Reproduction, sharing and modification",
  147. "public" => "Public domain"
  148. ]
  149. ]
  150. ]
  151. );
  152. break;
  153. case "videos":
  154. $base = array_merge(
  155. $base,
  156. [
  157. "order" => [
  158. "display" => "Order by",
  159. "option" => [
  160. "relevance" => "Relevance",
  161. "views" => "Views",
  162. "date" => "Most recent",
  163. ]
  164. ],
  165. "source" => [
  166. "display" => "Source",
  167. "option" => [
  168. "any" => "Any source",
  169. "youtube" => "YouTube",
  170. "dailymotion" => "Dailymotion",
  171. ]
  172. ]
  173. ]
  174. );
  175. break;
  176. case "news":
  177. $base = array_merge(
  178. $base,
  179. [
  180. "time" => [
  181. "display" => "Time posted",
  182. "option" => [
  183. "any" => "Any time",
  184. "hour" => "Less than 1 hour ago",
  185. "day" => "Past 24 hours",
  186. "week" => "Past week",
  187. "month" => "Past month"
  188. ]
  189. ],
  190. "order" => [
  191. "display" => "Order by",
  192. "option" => [
  193. "relevance" => "Relevance",
  194. "date" => "Most recent"
  195. ]
  196. ]
  197. ]
  198. );
  199. break;
  200. }
  201. return $base;
  202. }
  203. private function get($proxy, $url, $get = []){
  204. $headers = [
  205. "User-Agent: " . config::USER_AGENT,
  206. "Accept: application/json, text/plain, */*",
  207. "Accept-Language: en-US,en;q=0.5",
  208. "Accept-Encoding: gzip",
  209. "DNT: 1",
  210. "Connection: keep-alive",
  211. "Origin: https://www.qwant.com",
  212. "Referer: https://www.qwant.com/",
  213. "Sec-Fetch-Dest: empty",
  214. "Sec-Fetch-Mode: cors",
  215. "Sec-Fetch-Site: same-site",
  216. "TE: trailers"
  217. ];
  218. $curlproc = curl_init();
  219. if($get !== []){
  220. $get = http_build_query($get);
  221. $url .= "?" . $get;
  222. }
  223. curl_setopt($curlproc, CURLOPT_URL, $url);
  224. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  225. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  226. // Bypass HTTP/2 check
  227. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  228. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  229. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  230. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  231. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  232. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  233. $this->backend->assign_proxy($curlproc, $proxy);
  234. $data = curl_exec($curlproc);
  235. if(curl_errno($curlproc)){
  236. throw new Exception(curl_error($curlproc));
  237. }
  238. curl_close($curlproc);
  239. return $data;
  240. }
  241. public function web($get){
  242. if($get["npt"]){
  243. // get next page data
  244. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  245. $params = json_decode($params, true);
  246. }else{
  247. // get _GET data instead
  248. $search = $get["s"];
  249. if(strlen($search) === 0){
  250. throw new Exception("Search term is empty!");
  251. }
  252. if(strlen($search) > 2048){
  253. throw new Exception("Search term is too long!");
  254. }
  255. $proxy = $this->backend->get_ip();
  256. $params = [
  257. "q" => $search,
  258. "freshness" => $get["time"],
  259. "count" => 10,
  260. "locale" => $get["country"],
  261. "offset" => 0,
  262. "device" => "desktop",
  263. "tgp" => 3,
  264. "safesearch" => 0,
  265. "displayed" => "true"
  266. ];
  267. switch($get["nsfw"]){
  268. case "yes": $params["safesearch"] = 0; break;
  269. case "maybe": $params["safesearch"] = 1; break;
  270. case "no": $params["safesearch"] = 2; break;
  271. }
  272. }
  273. /*
  274. $handle = fopen("scraper/qwant_web.json", "r");
  275. $json = fread($handle, filesize("scraper/qwant_web.json"));
  276. fclose($handle);*/
  277. try{
  278. $json =
  279. $this->get(
  280. $proxy,
  281. "https://fdn.qwant.com/v3/search/web",
  282. $params
  283. );
  284. }catch(Exception $error){
  285. throw new Exception("Could not fetch JSON");
  286. }
  287. $json = json_decode($json, true);
  288. if($json === NULL){
  289. throw new Exception("Failed to decode JSON");
  290. }
  291. if(isset($json["data"]["message"][0])){
  292. throw new Exception("Server returned an error:\n" . $json["data"]["message"][0]);
  293. }
  294. $out = [
  295. "status" => "ok",
  296. "spelling" => [
  297. "type" => "no_correction",
  298. "using" => null,
  299. "correction" => null
  300. ],
  301. "npt" => null,
  302. "answer" => [],
  303. "web" => [],
  304. "image" => [],
  305. "video" => [],
  306. "news" => [],
  307. "related" => []
  308. ];
  309. if(
  310. $json["status"] != "success" &&
  311. $json["data"]["error_code"] === 5
  312. ){
  313. // no results
  314. return $out;
  315. }
  316. $this->detect_errors($json);
  317. if(!isset($json["data"]["result"]["items"]["mainline"])){
  318. throw new Exception("Server did not return a result object");
  319. }
  320. // data is OK, parse
  321. // get instant answer
  322. if(
  323. $get["extendedsearch"] == "yes" &&
  324. isset($json["data"]["result"]["items"]["sidebar"][0]["endpoint"])
  325. ){
  326. try{
  327. $answer =
  328. $this->get(
  329. $proxy,
  330. "https://api.qwant.com/v3" .
  331. $json["data"]["result"]["items"]["sidebar"][0]["endpoint"],
  332. []
  333. );
  334. $answer = json_decode($answer, true);
  335. if(
  336. $answer === null ||
  337. $answer["status"] != "success" ||
  338. $answer["data"]["result"] === null
  339. ){
  340. throw new Exception();
  341. }
  342. // parse answer
  343. $out["answer"][] = [
  344. "title" => $answer["data"]["result"]["title"],
  345. "description" => [
  346. [
  347. "type" => "text",
  348. "value" => $this->trimdots($answer["data"]["result"]["description"])
  349. ]
  350. ],
  351. "url" => $answer["data"]["result"]["url"],
  352. "thumb" =>
  353. $answer["data"]["result"]["thumbnail"]["landscape"] == null ?
  354. null :
  355. $this->unshitimage(
  356. $answer["data"]["result"]["thumbnail"]["landscape"],
  357. false
  358. ),
  359. "table" => [],
  360. "sublink" => []
  361. ];
  362. }catch(Exception $error){
  363. // do nothing in case of failure
  364. }
  365. }
  366. // get word correction
  367. if(isset($json["data"]["query"]["queryContext"]["alteredQuery"])){
  368. $out["spelling"] = [
  369. "type" => "including",
  370. "using" => $json["data"]["query"]["queryContext"]["alteredQuery"],
  371. "correction" => $json["data"]["query"]["queryContext"]["alterationOverrideQuery"]
  372. ];
  373. }
  374. // check for next page
  375. if($json["data"]["result"]["lastPage"] === false){
  376. $params["offset"] = $params["offset"] + 10;
  377. $out["npt"] =
  378. $this->backend->store(
  379. json_encode($params),
  380. "web",
  381. $proxy
  382. );
  383. }
  384. // parse results
  385. foreach($json["data"]["result"]["items"]["mainline"] as $item){
  386. switch($item["type"]){ // ignores ads
  387. case "web":
  388. $first_iteration = true;
  389. foreach($item["items"] as $result){
  390. if(isset($result["thumbnailUrl"])){
  391. $thumb = [
  392. "url" => $this->unshitimage($result["thumbnailUrl"]),
  393. "ratio" => "16:9"
  394. ];
  395. }else{
  396. $thumb = [
  397. "url" => null,
  398. "ratio" => null
  399. ];
  400. }
  401. $sublinks = [];
  402. if(isset($result["links"])){
  403. foreach($result["links"] as $link){
  404. $sublinks[] = [
  405. "title" => $this->trimdots($link["title"]),
  406. "date" => null,
  407. "description" => isset($link["desc"]) ? $this->trimdots($link["desc"]) : null,
  408. "url" => $link["url"]
  409. ];
  410. }
  411. }
  412. // detect gibberish results
  413. if(
  414. $first_iteration &&
  415. !isset($result["urlPingSuffix"])
  416. ){
  417. throw new Exception("Qwant returned gibberish results");
  418. }
  419. $out["web"][] = [
  420. "title" => $this->trimdots($result["title"]),
  421. "description" => $this->trimdots($result["desc"]),
  422. "url" => $result["url"],
  423. "date" => null,
  424. "type" => "web",
  425. "thumb" => $thumb,
  426. "sublink" => $sublinks,
  427. "table" => []
  428. ];
  429. $first_iteration = false;
  430. }
  431. break;
  432. case "images":
  433. foreach($item["items"] as $image){
  434. $out["image"][] = [
  435. "title" => $image["title"],
  436. "source" => [
  437. [
  438. "url" => $image["media"],
  439. "width" => (int)$image["width"],
  440. "height" => (int)$image["height"]
  441. ],
  442. [
  443. "url" => $this->unshitimage($image["thumbnail"]),
  444. "width" => $image["thumb_width"],
  445. "height" => $image["thumb_height"]
  446. ]
  447. ],
  448. "url" => $image["url"]
  449. ];
  450. }
  451. break;
  452. case "videos":
  453. foreach($item["items"] as $video){
  454. $out["video"][] = [
  455. "title" => $video["title"],
  456. "description" => null,
  457. "date" => (int)$video["date"],
  458. "duration" => $video["duration"] === null ? null : $video["duration"] / 1000,
  459. "views" => null,
  460. "thumb" =>
  461. $video["thumbnail"] === null ?
  462. [
  463. "url" => null,
  464. "ratio" => null,
  465. ] :
  466. [
  467. "url" => $this->unshitimage($video["thumbnail"]),
  468. "ratio" => "16:9",
  469. ],
  470. "url" => $video["url"]
  471. ];
  472. }
  473. break;
  474. case "related_searches":
  475. foreach($item["items"] as $related){
  476. $out["related"][] = $related["text"];
  477. }
  478. break;
  479. }
  480. }
  481. return $out;
  482. }
  483. public function image($get){
  484. if($get["npt"]){
  485. [$params, $proxy] =
  486. $this->backend->get(
  487. $get["npt"],
  488. "images"
  489. );
  490. $params = json_decode($params, true);
  491. }else{
  492. $search = $get["s"];
  493. if(strlen($search) === 0){
  494. throw new Exception("Search term is empty!");
  495. }
  496. $proxy = $this->backend->get_ip();
  497. $params = [
  498. "t" => "images",
  499. "q" => $search,
  500. "count" => 125,
  501. "locale" => $get["country"],
  502. "offset" => 0, // increment by 125
  503. "device" => "desktop",
  504. "tgp" => 3
  505. ];
  506. if($get["time"] != "any"){
  507. $params["freshness"] = $get["time"];
  508. }
  509. foreach(["size", "color", "imagetype", "license"] as $p){
  510. if($get[$p] != "any"){
  511. $params[$p] = $get[$p];
  512. }
  513. }
  514. switch($get["nsfw"]){
  515. case "yes": $params["safesearch"] = 0; break;
  516. case "maybe": $params["safesearch"] = 1; break;
  517. case "no": $params["safesearch"] = 2; break;
  518. }
  519. }
  520. try{
  521. $json = $this->get(
  522. $proxy,
  523. "https://api.qwant.com/v3/search/images",
  524. $params,
  525. );
  526. }catch(Exception $err){
  527. throw new Exception("Failed to get JSON");
  528. }
  529. /*
  530. $handle = fopen("scraper/yandex.json", "r");
  531. $json = fread($handle, filesize("scraper/yandex.json"));
  532. fclose($handle);*/
  533. $json = json_decode($json, true);
  534. if($json === null){
  535. throw new Exception("Failed to decode JSON");
  536. }
  537. $this->detect_errors($json);
  538. if(isset($json["data"]["result"]["items"]["mainline"])){
  539. throw new Exception("Qwant returned gibberish results");
  540. }
  541. $out = [
  542. "status" => "ok",
  543. "npt" => null,
  544. "image" => []
  545. ];
  546. if($json["data"]["result"]["lastPage"] === false){
  547. $params["offset"] = $params["offset"] + 125;
  548. $out["npt"] = $this->backend->store(
  549. json_encode($params),
  550. "images",
  551. $proxy
  552. );
  553. }
  554. foreach($json["data"]["result"]["items"] as $image){
  555. $out["image"][] = [
  556. "title" => $this->trimdots($image["title"]),
  557. "source" => [
  558. [
  559. "url" => $image["media"],
  560. "width" => $image["width"],
  561. "height" => $image["height"]
  562. ],
  563. [
  564. "url" => $this->unshitimage($image["thumbnail"]),
  565. "width" => $image["thumb_width"],
  566. "height" => $image["thumb_height"]
  567. ]
  568. ],
  569. "url" => $image["url"]
  570. ];
  571. }
  572. return $out;
  573. }
  574. public function video($get){
  575. $search = $get["s"];
  576. if(strlen($search) === 0){
  577. throw new Exception("Search term is empty!");
  578. }
  579. $params = [
  580. "t" => "videos",
  581. "q" => $search,
  582. "count" => 50,
  583. "locale" => $get["country"],
  584. "offset" => 0, // dont implement pagination
  585. "device" => "desktop",
  586. "tgp" => 3
  587. ];
  588. switch($get["nsfw"]){
  589. case "yes": $params["safesearch"] = 0; break;
  590. case "maybe": $params["safesearch"] = 1; break;
  591. case "no": $params["safesearch"] = 2; break;
  592. }
  593. try{
  594. $json =
  595. $this->get(
  596. $this->backend->get_ip(),
  597. "https://api.qwant.com/v3/search/videos",
  598. $params
  599. );
  600. }catch(Exception $error){
  601. throw new Exception("Could not fetch JSON");
  602. }
  603. /*
  604. $handle = fopen("scraper/yandex-video.json", "r");
  605. $json = fread($handle, filesize("scraper/yandex-video.json"));
  606. fclose($handle);
  607. */
  608. $json = json_decode($json, true);
  609. if($json === null){
  610. throw new Exception("Could not parse JSON");
  611. }
  612. $this->detect_errors($json);
  613. if(isset($json["data"]["result"]["items"]["mainline"])){
  614. throw new Exception("Qwant returned gibberish results");
  615. }
  616. $out = [
  617. "status" => "ok",
  618. "npt" => null,
  619. "video" => [],
  620. "author" => [],
  621. "livestream" => [],
  622. "playlist" => [],
  623. "reel" => []
  624. ];
  625. foreach($json["data"]["result"]["items"] as $video){
  626. if(empty($video["thumbnail"])){
  627. $thumb = [
  628. "url" => null,
  629. "ratio" => null
  630. ];
  631. }else{
  632. $thumb = [
  633. "url" => $this->unshitimage($video["thumbnail"], false),
  634. "ratio" => "16:9"
  635. ];
  636. }
  637. $duration = (int)$video["duration"];
  638. $out["video"][] = [
  639. "title" => $video["title"],
  640. "description" => $this->limitstrlen($video["desc"]),
  641. "author" => [
  642. "name" => $video["channel"],
  643. "url" => null,
  644. "avatar" => null
  645. ],
  646. "date" => (int)$video["date"],
  647. "duration" => $duration === 0 ? null : $duration,
  648. "views" => null,
  649. "thumb" => $thumb,
  650. "url" => preg_replace("/\?syndication=.+/", "", $video["url"])
  651. ];
  652. }
  653. return $out;
  654. }
  655. public function news($get){
  656. $search = $get["s"];
  657. if(strlen($search) === 0){
  658. throw new Exception("Search term is empty!");
  659. }
  660. $params = [
  661. "t" => "news",
  662. "q" => $search,
  663. "count" => 50,
  664. "locale" => $get["country"],
  665. "offset" => 0, // dont implement pagination
  666. "device" => "desktop",
  667. "tgp" => 3
  668. ];
  669. switch($get["nsfw"]){
  670. case "yes": $params["safesearch"] = 0; break;
  671. case "maybe": $params["safesearch"] = 1; break;
  672. case "no": $params["safesearch"] = 2; break;
  673. }
  674. try{
  675. $json =
  676. $this->get(
  677. $this->backend->get_ip(),
  678. "https://api.qwant.com/v3/search/news",
  679. $params
  680. );
  681. }catch(Exception $error){
  682. throw new Exception("Could not fetch JSON");
  683. }
  684. /*
  685. $handle = fopen("scraper/yandex-video.json", "r");
  686. $json = fread($handle, filesize("scraper/yandex-video.json"));
  687. fclose($handle);
  688. */
  689. $json = json_decode($json, true);
  690. if($json === null){
  691. throw new Exception("Could not parse JSON");
  692. }
  693. $this->detect_errors($json);
  694. if(isset($json["data"]["result"]["items"]["mainline"])){
  695. throw new Exception("Qwant returned gibberish results");
  696. }
  697. $out = [
  698. "status" => "ok",
  699. "npt" => null,
  700. "news" => []
  701. ];
  702. foreach($json["data"]["result"]["items"] as $news){
  703. if(empty($news["media"][0]["pict_big"]["url"])){
  704. $thumb = [
  705. "url" => null,
  706. "ratio" => null
  707. ];
  708. }else{
  709. $thumb = [
  710. "url" => $this->unshitimage($news["media"][0]["pict_big"]["url"], false),
  711. "ratio" => "16:9"
  712. ];
  713. }
  714. $out["news"][] = [
  715. "title" => $news["title"],
  716. "author" => $news["press_name"],
  717. "description" => $this->trimdots($news["desc"]),
  718. "date" => (int)$news["date"],
  719. "thumb" => $thumb,
  720. "url" => $news["url"]
  721. ];
  722. }
  723. return $out;
  724. }
  725. private function detect_errors($json){
  726. if(
  727. isset($json["status"]) &&
  728. $json["status"] == "error"
  729. ){
  730. if(isset($json["data"]["error_data"]["captchaUrl"])){
  731. throw new Exception("Qwant returned a captcha");
  732. }elseif(isset($json["data"]["error_data"]["error_code"])){
  733. throw new Exception(
  734. "Qwant returned an API error: " .
  735. $json["data"]["error_data"]["error_code"]
  736. );
  737. }
  738. throw new Exception("Qwant returned an API error");
  739. }
  740. }
  741. private function limitstrlen($text){
  742. return explode("\n", wordwrap($text, 300, "\n"))[0];
  743. }
  744. private function trimdots($text){
  745. return trim($text, ". ");
  746. }
  747. private function unshitimage($url, $is_bing = true){
  748. // 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
  749. parse_str(parse_url($url)["query"], $parts);
  750. if($is_bing){
  751. $parse = parse_url($parts["u"]);
  752. parse_str($parse["query"], $parts);
  753. return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
  754. }
  755. return $parts["u"];
  756. }
  757. }