google_api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. $date = null;
  518. //
  519. // probe for thumbnail
  520. //
  521. $probes = [
  522. isset($result["pagemap"]["cse_thumbnail"][0]["src"]) ? $result["pagemap"]["cse_thumbnail"][0]["src"] : null,
  523. isset($result["pagemap"]["cse_image"][0]["src"]) ? $result["pagemap"]["cse_image"][0]["src"] : null,
  524. isset($result["pagemap"]["metatags"][0]["twitter:image"]) ? $result["pagemap"]["metatags"][0]["twitter:image"] : null,
  525. isset($result["pagemap"]["metatags"][0]["og:image"]) ? $result["pagemap"]["metatags"][0]["og:image"] : null
  526. ];
  527. $thumb = [
  528. "url" => null,
  529. "ratio" => null
  530. ];
  531. foreach($probes as $probe){
  532. if($probe !== null){
  533. $thumb = [
  534. "url" => $probe,
  535. "ratio" => "16:9"
  536. ];
  537. break;
  538. }
  539. }
  540. //
  541. // probe for page format
  542. //
  543. $mime = "web";
  544. if(isset($result["mime"])){
  545. $result["mime"] =
  546. explode(
  547. "/",
  548. $result["mime"],
  549. 2
  550. );
  551. if(count($result["mime"]) === 2){
  552. $mime = strtoupper($result["mime"][1]);
  553. }
  554. }
  555. $description = $result["snippet"];
  556. //
  557. // Get date
  558. //
  559. $description_split =
  560. explode(
  561. "...", $description, 2
  562. );
  563. if(count($description_split) === 1){
  564. $description = $result["snippet"];
  565. }elseif(strlen($description_split[0]) < 17){
  566. $date = trim($description_split[0]);
  567. $date_probe = strtotime($date);
  568. if($date_probe !== false){
  569. $description = $description_split[1];
  570. }else{
  571. //
  572. // fallback to getting date from meta tags
  573. //
  574. if(isset($result["pagemap"]["metatags"][0]["creationdate"])){
  575. $date = $result["pagemap"]["metatags"][0]["creationdate"];
  576. }elseif(isset($result["pagemap"]["metatags"][0]["moddate"])){
  577. $date = $result["pagemap"]["metatags"][0]["moddate"];
  578. }else{
  579. $date = null;
  580. }
  581. $description = $result["snippet"];
  582. }
  583. }
  584. if($date !== null){
  585. $date =
  586. strtotime(
  587. trim(
  588. str_replace(
  589. ["D:", "'"],
  590. "",
  591. $date
  592. )
  593. )
  594. );
  595. if($date === false){
  596. $date = null;
  597. }
  598. }
  599. $out["web"][] = [
  600. "title" =>
  601. $this->titledots(
  602. $result["title"]
  603. ),
  604. "description" =>
  605. $this->titledots(
  606. $description
  607. ),
  608. "url" => $result["link"],
  609. "date" => $date,
  610. "type" => $mime,
  611. "thumb" => $thumb,
  612. "sublink" => [],
  613. "table" => []
  614. ];
  615. }
  616. // get npt
  617. if(isset($json["queries"]["nextPage"][0]["startIndex"])){
  618. unset($params["key"]);
  619. $params["start"] = (int)$json["queries"]["nextPage"][0]["startIndex"];
  620. $out["npt"] =
  621. $this->backend->store(
  622. json_encode($params),
  623. "web",
  624. $proxy
  625. );
  626. }
  627. return $out;
  628. }
  629. public function image($get){
  630. // rotate proxy + key on EVERY request
  631. $keydata = $this->backend->get_key();
  632. $proxy = $this->backend->get_ip($keydata["increment"]);
  633. if($get["npt"]){
  634. // $p is never used
  635. [$params, $p] = $this->backend->get(
  636. $get["npt"],
  637. "web"
  638. );
  639. $params = json_decode($params, true);
  640. $params["key"] = $keydata["key"];
  641. }else{
  642. //$json = file_get_contents("scraper/google.json");
  643. $params = [
  644. "q" => $get["s"],
  645. "cx" => config::GOOGLE_CX_ENDPOINT,
  646. "num" => 10,
  647. "start" => 1,
  648. "searchType" => "image",
  649. "key" => $keydata["key"]
  650. ];
  651. //
  652. // parse filters
  653. //
  654. if($get["newer"] !== false){
  655. $params["dateRestrict"] = "d" . (round((time() - $get["newer"]) / 100000));
  656. }
  657. if($get["rm_dupes"] == "no"){ $params["filter"] = "0"; }
  658. if($get["country"] != "any"){ $params["gl"] = $get["country"]; }
  659. if($get["nsfw"] == "yes"){
  660. $params["safe"] = "off";
  661. }else{
  662. $params["safe"] = "active";
  663. }
  664. if($get["sort"] != "any"){ $params["sort"] = $get["sort"]; }
  665. // image filters
  666. if($get["size"] != "any"){ $params["imgSize"] = $get["size"]; }
  667. if($get["format"] != "any"){ $params["fileType"] = $get["format"]; }
  668. switch($get["color"]){
  669. case "any":
  670. break;
  671. case "color":
  672. case "mono":
  673. case "trans":
  674. $params["imgColorType"] = $get["color"];
  675. break;
  676. default:
  677. $params["imgDominantColor"] = $get["color"];
  678. break;
  679. }
  680. if($get["type"] != "any"){ $params["imgType"] = $get["type"]; }
  681. if($get["rights"] != "any"){ $params["rights"] = $get["rights"]; }
  682. }
  683. try{
  684. $json =
  685. $this->get(
  686. $proxy,
  687. "https://www.googleapis.com/customsearch/v1",
  688. $params
  689. );
  690. }catch(Exception $error){
  691. throw new Exception("Failed to fetch JSON");
  692. }
  693. $json = json_decode($json, true);
  694. if($json === null){
  695. throw new Exception("Failed to decode JSON");
  696. }
  697. $out = [
  698. "status" => "ok",
  699. "npt" => null,
  700. "image" => []
  701. ];
  702. if(isset($json["error"]["message"])){
  703. throw new Exception(
  704. "API returned an error: " .
  705. $json["error"]["message"] .
  706. " (key #" . $keydata["increment"] . ")"
  707. );
  708. }
  709. if(!isset($json["items"])){
  710. // google just doesnt return items when theres no results
  711. return $out;
  712. }
  713. foreach($json["items"] as $image){
  714. $out["image"][] = [
  715. "title" => $this->titledots($image["title"]),
  716. "source" => [
  717. [
  718. "url" => $image["link"],
  719. "width" => (int)$image["image"]["width"],
  720. "height" => (int)$image["image"]["height"]
  721. ],
  722. [
  723. "url" => $image["image"]["thumbnailLink"],
  724. "width" => (int)$image["image"]["thumbnailWidth"],
  725. "height" => (int)$image["image"]["thumbnailHeight"]
  726. ]
  727. ],
  728. "url" => $image["image"]["contextLink"]
  729. ];
  730. }
  731. // get npt
  732. if(isset($json["queries"]["nextPage"][0]["startIndex"])){
  733. unset($params["key"]);
  734. $params["start"] = (int)$json["queries"]["nextPage"][0]["startIndex"];
  735. $out["npt"] =
  736. $this->backend->store(
  737. json_encode($params),
  738. "web",
  739. $proxy
  740. );
  741. }
  742. return $out;
  743. }
  744. private function titledots($title){
  745. return trim($title, " .\t\n\r\0\x0B…");
  746. }
  747. }