qwant.php 19 KB

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