google.php 58 KB

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