1
0

google_api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. // @TODO check for consent.google.com page, if need be
  3. class google_api{
  4. public function __construct(){
  5. include "lib/backend.php";
  6. $this->backend = new backend("google_api");
  7. }
  8. public function getfilters($page){
  9. $base = [
  10. "country" => [ // gl=<country> (image: cr=countryAF)
  11. "display" => "Country",
  12. "option" => [
  13. "any" => "Instance's country",
  14. "af" => "Afghanistan",
  15. "al" => "Albania",
  16. "dz" => "Algeria",
  17. "as" => "American Samoa",
  18. "ad" => "Andorra",
  19. "ao" => "Angola",
  20. "ai" => "Anguilla",
  21. "aq" => "Antarctica",
  22. "ag" => "Antigua and Barbuda",
  23. "ar" => "Argentina",
  24. "am" => "Armenia",
  25. "aw" => "Aruba",
  26. "au" => "Australia",
  27. "at" => "Austria",
  28. "az" => "Azerbaijan",
  29. "bs" => "Bahamas",
  30. "bh" => "Bahrain",
  31. "bd" => "Bangladesh",
  32. "bb" => "Barbados",
  33. "by" => "Belarus",
  34. "be" => "Belgium",
  35. "bz" => "Belize",
  36. "bj" => "Benin",
  37. "bm" => "Bermuda",
  38. "bt" => "Bhutan",
  39. "bo" => "Bolivia",
  40. "ba" => "Bosnia and Herzegovina",
  41. "bw" => "Botswana",
  42. "bv" => "Bouvet Island",
  43. "br" => "Brazil",
  44. "io" => "British Indian Ocean Territory",
  45. "bn" => "Brunei Darussalam",
  46. "bg" => "Bulgaria",
  47. "bf" => "Burkina Faso",
  48. "bi" => "Burundi",
  49. "kh" => "Cambodia",
  50. "cm" => "Cameroon",
  51. "ca" => "Canada",
  52. "cv" => "Cape Verde",
  53. "ky" => "Cayman Islands",
  54. "cf" => "Central African Republic",
  55. "td" => "Chad",
  56. "cl" => "Chile",
  57. "cn" => "China",
  58. "cx" => "Christmas Island",
  59. "cc" => "Cocos (Keeling) Islands",
  60. "co" => "Colombia",
  61. "km" => "Comoros",
  62. "cg" => "Congo",
  63. "cd" => "Congo, the Democratic Republic",
  64. "ck" => "Cook Islands",
  65. "cr" => "Costa Rica",
  66. "ci" => "Cote D'ivoire",
  67. "hr" => "Croatia",
  68. "cu" => "Cuba",
  69. "cy" => "Cyprus",
  70. "cz" => "Czech Republic",
  71. "dk" => "Denmark",
  72. "dj" => "Djibouti",
  73. "dm" => "Dominica",
  74. "do" => "Dominican Republic",
  75. "ec" => "Ecuador",
  76. "eg" => "Egypt",
  77. "sv" => "El Salvador",
  78. "gq" => "Equatorial Guinea",
  79. "er" => "Eritrea",
  80. "ee" => "Estonia",
  81. "et" => "Ethiopia",
  82. "fk" => "Falkland Islands (Malvinas)",
  83. "fo" => "Faroe Islands",
  84. "fj" => "Fiji",
  85. "fi" => "Finland",
  86. "fr" => "France",
  87. "gf" => "French Guiana",
  88. "pf" => "French Polynesia",
  89. "tf" => "French Southern Territories",
  90. "ga" => "Gabon",
  91. "gm" => "Gambia",
  92. "ge" => "Georgia",
  93. "de" => "Germany",
  94. "gh" => "Ghana",
  95. "gi" => "Gibraltar",
  96. "gr" => "Greece",
  97. "gl" => "Greenland",
  98. "gd" => "Grenada",
  99. "gp" => "Guadeloupe",
  100. "gu" => "Guam",
  101. "gt" => "Guatemala",
  102. "gn" => "Guinea",
  103. "gw" => "Guinea-Bissau",
  104. "gy" => "Guyana",
  105. "ht" => "Haiti",
  106. "hm" => "Heard Island and Mcdonald Islands",
  107. "va" => "Holy See (Vatican City State)",
  108. "hn" => "Honduras",
  109. "hk" => "Hong Kong",
  110. "hu" => "Hungary",
  111. "is" => "Iceland",
  112. "in" => "India",
  113. "id" => "Indonesia",
  114. "ir" => "Iran, Islamic Republic",
  115. "iq" => "Iraq",
  116. "ie" => "Ireland",
  117. "il" => "Israel",
  118. "it" => "Italy",
  119. "jm" => "Jamaica",
  120. "jp" => "Japan",
  121. "jo" => "Jordan",
  122. "kz" => "Kazakhstan",
  123. "ke" => "Kenya",
  124. "ki" => "Kiribati",
  125. "kp" => "Korea, Democratic People's Republic",
  126. "kr" => "Korea, Republic",
  127. "kw" => "Kuwait",
  128. "kg" => "Kyrgyzstan",
  129. "la" => "Lao People's Democratic Republic",
  130. "lv" => "Latvia",
  131. "lb" => "Lebanon",
  132. "ls" => "Lesotho",
  133. "lr" => "Liberia",
  134. "ly" => "Libyan Arab Jamahiriya",
  135. "li" => "Liechtenstein",
  136. "lt" => "Lithuania",
  137. "lu" => "Luxembourg",
  138. "mo" => "Macao",
  139. "mk" => "Macedonia, the Former Yugosalv Republic",
  140. "mg" => "Madagascar",
  141. "mw" => "Malawi",
  142. "my" => "Malaysia",
  143. "mv" => "Maldives",
  144. "ml" => "Mali",
  145. "mt" => "Malta",
  146. "mh" => "Marshall Islands",
  147. "mq" => "Martinique",
  148. "mr" => "Mauritania",
  149. "mu" => "Mauritius",
  150. "yt" => "Mayotte",
  151. "mx" => "Mexico",
  152. "fm" => "Micronesia, Federated States",
  153. "md" => "Moldova, Republic",
  154. "mc" => "Monaco",
  155. "mn" => "Mongolia",
  156. "ms" => "Montserrat",
  157. "ma" => "Morocco",
  158. "mz" => "Mozambique",
  159. "mm" => "Myanmar",
  160. "na" => "Namibia",
  161. "nr" => "Nauru",
  162. "np" => "Nepal",
  163. "nl" => "Netherlands",
  164. "an" => "Netherlands Antilles",
  165. "nc" => "New Caledonia",
  166. "nz" => "New Zealand",
  167. "ni" => "Nicaragua",
  168. "ne" => "Niger",
  169. "ng" => "Nigeria",
  170. "nu" => "Niue",
  171. "nf" => "Norfolk Island",
  172. "mp" => "Northern Mariana Islands",
  173. "no" => "Norway",
  174. "om" => "Oman",
  175. "pk" => "Pakistan",
  176. "pw" => "Palau",
  177. "ps" => "Palestinian Territory, Occupied",
  178. "pa" => "Panama",
  179. "pg" => "Papua New Guinea",
  180. "py" => "Paraguay",
  181. "pe" => "Peru",
  182. "ph" => "Philippines",
  183. "pn" => "Pitcairn",
  184. "pl" => "Poland",
  185. "pt" => "Portugal",
  186. "pr" => "Puerto Rico",
  187. "qa" => "Qatar",
  188. "re" => "Reunion",
  189. "ro" => "Romania",
  190. "ru" => "Russian Federation",
  191. "rw" => "Rwanda",
  192. "sh" => "Saint Helena",
  193. "kn" => "Saint Kitts and Nevis",
  194. "lc" => "Saint Lucia",
  195. "pm" => "Saint Pierre and Miquelon",
  196. "vc" => "Saint Vincent and the Grenadines",
  197. "ws" => "Samoa",
  198. "sm" => "San Marino",
  199. "st" => "Sao Tome and Principe",
  200. "sa" => "Saudi Arabia",
  201. "sn" => "Senegal",
  202. "cs" => "Serbia and Montenegro",
  203. "sc" => "Seychelles",
  204. "sl" => "Sierra Leone",
  205. "sg" => "Singapore",
  206. "sk" => "Slovakia",
  207. "si" => "Slovenia",
  208. "sb" => "Solomon Islands",
  209. "so" => "Somalia",
  210. "za" => "South Africa",
  211. "gs" => "South Georgia and the South Sandwich Islands",
  212. "es" => "Spain",
  213. "lk" => "Sri Lanka",
  214. "sd" => "Sudan",
  215. "sr" => "Suriname",
  216. "sj" => "Svalbard and Jan Mayen",
  217. "sz" => "Swaziland",
  218. "se" => "Sweden",
  219. "ch" => "Switzerland",
  220. "sy" => "Syrian Arab Republic",
  221. "tw" => "Taiwan, Province of China",
  222. "tj" => "Tajikistan",
  223. "tz" => "Tanzania, United Republic",
  224. "th" => "Thailand",
  225. "tl" => "Timor-Leste",
  226. "tg" => "Togo",
  227. "tk" => "Tokelau",
  228. "to" => "Tonga",
  229. "tt" => "Trinidad and Tobago",
  230. "tn" => "Tunisia",
  231. "tr" => "Turkey",
  232. "tm" => "Turkmenistan",
  233. "tc" => "Turks and Caicos Islands",
  234. "tv" => "Tuvalu",
  235. "ug" => "Uganda",
  236. "ua" => "Ukraine",
  237. "ae" => "United Arab Emirates",
  238. "uk" => "United Kingdom",
  239. "us" => "United States",
  240. "um" => "United States Minor Outlying Islands",
  241. "uy" => "Uruguay",
  242. "uz" => "Uzbekistan",
  243. "vu" => "Vanuatu",
  244. "ve" => "Venezuela",
  245. "vn" => "Viet Nam",
  246. "vg" => "Virgin Islands, British",
  247. "vi" => "Virgin Islands, U.S.",
  248. "wf" => "Wallis and Futuna",
  249. "eh" => "Western Sahara",
  250. "ye" => "Yemen",
  251. "zm" => "Zambia",
  252. "zw" => "Zimbabwe"
  253. ]
  254. ],
  255. "nsfw" => [
  256. "display" => "NSFW",
  257. "option" => [
  258. "yes" => "Yes", // safe=active
  259. "no" => "No" // safe=off
  260. ]
  261. ],
  262. "sort" => [ // sort
  263. "display" => "Sort by",
  264. "option" => [
  265. "any" => "Any order",
  266. "date:d" => "Oldest",
  267. "date:a" => "Newest"
  268. ]
  269. ],
  270. "newer" => [
  271. "display" => "Newer than",
  272. "option" => "_DATE"
  273. ],
  274. "rm_dupes" => [ // filter
  275. "display" => "Remove duplicates",
  276. "option" => [
  277. "yes" => "Yes", // 1
  278. "no" => "No" // 0
  279. ]
  280. ]
  281. ];
  282. switch($page){
  283. case "web":
  284. return array_merge(
  285. $base,
  286. [
  287. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  288. "display" => "Language",
  289. "option" => [
  290. "any" => "Any language",
  291. "ar" => "Arabic",
  292. "bg" => "Bulgarian",
  293. "ca" => "Catalan",
  294. "cs" => "Czech",
  295. "da" => "Danish",
  296. "de" => "German",
  297. "el" => "Greek",
  298. "en" => "English",
  299. "es" => "Spanish",
  300. "et" => "Estonian",
  301. "fi" => "Finnish",
  302. "fr" => "French",
  303. "hr" => "Croatian",
  304. "hu" => "Hungarian",
  305. "id" => "Indonesian",
  306. "is" => "Icelandic",
  307. "it" => "Italian",
  308. "iw" => "Hebrew",
  309. "ja" => "Japanese",
  310. "ko" => "Korean",
  311. "lt" => "Lithuanian",
  312. "lv" => "Latvian",
  313. "nl" => "Dutch",
  314. "no" => "Norwegian",
  315. "pl" => "Polish",
  316. "pt" => "Portuguese",
  317. "ro" => "Romanian",
  318. "ru" => "Russian",
  319. "sk" => "Slovak",
  320. "sl" => "Slovenian",
  321. "sr" => "Serbian",
  322. "sv" => "Swedish",
  323. "tr" => "Turkish",
  324. "zh-CN" => "Chinese (Simplified)",
  325. "zh-TW" => "Chinese (Traditional)"
  326. ]
  327. ]
  328. ]
  329. );
  330. break;
  331. case "images":
  332. return array_merge(
  333. $base,
  334. [
  335. "size" => [ // imgSize
  336. "display" => "Size",
  337. "option" => [
  338. "any" => "Any size",
  339. "icon" => "Icon",
  340. "small" => "Small",
  341. "medium" => "Medium",
  342. "large" => "Large",
  343. "xlarge" => "X-Large",
  344. "xxlarge" => "XX-Large",
  345. "huge" => "Huge"
  346. ]
  347. ],
  348. "format" => [ // fileType
  349. "display" => "Format",
  350. "option" => [
  351. "any" => "Any format",
  352. "jpg" => "JPG",
  353. "gif" => "GIF",
  354. "png" => "PNG",
  355. "bmp" => "BMP",
  356. "svg" => "SVG",
  357. "webp" => "WEBP",
  358. "ico" => "ICO",
  359. "craw" => "RAW"
  360. ]
  361. ],
  362. "color" => [
  363. "display" => "Color",
  364. "option" => [
  365. "any" => "Any color",
  366. "color" => "Full color", // imgColorType
  367. "mono" => "Black & White",
  368. "trans" => "Transparent background",
  369. "red" => "Red", // imgDominantColor
  370. "orange" => "Orange",
  371. "yellow" => "Yellow",
  372. "green" => "Green",
  373. "teal" => "Teal",
  374. "blue" => "Blue",
  375. "purple" => "Purple",
  376. "pink" => "Pink",
  377. "white" => "White",
  378. "gray" => "Gray",
  379. "black" => "Black",
  380. "brown" => "Brown"
  381. ]
  382. ],
  383. "type" => [ // imgType
  384. "display" => "Type",
  385. "option" => [
  386. "any" => "Any type",
  387. "clipart" => "Clip Art",
  388. "face" => "Faces",
  389. "lineart" => "Line Drawing",
  390. "stock" => "Stock photos",
  391. "photo" => "Photos",
  392. "animated" => "Animated",
  393. ]
  394. ],
  395. "rights" => [ // rights
  396. "display" => "Usage rights",
  397. "option" => [
  398. "any" => "Any license",
  399. "cc_publicdomain" => "Public domain",
  400. "cc_attribute" => "Attribution required",
  401. "cc_sharealike" => "Sharealike",
  402. "cc_noncommercial" => "Non-commercial use only",
  403. "cc_nonderived" => "Original works"
  404. ]
  405. ]
  406. ]
  407. );
  408. break;
  409. }
  410. }
  411. private function get($proxy, $url, $get = []){
  412. $curlproc = curl_init();
  413. $headers = [
  414. "Accept: application/json",
  415. "Accept-Encoding: gzip"
  416. ];
  417. if($get !== []){
  418. $get = http_build_query($get);
  419. $url .= "?" . $get;
  420. }
  421. curl_setopt($curlproc, CURLOPT_URL, $url);
  422. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  423. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  424. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  425. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  426. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  427. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  428. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  429. // follow redirects
  430. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  431. $this->backend->assign_proxy($curlproc, $proxy);
  432. $data = curl_exec($curlproc);
  433. if(curl_errno($curlproc)){
  434. throw new Exception(curl_error($curlproc));
  435. }
  436. curl_close($curlproc);
  437. return $data;
  438. }
  439. public function web($get){
  440. // rotate proxy + key on EVERY request
  441. $keydata = $this->backend->get_key();
  442. $proxy = $this->backend->get_ip($keydata["increment"]);
  443. if($get["npt"]){
  444. // $p is never used
  445. [$params, $p] = $this->backend->get(
  446. $get["npt"],
  447. "web"
  448. );
  449. $params = json_decode($params, true);
  450. $params["key"] = $keydata["key"];
  451. }else{
  452. //$json = file_get_contents("scraper/google.json");
  453. $params = [
  454. "q" => $get["s"],
  455. "cx" => config::GOOGLE_CX_ENDPOINT,
  456. "num" => 10,
  457. "start" => 1,
  458. "key" => $keydata["key"]
  459. ];
  460. //
  461. // parse filters
  462. //
  463. if($get["newer"] !== false){
  464. $params["dateRestrict"] = "d" . (round((time() - $get["newer"]) / 100000));
  465. }
  466. if($get["rm_dupes"] == "no"){ $params["filter"] = "0"; }
  467. if($get["country"] != "any"){ $params["gl"] = $get["country"]; }
  468. if($get["lang"] != "any"){ $params["lr"] = "lang_" . $get["lang"]; }
  469. if($get["nsfw"] == "yes"){
  470. $params["safe"] = "off";
  471. }else{
  472. $params["safe"] = "active";
  473. }
  474. if($get["sort"] != "any"){ $params["sort"] = $get["sort"]; }
  475. }
  476. try{
  477. $json =
  478. $this->get(
  479. $proxy,
  480. "https://www.googleapis.com/customsearch/v1",
  481. $params
  482. );
  483. }catch(Exception $error){
  484. throw new Exception("Failed to fetch JSON");
  485. }
  486. $json = json_decode($json, true);
  487. if($json === null){
  488. throw new Exception("Failed to decode JSON");
  489. }
  490. $out = [
  491. "status" => "ok",
  492. "spelling" => [
  493. "type" => "no_correction",
  494. "using" => null,
  495. "correction" => null
  496. ],
  497. "npt" => null,
  498. "answer" => [],
  499. "web" => [],
  500. "image" => [],
  501. "video" => [],
  502. "news" => [],
  503. "related" => []
  504. ];
  505. if(isset($json["error"]["message"])){
  506. throw new Exception(
  507. "API returned an error: " .
  508. $json["error"]["message"] .
  509. " (key #" . $keydata["increment"] . ")"
  510. );
  511. }
  512. if(!isset($json["items"])){
  513. // google just doesnt return items when theres no results
  514. return $out;
  515. }
  516. foreach($json["items"] as $result){
  517. //
  518. // probe for thumbnail
  519. //
  520. $probes = [
  521. isset($result["pagemap"]["cse_thumbnail"][0]["src"]) ? $result["pagemap"]["cse_thumbnail"][0]["src"] : null,
  522. isset($result["pagemap"]["cse_image"][0]["src"]) ? $result["pagemap"]["cse_image"][0]["src"] : null,
  523. isset($result["pagemap"]["metatags"][0]["twitter:image"]) ? $result["pagemap"]["metatags"][0]["twitter:image"] : null,
  524. isset($result["pagemap"]["metatags"][0]["og:image"]) ? $result["pagemap"]["metatags"][0]["og:image"] : null
  525. ];
  526. $thumb = [
  527. "url" => null,
  528. "ratio" => null
  529. ];
  530. foreach($probes as $probe){
  531. if($probe !== null){
  532. $thumb = [
  533. "url" => $probe,
  534. "ratio" => "16:9"
  535. ];
  536. break;
  537. }
  538. }
  539. //
  540. // probe for page format
  541. //
  542. $mime = "web";
  543. if(isset($result["mime"])){
  544. $result["mime"] =
  545. explode(
  546. "/",
  547. $result["mime"],
  548. 2
  549. );
  550. if(count($result["mime"]) === 2){
  551. $mime = strtoupper($result["mime"][1]);
  552. }
  553. }
  554. $description = $result["snippet"];
  555. //
  556. // Get date
  557. //
  558. $description_split =
  559. explode(
  560. "...", $description, 2
  561. );
  562. if(count($description_split) === 1){
  563. $description = $result["snippet"];
  564. }elseif(strlen($description_split[0]) < 17){
  565. $date = trim($description_split[0]);
  566. $date_probe = strtotime($date);
  567. if($date_probe !== false){
  568. $description = $description_split[1];
  569. }else{
  570. //
  571. // fallback to getting date from meta tags
  572. //
  573. if(isset($result["pagemap"]["metatags"][0]["creationdate"])){
  574. $date = $result["pagemap"]["metatags"][0]["creationdate"];
  575. }elseif(isset($result["pagemap"]["metatags"][0]["moddate"])){
  576. $date = $result["pagemap"]["metatags"][0]["moddate"];
  577. }else{
  578. $date = null;
  579. }
  580. $description = $result["snippet"];
  581. }
  582. }
  583. if($date !== null){
  584. $date =
  585. strtotime(
  586. trim(
  587. str_replace(
  588. ["D:", "'"],
  589. "",
  590. $date
  591. )
  592. )
  593. );
  594. if($date === false){
  595. $date = null;
  596. }
  597. }
  598. $out["web"][] = [
  599. "title" =>
  600. $this->titledots(
  601. $result["title"]
  602. ),
  603. "description" =>
  604. $this->titledots(
  605. $description
  606. ),
  607. "url" => $result["link"],
  608. "date" => $date,
  609. "type" => $mime,
  610. "thumb" => $thumb,
  611. "sublink" => [],
  612. "table" => []
  613. ];
  614. }
  615. // get npt
  616. if(isset($json["queries"]["nextPage"][0]["startIndex"])){
  617. unset($params["key"]);
  618. $params["start"] = (int)$json["queries"]["nextPage"][0]["startIndex"];
  619. $out["npt"] =
  620. $this->backend->store(
  621. json_encode($params),
  622. "web",
  623. $proxy
  624. );
  625. }
  626. return $out;
  627. }
  628. public function image($get){
  629. // rotate proxy + key on EVERY request
  630. $keydata = $this->backend->get_key();
  631. $proxy = $this->backend->get_ip($keydata["increment"]);
  632. if($get["npt"]){
  633. // $p is never used
  634. [$params, $p] = $this->backend->get(
  635. $get["npt"],
  636. "web"
  637. );
  638. $params = json_decode($params, true);
  639. $params["key"] = $keydata["key"];
  640. }else{
  641. //$json = file_get_contents("scraper/google.json");
  642. $params = [
  643. "q" => $get["s"],
  644. "cx" => config::GOOGLE_CX_ENDPOINT,
  645. "num" => 10,
  646. "start" => 1,
  647. "searchType" => "image",
  648. "key" => $keydata["key"]
  649. ];
  650. //
  651. // parse filters
  652. //
  653. if($get["newer"] !== false){
  654. $params["dateRestrict"] = "d" . (round((time() - $get["newer"]) / 100000));
  655. }
  656. if($get["rm_dupes"] == "no"){ $params["filter"] = "0"; }
  657. if($get["country"] != "any"){ $params["gl"] = $get["country"]; }
  658. if($get["nsfw"] == "yes"){
  659. $params["safe"] = "off";
  660. }else{
  661. $params["safe"] = "active";
  662. }
  663. if($get["sort"] != "any"){ $params["sort"] = $get["sort"]; }
  664. // image filters
  665. if($get["size"] != "any"){ $params["imgSize"] = $get["size"]; }
  666. if($get["format"] != "any"){ $params["fileType"] = $get["format"]; }
  667. switch($get["color"]){
  668. case "any":
  669. break;
  670. case "color":
  671. case "mono":
  672. case "trans":
  673. $params["imgColorType"] = $get["color"];
  674. break;
  675. default:
  676. $params["imgDominantColor"] = $get["color"];
  677. break;
  678. }
  679. if($get["type"] != "any"){ $params["imgType"] = $get["type"]; }
  680. if($get["rights"] != "any"){ $params["rights"] = $get["rights"]; }
  681. }
  682. try{
  683. $json =
  684. $this->get(
  685. $proxy,
  686. "https://www.googleapis.com/customsearch/v1",
  687. $params
  688. );
  689. }catch(Exception $error){
  690. throw new Exception("Failed to fetch JSON");
  691. }
  692. $json = json_decode($json, true);
  693. if($json === null){
  694. throw new Exception("Failed to decode JSON");
  695. }
  696. $out = [
  697. "status" => "ok",
  698. "npt" => null,
  699. "image" => []
  700. ];
  701. if(isset($json["error"]["message"])){
  702. throw new Exception(
  703. "API returned an error: " .
  704. $json["error"]["message"] .
  705. " (key #" . $keydata["increment"] . ")"
  706. );
  707. }
  708. if(!isset($json["items"])){
  709. // google just doesnt return items when theres no results
  710. return $out;
  711. }
  712. foreach($json["items"] as $image){
  713. $out["image"][] = [
  714. "title" => $this->titledots($image["title"]),
  715. "source" => [
  716. [
  717. "url" => $image["link"],
  718. "width" => (int)$image["image"]["width"],
  719. "height" => (int)$image["image"]["height"]
  720. ],
  721. [
  722. "url" => $image["image"]["thumbnailLink"],
  723. "width" => (int)$image["image"]["thumbnailWidth"],
  724. "height" => (int)$image["image"]["thumbnailHeight"]
  725. ]
  726. ],
  727. "url" => $image["image"]["contextLink"]
  728. ];
  729. }
  730. // get npt
  731. if(isset($json["queries"]["nextPage"][0]["startIndex"])){
  732. unset($params["key"]);
  733. $params["start"] = (int)$json["queries"]["nextPage"][0]["startIndex"];
  734. $out["npt"] =
  735. $this->backend->store(
  736. json_encode($params),
  737. "web",
  738. $proxy
  739. );
  740. }
  741. return $out;
  742. }
  743. private function titledots($title){
  744. return trim($title, " .\t\n\r\0\x0B…");
  745. }
  746. }