qwant.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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($json["status"] != "success"){
  310. if($json["data"]["error_code"] === 5){
  311. return $out;
  312. }
  313. if(isset($json["data"]["error_code"])){
  314. switch($json["data"]["error_code"]){
  315. case 27:
  316. throw new Exception("Qwant returned a captcha");
  317. break;
  318. }
  319. }
  320. throw new Exception("Qwant returned an error code: " . $json["data"]["error_code"]);
  321. }
  322. if(!isset($json["data"]["result"]["items"]["mainline"])){
  323. throw new Exception("Server did not return a result object");
  324. }
  325. // data is OK, parse
  326. // get instant answer
  327. if(
  328. $get["extendedsearch"] == "yes" &&
  329. isset($json["data"]["result"]["items"]["sidebar"][0]["endpoint"])
  330. ){
  331. try{
  332. $answer =
  333. $this->get(
  334. $proxy,
  335. "https://api.qwant.com/v3" .
  336. $json["data"]["result"]["items"]["sidebar"][0]["endpoint"],
  337. []
  338. );
  339. $answer = json_decode($answer, true);
  340. if(
  341. $answer === null ||
  342. $answer["status"] != "success" ||
  343. $answer["data"]["result"] === null
  344. ){
  345. throw new Exception();
  346. }
  347. // parse answer
  348. $out["answer"][] = [
  349. "title" => $answer["data"]["result"]["title"],
  350. "description" => [
  351. [
  352. "type" => "text",
  353. "value" => $this->trimdots($answer["data"]["result"]["description"])
  354. ]
  355. ],
  356. "url" => $answer["data"]["result"]["url"],
  357. "thumb" =>
  358. $answer["data"]["result"]["thumbnail"]["landscape"] == null ?
  359. null :
  360. $this->unshitimage(
  361. $answer["data"]["result"]["thumbnail"]["landscape"],
  362. false
  363. ),
  364. "table" => [],
  365. "sublink" => []
  366. ];
  367. }catch(Exception $error){
  368. // do nothing in case of failure
  369. }
  370. }
  371. // get word correction
  372. if(isset($json["data"]["query"]["queryContext"]["alteredQuery"])){
  373. $out["spelling"] = [
  374. "type" => "including",
  375. "using" => $json["data"]["query"]["queryContext"]["alteredQuery"],
  376. "correction" => $json["data"]["query"]["queryContext"]["alterationOverrideQuery"]
  377. ];
  378. }
  379. // check for next page
  380. if($json["data"]["result"]["lastPage"] === false){
  381. $params["offset"] = $params["offset"] + 10;
  382. $out["npt"] =
  383. $this->backend->store(
  384. json_encode($params),
  385. "web",
  386. $proxy
  387. );
  388. }
  389. // parse results
  390. foreach($json["data"]["result"]["items"]["mainline"] as $item){
  391. switch($item["type"]){ // ignores ads
  392. case "web":
  393. $first_iteration = true;
  394. foreach($item["items"] as $result){
  395. if(isset($result["thumbnailUrl"])){
  396. $thumb = [
  397. "url" => $this->unshitimage($result["thumbnailUrl"]),
  398. "ratio" => "16:9"
  399. ];
  400. }else{
  401. $thumb = [
  402. "url" => null,
  403. "ratio" => null
  404. ];
  405. }
  406. $sublinks = [];
  407. if(isset($result["links"])){
  408. foreach($result["links"] as $link){
  409. $sublinks[] = [
  410. "title" => $this->trimdots($link["title"]),
  411. "date" => null,
  412. "description" => isset($link["desc"]) ? $this->trimdots($link["desc"]) : null,
  413. "url" => $link["url"]
  414. ];
  415. }
  416. }
  417. // detect gibberish results
  418. if(
  419. $first_iteration &&
  420. !isset($result["urlPingSuffix"])
  421. ){
  422. throw new Exception("Qwant returned gibberish results");
  423. }
  424. $out["web"][] = [
  425. "title" => $this->trimdots($result["title"]),
  426. "description" => $this->trimdots($result["desc"]),
  427. "url" => $result["url"],
  428. "date" => null,
  429. "type" => "web",
  430. "thumb" => $thumb,
  431. "sublink" => $sublinks,
  432. "table" => []
  433. ];
  434. $first_iteration = false;
  435. }
  436. break;
  437. case "images":
  438. foreach($item["items"] as $image){
  439. $out["image"][] = [
  440. "title" => $image["title"],
  441. "source" => [
  442. [
  443. "url" => $image["media"],
  444. "width" => (int)$image["width"],
  445. "height" => (int)$image["height"]
  446. ],
  447. [
  448. "url" => $this->unshitimage($image["thumbnail"]),
  449. "width" => $image["thumb_width"],
  450. "height" => $image["thumb_height"]
  451. ]
  452. ],
  453. "url" => $image["url"]
  454. ];
  455. }
  456. break;
  457. case "videos":
  458. foreach($item["items"] as $video){
  459. $out["video"][] = [
  460. "title" => $video["title"],
  461. "description" => null,
  462. "date" => (int)$video["date"],
  463. "duration" => $video["duration"] === null ? null : $video["duration"] / 1000,
  464. "views" => null,
  465. "thumb" =>
  466. $video["thumbnail"] === null ?
  467. [
  468. "url" => null,
  469. "ratio" => null,
  470. ] :
  471. [
  472. "url" => $this->unshitimage($video["thumbnail"]),
  473. "ratio" => "16:9",
  474. ],
  475. "url" => $video["url"]
  476. ];
  477. }
  478. break;
  479. case "related_searches":
  480. foreach($item["items"] as $related){
  481. $out["related"][] = $related["text"];
  482. }
  483. break;
  484. }
  485. }
  486. return $out;
  487. }
  488. public function image($get){
  489. if($get["npt"]){
  490. [$params, $proxy] =
  491. $this->backend->get(
  492. $get["npt"],
  493. "images"
  494. );
  495. $params = json_decode($params, true);
  496. }else{
  497. $search = $get["s"];
  498. if(strlen($search) === 0){
  499. throw new Exception("Search term is empty!");
  500. }
  501. $proxy = $this->backend->get_ip();
  502. $params = [
  503. "t" => "images",
  504. "q" => $search,
  505. "count" => 125,
  506. "locale" => $get["country"],
  507. "offset" => 0, // increment by 125
  508. "device" => "desktop",
  509. "tgp" => 3
  510. ];
  511. if($get["time"] != "any"){
  512. $params["freshness"] = $get["time"];
  513. }
  514. foreach(["size", "color", "imagetype", "license"] as $p){
  515. if($get[$p] != "any"){
  516. $params[$p] = $get[$p];
  517. }
  518. }
  519. switch($get["nsfw"]){
  520. case "yes": $params["safesearch"] = 0; break;
  521. case "maybe": $params["safesearch"] = 1; break;
  522. case "no": $params["safesearch"] = 2; break;
  523. }
  524. }
  525. try{
  526. $json = $this->get(
  527. $proxy,
  528. "https://api.qwant.com/v3/search/images",
  529. $params,
  530. );
  531. }catch(Exception $err){
  532. throw new Exception("Failed to get JSON");
  533. }
  534. /*
  535. $handle = fopen("scraper/yandex.json", "r");
  536. $json = fread($handle, filesize("scraper/yandex.json"));
  537. fclose($handle);*/
  538. $json = json_decode($json, true);
  539. if($json === null){
  540. throw new Exception("Failed to decode JSON");
  541. }
  542. if($json["status"] != "success"){
  543. throw new Exception("Qwant returned an API error");
  544. }
  545. if(isset($json["data"]["result"]["items"]["mainline"])){
  546. throw new Exception("Qwant returned gibberish results");
  547. }
  548. $out = [
  549. "status" => "ok",
  550. "npt" => null,
  551. "image" => []
  552. ];
  553. if($json["data"]["result"]["lastPage"] === false){
  554. $params["offset"] = $params["offset"] + 125;
  555. $out["npt"] = $this->backend->store(
  556. json_encode($params),
  557. "images",
  558. $proxy
  559. );
  560. }
  561. foreach($json["data"]["result"]["items"] as $image){
  562. $out["image"][] = [
  563. "title" => $this->trimdots($image["title"]),
  564. "source" => [
  565. [
  566. "url" => $image["media"],
  567. "width" => $image["width"],
  568. "height" => $image["height"]
  569. ],
  570. [
  571. "url" => $this->unshitimage($image["thumbnail"]),
  572. "width" => $image["thumb_width"],
  573. "height" => $image["thumb_height"]
  574. ]
  575. ],
  576. "url" => $image["url"]
  577. ];
  578. }
  579. return $out;
  580. }
  581. public function video($get){
  582. $search = $get["s"];
  583. if(strlen($search) === 0){
  584. throw new Exception("Search term is empty!");
  585. }
  586. $params = [
  587. "t" => "videos",
  588. "q" => $search,
  589. "count" => 50,
  590. "locale" => $get["country"],
  591. "offset" => 0, // dont implement pagination
  592. "device" => "desktop",
  593. "tgp" => 3
  594. ];
  595. switch($get["nsfw"]){
  596. case "yes": $params["safesearch"] = 0; break;
  597. case "maybe": $params["safesearch"] = 1; break;
  598. case "no": $params["safesearch"] = 2; break;
  599. }
  600. try{
  601. $json =
  602. $this->get(
  603. $this->backend->get_ip(),
  604. "https://api.qwant.com/v3/search/videos",
  605. $params
  606. );
  607. }catch(Exception $error){
  608. throw new Exception("Could not fetch JSON");
  609. }
  610. /*
  611. $handle = fopen("scraper/yandex-video.json", "r");
  612. $json = fread($handle, filesize("scraper/yandex-video.json"));
  613. fclose($handle);
  614. */
  615. $json = json_decode($json, true);
  616. if($json === null){
  617. throw new Exception("Could not parse JSON");
  618. }
  619. if($json["status"] != "success"){
  620. throw new Exception("Qwant returned an API error");
  621. }
  622. if(isset($json["data"]["result"]["items"]["mainline"])){
  623. throw new Exception("Qwant returned gibberish results");
  624. }
  625. $out = [
  626. "status" => "ok",
  627. "npt" => null,
  628. "video" => [],
  629. "author" => [],
  630. "livestream" => [],
  631. "playlist" => [],
  632. "reel" => []
  633. ];
  634. foreach($json["data"]["result"]["items"] as $video){
  635. if(empty($video["thumbnail"])){
  636. $thumb = [
  637. "url" => null,
  638. "ratio" => null
  639. ];
  640. }else{
  641. $thumb = [
  642. "url" => $this->unshitimage($video["thumbnail"], false),
  643. "ratio" => "16:9"
  644. ];
  645. }
  646. $duration = (int)$video["duration"];
  647. $out["video"][] = [
  648. "title" => $video["title"],
  649. "description" => $this->limitstrlen($video["desc"]),
  650. "author" => [
  651. "name" => $video["channel"],
  652. "url" => null,
  653. "avatar" => null
  654. ],
  655. "date" => (int)$video["date"],
  656. "duration" => $duration === 0 ? null : $duration,
  657. "views" => null,
  658. "thumb" => $thumb,
  659. "url" => preg_replace("/\?syndication=.+/", "", $video["url"])
  660. ];
  661. }
  662. return $out;
  663. }
  664. public function news($get){
  665. $search = $get["s"];
  666. if(strlen($search) === 0){
  667. throw new Exception("Search term is empty!");
  668. }
  669. $params = [
  670. "t" => "news",
  671. "q" => $search,
  672. "count" => 50,
  673. "locale" => $get["country"],
  674. "offset" => 0, // dont implement pagination
  675. "device" => "desktop",
  676. "tgp" => 3
  677. ];
  678. switch($get["nsfw"]){
  679. case "yes": $params["safesearch"] = 0; break;
  680. case "maybe": $params["safesearch"] = 1; break;
  681. case "no": $params["safesearch"] = 2; break;
  682. }
  683. try{
  684. $json =
  685. $this->get(
  686. $this->backend->get_ip(),
  687. "https://api.qwant.com/v3/search/news",
  688. $params
  689. );
  690. }catch(Exception $error){
  691. throw new Exception("Could not fetch JSON");
  692. }
  693. /*
  694. $handle = fopen("scraper/yandex-video.json", "r");
  695. $json = fread($handle, filesize("scraper/yandex-video.json"));
  696. fclose($handle);
  697. */
  698. $json = json_decode($json, true);
  699. if($json === null){
  700. throw new Exception("Could not parse JSON");
  701. }
  702. if($json["status"] != "success"){
  703. throw new Exception("Qwant returned an API error");
  704. }
  705. if(isset($json["data"]["result"]["items"]["mainline"])){
  706. throw new Exception("Qwant returned gibberish results");
  707. }
  708. $out = [
  709. "status" => "ok",
  710. "npt" => null,
  711. "news" => []
  712. ];
  713. foreach($json["data"]["result"]["items"] as $news){
  714. if(empty($news["media"][0]["pict_big"]["url"])){
  715. $thumb = [
  716. "url" => null,
  717. "ratio" => null
  718. ];
  719. }else{
  720. $thumb = [
  721. "url" => $this->unshitimage($news["media"][0]["pict_big"]["url"], false),
  722. "ratio" => "16:9"
  723. ];
  724. }
  725. $out["news"][] = [
  726. "title" => $news["title"],
  727. "author" => $news["press_name"],
  728. "description" => $this->trimdots($news["desc"]),
  729. "date" => (int)$news["date"],
  730. "thumb" => $thumb,
  731. "url" => $news["url"]
  732. ];
  733. }
  734. return $out;
  735. }
  736. private function limitstrlen($text){
  737. return explode("\n", wordwrap($text, 300, "\n"))[0];
  738. }
  739. private function trimdots($text){
  740. return trim($text, ". ");
  741. }
  742. private function unshitimage($url, $is_bing = true){
  743. // 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
  744. parse_str(parse_url($url)["query"], $parts);
  745. if($is_bing){
  746. $parse = parse_url($parts["u"]);
  747. parse_str($parse["query"], $parts);
  748. return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
  749. }
  750. return $parts["u"];
  751. }
  752. }