google.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  1. <?php
  2. // @TODO check for consent.google.com page, if need be
  3. class google{
  4. public function __construct(){
  5. include "lib/fuckhtml.php";
  6. $this->fuckhtml = new fuckhtml();
  7. include "lib/backend.php";
  8. $this->backend = new backend("google");
  9. }
  10. public function getfilters($page){
  11. $base = [
  12. "country" => [ // gl=<country> (image: cr=countryAF)
  13. "display" => "Country",
  14. "option" => [
  15. "any" => "Instance's country",
  16. "af" => "Afghanistan",
  17. "al" => "Albania",
  18. "dz" => "Algeria",
  19. "as" => "American Samoa",
  20. "ad" => "Andorra",
  21. "ao" => "Angola",
  22. "ai" => "Anguilla",
  23. "aq" => "Antarctica",
  24. "ag" => "Antigua and Barbuda",
  25. "ar" => "Argentina",
  26. "am" => "Armenia",
  27. "aw" => "Aruba",
  28. "au" => "Australia",
  29. "at" => "Austria",
  30. "az" => "Azerbaijan",
  31. "bs" => "Bahamas",
  32. "bh" => "Bahrain",
  33. "bd" => "Bangladesh",
  34. "bb" => "Barbados",
  35. "by" => "Belarus",
  36. "be" => "Belgium",
  37. "bz" => "Belize",
  38. "bj" => "Benin",
  39. "bm" => "Bermuda",
  40. "bt" => "Bhutan",
  41. "bo" => "Bolivia",
  42. "ba" => "Bosnia and Herzegovina",
  43. "bw" => "Botswana",
  44. "bv" => "Bouvet Island",
  45. "br" => "Brazil",
  46. "io" => "British Indian Ocean Territory",
  47. "bn" => "Brunei Darussalam",
  48. "bg" => "Bulgaria",
  49. "bf" => "Burkina Faso",
  50. "bi" => "Burundi",
  51. "kh" => "Cambodia",
  52. "cm" => "Cameroon",
  53. "ca" => "Canada",
  54. "cv" => "Cape Verde",
  55. "ky" => "Cayman Islands",
  56. "cf" => "Central African Republic",
  57. "td" => "Chad",
  58. "cl" => "Chile",
  59. "cn" => "China",
  60. "cx" => "Christmas Island",
  61. "cc" => "Cocos (Keeling) Islands",
  62. "co" => "Colombia",
  63. "km" => "Comoros",
  64. "cg" => "Congo",
  65. "cd" => "Congo, the Democratic Republic",
  66. "ck" => "Cook Islands",
  67. "cr" => "Costa Rica",
  68. "ci" => "Cote D'ivoire",
  69. "hr" => "Croatia",
  70. "cu" => "Cuba",
  71. "cy" => "Cyprus",
  72. "cz" => "Czech Republic",
  73. "dk" => "Denmark",
  74. "dj" => "Djibouti",
  75. "dm" => "Dominica",
  76. "do" => "Dominican Republic",
  77. "ec" => "Ecuador",
  78. "eg" => "Egypt",
  79. "sv" => "El Salvador",
  80. "gq" => "Equatorial Guinea",
  81. "er" => "Eritrea",
  82. "ee" => "Estonia",
  83. "et" => "Ethiopia",
  84. "fk" => "Falkland Islands (Malvinas)",
  85. "fo" => "Faroe Islands",
  86. "fj" => "Fiji",
  87. "fi" => "Finland",
  88. "fr" => "France",
  89. "gf" => "French Guiana",
  90. "pf" => "French Polynesia",
  91. "tf" => "French Southern Territories",
  92. "ga" => "Gabon",
  93. "gm" => "Gambia",
  94. "ge" => "Georgia",
  95. "de" => "Germany",
  96. "gh" => "Ghana",
  97. "gi" => "Gibraltar",
  98. "gr" => "Greece",
  99. "gl" => "Greenland",
  100. "gd" => "Grenada",
  101. "gp" => "Guadeloupe",
  102. "gu" => "Guam",
  103. "gt" => "Guatemala",
  104. "gn" => "Guinea",
  105. "gw" => "Guinea-Bissau",
  106. "gy" => "Guyana",
  107. "ht" => "Haiti",
  108. "hm" => "Heard Island and Mcdonald Islands",
  109. "va" => "Holy See (Vatican City State)",
  110. "hn" => "Honduras",
  111. "hk" => "Hong Kong",
  112. "hu" => "Hungary",
  113. "is" => "Iceland",
  114. "in" => "India",
  115. "id" => "Indonesia",
  116. "ir" => "Iran, Islamic Republic",
  117. "iq" => "Iraq",
  118. "ie" => "Ireland",
  119. "il" => "Israel",
  120. "it" => "Italy",
  121. "jm" => "Jamaica",
  122. "jp" => "Japan",
  123. "jo" => "Jordan",
  124. "kz" => "Kazakhstan",
  125. "ke" => "Kenya",
  126. "ki" => "Kiribati",
  127. "kp" => "Korea, Democratic People's Republic",
  128. "kr" => "Korea, Republic",
  129. "kw" => "Kuwait",
  130. "kg" => "Kyrgyzstan",
  131. "la" => "Lao People's Democratic Republic",
  132. "lv" => "Latvia",
  133. "lb" => "Lebanon",
  134. "ls" => "Lesotho",
  135. "lr" => "Liberia",
  136. "ly" => "Libyan Arab Jamahiriya",
  137. "li" => "Liechtenstein",
  138. "lt" => "Lithuania",
  139. "lu" => "Luxembourg",
  140. "mo" => "Macao",
  141. "mk" => "Macedonia, the Former Yugosalv Republic",
  142. "mg" => "Madagascar",
  143. "mw" => "Malawi",
  144. "my" => "Malaysia",
  145. "mv" => "Maldives",
  146. "ml" => "Mali",
  147. "mt" => "Malta",
  148. "mh" => "Marshall Islands",
  149. "mq" => "Martinique",
  150. "mr" => "Mauritania",
  151. "mu" => "Mauritius",
  152. "yt" => "Mayotte",
  153. "mx" => "Mexico",
  154. "fm" => "Micronesia, Federated States",
  155. "md" => "Moldova, Republic",
  156. "mc" => "Monaco",
  157. "mn" => "Mongolia",
  158. "ms" => "Montserrat",
  159. "ma" => "Morocco",
  160. "mz" => "Mozambique",
  161. "mm" => "Myanmar",
  162. "na" => "Namibia",
  163. "nr" => "Nauru",
  164. "np" => "Nepal",
  165. "nl" => "Netherlands",
  166. "an" => "Netherlands Antilles",
  167. "nc" => "New Caledonia",
  168. "nz" => "New Zealand",
  169. "ni" => "Nicaragua",
  170. "ne" => "Niger",
  171. "ng" => "Nigeria",
  172. "nu" => "Niue",
  173. "nf" => "Norfolk Island",
  174. "mp" => "Northern Mariana Islands",
  175. "no" => "Norway",
  176. "om" => "Oman",
  177. "pk" => "Pakistan",
  178. "pw" => "Palau",
  179. "ps" => "Palestinian Territory, Occupied",
  180. "pa" => "Panama",
  181. "pg" => "Papua New Guinea",
  182. "py" => "Paraguay",
  183. "pe" => "Peru",
  184. "ph" => "Philippines",
  185. "pn" => "Pitcairn",
  186. "pl" => "Poland",
  187. "pt" => "Portugal",
  188. "pr" => "Puerto Rico",
  189. "qa" => "Qatar",
  190. "re" => "Reunion",
  191. "ro" => "Romania",
  192. "ru" => "Russian Federation",
  193. "rw" => "Rwanda",
  194. "sh" => "Saint Helena",
  195. "kn" => "Saint Kitts and Nevis",
  196. "lc" => "Saint Lucia",
  197. "pm" => "Saint Pierre and Miquelon",
  198. "vc" => "Saint Vincent and the Grenadines",
  199. "ws" => "Samoa",
  200. "sm" => "San Marino",
  201. "st" => "Sao Tome and Principe",
  202. "sa" => "Saudi Arabia",
  203. "sn" => "Senegal",
  204. "cs" => "Serbia and Montenegro",
  205. "sc" => "Seychelles",
  206. "sl" => "Sierra Leone",
  207. "sg" => "Singapore",
  208. "sk" => "Slovakia",
  209. "si" => "Slovenia",
  210. "sb" => "Solomon Islands",
  211. "so" => "Somalia",
  212. "za" => "South Africa",
  213. "gs" => "South Georgia and the South Sandwich Islands",
  214. "es" => "Spain",
  215. "lk" => "Sri Lanka",
  216. "sd" => "Sudan",
  217. "sr" => "Suriname",
  218. "sj" => "Svalbard and Jan Mayen",
  219. "sz" => "Swaziland",
  220. "se" => "Sweden",
  221. "ch" => "Switzerland",
  222. "sy" => "Syrian Arab Republic",
  223. "tw" => "Taiwan, Province of China",
  224. "tj" => "Tajikistan",
  225. "tz" => "Tanzania, United Republic",
  226. "th" => "Thailand",
  227. "tl" => "Timor-Leste",
  228. "tg" => "Togo",
  229. "tk" => "Tokelau",
  230. "to" => "Tonga",
  231. "tt" => "Trinidad and Tobago",
  232. "tn" => "Tunisia",
  233. "tr" => "Turkey",
  234. "tm" => "Turkmenistan",
  235. "tc" => "Turks and Caicos Islands",
  236. "tv" => "Tuvalu",
  237. "ug" => "Uganda",
  238. "ua" => "Ukraine",
  239. "ae" => "United Arab Emirates",
  240. "uk" => "United Kingdom",
  241. "us" => "United States",
  242. "um" => "United States Minor Outlying Islands",
  243. "uy" => "Uruguay",
  244. "uz" => "Uzbekistan",
  245. "vu" => "Vanuatu",
  246. "ve" => "Venezuela",
  247. "vn" => "Viet Nam",
  248. "vg" => "Virgin Islands, British",
  249. "vi" => "Virgin Islands, U.S.",
  250. "wf" => "Wallis and Futuna",
  251. "eh" => "Western Sahara",
  252. "ye" => "Yemen",
  253. "zm" => "Zambia",
  254. "zw" => "Zimbabwe"
  255. ]
  256. ],
  257. "nsfw" => [
  258. "display" => "NSFW",
  259. "option" => [
  260. "yes" => "Yes", // safe=active
  261. "no" => "No" // safe=off
  262. ]
  263. ]
  264. ];
  265. switch($page){
  266. case "web":
  267. return array_merge(
  268. $base,
  269. [
  270. "lang" => [ // lr=<lang> (prefix lang with "lang_")
  271. "display" => "Language",
  272. "option" => [
  273. "any" => "Any language",
  274. "ar" => "Arabic",
  275. "bg" => "Bulgarian",
  276. "ca" => "Catalan",
  277. "cs" => "Czech",
  278. "da" => "Danish",
  279. "de" => "German",
  280. "el" => "Greek",
  281. "en" => "English",
  282. "es" => "Spanish",
  283. "et" => "Estonian",
  284. "fi" => "Finnish",
  285. "fr" => "French",
  286. "hr" => "Croatian",
  287. "hu" => "Hungarian",
  288. "id" => "Indonesian",
  289. "is" => "Icelandic",
  290. "it" => "Italian",
  291. "iw" => "Hebrew",
  292. "ja" => "Japanese",
  293. "ko" => "Korean",
  294. "lt" => "Lithuanian",
  295. "lv" => "Latvian",
  296. "nl" => "Dutch",
  297. "no" => "Norwegian",
  298. "pl" => "Polish",
  299. "pt" => "Portuguese",
  300. "ro" => "Romanian",
  301. "ru" => "Russian",
  302. "sk" => "Slovak",
  303. "sl" => "Slovenian",
  304. "sr" => "Serbian",
  305. "sv" => "Swedish",
  306. "tr" => "Turkish",
  307. "zh-CN" => "Chinese (Simplified)",
  308. "zh-TW" => "Chinese (Traditional)"
  309. ]
  310. ],
  311. "newer" => [ // tbs
  312. "display" => "Newer than",
  313. "option" => "_DATE"
  314. ],
  315. "older" => [
  316. "display" => "Older than",
  317. "option" => "_DATE"
  318. ],
  319. "spellcheck" => [
  320. "display" => "Spellcheck",
  321. "option" => [
  322. "yes" => "Yes",
  323. "no" => "No"
  324. ]
  325. ]
  326. ]
  327. );
  328. break;
  329. case "images":
  330. return array_merge(
  331. $base,
  332. [
  333. "time" => [ // tbs=qdr:<time>
  334. "display" => "Time posted",
  335. "option" => [
  336. "any" => "Any time",
  337. "d" => "Past 24 hours",
  338. "w" => "Past week",
  339. "m" => "Past month",
  340. "y" => "Past year"
  341. ]
  342. ],
  343. "size" => [ // imgsz
  344. "display" => "Size",
  345. "option" => [
  346. "any" => "Any size",
  347. "l" => "Large",
  348. "m" => "Medium",
  349. "i" => "Icon",
  350. "qsvga" => "Larger than 400x300",
  351. "vga" => "Larger than 640x480",
  352. "svga" => "Larger than 800x600",
  353. "xga" => "Larger than 1024x768",
  354. "2mp" => "Larger than 2MP",
  355. "4mp" => "Larger than 4MP",
  356. "6mp" => "Larger than 6MP",
  357. "8mp" => "Larger than 8MP",
  358. "10mp" => "Larger than 10MP",
  359. "12mp" => "Larger than 12MP",
  360. "15mp" => "Larger than 15MP",
  361. "20mp" => "Larger than 20MP",
  362. "40mp" => "Larger than 40MP",
  363. "70mp" => "Larger than 70MP"
  364. ]
  365. ],
  366. "ratio" => [ // imgar
  367. "display" => "Aspect ratio",
  368. "option" => [
  369. "any" => "Any ratio",
  370. "t|xt" => "Tall",
  371. "s" => "Square",
  372. "w" => "Wide",
  373. "xw" => "Panoramic"
  374. ]
  375. ],
  376. "color" => [ // imgc
  377. "display" => "Color",
  378. "option" => [
  379. "any" => "Any color",
  380. "color" => "Full color",
  381. "bnw" => "Black & white",
  382. "trans" => "Transparent",
  383. // from here, imgcolor
  384. "red" => "Red",
  385. "orange" => "Orange",
  386. "yellow" => "Yellow",
  387. "green" => "Green",
  388. "teal" => "Teal",
  389. "blue" => "Blue",
  390. "purple" => "Purple",
  391. "pink" => "Pink",
  392. "white" => "White",
  393. "gray" => "Gray",
  394. "black" => "Black",
  395. "brown" => "Brown"
  396. ]
  397. ],
  398. "type" => [ // tbs=itp:<type>
  399. "display" => "Type",
  400. "option" => [
  401. "any" => "Any type",
  402. "clipart" => "Clip Art",
  403. "lineart" => "Line Drawing",
  404. "animated" => "Animated"
  405. ]
  406. ],
  407. "format" => [ // as_filetype
  408. "display" => "Format",
  409. "option" => [
  410. "any" => "Any format",
  411. "jpg" => "JPG",
  412. "gif" => "GIF",
  413. "png" => "PNG",
  414. "bmp" => "BMP",
  415. "svg" => "SVG",
  416. "webp" => "WEBP",
  417. "ico" => "ICO",
  418. "craw" => "RAW"
  419. ]
  420. ],
  421. "rights" => [ // tbs=sur:<rights>
  422. "display" => "Usage rights",
  423. "option" => [
  424. "any" => "Any license",
  425. "cl" => "Creative Commons licenses",
  426. "ol" => "Commercial & other licenses"
  427. ]
  428. ]
  429. ]
  430. );
  431. break;
  432. case "videos":
  433. return array_merge(
  434. $base,
  435. [
  436. "newer" => [ // tbs
  437. "display" => "Newer than",
  438. "option" => "_DATE"
  439. ],
  440. "older" => [
  441. "display" => "Older than",
  442. "option" => "_DATE"
  443. ],
  444. "duration" => [
  445. "display" => "Duration",
  446. "option" => [
  447. "any" => "Any duration",
  448. "s" => "Short (0-4min)", // tbs=dur:s
  449. "m" => "Medium (4-20min)", // tbs=dur:m
  450. "l" => "Long (20+ min)" // tbs=dur:l
  451. ]
  452. ],
  453. "quality" => [
  454. "display" => "Quality",
  455. "option" => [
  456. "any" => "Any quality",
  457. "h" => "High quality" // tbs=hq:h
  458. ]
  459. ],
  460. "captions" => [
  461. "display" => "Captions",
  462. "option" => [
  463. "any" => "No preference",
  464. "yes" => "Closed captioned" // tbs=cc:1
  465. ]
  466. ]
  467. ]
  468. );
  469. break;
  470. case "news":
  471. return array_merge(
  472. $base,
  473. [
  474. "newer" => [ // tbs
  475. "display" => "Newer than",
  476. "option" => "_DATE"
  477. ],
  478. "older" => [
  479. "display" => "Older than",
  480. "option" => "_DATE"
  481. ],
  482. "sort" => [
  483. "display" => "Sort",
  484. "option" => [
  485. "relevance" => "Relevance",
  486. "date" => "Date" // sbd:1
  487. ]
  488. ]
  489. ]
  490. );
  491. break;
  492. }
  493. }
  494. private function get($proxy, $url, $get = [], $use_lynx = false){
  495. $curlproc = curl_init();
  496. if($use_lynx === false){
  497. $headers = [
  498. "User-Agent: " . config::USER_AGENT,
  499. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  500. "Accept-Language: en-US,en;q=0.5",
  501. "Accept-Encoding: gzip",
  502. "DNT: 1",
  503. //"Cookie: SOCS=CAESNQgCEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjQwMzE3LjA4X3AwGgJlbiAEGgYIgM7orwY",
  504. "Connection: keep-alive",
  505. "Upgrade-Insecure-Requests: 1",
  506. "Sec-Fetch-Dest: document",
  507. "Sec-Fetch-Mode: navigate",
  508. "Sec-Fetch-Site: none",
  509. "Sec-Fetch-User: ?1",
  510. "Priority: u=1",
  511. "TE: trailers"
  512. ];
  513. // use http2
  514. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  515. }else{
  516. $headers = [
  517. "Accept: text/html, text/plain, text/sgml, */*;q=0.01",
  518. "Accept-Encoding: gzip, compress, bzip2",
  519. "Accept-Language: en",
  520. "User-Agent: Lynx/2.9.0dev.12 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.7.8"
  521. ];
  522. }
  523. if($get !== []){
  524. $get = http_build_query($get);
  525. $url .= "?" . $get;
  526. }
  527. curl_setopt($curlproc, CURLOPT_URL, $url);
  528. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  529. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  530. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  531. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  532. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  533. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  534. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  535. // follow redirects
  536. curl_setopt($curlproc, CURLOPT_FOLLOWLOCATION, true);
  537. $this->backend->assign_proxy($curlproc, $proxy);
  538. $data = curl_exec($curlproc);
  539. if(curl_errno($curlproc)){
  540. throw new Exception(curl_error($curlproc));
  541. }
  542. curl_close($curlproc);
  543. if($use_lynx){
  544. return mb_convert_encoding($data, "UTF-8", "ISO-8859-1");
  545. }
  546. return $data;
  547. }
  548. private function scrape_dimg($html){
  549. // get images loaded through javascript
  550. $this->dimg = [];
  551. preg_match_all(
  552. '/function\(\){google\.ldi=({.*?});/',
  553. $html,
  554. $dimg
  555. );
  556. if(isset($dimg[1])){
  557. foreach($dimg[1] as $i){
  558. $tmp = json_decode($i, true);
  559. foreach($tmp as $key => $value){
  560. $this->dimg[$key] =
  561. $this->unshit_thumb(
  562. $value
  563. );
  564. }
  565. }
  566. }
  567. // get additional javascript base64 images
  568. preg_match_all(
  569. '/var s=\'(data:image\/[^\']+)\';var ii=\[((?:\'[^\']+\',?)+)\];/',
  570. $html,
  571. $dimg
  572. );
  573. if(isset($dimg[1])){
  574. for($i=0; $i<count($dimg[1]); $i++){
  575. $delims = explode(",", $dimg[2][$i]);
  576. $string =
  577. $this->fuckhtml
  578. ->parseJsString(
  579. $dimg[1][$i]
  580. );
  581. foreach($delims as $delim){
  582. $this->dimg[trim($delim, "'")] = $string;
  583. }
  584. }
  585. }
  586. }
  587. private function scrape_imagearr($html){
  588. // get image links arrays
  589. preg_match_all(
  590. '/\[0,"([^"]+)",\["([^"]+)\",([0-9]+),([0-9]+)\],\["([^"]+)",([0-9]+),([0-9]+)\]/',
  591. $html,
  592. $image_arr
  593. );
  594. $this->image_arr = [];
  595. if(isset($image_arr[1])){
  596. for($i=0; $i<count($image_arr[1]); $i++){
  597. $this->image_arr[$image_arr[1][$i]] =
  598. [
  599. [
  600. "url" =>
  601. $this->fuckhtml
  602. ->parseJsString(
  603. $image_arr[5][$i]
  604. ),
  605. "width" => (int)$image_arr[7][$i],
  606. "height" => (int)$image_arr[6][$i]
  607. ],
  608. [
  609. "url" =>
  610. $this->unshit_thumb(
  611. $this->fuckhtml
  612. ->parseJsString(
  613. $image_arr[2][$i]
  614. )
  615. ),
  616. "width" => (int)$image_arr[4][$i],
  617. "height" => (int)$image_arr[3][$i]
  618. ]
  619. ];
  620. }
  621. }
  622. }
  623. private function getdimg($dimg){
  624. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  625. }
  626. private function unshit_thumb($url){
  627. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  628. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  629. $parts = parse_url($url);
  630. if(
  631. isset($parts["host"]) &&
  632. preg_match(
  633. '/tbn.*\.gstatic\.com/',
  634. $parts["host"]
  635. )
  636. ){
  637. parse_str($parts["query"], $params);
  638. if(isset($params["q"])){
  639. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  640. }
  641. }
  642. return $url;
  643. }
  644. private function parsestyles(){
  645. $styles = [];
  646. $style_div =
  647. $this->fuckhtml
  648. ->getElementsByTagName(
  649. "style"
  650. );
  651. $raw_styles = "";
  652. foreach($style_div as $style){
  653. $raw_styles .= $style["innerHTML"];
  654. }
  655. // filter out media/keyframe queries
  656. $raw_styles =
  657. preg_replace(
  658. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  659. "",
  660. $raw_styles
  661. );
  662. // get styles
  663. preg_match_all(
  664. '/(.+?){([\S\s]*?)}/',
  665. $raw_styles,
  666. $matches
  667. );
  668. for($i=0; $i<count($matches[1]); $i++){
  669. // get style values
  670. preg_match_all(
  671. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  672. $matches[2][$i],
  673. $values_regex
  674. );
  675. $values = [];
  676. for($k=0; $k<count($values_regex[1]); $k++){
  677. $values[trim($values_regex[1][$k])] =
  678. strtolower(trim($values_regex[2][$k]));
  679. }
  680. $names = explode(",", $matches[1][$i]);
  681. // h1,h2,h3 will each get their own array index
  682. foreach($names as $name){
  683. $name = trim($name, "}\t\n\r\0\x0B");
  684. foreach($values as $key => $value){
  685. $styles[$name][$key] = $value;
  686. }
  687. }
  688. }
  689. foreach($styles as $key => $values){
  690. $styles[$key]["_c"] = count($values);
  691. }
  692. $this->styles = $styles;
  693. // get CSS colors
  694. $this->css_colors = [];
  695. if(isset($this->styles[":root"])){
  696. foreach($this->styles[":root"] as $key => $value){
  697. $this->css_colors[$value] = strtolower($key);
  698. }
  699. }
  700. }
  701. private function getstyle($styles){
  702. $styles["_c"] = count($styles);
  703. foreach($this->styles as $style_key => $style_values){
  704. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  705. $style_key =
  706. explode(" ", $style_key);
  707. $style_key = $style_key[count($style_key) - 1];
  708. return
  709. ltrim(
  710. str_replace(
  711. [".", "#"],
  712. " ",
  713. $style_key
  714. )
  715. );
  716. }
  717. }
  718. return false;
  719. }
  720. private function getcolorvar($color){
  721. if(isset($this->css_colors[$color])){
  722. return $this->css_colors[$color];
  723. }
  724. return null;
  725. }
  726. public function web($get){
  727. if($get["npt"]){
  728. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  729. try{
  730. $html =
  731. $this->get(
  732. $proxy,
  733. "https://www.google.com" . $get,
  734. [],
  735. true
  736. );
  737. }catch(Exception $error){
  738. throw new Exception("Failed to get HTML");
  739. }
  740. }else{
  741. $search = $get["s"];
  742. $country = $get["country"];
  743. $nsfw = $get["nsfw"];
  744. $lang = $get["lang"];
  745. $older = $get["older"];
  746. $newer = $get["newer"];
  747. $spellcheck = $get["spellcheck"];
  748. $proxy = $this->backend->get_ip();
  749. $offset = 0;
  750. $params = [
  751. "q" => $search,
  752. "hl" => "en",
  753. "num" => 20
  754. ];
  755. // country
  756. if($country != "any"){
  757. $params["gl"] = $country;
  758. }
  759. // nsfw
  760. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  761. // language
  762. if($lang != "any"){
  763. $params["lr"] = "lang_" . $lang;
  764. }
  765. // generate tbs
  766. $tbs = [];
  767. // get date
  768. $older = $older === false ? null : date("m/d/Y", $older);
  769. $newer = $newer === false ? null : date("m/d/Y", $newer);
  770. if(
  771. $older !== null ||
  772. $newer !== null
  773. ){
  774. $tbs["cdr"] = "1";
  775. $tbs["cd_min"] = $newer;
  776. $tbs["cd_max"] = $older;
  777. }
  778. // spellcheck filter
  779. if($spellcheck == "no"){
  780. $params["nfpr"] = "1";
  781. }
  782. if(count($tbs) !== 0){
  783. $params["tbs"] = "";
  784. foreach($tbs as $key => $value){
  785. $params["tbs"] .= $key . ":" . $value . ",";
  786. }
  787. $params["tbs"] = rtrim($params["tbs"], ",");
  788. }
  789. try{
  790. $html =
  791. $this->get(
  792. $proxy,
  793. "https://www.google.com/search",
  794. $params,
  795. true
  796. );
  797. }catch(Exception $error){
  798. throw new Exception("Failed to get HTML");
  799. }
  800. //$html = file_get_contents("scraper/google.html");
  801. }
  802. $out = [
  803. "status" => "ok",
  804. "spelling" => [
  805. "type" => "no_correction",
  806. "using" => null,
  807. "correction" => null
  808. ],
  809. "npt" => null,
  810. "answer" => [],
  811. "web" => [],
  812. "image" => [],
  813. "video" => [],
  814. "news" => [],
  815. "related" => []
  816. ];
  817. $this->fuckhtml->load($html);
  818. $this->parsestyles();
  819. $boxes =
  820. $this->fuckhtml
  821. ->getElementsByClassName(
  822. $this->getstyle([
  823. "border" => "thin solid #dadce0",
  824. "padding" => "12px 16px 12px 16px",
  825. "margin-bottom" => "10px",
  826. "font-family" => "sans-serif"
  827. ]),
  828. "div"
  829. );
  830. $skip_next = false;
  831. // get next page token
  832. $npt =
  833. $this->fuckhtml
  834. ->getElementsByClassName(
  835. $this->getstyle([
  836. "border" => "thin solid #dadce0",
  837. "color" => "#70757a",
  838. "font-size" => "14px",
  839. "text-align" => "center",
  840. "table-layout" => "fixed",
  841. "width" => "100%"
  842. ]),
  843. "table"
  844. );
  845. if(count($npt) !== 0){
  846. $this->fuckhtml->load($npt[0]);
  847. $as =
  848. $this->fuckhtml
  849. ->getElementsByTagName(
  850. "a"
  851. );
  852. foreach($as as $a){
  853. $text =
  854. $this->fuckhtml
  855. ->getTextContent(
  856. $a
  857. );
  858. if(
  859. $text == "Next&nbsp;>" ||
  860. $text == ">"
  861. ){
  862. $out["npt"] =
  863. $this->backend->store(
  864. $this->fuckhtml
  865. ->getTextContent(
  866. $a["attributes"]["href"]
  867. ),
  868. "web",
  869. $proxy
  870. );
  871. }
  872. }
  873. $this->fuckhtml->load($html);
  874. }
  875. $first_box = true;
  876. foreach($boxes as $box){
  877. $this->fuckhtml->load($box);
  878. if($first_box){
  879. //
  880. // Probe for word correction
  881. //
  882. $first_box = false;
  883. $txt =
  884. $this->fuckhtml
  885. ->getTextContent($box);
  886. if(
  887. preg_match(
  888. '/^Showing results for /',
  889. $txt
  890. )
  891. ){
  892. $as =
  893. $this->fuckhtml
  894. ->getElementsByTagName(
  895. "a"
  896. );
  897. if(count($as) === 2){
  898. $out["spelling"] = [
  899. "type" => "including",
  900. "using" =>
  901. $this->fuckhtml
  902. ->getTextContent(
  903. $as[0]
  904. ),
  905. "correction" =>
  906. $this->fuckhtml
  907. ->getTextContent(
  908. $as[1]
  909. )
  910. ];
  911. }
  912. continue;
  913. }
  914. }
  915. // probe for custom container
  916. $container_title =
  917. $this->fuckhtml
  918. ->getElementsByClassName(
  919. $this->getstyle([
  920. "font-weight" => "bold"
  921. ])
  922. );
  923. if(count($container_title) !== 0){
  924. $container_title =
  925. strtolower(
  926. $this->fuckhtml
  927. ->getTextContent(
  928. $container_title[0]
  929. )
  930. );
  931. if($container_title == "images"){
  932. //
  933. // Parse image carousel
  934. //
  935. $images =
  936. $this->fuckhtml
  937. ->getElementsByClassName(
  938. $this->getstyle([
  939. "display" => "inline-block",
  940. "padding" => "2px",
  941. "padding-bottom" => "4px"
  942. ]),
  943. "a"
  944. );
  945. foreach($images as $image){
  946. $this->fuckhtml->load($image);
  947. $image_data =
  948. $this->unshiturl(
  949. $image["attributes"]["href"],
  950. true
  951. );
  952. $img =
  953. $this->fuckhtml
  954. ->getElementsByTagName(
  955. "img"
  956. )[0];
  957. $out["image"][] = [
  958. "title" =>
  959. $this->titledots(
  960. $this->fuckhtml
  961. ->getTextContent(
  962. $img["attributes"]["alt"]
  963. )
  964. ),
  965. "source" => [
  966. [
  967. "url" => $image_data["url"],
  968. "width" => $image_data["image_width"],
  969. "height" => $image_data["image_height"]
  970. ],
  971. [
  972. "url" =>
  973. $this->fuckhtml
  974. ->getTextContent(
  975. $img["attributes"]["src"]
  976. ),
  977. "width" => $image_data["thumb_width"],
  978. "height" => $image_data["thumb_height"]
  979. ]
  980. ],
  981. "url" => $image_data["ref"]
  982. ];
  983. }
  984. continue;
  985. }
  986. if(
  987. $container_title == "related searches" ||
  988. $container_title == "people also search for"
  989. ){
  990. $as =
  991. $this->fuckhtml
  992. ->getElementsByClassName(
  993. $this->getstyle([
  994. "color" => "#202124",
  995. "font-size" => "13px",
  996. "line-height" => "20px"
  997. ]),
  998. "span"
  999. );
  1000. foreach($as as $a){
  1001. $out["related"][] =
  1002. $this->fuckhtml
  1003. ->getTextContent(
  1004. $a
  1005. );
  1006. }
  1007. continue;
  1008. }
  1009. }
  1010. // probe for website link
  1011. $link =
  1012. $this->fuckhtml
  1013. ->getElementsByClassName(
  1014. $this->getstyle([
  1015. "color" => "#1967d2",
  1016. "font-size" => "18px",
  1017. "line-height" => "24px"
  1018. ]),
  1019. "a"
  1020. );
  1021. if(count($link) !== 0){
  1022. //
  1023. // Parse search result
  1024. //
  1025. $this->fuckhtml->load($link[0]);
  1026. $title =
  1027. $this->fuckhtml
  1028. ->getElementsByClassName(
  1029. $this->getstyle([
  1030. "color" => "#1967d2",
  1031. "font-size" => "18px",
  1032. "line-height" => "24px"
  1033. ]),
  1034. "span"
  1035. );
  1036. if(count($title) === 0){
  1037. continue;
  1038. }
  1039. $this->fuckhtml->load($box);
  1040. $sublinks = [];
  1041. $table = [];
  1042. $categories =
  1043. $this->fuckhtml
  1044. ->getElementsByClassName(
  1045. $this->getstyle([
  1046. "color" => "#202124",
  1047. "font-size" => "13px",
  1048. "line-height" => "20px"
  1049. ]),
  1050. "span"
  1051. );
  1052. $i = 0;
  1053. foreach($categories as $category){
  1054. $this->fuckhtml->load($category);
  1055. // probe for sublinks
  1056. $subs =
  1057. $this->fuckhtml
  1058. ->getElementsByClassName(
  1059. $this->getstyle([
  1060. "color" => "#1967d2"
  1061. ]),
  1062. "a"
  1063. );
  1064. if(count($subs) !== 0){
  1065. foreach($subs as $sub){
  1066. $url =
  1067. $this->unshiturl(
  1068. $this->fuckhtml
  1069. ->getTextContent(
  1070. $sub["attributes"]["href"]
  1071. )
  1072. );
  1073. if(
  1074. preg_match(
  1075. '/^https?:\/\//',
  1076. $url
  1077. )
  1078. ){
  1079. $sublinks[] = [
  1080. "title" =>
  1081. $this->titledots(
  1082. $this->fuckhtml
  1083. ->getTextContent(
  1084. $sub
  1085. )
  1086. ),
  1087. "description" => null,
  1088. "url" =>
  1089. $this->unshiturl(
  1090. $this->fuckhtml
  1091. ->getTextContent(
  1092. $sub["attributes"]["href"]
  1093. )
  1094. ),
  1095. "date" => null
  1096. ];
  1097. }
  1098. }
  1099. unset($categories[$i]);
  1100. }
  1101. $i++;
  1102. }
  1103. // get description & date
  1104. $date = null;
  1105. $categories = array_values($categories);
  1106. //print_r($categories);
  1107. $c = count($categories) - 1;
  1108. $description =
  1109. $this->fuckhtml
  1110. ->getTextContent(
  1111. $categories[$c]
  1112. );
  1113. // remove last category since we're done with it
  1114. unset($categories[$c]);
  1115. // probe for date
  1116. $description_tmp = explode("·", $description, 2);
  1117. $date_tmp = strtotime(trim($description_tmp[0]));
  1118. if(
  1119. count($description_tmp) === 2 &&
  1120. strlen($description_tmp[0]) <= 20 &&
  1121. $date_tmp !== false
  1122. ){
  1123. $description =
  1124. ltrim(
  1125. $this->titledots(
  1126. $description_tmp[1]
  1127. )
  1128. );
  1129. $date = $date_tmp;
  1130. }else{
  1131. $description =
  1132. $this->titledots(
  1133. $description
  1134. );
  1135. }
  1136. // remaining categories should all be greytext
  1137. if(count($categories) !== 0){
  1138. $texts =
  1139. explode(
  1140. "·",
  1141. preg_replace(
  1142. '/\s+/',
  1143. " ",
  1144. $this->fuckhtml
  1145. ->getTextContent(
  1146. $categories[0]
  1147. )
  1148. )
  1149. );
  1150. foreach($texts as $text){
  1151. $text = trim($text);
  1152. if(
  1153. preg_match(
  1154. '/^Rating ([0-9.]+)(?: \(([0-9,]+)\))?/',
  1155. $text,
  1156. $rating
  1157. )
  1158. ){
  1159. $table["Rating"] = $rating[1];
  1160. if(isset($rating[2])){
  1161. $table["Rating"] .= " (" . $rating[2] . " votes)";
  1162. }
  1163. continue;
  1164. }
  1165. if(stripos($text, "stock") !== false){
  1166. $table["Stock"] = $text;
  1167. continue;
  1168. }
  1169. }
  1170. }
  1171. $out["web"][] = [
  1172. "title" =>
  1173. $this->titledots(
  1174. $this->fuckhtml
  1175. ->getTextContent(
  1176. $title[0]
  1177. )
  1178. ),
  1179. "description" => $description,
  1180. "url" =>
  1181. $this->unshiturl(
  1182. $link[0]["attributes"]["href"]
  1183. ),
  1184. "date" => $date,
  1185. "type" => "web",
  1186. "thumb" => [
  1187. "url" => null,
  1188. "ratio" => null
  1189. ],
  1190. "sublink" => $sublinks,
  1191. "table" => $table
  1192. ];
  1193. continue;
  1194. }
  1195. // parse wikipedia heads
  1196. $wiki_title =
  1197. $this->fuckhtml
  1198. ->getElementsByClassName(
  1199. $this->getstyle([
  1200. "color" => "#202124",
  1201. "font-size" => "18px",
  1202. "line-height" => "24px"
  1203. ]),
  1204. "span"
  1205. );
  1206. if(count($wiki_title) !== 0){
  1207. $wiki_title =
  1208. $this->fuckhtml
  1209. ->getTextContent(
  1210. $wiki_title[0]
  1211. );
  1212. if($wiki_title == "See results about"){
  1213. // ignore
  1214. continue;
  1215. }
  1216. if($wiki_title == "Top stories"){
  1217. //
  1218. // Parse news
  1219. //
  1220. $tds =
  1221. $this->fuckhtml
  1222. ->getElementsByTagName(
  1223. "td"
  1224. );
  1225. foreach($tds as $td){
  1226. $this->fuckhtml->load($td);
  1227. $a =
  1228. $this->fuckhtml
  1229. ->getElementsByTagName(
  1230. "a"
  1231. );
  1232. if(count($a) === 0){
  1233. continue;
  1234. }
  1235. $title =
  1236. $this->fuckhtml
  1237. ->getElementsByClassName(
  1238. $this->getstyle([
  1239. "color" => "#1967d2"
  1240. ]),
  1241. "span"
  1242. );
  1243. if(count($title) === 0){
  1244. continue;
  1245. }
  1246. $date = null;
  1247. $meta_div =
  1248. $this->fuckhtml
  1249. ->getElementsByClassName(
  1250. $this->getstyle([
  1251. "color" => "#70757a",
  1252. "font-size" => "13px",
  1253. "line-height" => "20px"
  1254. ]),
  1255. "span"
  1256. );
  1257. $meta_div =
  1258. explode(
  1259. "·",
  1260. $this->fuckhtml
  1261. ->getTextContent(
  1262. $meta_div[count($meta_div) - 1]
  1263. ),
  1264. 2
  1265. );
  1266. if(count($meta_div) === 2){
  1267. $date = strtotime($meta_div[count($meta_div) - 1]);
  1268. if($date === false){
  1269. $date = null;
  1270. }
  1271. }
  1272. $out["news"][] = [
  1273. "title" =>
  1274. $this->titledots(
  1275. $this->fuckhtml
  1276. ->getTextContent(
  1277. $title[0]
  1278. )
  1279. ),
  1280. "description" => null,
  1281. "date" => $date,
  1282. "thumb" => [
  1283. "url" => null,
  1284. "ratio" => null
  1285. ],
  1286. "url" =>
  1287. $this->unshiturl(
  1288. $a[0]["attributes"]["href"]
  1289. )
  1290. ];
  1291. }
  1292. continue;
  1293. }
  1294. //
  1295. // Parse wikipedia heads
  1296. //
  1297. $table_div =
  1298. $this->fuckhtml
  1299. ->getElementsByTagName(
  1300. "table"
  1301. );
  1302. if(count($table_div) === 0){
  1303. continue;
  1304. }
  1305. $this->fuckhtml->load($table_div[0]);
  1306. // remove table from box
  1307. $box["innerHTML"] =
  1308. str_replace(
  1309. $table_div[0]["outerHTML"],
  1310. "",
  1311. $box["innerHTML"]
  1312. );
  1313. // find wiki image
  1314. $thumb = null;
  1315. $img =
  1316. $this->fuckhtml
  1317. ->getElementsByTagName(
  1318. "img"
  1319. );
  1320. if(count($img) !== 0){
  1321. $thumb =
  1322. $this->fuckhtml
  1323. ->getTextContent(
  1324. $img[0]["attributes"]["src"]
  1325. );
  1326. }
  1327. $tds =
  1328. $this->fuckhtml
  1329. ->getElementsByTagName(
  1330. "td"
  1331. );
  1332. $description = [];
  1333. foreach($tds as $td){
  1334. // probe for subtitle
  1335. $this->fuckhtml->load($td);
  1336. $subtext =
  1337. $this->fuckhtml
  1338. ->getElementsByClassName(
  1339. $this->getstyle([
  1340. "color" => "#70757a",
  1341. "font-size" => "13px",
  1342. "line-height" => "20px"
  1343. ])
  1344. );
  1345. if(count($subtext) !== 0){
  1346. $description[] = [
  1347. "type" => "quote",
  1348. "value" =>
  1349. $this->fuckhtml
  1350. ->getTextContent(
  1351. $subtext[0]
  1352. )
  1353. ];
  1354. break;
  1355. }
  1356. }
  1357. $this->fuckhtml->load($box);
  1358. // probe for word definition
  1359. $lists =
  1360. $this->fuckhtml
  1361. ->getElementsByTagName(
  1362. "ol"
  1363. );
  1364. if(count($lists) !== 0){
  1365. $description = [];
  1366. foreach($lists as $list){
  1367. $box["innerHTML"] =
  1368. explode(
  1369. $list["outerHTML"],
  1370. $box["innerHTML"],
  1371. 2
  1372. );
  1373. if(
  1374. count($box["innerHTML"]) === 1 ||
  1375. trim($box["innerHTML"][0]) == ""
  1376. ){
  1377. break;
  1378. }
  1379. $description[] = [
  1380. "type" => "title",
  1381. "value" =>
  1382. $this->fuckhtml
  1383. ->getTextContent(
  1384. $box["innerHTML"][0]
  1385. )
  1386. ];
  1387. $this->fuckhtml->load($list);
  1388. $lis =
  1389. $this->fuckhtml
  1390. ->getElementsByTagName(
  1391. "li"
  1392. );
  1393. $increment = 1;
  1394. foreach($lis as $li){
  1395. $this->fuckhtml->load($li);
  1396. $list_items =
  1397. $this->fuckhtml
  1398. ->getElementsByClassName(
  1399. $this->getstyle([
  1400. "color" => "#202124",
  1401. "font-size" => "13px",
  1402. "line-height" => "20px"
  1403. ])
  1404. );
  1405. $first_item = true;
  1406. foreach($list_items as $it){
  1407. if($first_item){
  1408. $first_item = false;
  1409. $c = count($description);
  1410. if(
  1411. $c !== 0 &&
  1412. $description[$c - 1]["type"] == "text"
  1413. ){
  1414. $description[$c - 1]["value"] .=
  1415. "\n\n" .
  1416. $increment . ". " . $this->fuckhtml
  1417. ->getTextContent(
  1418. $it
  1419. );
  1420. }else{
  1421. $description[] = [
  1422. "type" => "text",
  1423. "value" =>
  1424. $increment . ". " . $this->fuckhtml
  1425. ->getTextContent(
  1426. $it
  1427. )
  1428. ];
  1429. }
  1430. }else{
  1431. $description[] = [
  1432. "type" => "quote",
  1433. "value" =>
  1434. $this->fuckhtml
  1435. ->getTextContent(
  1436. $it
  1437. )
  1438. ];
  1439. }
  1440. $increment++;
  1441. }
  1442. }
  1443. $box["innerHTML"] = $box["innerHTML"][1];
  1444. }
  1445. $out["answer"][] = [
  1446. "title" => $wiki_title,
  1447. "description" => $description,
  1448. "url" => null,
  1449. "thumb" => null,
  1450. "table" => [],
  1451. "sublink" => []
  1452. ];
  1453. continue;
  1454. }
  1455. // get separator between description and facts
  1456. $separator =
  1457. $this->fuckhtml
  1458. ->getElementsByClassName(
  1459. $this->getstyle([
  1460. "height" => "4px"
  1461. ]),
  1462. "div"
  1463. );
  1464. $box_html = [];
  1465. $table = [];
  1466. if(count($separator) !== 0){
  1467. $box_html =
  1468. explode(
  1469. $separator[0]["outerHTML"],
  1470. $box["innerHTML"],
  1471. 2
  1472. );
  1473. if(count($box_html) === 2){
  1474. $box["innerHTML"] = $box_html[0];
  1475. }
  1476. $this->fuckhtml->load($box_html[1]);
  1477. // get all facts
  1478. $facts =
  1479. $this->fuckhtml
  1480. ->getElementsByTagName(
  1481. "div"
  1482. );
  1483. foreach($facts as $fact){
  1484. if($fact["level"] !== 1){ continue; }
  1485. $fact =
  1486. explode(
  1487. ":",
  1488. $this->fuckhtml
  1489. ->getTextContent(
  1490. $fact
  1491. )
  1492. );
  1493. $table[trim(preg_replace('/\s+/', " ", $fact[0]))] =
  1494. trim(preg_replace('/\s+/', " ", $fact[1]));
  1495. }
  1496. $this->fuckhtml->load($box);
  1497. }
  1498. // remove wikipedia link
  1499. $wiki_link =
  1500. $this->fuckhtml
  1501. ->getElementsByClassName(
  1502. $this->getstyle([
  1503. "color" => "#1967d2"
  1504. ]),
  1505. "a"
  1506. );
  1507. $url = null;
  1508. if(count($wiki_link) !== 0){
  1509. foreach($wiki_link as $link){
  1510. if(
  1511. strtolower(
  1512. $this->fuckhtml
  1513. ->getTextContent(
  1514. $link
  1515. )
  1516. ) == "wikipedia"
  1517. ){
  1518. $box["innerHTML"] =
  1519. str_replace(
  1520. $link["outerHTML"],
  1521. "",
  1522. $box["innerHTML"]
  1523. );
  1524. $url =
  1525. $this->unshiturl(
  1526. $link["attributes"]["href"]
  1527. );
  1528. $this->fuckhtml->load($box);
  1529. break;
  1530. }
  1531. }
  1532. }
  1533. // remains of box should be description
  1534. $description[] = [
  1535. "type" => "text",
  1536. "value" =>
  1537. $this->titledots(
  1538. $this->fuckhtml
  1539. ->getTextContent(
  1540. $box
  1541. )
  1542. )
  1543. ];
  1544. $out["answer"][] = [
  1545. "title" => $wiki_title,
  1546. "description" => $description,
  1547. "url" => $url,
  1548. "thumb" => $thumb,
  1549. "table" => $table,
  1550. "sublink" => []
  1551. ];
  1552. }
  1553. }
  1554. return $out;
  1555. }
  1556. public function video($get){
  1557. if($get["npt"]){
  1558. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1559. $params = json_decode($params, true);
  1560. }else{
  1561. $search = $get["s"];
  1562. $country = $get["country"];
  1563. $nsfw = $get["nsfw"];
  1564. $older = $get["older"];
  1565. $newer = $get["newer"];
  1566. $duration = $get["duration"];
  1567. $quality = $get["quality"];
  1568. $captions = $get["captions"];
  1569. $proxy = $this->backend->get_ip();
  1570. $params = [
  1571. "q" => $search,
  1572. "udm" => "7",
  1573. "hl" => "en",
  1574. "num" => 20
  1575. ];
  1576. // country
  1577. if($country != "any"){
  1578. $params["gl"] = $country;
  1579. }
  1580. // nsfw
  1581. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1582. $tbs = [];
  1583. // get date
  1584. $older = $older === false ? null : date("m/d/Y", $older);
  1585. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1586. if(
  1587. $older !== null ||
  1588. $newer !== null
  1589. ){
  1590. $tbs["cdr"] = "1";
  1591. $tbs["cd_min"] = $newer;
  1592. $tbs["cd_max"] = $older;
  1593. }
  1594. // duration
  1595. if($duration != "any"){
  1596. $tbs[] = "dur:" . $duration;
  1597. }
  1598. // quality
  1599. if($quality != "any"){
  1600. $tbs[] = "hq:" . $quality;
  1601. }
  1602. // captions
  1603. if($captions != "any"){
  1604. $tbs[] = "cc:" . $captions;
  1605. }
  1606. // append tbs
  1607. if(count($tbs) !== 0){
  1608. $params["tbs"] =
  1609. implode(",", $tbs);
  1610. }
  1611. }
  1612. try{
  1613. $html =
  1614. $this->get(
  1615. $proxy,
  1616. "https://www.google.com/search",
  1617. $params
  1618. );
  1619. }catch(Exception $error){
  1620. throw new Exception("Failed to get HTML");
  1621. }
  1622. if(!isset($params["start"])){
  1623. $params["start"] = 0;
  1624. }
  1625. $params["start"] += 20;
  1626. $this->fuckhtml->load($html);
  1627. //
  1628. // Parse web video page
  1629. //
  1630. $this->detect_sorry();
  1631. // parse all <style> tags
  1632. $this->parsestyles();
  1633. // get javascript images
  1634. $this->scrape_dimg($html);
  1635. $this->scrape_imagearr($html);
  1636. $out = [
  1637. "status" => "ok",
  1638. "npt" =>
  1639. $this->backend->store(
  1640. json_encode($params),
  1641. "videos",
  1642. $proxy
  1643. ),
  1644. "video" => [],
  1645. "author" => [],
  1646. "livestream" => [],
  1647. "playlist" => [],
  1648. "reel" => []
  1649. ];
  1650. $search_div =
  1651. $this->fuckhtml
  1652. ->getElementById(
  1653. "center_col"
  1654. );
  1655. if($search_div === false){
  1656. throw new Exception("Failed to grep search div");
  1657. }
  1658. $this->fuckhtml->load($search_div);
  1659. $results =
  1660. $this->fuckhtml
  1661. ->getElementsByClassName(
  1662. $this->getstyle([
  1663. "margin" => "0px 0px 30px"
  1664. ]),
  1665. "div"
  1666. );
  1667. foreach($results as $result){
  1668. $this->fuckhtml->load($result);
  1669. $url =
  1670. $this->fuckhtml
  1671. ->getElementsByTagName(
  1672. "a"
  1673. );
  1674. if(count($url) === 0){
  1675. // no url, weird, continue
  1676. continue;
  1677. }
  1678. $title =
  1679. $this->fuckhtml
  1680. ->getElementsByTagName(
  1681. "h3"
  1682. );
  1683. if(count($title) === 0){
  1684. // no title, weird, continue
  1685. continue;
  1686. }
  1687. // get description
  1688. $description =
  1689. $this->fuckhtml
  1690. ->getElementsByClassName(
  1691. $this->getstyle([
  1692. "-webkit-box-orient" => "vertical",
  1693. "display" => "-webkit-box",
  1694. "-webkit-line-clamp" => "2",
  1695. "overflow" => "hidden",
  1696. "word-break" => "break-word"
  1697. ]),
  1698. "div"
  1699. );
  1700. if(count($description) === 0){
  1701. $description = null;
  1702. }else{
  1703. $description =
  1704. html_entity_decode(
  1705. $this->titledots(
  1706. $this->fuckhtml
  1707. ->getTextContent(
  1708. $description[0]
  1709. )
  1710. )
  1711. );
  1712. }
  1713. // get author + date posted
  1714. $metadiv =
  1715. $this->fuckhtml
  1716. ->getElementsByClassName(
  1717. $this->getstyle([
  1718. "margin-top" => "12px"
  1719. ]),
  1720. "div"
  1721. );
  1722. $author = null;
  1723. $date = null;
  1724. if(count($metadiv) !== 0){
  1725. $metadiv =
  1726. explode(
  1727. "·",
  1728. $this->fuckhtml
  1729. ->getTextContent(
  1730. $metadiv[0]
  1731. )
  1732. );
  1733. if(count($metadiv) === 3){
  1734. $author = trim($metadiv[1]);
  1735. $date = strtotime(trim($metadiv[2]));
  1736. }elseif(count($metadiv) === 2){
  1737. $author = trim($metadiv[0]);
  1738. $date = strtotime(trim($metadiv[1]));
  1739. }
  1740. }
  1741. $thumb = [
  1742. "url" => null,
  1743. "ratio" => null
  1744. ];
  1745. $image =
  1746. $this->fuckhtml
  1747. ->getElementsByTagName(
  1748. "img"
  1749. );
  1750. $duration = null;
  1751. if(
  1752. count($image) !== 0 &&
  1753. isset($image[0]["attributes"]["id"])
  1754. ){
  1755. $thumb = [
  1756. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1757. "ratio" => "16:9"
  1758. ];
  1759. // get duration
  1760. $duration =
  1761. $this->fuckhtml
  1762. ->getElementsByClassName(
  1763. $this->getstyle([
  1764. "background-color" => "rgba(0,0,0,0.6)",
  1765. "color" => "#fff",
  1766. "fill" => "#fff"
  1767. ])
  1768. );
  1769. if(count($duration) !== 0){
  1770. $duration =
  1771. $this->hms2int(
  1772. $this->fuckhtml
  1773. ->getTextContent(
  1774. $duration[0]
  1775. ));
  1776. }else{
  1777. $duration = null;
  1778. }
  1779. }
  1780. $out["video"][] = [
  1781. "title" =>
  1782. $this->titledots(
  1783. $this->fuckhtml
  1784. ->getTextContent(
  1785. $title[0]
  1786. )
  1787. ),
  1788. "description" => $description,
  1789. "author" => [
  1790. "name" => $author,
  1791. "url" => null,
  1792. "avatar" => null
  1793. ],
  1794. "date" => $date,
  1795. "duration" => $duration,
  1796. "views" => null,
  1797. "thumb" => $thumb,
  1798. "url" =>
  1799. $this->fuckhtml
  1800. ->getTextContent(
  1801. $url[0]["attributes"]["href"]
  1802. )
  1803. ];
  1804. }
  1805. return $out;
  1806. }
  1807. public function news($get){
  1808. if($get["npt"]){
  1809. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1810. /*parse_str(
  1811. parse_url($req, PHP_URL_QUERY),
  1812. $search
  1813. );*/
  1814. try{
  1815. $html =
  1816. $this->get(
  1817. $proxy,
  1818. "https://www.google.com" . $req,
  1819. []
  1820. );
  1821. }catch(Exception $error){
  1822. throw new Exception("Failed to get HTML");
  1823. }
  1824. }else{
  1825. $search = $get["s"];
  1826. $country = $get["country"];
  1827. $nsfw = $get["nsfw"];
  1828. $older = $get["older"];
  1829. $newer = $get["newer"];
  1830. $sort = $get["sort"];
  1831. $proxy = $this->backend->get_ip();
  1832. $params = [
  1833. "q" => $search,
  1834. "tbm" => "nws",
  1835. "hl" => "en",
  1836. "num" => "20"
  1837. ];
  1838. // country
  1839. if($country != "any"){
  1840. $params["gl"] = $country;
  1841. }
  1842. // nsfw
  1843. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1844. $tbs = [];
  1845. // get date
  1846. $older = $older === false ? null : date("m/d/Y", $older);
  1847. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1848. if(
  1849. $older !== null ||
  1850. $newer !== null
  1851. ){
  1852. $tbs["cdr"] = "1";
  1853. $tbs["cd_min"] = $newer;
  1854. $tbs["cd_max"] = $older;
  1855. }
  1856. // relevance
  1857. if($sort == "date"){
  1858. $tbs["sbd"] = "1";
  1859. }
  1860. // append tbs
  1861. if(count($tbs) !== 0){
  1862. $params["tbs"] = "";
  1863. foreach($tbs as $key => $value){
  1864. $params["tbs"] .= $key . ":" . $value . ",";
  1865. }
  1866. $params["tbs"] = rtrim($params["tbs"], ",");
  1867. }
  1868. //$html = file_get_contents("scraper/google-news.html");
  1869. $html =
  1870. $this->get(
  1871. $proxy,
  1872. "https://www.google.com/search",
  1873. $params
  1874. );
  1875. }
  1876. $out = [
  1877. "status" => "ok",
  1878. "npt" => null,
  1879. "news" => []
  1880. ];
  1881. $this->fuckhtml->load($html);
  1882. $this->detect_sorry();
  1883. // get images
  1884. $this->scrape_dimg($html);
  1885. // parse styles
  1886. $this->parsestyles();
  1887. $center_col =
  1888. $this->fuckhtml
  1889. ->getElementById(
  1890. "center_col",
  1891. "div"
  1892. );
  1893. if($center_col === null){
  1894. throw new Exception("Could not grep result div");
  1895. }
  1896. $this->fuckhtml->load($center_col);
  1897. // get next page
  1898. $npt =
  1899. $this->fuckhtml
  1900. ->getElementById(
  1901. "pnnext",
  1902. "a"
  1903. );
  1904. if($npt !== false){
  1905. $out["npt"] =
  1906. $this->backend->store(
  1907. $this->fuckhtml
  1908. ->getTextContent(
  1909. $npt["attributes"]
  1910. ["href"]
  1911. ),
  1912. "news",
  1913. $proxy
  1914. );
  1915. }
  1916. $as =
  1917. $this->fuckhtml
  1918. ->getElementsByAttributeName(
  1919. "jsname",
  1920. "a"
  1921. );
  1922. foreach($as as $a){
  1923. $this->fuckhtml->load($a);
  1924. // get title
  1925. $title =
  1926. $this->fuckhtml
  1927. ->getElementsByAttributeValue(
  1928. "role",
  1929. "heading",
  1930. "div"
  1931. );
  1932. if(count($title) === 0){
  1933. continue;
  1934. }
  1935. $title =
  1936. $this->titledots(
  1937. $this->fuckhtml
  1938. ->getTextContent(
  1939. $title[0]
  1940. )
  1941. );
  1942. // get thumbnail
  1943. $image =
  1944. $this->fuckhtml
  1945. ->getElementsByAttributeName(
  1946. "id",
  1947. "img"
  1948. );
  1949. // check for padded title node, if found, we're inside a carousel
  1950. $probe =
  1951. $this->fuckhtml
  1952. ->getElementsByClassName(
  1953. $this->getstyle(
  1954. [
  1955. "padding" => "16px 16px 40px 16px"
  1956. ]
  1957. ),
  1958. "div"
  1959. );
  1960. if(count($probe) !== 0){
  1961. $probe = true;
  1962. }else{
  1963. $probe = false;
  1964. }
  1965. if(
  1966. count($image) !== 0 &&
  1967. !isset($image[0]["attributes"]["width"])
  1968. ){
  1969. $thumb = [
  1970. "url" =>
  1971. $this->getdimg(
  1972. $image[0]["attributes"]["id"]
  1973. ),
  1974. "ratio" => $probe === true ? "16:9" : "1:1"
  1975. ];
  1976. }else{
  1977. $thumb = [
  1978. "url" => null,
  1979. "ratio" => null
  1980. ];
  1981. }
  1982. $description = null;
  1983. if($probe === false){
  1984. $desc_divs =
  1985. $this->fuckhtml
  1986. ->getElementsByAttributeName(
  1987. "style",
  1988. "div"
  1989. );
  1990. foreach($desc_divs as $desc){
  1991. if(
  1992. strpos(
  1993. $desc["attributes"]["style"],
  1994. "margin-top:"
  1995. ) !== false
  1996. ){
  1997. $description =
  1998. $this->titledots(
  1999. $this->fuckhtml
  2000. ->getTextContent(
  2001. $desc
  2002. )
  2003. );
  2004. break;
  2005. }
  2006. }
  2007. }
  2008. // get author
  2009. $author =
  2010. $this->fuckhtml
  2011. ->getElementsByClassName(
  2012. $this->getstyle(
  2013. [
  2014. "overflow" => "hidden",
  2015. "text-align" => "left",
  2016. "text-overflow" => "ellipsis",
  2017. "white-space" => "nowrap",
  2018. "margin-bottom" => "8px"
  2019. ]
  2020. ),
  2021. "div"
  2022. );
  2023. if(count($author) !== 0){
  2024. $author =
  2025. $this->fuckhtml
  2026. ->getTextContent(
  2027. $author[0]
  2028. );
  2029. }else{
  2030. $author = null;
  2031. }
  2032. // get date
  2033. $date = null;
  2034. $date_div =
  2035. $this->fuckhtml
  2036. ->getElementsByAttributeName(
  2037. "style",
  2038. "div"
  2039. );
  2040. foreach($date_div as $d){
  2041. $this->fuckhtml->load($d);
  2042. $span =
  2043. $this->fuckhtml
  2044. ->getElementsByTagName(
  2045. "span"
  2046. );
  2047. if(
  2048. strpos(
  2049. $d["attributes"]["style"],
  2050. "bottom:"
  2051. ) !== false
  2052. ){
  2053. $date =
  2054. strtotime(
  2055. $this->fuckhtml
  2056. ->getTextContent(
  2057. $span[count($span) - 1]
  2058. )
  2059. );
  2060. break;
  2061. }
  2062. }
  2063. $out["news"][] = [
  2064. "title" => $title,
  2065. "author" => $author,
  2066. "description" => $description,
  2067. "date" => $date,
  2068. "thumb" => $thumb,
  2069. "url" =>
  2070. $this->unshiturl(
  2071. $a["attributes"]
  2072. ["href"]
  2073. )
  2074. ];
  2075. }
  2076. return $out;
  2077. }
  2078. public function image($get){
  2079. // generate parameters
  2080. if($get["npt"]){
  2081. [$params, $proxy] =
  2082. $this->backend->get(
  2083. $get["npt"],
  2084. "images"
  2085. );
  2086. $params = json_decode($params, true);
  2087. }else{
  2088. $search = $get["s"];
  2089. if(strlen($search) === 0){
  2090. throw new Exception("Search term is empty!");
  2091. }
  2092. $proxy = $this->backend->get_ip();
  2093. $country = $get["country"];
  2094. $nsfw = $get["nsfw"];
  2095. $time = $get["time"];
  2096. $size = $get["size"];
  2097. $ratio = $get["ratio"];
  2098. $color = $get["color"];
  2099. $type = $get["type"];
  2100. $format = $get["format"];
  2101. $rights = $get["rights"];
  2102. $params = [
  2103. "q" => $search,
  2104. "udm" => "2" // get images
  2105. ];
  2106. // country (image search uses cr instead of gl)
  2107. if($country != "any"){
  2108. $params["cr"] = "country" . strtoupper($country);
  2109. }
  2110. // nsfw
  2111. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2112. // generate tbs
  2113. $tbs = [];
  2114. // time
  2115. if($time != "any"){
  2116. $tbs["qdr"] = $time;
  2117. }
  2118. // size
  2119. if($size != "any"){
  2120. $params["imgsz"] = $size;
  2121. }
  2122. // ratio
  2123. if($ratio != "any"){
  2124. $params["imgar"] = $ratio;
  2125. }
  2126. // color
  2127. if($color != "any"){
  2128. if(
  2129. $color == "color" ||
  2130. $color == "trans"
  2131. ){
  2132. $params["imgc"] = $color;
  2133. }elseif($color == "bnw"){
  2134. $params["imgc"] = "gray";
  2135. }else{
  2136. $tbs["ic"] = "specific";
  2137. $tbs["isc"] = $color;
  2138. }
  2139. }
  2140. // type
  2141. if($type != "any"){
  2142. $tbs["itp"] = $type;
  2143. }
  2144. // format
  2145. if($format != "any"){
  2146. $params["as_filetype"] = $format;
  2147. }
  2148. // rights (tbs)
  2149. if($rights != "any"){
  2150. $tbs["sur"] = $rights;
  2151. }
  2152. // append tbs
  2153. if(count($tbs) !== 0){
  2154. $params["tbs"] = "";
  2155. foreach($tbs as $key => $value){
  2156. $params["tbs"] .= $key . ":" . $value . ",";
  2157. }
  2158. $params["tbs"] = rtrim($params["tbs"], ",");
  2159. }
  2160. }
  2161. /*
  2162. $handle = fopen("scraper/google-img.html", "r");
  2163. $html = fread($handle, filesize("scraper/google-img.html"));
  2164. fclose($handle);*/
  2165. try{
  2166. $html =
  2167. $this->get(
  2168. $proxy,
  2169. "https://www.google.com/search",
  2170. $params
  2171. );
  2172. }catch(Exception $error){
  2173. throw new Exception("Failed to get search page");
  2174. }
  2175. $this->fuckhtml->load($html);
  2176. $this->detect_sorry();
  2177. // get javascript images
  2178. $this->scrape_imagearr($html);
  2179. $out = [
  2180. "status" => "ok",
  2181. "npt" => null,
  2182. "image" => []
  2183. ];
  2184. $images =
  2185. $this->fuckhtml
  2186. ->getElementsByClassName(
  2187. "ivg-i",
  2188. "div"
  2189. );
  2190. foreach($images as $div){
  2191. $this->fuckhtml->load($div);
  2192. $image =
  2193. $this->fuckhtml
  2194. ->getElementsByTagName("img")[0];
  2195. $out["image"][] = [
  2196. "title" =>
  2197. $this->titledots(
  2198. $this->fuckhtml
  2199. ->getTextContent(
  2200. $image["attributes"]["alt"]
  2201. )
  2202. ),
  2203. "source" =>
  2204. $this->image_arr[
  2205. $div["attributes"]["data-docid"]
  2206. ],
  2207. "url" =>
  2208. $this->fuckhtml
  2209. ->getTextContent(
  2210. $div["attributes"]["data-lpage"]
  2211. )
  2212. ];
  2213. }
  2214. // as usual, no way to check if there is a next page reliably
  2215. if(count($out["image"]) > 50){
  2216. if(!isset($params["start"])){
  2217. $params["start"] = 10;
  2218. }else{
  2219. $params["start"] += 10;
  2220. }
  2221. $out["npt"] =
  2222. $this->backend
  2223. ->store(
  2224. json_encode($params),
  2225. "image",
  2226. $proxy
  2227. );
  2228. }
  2229. return $out;
  2230. }
  2231. private function unshiturl($url, $return_size = false){
  2232. // decode
  2233. $url =
  2234. $this->fuckhtml
  2235. ->getTextContent(
  2236. $url
  2237. );
  2238. $url_parts = parse_url($url);
  2239. if(isset($url_parts["query"])){
  2240. parse_str($url_parts["query"], $query);
  2241. }else{
  2242. $query = [];
  2243. }
  2244. if(
  2245. !isset(
  2246. $url_parts["host"]
  2247. ) ||
  2248. stripos($url_parts["host"], "google.") !== false
  2249. ){
  2250. // no host, we have a tracking url
  2251. if(isset($query["imgurl"])){
  2252. $url = $query["imgurl"];
  2253. }
  2254. elseif(isset($query["q"])){
  2255. $url = $query["q"];
  2256. }
  2257. }
  2258. // rewrite URLs to remove extra tracking parameters
  2259. $domain = parse_url($url, PHP_URL_HOST);
  2260. if(
  2261. preg_match(
  2262. '/wikipedia.org$/',
  2263. $domain
  2264. )
  2265. ){
  2266. // rewrite wikipedia mobile URLs to desktop
  2267. $url =
  2268. $this->replacedomain(
  2269. $url,
  2270. preg_replace(
  2271. '/([a-z0-9]+)(\.m\.)/',
  2272. '$1.',
  2273. $domain
  2274. )
  2275. );
  2276. }
  2277. elseif(
  2278. preg_match(
  2279. '/imdb\.com$|youtube\.[^.]+$/',
  2280. $domain
  2281. )
  2282. ){
  2283. // rewrite imdb and youtube mobile URLs too
  2284. $url =
  2285. $this->replacedomain(
  2286. $url,
  2287. preg_replace(
  2288. '/^m\./',
  2289. "",
  2290. $domain
  2291. )
  2292. );
  2293. }
  2294. elseif(
  2295. preg_match(
  2296. '/play\.google\.[^.]+$/',
  2297. $domain
  2298. )
  2299. ){
  2300. // remove referrers from play.google.com
  2301. $u_query = parse_url($url, PHP_URL_QUERY);
  2302. if($u_query !== null){
  2303. parse_str($u_query, $u_query);
  2304. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2305. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2306. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2307. $query = http_build_query($query);
  2308. $url =
  2309. str_replace(
  2310. $u_query,
  2311. $u_query,
  2312. $url
  2313. );
  2314. }
  2315. }
  2316. elseif(
  2317. preg_match(
  2318. '/twitter\.com$/',
  2319. $domain
  2320. )
  2321. ){
  2322. // remove more referrers from twitter.com
  2323. $u_query = parse_url($url, PHP_URL_QUERY);
  2324. if($u_query !== null){
  2325. parse_str($u_query, $u_query);
  2326. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2327. $u_query = http_build_query($u_query);
  2328. $url =
  2329. str_replace(
  2330. $oldquery,
  2331. $u_query,
  2332. $url
  2333. );
  2334. }
  2335. }
  2336. elseif(
  2337. preg_match(
  2338. '/maps\.google\.[^.]+/',
  2339. $domain
  2340. )
  2341. ){
  2342. if(stripos($url, "maps?") !== false){
  2343. $u_query = parse_url($url, PHP_URL_QUERY);
  2344. if($u_query !== null){
  2345. parse_str($u_query, $u_query);
  2346. if(isset($u_query["daddr"])){
  2347. $url =
  2348. "https://maps.google.com/maps?daddr=" .
  2349. urlencode($u_query["daddr"]);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. if($return_size){
  2355. return [
  2356. "url" => $url,
  2357. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2358. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2359. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2360. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2361. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2362. ];
  2363. }
  2364. return $url;
  2365. }
  2366. private function replacedomain($url, $domain){
  2367. return
  2368. preg_replace(
  2369. '/(https?:\/\/)([^\/]+)/',
  2370. '$1' . $domain,
  2371. $url
  2372. );
  2373. }
  2374. private function titledots($title){
  2375. return trim($title, " .\t\n\r\0\x0B…");
  2376. }
  2377. private function hms2int($time){
  2378. $parts = explode(":", $time, 3);
  2379. $time = 0;
  2380. if(count($parts) === 3){
  2381. // hours
  2382. $time = $time + ((int)$parts[0] * 3600);
  2383. array_shift($parts);
  2384. }
  2385. if(count($parts) === 2){
  2386. // minutes
  2387. $time = $time + ((int)$parts[0] * 60);
  2388. array_shift($parts);
  2389. }
  2390. // seconds
  2391. $time = $time + (int)$parts[0];
  2392. return $time;
  2393. }
  2394. private function detect_sorry(){
  2395. $recaptcha =
  2396. $this->fuckhtml
  2397. ->getElementById(
  2398. "recaptcha",
  2399. "div"
  2400. );
  2401. if($recaptcha !== false){
  2402. throw new Exception("Google returned a captcha");
  2403. }
  2404. }
  2405. }