google.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  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-9]+,"([^"]+)",\["([^"]+)\",([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. $original =
  598. $this->fuckhtml
  599. ->parseJsString(
  600. $image_arr[5][$i]
  601. );
  602. if(
  603. preg_match(
  604. '/^x-raw-image/',
  605. $original
  606. )
  607. ){
  608. // only add thumbnail, google doesnt have OG resolution
  609. $this->image_arr[$image_arr[1][$i]] = [
  610. [
  611. "url" =>
  612. $this->unshit_thumb(
  613. $this->fuckhtml
  614. ->parseJsString(
  615. $image_arr[2][$i]
  616. )
  617. ),
  618. "width" => (int)$image_arr[7][$i], // pass the OG image width & height
  619. "height" => (int)$image_arr[6][$i]
  620. ]
  621. ];
  622. continue;
  623. }
  624. $this->image_arr[$image_arr[1][$i]] =
  625. [
  626. [
  627. "url" => $original,
  628. "width" => (int)$image_arr[7][$i],
  629. "height" => (int)$image_arr[6][$i]
  630. ],
  631. [
  632. "url" =>
  633. $this->unshit_thumb(
  634. $this->fuckhtml
  635. ->parseJsString(
  636. $image_arr[2][$i]
  637. )
  638. ),
  639. "width" => (int)$image_arr[4][$i],
  640. "height" => (int)$image_arr[3][$i]
  641. ]
  642. ];
  643. }
  644. }
  645. }
  646. private function getdimg($dimg){
  647. return isset($this->dimg[$dimg]) ? $this->dimg[$dimg] : null;
  648. }
  649. private function unshit_thumb($url){
  650. // https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQINE2vbnNLHXqoZr3RVsaEJFyOsj1_BiBnJch-e1nyz3oia7Aj5xVj
  651. // https://i.ytimg.com/vi/PZVIyA5ER3Y/mqdefault.jpg?sqp=-oaymwEFCJQBEFM&rs=AMzJL3nXeaCpdIar-ltNwl82Y82cIJfphA
  652. $parts = parse_url($url);
  653. if(
  654. isset($parts["host"]) &&
  655. preg_match(
  656. '/tbn.*\.gstatic\.com/',
  657. $parts["host"]
  658. )
  659. ){
  660. parse_str($parts["query"], $params);
  661. if(isset($params["q"])){
  662. return "https://" . $parts["host"] . "/images?q=" . $params["q"];
  663. }
  664. }
  665. return $url;
  666. }
  667. private function parsestyles(){
  668. $styles = [];
  669. $style_div =
  670. $this->fuckhtml
  671. ->getElementsByTagName(
  672. "style"
  673. );
  674. $raw_styles = "";
  675. foreach($style_div as $style){
  676. $raw_styles .= $style["innerHTML"];
  677. }
  678. // filter out media/keyframe queries
  679. $raw_styles =
  680. preg_replace(
  681. '/@\s*(?!font-face)[^{]+\s*{[\S\s]+?}\s*}/',
  682. "",
  683. $raw_styles
  684. );
  685. // get styles
  686. preg_match_all(
  687. '/(.+?){([\S\s]*?)}/',
  688. $raw_styles,
  689. $matches
  690. );
  691. for($i=0; $i<count($matches[1]); $i++){
  692. // get style values
  693. preg_match_all(
  694. '/([^:;]+):([^;]*?(?:\([^)]+\)[^;]*?)?)(?:;|$)/',
  695. $matches[2][$i],
  696. $values_regex
  697. );
  698. $values = [];
  699. for($k=0; $k<count($values_regex[1]); $k++){
  700. $values[trim($values_regex[1][$k])] =
  701. strtolower(trim($values_regex[2][$k]));
  702. }
  703. $names = explode(",", $matches[1][$i]);
  704. // h1,h2,h3 will each get their own array index
  705. foreach($names as $name){
  706. $name = trim($name, "}\t\n\r\0\x0B");
  707. foreach($values as $key => $value){
  708. $styles[$name][$key] = $value;
  709. }
  710. }
  711. }
  712. foreach($styles as $key => $values){
  713. $styles[$key]["_c"] = count($values);
  714. }
  715. $this->styles = $styles;
  716. // get CSS colors
  717. $this->css_colors = [];
  718. if(isset($this->styles[":root"])){
  719. foreach($this->styles[":root"] as $key => $value){
  720. $this->css_colors[$value] = strtolower($key);
  721. }
  722. }
  723. }
  724. private function getstyle($styles){
  725. $styles["_c"] = count($styles);
  726. foreach($this->styles as $style_key => $style_values){
  727. if(count(array_intersect_assoc($style_values, $styles)) === $styles["_c"] + 1){
  728. $style_key =
  729. explode(" ", $style_key);
  730. $style_key = $style_key[count($style_key) - 1];
  731. return
  732. ltrim(
  733. str_replace(
  734. [".", "#"],
  735. " ",
  736. $style_key
  737. )
  738. );
  739. }
  740. }
  741. return false;
  742. }
  743. private function getcolorvar($color){
  744. if(isset($this->css_colors[$color])){
  745. return $this->css_colors[$color];
  746. }
  747. return null;
  748. }
  749. public function web($get){
  750. if($get["npt"]){
  751. [$get, $proxy] = $this->backend->get($get["npt"], "web");
  752. try{
  753. $html =
  754. $this->get(
  755. $proxy,
  756. "https://www.google.com" . $get,
  757. [],
  758. true
  759. );
  760. }catch(Exception $error){
  761. throw new Exception("Failed to get HTML");
  762. }
  763. }else{
  764. $search = $get["s"];
  765. $country = $get["country"];
  766. $nsfw = $get["nsfw"];
  767. $lang = $get["lang"];
  768. $older = $get["older"];
  769. $newer = $get["newer"];
  770. $spellcheck = $get["spellcheck"];
  771. $proxy = $this->backend->get_ip();
  772. $offset = 0;
  773. $params = [
  774. "q" => $search,
  775. "hl" => "en",
  776. "num" => 20
  777. ];
  778. // country
  779. if($country != "any"){
  780. $params["gl"] = $country;
  781. }
  782. // nsfw
  783. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  784. // language
  785. if($lang != "any"){
  786. $params["lr"] = "lang_" . $lang;
  787. }
  788. // generate tbs
  789. $tbs = [];
  790. // get date
  791. $older = $older === false ? null : date("m/d/Y", $older);
  792. $newer = $newer === false ? null : date("m/d/Y", $newer);
  793. if(
  794. $older !== null ||
  795. $newer !== null
  796. ){
  797. $tbs["cdr"] = "1";
  798. $tbs["cd_min"] = $newer;
  799. $tbs["cd_max"] = $older;
  800. }
  801. // spellcheck filter
  802. if($spellcheck == "no"){
  803. $params["nfpr"] = "1";
  804. }
  805. if(count($tbs) !== 0){
  806. $params["tbs"] = "";
  807. foreach($tbs as $key => $value){
  808. $params["tbs"] .= $key . ":" . $value . ",";
  809. }
  810. $params["tbs"] = rtrim($params["tbs"], ",");
  811. }
  812. try{
  813. $html =
  814. $this->get(
  815. $proxy,
  816. "https://www.google.com/search",
  817. $params,
  818. true
  819. );
  820. }catch(Exception $error){
  821. throw new Exception("Failed to get HTML");
  822. }
  823. //$html = file_get_contents("scraper/google.html");
  824. }
  825. $out = [
  826. "status" => "ok",
  827. "spelling" => [
  828. "type" => "no_correction",
  829. "using" => null,
  830. "correction" => null
  831. ],
  832. "npt" => null,
  833. "answer" => [],
  834. "web" => [],
  835. "image" => [],
  836. "video" => [],
  837. "news" => [],
  838. "related" => []
  839. ];
  840. $this->fuckhtml->load($html);
  841. $this->parsestyles();
  842. $boxes =
  843. $this->fuckhtml
  844. ->getElementsByClassName(
  845. $this->getstyle([
  846. "border" => "thin solid #dadce0",
  847. "padding" => "12px 16px 12px 16px",
  848. "margin-bottom" => "10px",
  849. "font-family" => "sans-serif"
  850. ]),
  851. "div"
  852. );
  853. $skip_next = false;
  854. // get next page token
  855. $npt =
  856. $this->fuckhtml
  857. ->getElementsByClassName(
  858. $this->getstyle([
  859. "border" => "thin solid #dadce0",
  860. "color" => "#70757a",
  861. "font-size" => "14px",
  862. "text-align" => "center",
  863. "table-layout" => "fixed",
  864. "width" => "100%"
  865. ]),
  866. "table"
  867. );
  868. if(count($npt) !== 0){
  869. $this->fuckhtml->load($npt[0]);
  870. $as =
  871. $this->fuckhtml
  872. ->getElementsByTagName(
  873. "a"
  874. );
  875. foreach($as as $a){
  876. $text =
  877. $this->fuckhtml
  878. ->getTextContent(
  879. $a
  880. );
  881. if(
  882. $text == "Next&nbsp;>" ||
  883. $text == ">"
  884. ){
  885. $out["npt"] =
  886. $this->backend->store(
  887. $this->fuckhtml
  888. ->getTextContent(
  889. $a["attributes"]["href"]
  890. ),
  891. "web",
  892. $proxy
  893. );
  894. }
  895. }
  896. $this->fuckhtml->load($html);
  897. }
  898. $first_box = true;
  899. foreach($boxes as $box){
  900. $this->fuckhtml->load($box);
  901. if($first_box){
  902. //
  903. // Probe for word correction
  904. //
  905. $first_box = false;
  906. $txt =
  907. $this->fuckhtml
  908. ->getTextContent($box);
  909. if(
  910. preg_match(
  911. '/^Showing results for /',
  912. $txt
  913. )
  914. ){
  915. $as =
  916. $this->fuckhtml
  917. ->getElementsByTagName(
  918. "a"
  919. );
  920. if(count($as) === 2){
  921. $out["spelling"] = [
  922. "type" => "including",
  923. "using" =>
  924. $this->fuckhtml
  925. ->getTextContent(
  926. $as[0]
  927. ),
  928. "correction" =>
  929. $this->fuckhtml
  930. ->getTextContent(
  931. $as[1]
  932. )
  933. ];
  934. }
  935. continue;
  936. }
  937. }
  938. // probe for custom container
  939. $container_title =
  940. $this->fuckhtml
  941. ->getElementsByClassName(
  942. $this->getstyle([
  943. "font-weight" => "bold"
  944. ])
  945. );
  946. if(count($container_title) !== 0){
  947. $container_title =
  948. strtolower(
  949. $this->fuckhtml
  950. ->getTextContent(
  951. $container_title[0]
  952. )
  953. );
  954. if($container_title == "images"){
  955. //
  956. // Parse image carousel
  957. //
  958. $images =
  959. $this->fuckhtml
  960. ->getElementsByClassName(
  961. $this->getstyle([
  962. "display" => "inline-block",
  963. "padding" => "2px",
  964. "padding-bottom" => "4px"
  965. ]),
  966. "a"
  967. );
  968. foreach($images as $image){
  969. $this->fuckhtml->load($image);
  970. $image_data =
  971. $this->unshiturl(
  972. $image["attributes"]["href"],
  973. true
  974. );
  975. $img =
  976. $this->fuckhtml
  977. ->getElementsByTagName(
  978. "img"
  979. )[0];
  980. $out["image"][] = [
  981. "title" =>
  982. $this->titledots(
  983. $this->fuckhtml
  984. ->getTextContent(
  985. $img["attributes"]["alt"]
  986. )
  987. ),
  988. "source" => [
  989. [
  990. "url" => $image_data["url"],
  991. "width" => $image_data["image_width"],
  992. "height" => $image_data["image_height"]
  993. ],
  994. [
  995. "url" =>
  996. $this->fuckhtml
  997. ->getTextContent(
  998. $img["attributes"]["src"]
  999. ),
  1000. "width" => $image_data["thumb_width"],
  1001. "height" => $image_data["thumb_height"]
  1002. ]
  1003. ],
  1004. "url" => $image_data["ref"]
  1005. ];
  1006. }
  1007. continue;
  1008. }
  1009. if(
  1010. $container_title == "related searches" ||
  1011. $container_title == "people also search for"
  1012. ){
  1013. $as =
  1014. $this->fuckhtml
  1015. ->getElementsByClassName(
  1016. $this->getstyle([
  1017. "color" => "#202124",
  1018. "font-size" => "13px",
  1019. "line-height" => "20px"
  1020. ]),
  1021. "span"
  1022. );
  1023. foreach($as as $a){
  1024. $out["related"][] =
  1025. $this->fuckhtml
  1026. ->getTextContent(
  1027. $a
  1028. );
  1029. }
  1030. continue;
  1031. }
  1032. }
  1033. // probe for website link
  1034. $link =
  1035. $this->fuckhtml
  1036. ->getElementsByClassName(
  1037. $this->getstyle([
  1038. "color" => "#1967d2",
  1039. "font-size" => "18px",
  1040. "line-height" => "24px"
  1041. ]),
  1042. "a"
  1043. );
  1044. if(count($link) !== 0){
  1045. //
  1046. // Parse search result
  1047. //
  1048. $this->fuckhtml->load($link[0]);
  1049. $title =
  1050. $this->fuckhtml
  1051. ->getElementsByClassName(
  1052. $this->getstyle([
  1053. "color" => "#1967d2",
  1054. "font-size" => "18px",
  1055. "line-height" => "24px"
  1056. ]),
  1057. "span"
  1058. );
  1059. if(count($title) === 0){
  1060. continue;
  1061. }
  1062. $this->fuckhtml->load($box);
  1063. $sublinks = [];
  1064. $table = [];
  1065. $categories =
  1066. $this->fuckhtml
  1067. ->getElementsByClassName(
  1068. $this->getstyle([
  1069. "color" => "#202124",
  1070. "font-size" => "13px",
  1071. "line-height" => "20px"
  1072. ]),
  1073. "span"
  1074. );
  1075. $i = 0;
  1076. foreach($categories as $category){
  1077. $this->fuckhtml->load($category);
  1078. // probe for sublinks
  1079. $subs =
  1080. $this->fuckhtml
  1081. ->getElementsByClassName(
  1082. $this->getstyle([
  1083. "color" => "#1967d2"
  1084. ]),
  1085. "a"
  1086. );
  1087. if(count($subs) !== 0){
  1088. foreach($subs as $sub){
  1089. $url =
  1090. $this->unshiturl(
  1091. $this->fuckhtml
  1092. ->getTextContent(
  1093. $sub["attributes"]["href"]
  1094. )
  1095. );
  1096. if(
  1097. preg_match(
  1098. '/^https?:\/\//',
  1099. $url
  1100. )
  1101. ){
  1102. $sublinks[] = [
  1103. "title" =>
  1104. $this->titledots(
  1105. $this->fuckhtml
  1106. ->getTextContent(
  1107. $sub
  1108. )
  1109. ),
  1110. "description" => null,
  1111. "url" =>
  1112. $this->unshiturl(
  1113. $this->fuckhtml
  1114. ->getTextContent(
  1115. $sub["attributes"]["href"]
  1116. )
  1117. ),
  1118. "date" => null
  1119. ];
  1120. }
  1121. }
  1122. unset($categories[$i]);
  1123. }
  1124. $i++;
  1125. }
  1126. // get description & date
  1127. $date = null;
  1128. $categories = array_values($categories);
  1129. //print_r($categories);
  1130. $c = count($categories) - 1;
  1131. $description =
  1132. $this->fuckhtml
  1133. ->getTextContent(
  1134. $categories[$c]
  1135. );
  1136. // remove last category since we're done with it
  1137. unset($categories[$c]);
  1138. // probe for date
  1139. $description_tmp = explode("·", $description, 2);
  1140. $date_tmp = strtotime(trim($description_tmp[0]));
  1141. if(
  1142. count($description_tmp) === 2 &&
  1143. strlen($description_tmp[0]) <= 20 &&
  1144. $date_tmp !== false
  1145. ){
  1146. $description =
  1147. ltrim(
  1148. $this->titledots(
  1149. $description_tmp[1]
  1150. )
  1151. );
  1152. $date = $date_tmp;
  1153. }else{
  1154. $description =
  1155. $this->titledots(
  1156. $description
  1157. );
  1158. }
  1159. // remaining categories should all be greytext
  1160. if(count($categories) !== 0){
  1161. $texts =
  1162. explode(
  1163. "·",
  1164. preg_replace(
  1165. '/\s+/',
  1166. " ",
  1167. $this->fuckhtml
  1168. ->getTextContent(
  1169. $categories[0]
  1170. )
  1171. )
  1172. );
  1173. foreach($texts as $text){
  1174. $text = trim($text);
  1175. if(
  1176. preg_match(
  1177. '/^Rating ([0-9.]+)(?: \(([0-9,]+)\))?/',
  1178. $text,
  1179. $rating
  1180. )
  1181. ){
  1182. $table["Rating"] = $rating[1];
  1183. if(isset($rating[2])){
  1184. $table["Rating"] .= " (" . $rating[2] . " votes)";
  1185. }
  1186. continue;
  1187. }
  1188. if(stripos($text, "stock") !== false){
  1189. $table["Stock"] = $text;
  1190. continue;
  1191. }
  1192. }
  1193. }
  1194. $out["web"][] = [
  1195. "title" =>
  1196. $this->titledots(
  1197. $this->fuckhtml
  1198. ->getTextContent(
  1199. $title[0]
  1200. )
  1201. ),
  1202. "description" => $description,
  1203. "url" =>
  1204. $this->unshiturl(
  1205. $link[0]["attributes"]["href"]
  1206. ),
  1207. "date" => $date,
  1208. "type" => "web",
  1209. "thumb" => [
  1210. "url" => null,
  1211. "ratio" => null
  1212. ],
  1213. "sublink" => $sublinks,
  1214. "table" => $table
  1215. ];
  1216. continue;
  1217. }
  1218. // parse wikipedia heads
  1219. $wiki_title =
  1220. $this->fuckhtml
  1221. ->getElementsByClassName(
  1222. $this->getstyle([
  1223. "color" => "#202124",
  1224. "font-size" => "18px",
  1225. "line-height" => "24px"
  1226. ]),
  1227. "span"
  1228. );
  1229. if(count($wiki_title) !== 0){
  1230. $wiki_title =
  1231. $this->fuckhtml
  1232. ->getTextContent(
  1233. $wiki_title[0]
  1234. );
  1235. if($wiki_title == "See results about"){
  1236. // ignore
  1237. continue;
  1238. }
  1239. if($wiki_title == "Top stories"){
  1240. //
  1241. // Parse news
  1242. //
  1243. $tds =
  1244. $this->fuckhtml
  1245. ->getElementsByTagName(
  1246. "td"
  1247. );
  1248. foreach($tds as $td){
  1249. $this->fuckhtml->load($td);
  1250. $a =
  1251. $this->fuckhtml
  1252. ->getElementsByTagName(
  1253. "a"
  1254. );
  1255. if(count($a) === 0){
  1256. continue;
  1257. }
  1258. $title =
  1259. $this->fuckhtml
  1260. ->getElementsByClassName(
  1261. $this->getstyle([
  1262. "color" => "#1967d2"
  1263. ]),
  1264. "span"
  1265. );
  1266. if(count($title) === 0){
  1267. continue;
  1268. }
  1269. $date = null;
  1270. $meta_div =
  1271. $this->fuckhtml
  1272. ->getElementsByClassName(
  1273. $this->getstyle([
  1274. "color" => "#70757a",
  1275. "font-size" => "13px",
  1276. "line-height" => "20px"
  1277. ]),
  1278. "span"
  1279. );
  1280. $meta_div =
  1281. explode(
  1282. "·",
  1283. $this->fuckhtml
  1284. ->getTextContent(
  1285. $meta_div[count($meta_div) - 1]
  1286. ),
  1287. 2
  1288. );
  1289. if(count($meta_div) === 2){
  1290. $date = strtotime($meta_div[count($meta_div) - 1]);
  1291. if($date === false){
  1292. $date = null;
  1293. }
  1294. }
  1295. $out["news"][] = [
  1296. "title" =>
  1297. $this->titledots(
  1298. $this->fuckhtml
  1299. ->getTextContent(
  1300. $title[0]
  1301. )
  1302. ),
  1303. "description" => null,
  1304. "date" => $date,
  1305. "thumb" => [
  1306. "url" => null,
  1307. "ratio" => null
  1308. ],
  1309. "url" =>
  1310. $this->unshiturl(
  1311. $a[0]["attributes"]["href"]
  1312. )
  1313. ];
  1314. }
  1315. continue;
  1316. }
  1317. //
  1318. // Parse wikipedia heads
  1319. //
  1320. $table_div =
  1321. $this->fuckhtml
  1322. ->getElementsByTagName(
  1323. "table"
  1324. );
  1325. if(count($table_div) === 0){
  1326. continue;
  1327. }
  1328. $this->fuckhtml->load($table_div[0]);
  1329. // remove table from box
  1330. $box["innerHTML"] =
  1331. str_replace(
  1332. $table_div[0]["outerHTML"],
  1333. "",
  1334. $box["innerHTML"]
  1335. );
  1336. // find wiki image
  1337. $thumb = null;
  1338. $img =
  1339. $this->fuckhtml
  1340. ->getElementsByTagName(
  1341. "img"
  1342. );
  1343. if(count($img) !== 0){
  1344. $thumb =
  1345. $this->fuckhtml
  1346. ->getTextContent(
  1347. $img[0]["attributes"]["src"]
  1348. );
  1349. }
  1350. $tds =
  1351. $this->fuckhtml
  1352. ->getElementsByTagName(
  1353. "td"
  1354. );
  1355. $description = [];
  1356. foreach($tds as $td){
  1357. // probe for subtitle
  1358. $this->fuckhtml->load($td);
  1359. $subtext =
  1360. $this->fuckhtml
  1361. ->getElementsByClassName(
  1362. $this->getstyle([
  1363. "color" => "#70757a",
  1364. "font-size" => "13px",
  1365. "line-height" => "20px"
  1366. ])
  1367. );
  1368. if(count($subtext) !== 0){
  1369. $description[] = [
  1370. "type" => "quote",
  1371. "value" =>
  1372. $this->fuckhtml
  1373. ->getTextContent(
  1374. $subtext[0]
  1375. )
  1376. ];
  1377. break;
  1378. }
  1379. }
  1380. $this->fuckhtml->load($box);
  1381. // probe for word definition
  1382. $lists =
  1383. $this->fuckhtml
  1384. ->getElementsByTagName(
  1385. "ol"
  1386. );
  1387. if(count($lists) !== 0){
  1388. $description = [];
  1389. foreach($lists as $list){
  1390. $box["innerHTML"] =
  1391. explode(
  1392. $list["outerHTML"],
  1393. $box["innerHTML"],
  1394. 2
  1395. );
  1396. if(
  1397. count($box["innerHTML"]) === 1 ||
  1398. trim($box["innerHTML"][0]) == ""
  1399. ){
  1400. break;
  1401. }
  1402. $description[] = [
  1403. "type" => "title",
  1404. "value" =>
  1405. $this->fuckhtml
  1406. ->getTextContent(
  1407. $box["innerHTML"][0]
  1408. )
  1409. ];
  1410. $this->fuckhtml->load($list);
  1411. $lis =
  1412. $this->fuckhtml
  1413. ->getElementsByTagName(
  1414. "li"
  1415. );
  1416. $increment = 1;
  1417. foreach($lis as $li){
  1418. $this->fuckhtml->load($li);
  1419. $list_items =
  1420. $this->fuckhtml
  1421. ->getElementsByClassName(
  1422. $this->getstyle([
  1423. "color" => "#202124",
  1424. "font-size" => "13px",
  1425. "line-height" => "20px"
  1426. ])
  1427. );
  1428. $first_item = true;
  1429. foreach($list_items as $it){
  1430. if($first_item){
  1431. $first_item = false;
  1432. $c = count($description);
  1433. if(
  1434. $c !== 0 &&
  1435. $description[$c - 1]["type"] == "text"
  1436. ){
  1437. $description[$c - 1]["value"] .=
  1438. "\n\n" .
  1439. $increment . ". " . $this->fuckhtml
  1440. ->getTextContent(
  1441. $it
  1442. );
  1443. }else{
  1444. $description[] = [
  1445. "type" => "text",
  1446. "value" =>
  1447. $increment . ". " . $this->fuckhtml
  1448. ->getTextContent(
  1449. $it
  1450. )
  1451. ];
  1452. }
  1453. }else{
  1454. $description[] = [
  1455. "type" => "quote",
  1456. "value" =>
  1457. $this->fuckhtml
  1458. ->getTextContent(
  1459. $it
  1460. )
  1461. ];
  1462. }
  1463. $increment++;
  1464. }
  1465. }
  1466. $box["innerHTML"] = $box["innerHTML"][1];
  1467. }
  1468. $out["answer"][] = [
  1469. "title" => $wiki_title,
  1470. "description" => $description,
  1471. "url" => null,
  1472. "thumb" => null,
  1473. "table" => [],
  1474. "sublink" => []
  1475. ];
  1476. continue;
  1477. }
  1478. // get separator between description and facts
  1479. $separator =
  1480. $this->fuckhtml
  1481. ->getElementsByClassName(
  1482. $this->getstyle([
  1483. "height" => "4px"
  1484. ]),
  1485. "div"
  1486. );
  1487. $box_html = [];
  1488. $table = [];
  1489. if(count($separator) !== 0){
  1490. $box_html =
  1491. explode(
  1492. $separator[0]["outerHTML"],
  1493. $box["innerHTML"],
  1494. 2
  1495. );
  1496. if(count($box_html) === 2){
  1497. $box["innerHTML"] = $box_html[0];
  1498. }
  1499. $this->fuckhtml->load($box_html[1]);
  1500. // get all facts
  1501. $facts =
  1502. $this->fuckhtml
  1503. ->getElementsByTagName(
  1504. "div"
  1505. );
  1506. foreach($facts as $fact){
  1507. if($fact["level"] !== 1){ continue; }
  1508. $fact =
  1509. explode(
  1510. ":",
  1511. $this->fuckhtml
  1512. ->getTextContent(
  1513. $fact
  1514. )
  1515. );
  1516. $table[trim(preg_replace('/\s+/', " ", $fact[0]))] =
  1517. trim(preg_replace('/\s+/', " ", $fact[1]));
  1518. }
  1519. $this->fuckhtml->load($box);
  1520. }
  1521. // remove wikipedia link
  1522. $wiki_link =
  1523. $this->fuckhtml
  1524. ->getElementsByClassName(
  1525. $this->getstyle([
  1526. "color" => "#1967d2"
  1527. ]),
  1528. "a"
  1529. );
  1530. $url = null;
  1531. if(count($wiki_link) !== 0){
  1532. foreach($wiki_link as $link){
  1533. if(
  1534. strtolower(
  1535. $this->fuckhtml
  1536. ->getTextContent(
  1537. $link
  1538. )
  1539. ) == "wikipedia"
  1540. ){
  1541. $box["innerHTML"] =
  1542. str_replace(
  1543. $link["outerHTML"],
  1544. "",
  1545. $box["innerHTML"]
  1546. );
  1547. $url =
  1548. $this->unshiturl(
  1549. $link["attributes"]["href"]
  1550. );
  1551. $this->fuckhtml->load($box);
  1552. break;
  1553. }
  1554. }
  1555. }
  1556. // remains of box should be description
  1557. $description[] = [
  1558. "type" => "text",
  1559. "value" =>
  1560. $this->titledots(
  1561. $this->fuckhtml
  1562. ->getTextContent(
  1563. $box
  1564. )
  1565. )
  1566. ];
  1567. $out["answer"][] = [
  1568. "title" => $wiki_title,
  1569. "description" => $description,
  1570. "url" => $url,
  1571. "thumb" => $thumb,
  1572. "table" => $table,
  1573. "sublink" => []
  1574. ];
  1575. }
  1576. }
  1577. return $out;
  1578. }
  1579. public function video($get){
  1580. if($get["npt"]){
  1581. [$params, $proxy] = $this->backend->get($get["npt"], "video");
  1582. $params = json_decode($params, true);
  1583. }else{
  1584. $search = $get["s"];
  1585. $country = $get["country"];
  1586. $nsfw = $get["nsfw"];
  1587. $older = $get["older"];
  1588. $newer = $get["newer"];
  1589. $duration = $get["duration"];
  1590. $quality = $get["quality"];
  1591. $captions = $get["captions"];
  1592. $proxy = $this->backend->get_ip();
  1593. $params = [
  1594. "q" => $search,
  1595. "udm" => "7",
  1596. "hl" => "en",
  1597. "num" => 20
  1598. ];
  1599. // country
  1600. if($country != "any"){
  1601. $params["gl"] = $country;
  1602. }
  1603. // nsfw
  1604. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1605. $tbs = [];
  1606. // get date
  1607. $older = $older === false ? null : date("m/d/Y", $older);
  1608. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1609. if(
  1610. $older !== null ||
  1611. $newer !== null
  1612. ){
  1613. $tbs["cdr"] = "1";
  1614. $tbs["cd_min"] = $newer;
  1615. $tbs["cd_max"] = $older;
  1616. }
  1617. // duration
  1618. if($duration != "any"){
  1619. $tbs[] = "dur:" . $duration;
  1620. }
  1621. // quality
  1622. if($quality != "any"){
  1623. $tbs[] = "hq:" . $quality;
  1624. }
  1625. // captions
  1626. if($captions != "any"){
  1627. $tbs[] = "cc:" . $captions;
  1628. }
  1629. // append tbs
  1630. if(count($tbs) !== 0){
  1631. $params["tbs"] =
  1632. implode(",", $tbs);
  1633. }
  1634. }
  1635. try{
  1636. $html =
  1637. $this->get(
  1638. $proxy,
  1639. "https://www.google.com/search",
  1640. $params
  1641. );
  1642. }catch(Exception $error){
  1643. throw new Exception("Failed to get HTML");
  1644. }
  1645. if(!isset($params["start"])){
  1646. $params["start"] = 0;
  1647. }
  1648. $params["start"] += 20;
  1649. $this->fuckhtml->load($html);
  1650. //
  1651. // Parse web video page
  1652. //
  1653. $this->detect_sorry();
  1654. // parse all <style> tags
  1655. $this->parsestyles();
  1656. // get javascript images
  1657. $this->scrape_dimg($html);
  1658. $this->scrape_imagearr($html);
  1659. $out = [
  1660. "status" => "ok",
  1661. "npt" =>
  1662. $this->backend->store(
  1663. json_encode($params),
  1664. "videos",
  1665. $proxy
  1666. ),
  1667. "video" => [],
  1668. "author" => [],
  1669. "livestream" => [],
  1670. "playlist" => [],
  1671. "reel" => []
  1672. ];
  1673. $search_div =
  1674. $this->fuckhtml
  1675. ->getElementById(
  1676. "center_col"
  1677. );
  1678. if($search_div === false){
  1679. throw new Exception("Failed to grep search div");
  1680. }
  1681. $this->fuckhtml->load($search_div);
  1682. $results =
  1683. $this->fuckhtml
  1684. ->getElementsByClassName(
  1685. $this->getstyle([
  1686. "margin" => "0px 0px 30px"
  1687. ]),
  1688. "div"
  1689. );
  1690. foreach($results as $result){
  1691. $this->fuckhtml->load($result);
  1692. $url =
  1693. $this->fuckhtml
  1694. ->getElementsByTagName(
  1695. "a"
  1696. );
  1697. if(count($url) === 0){
  1698. // no url, weird, continue
  1699. continue;
  1700. }
  1701. $title =
  1702. $this->fuckhtml
  1703. ->getElementsByTagName(
  1704. "h3"
  1705. );
  1706. if(count($title) === 0){
  1707. // no title, weird, continue
  1708. continue;
  1709. }
  1710. // get description
  1711. $description =
  1712. $this->fuckhtml
  1713. ->getElementsByClassName(
  1714. $this->getstyle([
  1715. "-webkit-box-orient" => "vertical",
  1716. "display" => "-webkit-box",
  1717. "-webkit-line-clamp" => "2",
  1718. "overflow" => "hidden",
  1719. "word-break" => "break-word"
  1720. ]),
  1721. "div"
  1722. );
  1723. if(count($description) === 0){
  1724. $description = null;
  1725. }else{
  1726. $description =
  1727. html_entity_decode(
  1728. $this->titledots(
  1729. $this->fuckhtml
  1730. ->getTextContent(
  1731. $description[0]
  1732. )
  1733. )
  1734. );
  1735. }
  1736. // get author + date posted
  1737. $metadiv =
  1738. $this->fuckhtml
  1739. ->getElementsByClassName(
  1740. $this->getstyle([
  1741. "margin-top" => "12px"
  1742. ]),
  1743. "div"
  1744. );
  1745. $author = null;
  1746. $date = null;
  1747. if(count($metadiv) !== 0){
  1748. $metadiv =
  1749. explode(
  1750. "·",
  1751. $this->fuckhtml
  1752. ->getTextContent(
  1753. $metadiv[0]
  1754. )
  1755. );
  1756. if(count($metadiv) === 3){
  1757. $author = trim($metadiv[1]);
  1758. $date = strtotime(trim($metadiv[2]));
  1759. }elseif(count($metadiv) === 2){
  1760. $author = trim($metadiv[0]);
  1761. $date = strtotime(trim($metadiv[1]));
  1762. }
  1763. }
  1764. $thumb = [
  1765. "url" => null,
  1766. "ratio" => null
  1767. ];
  1768. $image =
  1769. $this->fuckhtml
  1770. ->getElementsByTagName(
  1771. "img"
  1772. );
  1773. $duration = null;
  1774. if(
  1775. count($image) !== 0 &&
  1776. isset($image[0]["attributes"]["id"])
  1777. ){
  1778. $thumb = [
  1779. "url" => $this->getdimg($image[0]["attributes"]["id"]),
  1780. "ratio" => "16:9"
  1781. ];
  1782. // get duration
  1783. $duration =
  1784. $this->fuckhtml
  1785. ->getElementsByClassName(
  1786. $this->getstyle([
  1787. "background-color" => "rgba(0,0,0,0.6)",
  1788. "color" => "#fff",
  1789. "fill" => "#fff"
  1790. ])
  1791. );
  1792. if(count($duration) !== 0){
  1793. $duration =
  1794. $this->hms2int(
  1795. $this->fuckhtml
  1796. ->getTextContent(
  1797. $duration[0]
  1798. ));
  1799. }else{
  1800. $duration = null;
  1801. }
  1802. }
  1803. $out["video"][] = [
  1804. "title" =>
  1805. $this->titledots(
  1806. $this->fuckhtml
  1807. ->getTextContent(
  1808. $title[0]
  1809. )
  1810. ),
  1811. "description" => $description,
  1812. "author" => [
  1813. "name" => $author,
  1814. "url" => null,
  1815. "avatar" => null
  1816. ],
  1817. "date" => $date,
  1818. "duration" => $duration,
  1819. "views" => null,
  1820. "thumb" => $thumb,
  1821. "url" =>
  1822. $this->fuckhtml
  1823. ->getTextContent(
  1824. $url[0]["attributes"]["href"]
  1825. )
  1826. ];
  1827. }
  1828. return $out;
  1829. }
  1830. public function news($get){
  1831. if($get["npt"]){
  1832. [$req, $proxy] = $this->backend->get($get["npt"], "news");
  1833. /*parse_str(
  1834. parse_url($req, PHP_URL_QUERY),
  1835. $search
  1836. );*/
  1837. try{
  1838. $html =
  1839. $this->get(
  1840. $proxy,
  1841. "https://www.google.com" . $req,
  1842. []
  1843. );
  1844. }catch(Exception $error){
  1845. throw new Exception("Failed to get HTML");
  1846. }
  1847. }else{
  1848. $search = $get["s"];
  1849. $country = $get["country"];
  1850. $nsfw = $get["nsfw"];
  1851. $older = $get["older"];
  1852. $newer = $get["newer"];
  1853. $sort = $get["sort"];
  1854. $proxy = $this->backend->get_ip();
  1855. $params = [
  1856. "q" => $search,
  1857. "tbm" => "nws",
  1858. "hl" => "en",
  1859. "num" => "20"
  1860. ];
  1861. // country
  1862. if($country != "any"){
  1863. $params["gl"] = $country;
  1864. }
  1865. // nsfw
  1866. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  1867. $tbs = [];
  1868. // get date
  1869. $older = $older === false ? null : date("m/d/Y", $older);
  1870. $newer = $newer === false ? null : date("m/d/Y", $newer);
  1871. if(
  1872. $older !== null ||
  1873. $newer !== null
  1874. ){
  1875. $tbs["cdr"] = "1";
  1876. $tbs["cd_min"] = $newer;
  1877. $tbs["cd_max"] = $older;
  1878. }
  1879. // relevance
  1880. if($sort == "date"){
  1881. $tbs["sbd"] = "1";
  1882. }
  1883. // append tbs
  1884. if(count($tbs) !== 0){
  1885. $params["tbs"] = "";
  1886. foreach($tbs as $key => $value){
  1887. $params["tbs"] .= $key . ":" . $value . ",";
  1888. }
  1889. $params["tbs"] = rtrim($params["tbs"], ",");
  1890. }
  1891. //$html = file_get_contents("scraper/google-news.html");
  1892. $html =
  1893. $this->get(
  1894. $proxy,
  1895. "https://www.google.com/search",
  1896. $params
  1897. );
  1898. }
  1899. $out = [
  1900. "status" => "ok",
  1901. "npt" => null,
  1902. "news" => []
  1903. ];
  1904. $this->fuckhtml->load($html);
  1905. $this->detect_sorry();
  1906. // get images
  1907. $this->scrape_dimg($html);
  1908. // parse styles
  1909. $this->parsestyles();
  1910. $center_col =
  1911. $this->fuckhtml
  1912. ->getElementById(
  1913. "center_col",
  1914. "div"
  1915. );
  1916. if($center_col === null){
  1917. throw new Exception("Could not grep result div");
  1918. }
  1919. $this->fuckhtml->load($center_col);
  1920. // get next page
  1921. $npt =
  1922. $this->fuckhtml
  1923. ->getElementById(
  1924. "pnnext",
  1925. "a"
  1926. );
  1927. if($npt !== false){
  1928. $out["npt"] =
  1929. $this->backend->store(
  1930. $this->fuckhtml
  1931. ->getTextContent(
  1932. $npt["attributes"]
  1933. ["href"]
  1934. ),
  1935. "news",
  1936. $proxy
  1937. );
  1938. }
  1939. $as =
  1940. $this->fuckhtml
  1941. ->getElementsByAttributeName(
  1942. "jsname",
  1943. "a"
  1944. );
  1945. foreach($as as $a){
  1946. $this->fuckhtml->load($a);
  1947. // get title
  1948. $title =
  1949. $this->fuckhtml
  1950. ->getElementsByAttributeValue(
  1951. "role",
  1952. "heading",
  1953. "div"
  1954. );
  1955. if(count($title) === 0){
  1956. continue;
  1957. }
  1958. $title =
  1959. $this->titledots(
  1960. $this->fuckhtml
  1961. ->getTextContent(
  1962. $title[0]
  1963. )
  1964. );
  1965. // get thumbnail
  1966. $image =
  1967. $this->fuckhtml
  1968. ->getElementsByAttributeName(
  1969. "id",
  1970. "img"
  1971. );
  1972. // check for padded title node, if found, we're inside a carousel
  1973. $probe =
  1974. $this->fuckhtml
  1975. ->getElementsByClassName(
  1976. $this->getstyle(
  1977. [
  1978. "padding" => "16px 16px 40px 16px"
  1979. ]
  1980. ),
  1981. "div"
  1982. );
  1983. if(count($probe) !== 0){
  1984. $probe = true;
  1985. }else{
  1986. $probe = false;
  1987. }
  1988. if(
  1989. count($image) !== 0 &&
  1990. !isset($image[0]["attributes"]["width"])
  1991. ){
  1992. $thumb = [
  1993. "url" =>
  1994. $this->getdimg(
  1995. $image[0]["attributes"]["id"]
  1996. ),
  1997. "ratio" => $probe === true ? "16:9" : "1:1"
  1998. ];
  1999. }else{
  2000. $thumb = [
  2001. "url" => null,
  2002. "ratio" => null
  2003. ];
  2004. }
  2005. $description = null;
  2006. if($probe === false){
  2007. $desc_divs =
  2008. $this->fuckhtml
  2009. ->getElementsByAttributeName(
  2010. "style",
  2011. "div"
  2012. );
  2013. foreach($desc_divs as $desc){
  2014. if(
  2015. strpos(
  2016. $desc["attributes"]["style"],
  2017. "margin-top:"
  2018. ) !== false
  2019. ){
  2020. $description =
  2021. $this->titledots(
  2022. $this->fuckhtml
  2023. ->getTextContent(
  2024. $desc
  2025. )
  2026. );
  2027. break;
  2028. }
  2029. }
  2030. }
  2031. // get author
  2032. $author =
  2033. $this->fuckhtml
  2034. ->getElementsByClassName(
  2035. $this->getstyle(
  2036. [
  2037. "overflow" => "hidden",
  2038. "text-align" => "left",
  2039. "text-overflow" => "ellipsis",
  2040. "white-space" => "nowrap",
  2041. "margin-bottom" => "8px"
  2042. ]
  2043. ),
  2044. "div"
  2045. );
  2046. if(count($author) !== 0){
  2047. $author =
  2048. $this->fuckhtml
  2049. ->getTextContent(
  2050. $author[0]
  2051. );
  2052. }else{
  2053. $author = null;
  2054. }
  2055. // get date
  2056. $date = null;
  2057. $date_div =
  2058. $this->fuckhtml
  2059. ->getElementsByAttributeName(
  2060. "style",
  2061. "div"
  2062. );
  2063. foreach($date_div as $d){
  2064. $this->fuckhtml->load($d);
  2065. $span =
  2066. $this->fuckhtml
  2067. ->getElementsByTagName(
  2068. "span"
  2069. );
  2070. if(
  2071. strpos(
  2072. $d["attributes"]["style"],
  2073. "bottom:"
  2074. ) !== false
  2075. ){
  2076. $date =
  2077. strtotime(
  2078. $this->fuckhtml
  2079. ->getTextContent(
  2080. $span[count($span) - 1]
  2081. )
  2082. );
  2083. break;
  2084. }
  2085. }
  2086. $out["news"][] = [
  2087. "title" => $title,
  2088. "author" => $author,
  2089. "description" => $description,
  2090. "date" => $date,
  2091. "thumb" => $thumb,
  2092. "url" =>
  2093. $this->unshiturl(
  2094. $a["attributes"]
  2095. ["href"]
  2096. )
  2097. ];
  2098. }
  2099. return $out;
  2100. }
  2101. public function image($get){
  2102. // generate parameters
  2103. if($get["npt"]){
  2104. [$params, $proxy] =
  2105. $this->backend->get(
  2106. $get["npt"],
  2107. "images"
  2108. );
  2109. $params = json_decode($params, true);
  2110. }else{
  2111. $search = $get["s"];
  2112. if(strlen($search) === 0){
  2113. throw new Exception("Search term is empty!");
  2114. }
  2115. $proxy = $this->backend->get_ip();
  2116. $country = $get["country"];
  2117. $nsfw = $get["nsfw"];
  2118. $time = $get["time"];
  2119. $size = $get["size"];
  2120. $ratio = $get["ratio"];
  2121. $color = $get["color"];
  2122. $type = $get["type"];
  2123. $format = $get["format"];
  2124. $rights = $get["rights"];
  2125. $params = [
  2126. "q" => $search,
  2127. "udm" => "2" // get images
  2128. ];
  2129. // country (image search uses cr instead of gl)
  2130. if($country != "any"){
  2131. $params["cr"] = "country" . strtoupper($country);
  2132. }
  2133. // nsfw
  2134. $params["safe"] = $nsfw == "yes" ? "off" : "active";
  2135. // generate tbs
  2136. $tbs = [];
  2137. // time
  2138. if($time != "any"){
  2139. $tbs["qdr"] = $time;
  2140. }
  2141. // size
  2142. if($size != "any"){
  2143. $params["imgsz"] = $size;
  2144. }
  2145. // ratio
  2146. if($ratio != "any"){
  2147. $params["imgar"] = $ratio;
  2148. }
  2149. // color
  2150. if($color != "any"){
  2151. if(
  2152. $color == "color" ||
  2153. $color == "trans"
  2154. ){
  2155. $params["imgc"] = $color;
  2156. }elseif($color == "bnw"){
  2157. $params["imgc"] = "gray";
  2158. }else{
  2159. $tbs["ic"] = "specific";
  2160. $tbs["isc"] = $color;
  2161. }
  2162. }
  2163. // type
  2164. if($type != "any"){
  2165. $tbs["itp"] = $type;
  2166. }
  2167. // format
  2168. if($format != "any"){
  2169. $params["as_filetype"] = $format;
  2170. }
  2171. // rights (tbs)
  2172. if($rights != "any"){
  2173. $tbs["sur"] = $rights;
  2174. }
  2175. // append tbs
  2176. if(count($tbs) !== 0){
  2177. $params["tbs"] = "";
  2178. foreach($tbs as $key => $value){
  2179. $params["tbs"] .= $key . ":" . $value . ",";
  2180. }
  2181. $params["tbs"] = rtrim($params["tbs"], ",");
  2182. }
  2183. }
  2184. /*
  2185. $handle = fopen("scraper/page.html", "r");
  2186. $html = fread($handle, filesize("scraper/page.html"));
  2187. fclose($handle);*/
  2188. try{
  2189. $html =
  2190. $this->get(
  2191. $proxy,
  2192. "https://www.google.com/search",
  2193. $params
  2194. );
  2195. }catch(Exception $error){
  2196. throw new Exception("Failed to get search page");
  2197. }
  2198. $this->fuckhtml->load($html);
  2199. $this->detect_sorry();
  2200. // get javascript images
  2201. $this->scrape_imagearr($html);
  2202. $out = [
  2203. "status" => "ok",
  2204. "npt" => null,
  2205. "image" => []
  2206. ];
  2207. $images =
  2208. $this->fuckhtml
  2209. ->getElementsByClassName(
  2210. "ivg-i",
  2211. "div"
  2212. );
  2213. foreach($images as $div){
  2214. $this->fuckhtml->load($div);
  2215. $image =
  2216. $this->fuckhtml
  2217. ->getElementsByTagName("img")[0];
  2218. // make sure we dont attempt to show an image we dont have data for
  2219. if(
  2220. isset($div["attributes"]["data-docid"]) &&
  2221. isset($this->image_arr[$div["attributes"]["data-docid"]])
  2222. ){
  2223. $source =
  2224. $this->image_arr[
  2225. $div["attributes"]["data-docid"]
  2226. ];
  2227. }else{
  2228. continue;
  2229. }
  2230. $out["image"][] = [
  2231. "title" =>
  2232. $this->titledots(
  2233. $this->fuckhtml
  2234. ->getTextContent(
  2235. $image["attributes"]["alt"]
  2236. )
  2237. ),
  2238. "source" => $source,
  2239. "url" =>
  2240. $this->fuckhtml
  2241. ->getTextContent(
  2242. $div["attributes"]["data-lpage"]
  2243. )
  2244. ];
  2245. }
  2246. // as usual, no way to check if there is a next page reliably
  2247. if(count($out["image"]) > 50){
  2248. if(!isset($params["start"])){
  2249. $params["start"] = 10;
  2250. }else{
  2251. $params["start"] += 10;
  2252. }
  2253. $out["npt"] =
  2254. $this->backend
  2255. ->store(
  2256. json_encode($params),
  2257. "image",
  2258. $proxy
  2259. );
  2260. }
  2261. return $out;
  2262. }
  2263. private function unshiturl($url, $return_size = false){
  2264. // decode
  2265. $url =
  2266. $this->fuckhtml
  2267. ->getTextContent(
  2268. $url
  2269. );
  2270. $url_parts = parse_url($url);
  2271. if(isset($url_parts["query"])){
  2272. parse_str($url_parts["query"], $query);
  2273. }else{
  2274. $query = [];
  2275. }
  2276. if(
  2277. !isset(
  2278. $url_parts["host"]
  2279. ) ||
  2280. stripos($url_parts["host"], "google.") !== false
  2281. ){
  2282. // no host, we have a tracking url
  2283. if(isset($query["imgurl"])){
  2284. $url = $query["imgurl"];
  2285. }
  2286. elseif(isset($query["q"])){
  2287. $url = $query["q"];
  2288. }
  2289. }
  2290. // rewrite URLs to remove extra tracking parameters
  2291. $domain = parse_url($url, PHP_URL_HOST);
  2292. if(
  2293. preg_match(
  2294. '/wikipedia.org$/',
  2295. $domain
  2296. )
  2297. ){
  2298. // rewrite wikipedia mobile URLs to desktop
  2299. $url =
  2300. $this->replacedomain(
  2301. $url,
  2302. preg_replace(
  2303. '/([a-z0-9]+)(\.m\.)/',
  2304. '$1.',
  2305. $domain
  2306. )
  2307. );
  2308. }
  2309. elseif(
  2310. preg_match(
  2311. '/imdb\.com$|youtube\.[^.]+$/',
  2312. $domain
  2313. )
  2314. ){
  2315. // rewrite imdb and youtube mobile URLs too
  2316. $url =
  2317. $this->replacedomain(
  2318. $url,
  2319. preg_replace(
  2320. '/^m\./',
  2321. "",
  2322. $domain
  2323. )
  2324. );
  2325. }
  2326. elseif(
  2327. preg_match(
  2328. '/play\.google\.[^.]+$/',
  2329. $domain
  2330. )
  2331. ){
  2332. // remove referrers from play.google.com
  2333. $u_query = parse_url($url, PHP_URL_QUERY);
  2334. if($u_query !== null){
  2335. parse_str($u_query, $u_query);
  2336. if(isset($u_query["referrer"])){ unset($u_query["referrer"]); }
  2337. if(isset($u_query["hl"])){ unset($u_query["hl"]); }
  2338. if(isset($u_query["gl"])){ unset($u_query["gl"]); }
  2339. $query = http_build_query($query);
  2340. $url =
  2341. str_replace(
  2342. $u_query,
  2343. $u_query,
  2344. $url
  2345. );
  2346. }
  2347. }
  2348. elseif(
  2349. preg_match(
  2350. '/twitter\.com$/',
  2351. $domain
  2352. )
  2353. ){
  2354. // remove more referrers from twitter.com
  2355. $u_query = parse_url($url, PHP_URL_QUERY);
  2356. if($u_query !== null){
  2357. parse_str($u_query, $u_query);
  2358. if(isset($u_query["ref_src"])){ unset($u_query["ref_src"]); }
  2359. $u_query = http_build_query($u_query);
  2360. $url =
  2361. str_replace(
  2362. $oldquery,
  2363. $u_query,
  2364. $url
  2365. );
  2366. }
  2367. }
  2368. elseif(
  2369. preg_match(
  2370. '/maps\.google\.[^.]+/',
  2371. $domain
  2372. )
  2373. ){
  2374. if(stripos($url, "maps?") !== false){
  2375. $u_query = parse_url($url, PHP_URL_QUERY);
  2376. if($u_query !== null){
  2377. parse_str($u_query, $u_query);
  2378. if(isset($u_query["daddr"])){
  2379. $url =
  2380. "https://maps.google.com/maps?daddr=" .
  2381. urlencode($u_query["daddr"]);
  2382. }
  2383. }
  2384. }
  2385. }
  2386. if($return_size){
  2387. return [
  2388. "url" => $url,
  2389. "ref" => isset($query["imgrefurl"]) ? $query["imgrefurl"] : null,
  2390. "thumb_width" => isset($query["tbnw"]) ? (int)$query["tbnw"] : null,
  2391. "thumb_height" => isset($query["tbnh"]) ? (int)$query["tbnh"] : null,
  2392. "image_width" => isset($query["w"]) ? (int)$query["w"] : null,
  2393. "image_height" => isset($query["h"]) ? (int)$query["h"] : null
  2394. ];
  2395. }
  2396. return $url;
  2397. }
  2398. private function replacedomain($url, $domain){
  2399. return
  2400. preg_replace(
  2401. '/(https?:\/\/)([^\/]+)/',
  2402. '$1' . $domain,
  2403. $url
  2404. );
  2405. }
  2406. private function titledots($title){
  2407. return trim($title, " .\t\n\r\0\x0B…");
  2408. }
  2409. private function hms2int($time){
  2410. $parts = explode(":", $time, 3);
  2411. $time = 0;
  2412. if(count($parts) === 3){
  2413. // hours
  2414. $time = $time + ((int)$parts[0] * 3600);
  2415. array_shift($parts);
  2416. }
  2417. if(count($parts) === 2){
  2418. // minutes
  2419. $time = $time + ((int)$parts[0] * 60);
  2420. array_shift($parts);
  2421. }
  2422. // seconds
  2423. $time = $time + (int)$parts[0];
  2424. return $time;
  2425. }
  2426. private function detect_sorry(){
  2427. $recaptcha =
  2428. $this->fuckhtml
  2429. ->getElementById(
  2430. "recaptcha",
  2431. "div"
  2432. );
  2433. if($recaptcha !== false){
  2434. throw new Exception("Google returned a captcha");
  2435. }
  2436. }
  2437. }