mojeek.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. <?php
  2. class mojeek{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("mojeek");
  8. }
  9. public function getfilters($page){
  10. switch($page){
  11. case "web":
  12. return [
  13. "lang" => [ // lb
  14. "display" => "Language",
  15. "option" => [
  16. "any" => "Any language",
  17. "af" => "Afrikaans",
  18. "sq" => "Albanian",
  19. "an" => "Aragonese",
  20. "ay" => "Aymara",
  21. "bi" => "Bislama",
  22. "br" => "Breton",
  23. "ca" => "Catalan",
  24. "kw" => "Cornish",
  25. "co" => "Corsican",
  26. "hr" => "Croatian",
  27. "da" => "Danish",
  28. "nl" => "Dutch",
  29. "dz" => "Dzongkha",
  30. "en" => "English",
  31. "fj" => "Fijian",
  32. "fi" => "Finnish",
  33. "fr" => "French",
  34. "gd" => "Gaelic",
  35. "gl" => "Galician",
  36. "de" => "German",
  37. "ht" => "Haitian",
  38. "io" => "Ido",
  39. "id" => "Indonesian",
  40. "ia" => "Interlingua",
  41. "ie" => "Interlingue",
  42. "ga" => "Irish",
  43. "it" => "Italian",
  44. "rw" => "Kinyarwanda",
  45. "la" => "Latin",
  46. "li" => "Limburgish",
  47. "lb" => "Luxembourgish",
  48. "no" => "Norwegian",
  49. "nb" => "Norwegian Bokmål",
  50. "nn" => "Norwegian Nynorsk",
  51. "oc" => "Occitan (post 1500)",
  52. "pl" => "Polish",
  53. "pt" => "Portuguese",
  54. "rm" => "Romansh",
  55. "rn" => "Rundi",
  56. "sg" => "Sango",
  57. "so" => "Somali",
  58. "es" => "Spanish",
  59. "sw" => "Swahili",
  60. "ss" => "Swati",
  61. "sv" => "Swedish",
  62. "ty" => "Tahitian",
  63. "to" => "Tonga (Tonga Islands)",
  64. "ts" => "Tsonga",
  65. "vo" => "Volapük",
  66. "wa" => "Walloon",
  67. "cy" => "Welsh",
  68. "xh" => "Xhosa",
  69. "zu" => "Zulu"
  70. ]
  71. ],
  72. "country" => [ // rb
  73. "display" => "Country",
  74. "option" => [
  75. "any" => "No location bias",
  76. "af" => "Afghanistan",
  77. "ax" => "Åland Islands",
  78. "al" => "Albania",
  79. "dz" => "Algeria",
  80. "as" => "American Samoa",
  81. "ad" => "Andorra",
  82. "ao" => "Angola",
  83. "ai" => "Anguilla",
  84. "aq" => "Antarctica",
  85. "ag" => "Antigua and Barbuda",
  86. "ar" => "Argentina",
  87. "am" => "Armenia",
  88. "aw" => "Aruba",
  89. "au" => "Australia",
  90. "at" => "Austria",
  91. "az" => "Azerbaijan",
  92. "bs" => "Bahamas",
  93. "bh" => "Bahrain",
  94. "bd" => "Bangladesh",
  95. "bb" => "Barbados",
  96. "by" => "Belarus",
  97. "be" => "Belgium",
  98. "bz" => "Belize",
  99. "bj" => "Benin",
  100. "bm" => "Bermuda",
  101. "bt" => "Bhutan",
  102. "bo" => "Bolivia (Plurinational State of)",
  103. "bq" => "Bonaire, Sint Eustatius and Saba",
  104. "ba" => "Bosnia and Herzegovina",
  105. "bw" => "Botswana",
  106. "bv" => "Bouvet Island",
  107. "br" => "Brazil",
  108. "io" => "British Indian Ocean Territory",
  109. "bn" => "Brunei Darussalam",
  110. "bg" => "Bulgaria",
  111. "bf" => "Burkina Faso",
  112. "bi" => "Burundi",
  113. "cv" => "Cabo Verde",
  114. "kh" => "Cambodia",
  115. "cm" => "Cameroon",
  116. "ca" => "Canada",
  117. "ky" => "Cayman Islands",
  118. "cf" => "Central African Republic",
  119. "td" => "Chad",
  120. "cl" => "Chile",
  121. "cn" => "China",
  122. "cx" => "Christmas Island",
  123. "cc" => "Cocos (Keeling) Islands",
  124. "co" => "Colombia",
  125. "km" => "Comoros",
  126. "cg" => "Congo",
  127. "cd" => "Congo (Democratic Republic of the)",
  128. "ck" => "Cook Islands",
  129. "cr" => "Costa Rica",
  130. "ci" => "Côte d'Ivoire",
  131. "hr" => "Croatia",
  132. "cu" => "Cuba",
  133. "cw" => "Curaçao",
  134. "cy" => "Cyprus",
  135. "cz" => "Czechia",
  136. "dk" => "Denmark",
  137. "dj" => "Djibouti",
  138. "dm" => "Dominica",
  139. "do" => "Dominican Republic",
  140. "ec" => "Ecuador",
  141. "eg" => "Egypt",
  142. "sv" => "El Salvador",
  143. "gq" => "Equatorial Guinea",
  144. "er" => "Eritrea",
  145. "ee" => "Estonia",
  146. "et" => "Ethiopia",
  147. "fk" => "Falkland Islands (Malvinas)",
  148. "fo" => "Faroe Islands",
  149. "fj" => "Fiji",
  150. "fi" => "Finland",
  151. "fr" => "France",
  152. "gf" => "French Guiana",
  153. "pf" => "French Polynesia",
  154. "tf" => "French Southern Territories",
  155. "ga" => "Gabon",
  156. "gm" => "Gambia",
  157. "ge" => "Georgia",
  158. "de" => "Germany",
  159. "gh" => "Ghana",
  160. "gi" => "Gibraltar",
  161. "gr" => "Greece",
  162. "gl" => "Greenland",
  163. "gd" => "Grenada",
  164. "gp" => "Guadeloupe",
  165. "gu" => "Guam",
  166. "gt" => "Guatemala",
  167. "gg" => "Guernsey",
  168. "gn" => "Guinea",
  169. "gw" => "Guinea-Bissau",
  170. "gy" => "Guyana",
  171. "ht" => "Haiti",
  172. "hm" => "Heard Island and McDonald Islands",
  173. "va" => "Holy See",
  174. "hn" => "Honduras",
  175. "hk" => "Hong Kong",
  176. "hu" => "Hungary",
  177. "is" => "Iceland",
  178. "in" => "India",
  179. "id" => "Indonesia",
  180. "ir" => "Iran (Islamic Republic of)",
  181. "iq" => "Iraq",
  182. "ie" => "Ireland",
  183. "im" => "Isle of Man",
  184. "il" => "Israel",
  185. "it" => "Italy",
  186. "jm" => "Jamaica",
  187. "jp" => "Japan",
  188. "je" => "Jersey",
  189. "jo" => "Jordan",
  190. "kz" => "Kazakhstan",
  191. "ke" => "Kenya",
  192. "ki" => "Kiribati",
  193. "kp" => "Korea (Democratic People's Republic of)",
  194. "kr" => "Korea (Republic of)",
  195. "kw" => "Kuwait",
  196. "kg" => "Kyrgyzstan",
  197. "la" => "Lao People's Democratic Republic",
  198. "lv" => "Latvia",
  199. "lb" => "Lebanon",
  200. "ls" => "Lesotho",
  201. "lr" => "Liberia",
  202. "ly" => "Libya",
  203. "li" => "Liechtenstein",
  204. "lt" => "Lithuania",
  205. "lu" => "Luxembourg",
  206. "mo" => "Macao",
  207. "mk" => "Macedonia (the former Yugoslav Republic of)",
  208. "mg" => "Madagascar",
  209. "mw" => "Malawi",
  210. "my" => "Malaysia",
  211. "mv" => "Maldives",
  212. "ml" => "Mali",
  213. "mt" => "Malta",
  214. "mh" => "Marshall Islands",
  215. "mq" => "Martinique",
  216. "mr" => "Mauritania",
  217. "mu" => "Mauritius",
  218. "yt" => "Mayotte",
  219. "mx" => "Mexico",
  220. "fm" => "Micronesia (Federated States of)",
  221. "md" => "Moldova (Republic of)",
  222. "mc" => "Monaco",
  223. "mn" => "Mongolia",
  224. "me" => "Montenegro",
  225. "ms" => "Montserrat",
  226. "ma" => "Morocco",
  227. "mz" => "Mozambique",
  228. "mm" => "Myanmar",
  229. "na" => "Namibia",
  230. "nr" => "Nauru",
  231. "np" => "Nepal",
  232. "nl" => "Netherlands",
  233. "nc" => "New Caledonia",
  234. "nz" => "New Zealand",
  235. "ni" => "Nicaragua",
  236. "ne" => "Niger",
  237. "ng" => "Nigeria",
  238. "nu" => "Niue",
  239. "nf" => "Norfolk Island",
  240. "mp" => "Northern Mariana Islands",
  241. "no" => "Norway",
  242. "om" => "Oman",
  243. "pk" => "Pakistan",
  244. "pw" => "Palau",
  245. "ps" => "Palestine, State of",
  246. "pa" => "Panama",
  247. "pg" => "Papua New Guinea",
  248. "py" => "Paraguay",
  249. "pe" => "Peru",
  250. "ph" => "Philippines",
  251. "pn" => "Pitcairn",
  252. "pl" => "Poland",
  253. "pt" => "Portugal",
  254. "pr" => "Puerto Rico",
  255. "qa" => "Qatar",
  256. "re" => "Réunion",
  257. "ro" => "Romania",
  258. "ru" => "Russian Federation",
  259. "rw" => "Rwanda",
  260. "bl" => "Saint Barthélemy",
  261. "sh" => "Saint Helena, Ascension and Tristan da Cunha",
  262. "kn" => "Saint Kitts and Nevis",
  263. "lc" => "Saint Lucia",
  264. "mf" => "Saint Martin (French part)",
  265. "pm" => "Saint Pierre and Miquelon",
  266. "vc" => "Saint Vincent and the Grenadines",
  267. "ws" => "Samoa",
  268. "sm" => "San Marino",
  269. "st" => "Sao Tome and Principe",
  270. "sa" => "Saudi Arabia",
  271. "sn" => "Senegal",
  272. "rs" => "Serbia",
  273. "sc" => "Seychelles",
  274. "sl" => "Sierra Leone",
  275. "sg" => "Singapore",
  276. "sx" => "Sint Maarten (Dutch part)",
  277. "sk" => "Slovakia",
  278. "si" => "Slovenia",
  279. "sb" => "Solomon Islands",
  280. "so" => "Somalia",
  281. "za" => "South Africa",
  282. "gs" => "South Georgia and South Sandwich Islands",
  283. "ss" => "South Sudan",
  284. "es" => "Spain",
  285. "lk" => "Sri Lanka",
  286. "sd" => "Sudan",
  287. "sr" => "Suriname",
  288. "sj" => "Svalbard and Jan Mayen",
  289. "sz" => "Swaziland",
  290. "se" => "Sweden",
  291. "ch" => "Switzerland",
  292. "sy" => "Syrian Arab Republic",
  293. "tw" => "Taiwan",
  294. "tj" => "Tajikistan",
  295. "tz" => "Tanzania, United Republic of",
  296. "th" => "Thailand",
  297. "tl" => "Timor-Leste",
  298. "tg" => "Togo",
  299. "tk" => "Tokelau",
  300. "to" => "Tonga",
  301. "tt" => "Trinidad and Tobago",
  302. "tn" => "Tunisia",
  303. "tr" => "Turkey",
  304. "tm" => "Turkmenistan",
  305. "tc" => "Turks and Caicos Islands",
  306. "tv" => "Tuvalu",
  307. "ug" => "Uganda",
  308. "ua" => "Ukraine",
  309. "ae" => "United Arab Emirates",
  310. "gb" => "United Kingdom",
  311. "us" => "United States of America",
  312. "um" => "United States Minor Outlying Islands",
  313. "uy" => "Uruguay",
  314. "uz" => "Uzbekistan",
  315. "vu" => "Vanuatu",
  316. "ve" => "Venezuela (Bolivarian Republic of)",
  317. "vn" => "Viet Nam",
  318. "vg" => "Virgin Islands (British)",
  319. "vi" => "Virgin Islands (U.S.)",
  320. "wf" => "Wallis and Futuna",
  321. "eh" => "Western Sahara",
  322. "ye" => "Yemen",
  323. "zm" => "Zambia",
  324. "zw" => "Zimbabwe"
  325. ]
  326. ],
  327. "sort" => [
  328. "display" => "Sort by",
  329. "option" => [
  330. "relevance" => "Relevance",
  331. "date" => "Date" // datewr=100
  332. ]
  333. ],
  334. "newer" => [
  335. "display" => "Newer than",
  336. "option" => "_DATE"
  337. ],
  338. "older" => [
  339. "display" => "Older than",
  340. "option" => "_DATE"
  341. ]
  342. ];
  343. break;
  344. case "news":
  345. return [];
  346. }
  347. }
  348. private function get($proxy, $url, $get = [], $is_api = false){
  349. if($is_api){
  350. $headers = [
  351. "Accept: application/json"
  352. ];
  353. }else{
  354. $headers = [
  355. "User-Agent: " . config::USER_AGENT,
  356. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  357. "Accept-Language: en-US,en;q=0.9",
  358. "Accept-Encoding: gzip",
  359. "DNT: 1",
  360. "Connection: keep-alive",
  361. "Upgrade-Insecure-Requests: 1",
  362. "Sec-Fetch-Dest: document",
  363. "Sec-Fetch-Mode: navigate",
  364. "Sec-Fetch-Site: none",
  365. "Sec-Fetch-User: ?1"
  366. ];
  367. }
  368. $curlproc = curl_init();
  369. if($get !== []){
  370. $get = http_build_query($get);
  371. $url .= "?" . $get;
  372. }
  373. curl_setopt($curlproc, CURLOPT_URL, $url);
  374. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  375. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  376. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  377. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  378. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  379. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  380. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  381. $this->backend->assign_proxy($curlproc, $proxy);
  382. $data = curl_exec($curlproc);
  383. if(curl_errno($curlproc)){
  384. throw new Exception(curl_error($curlproc));
  385. }
  386. curl_close($curlproc);
  387. return $data;
  388. }
  389. public function web($get){
  390. return $this->web_api($get);
  391. }
  392. public function web_api($get){
  393. $key = $this->backend->get_key();
  394. $proxy = $this->backend->get_ip($key["increment"]);
  395. if($get["npt"]){
  396. [$params, $proxy] = $this->backend->get($get["npt"], "web");
  397. $params = json_decode($params, true);
  398. $params["api_key"] = $key["key"];
  399. }else{
  400. if(strlen($get["s"]) === 0){
  401. throw new Exception("Search term is empty!");
  402. }
  403. $params = [
  404. "api_key" => $key["key"],
  405. "q" => $get["s"],
  406. "date" => 1,
  407. "tlen" => 127, // title length
  408. "dlen" => 511, // description length
  409. "fmt" => "json"
  410. ];
  411. if($get["lang"] != "any"){
  412. $params["lb"] = $get["lang"];
  413. }
  414. if($get["country"] != "any"){
  415. $params["rb"] = $get["country"];
  416. }
  417. if($get["sort"] == "date"){
  418. $params["datewr"] = 100;
  419. }
  420. if($get["newer"] !== false){
  421. $params["since"] = date("Ymd", $get["newer"]);
  422. }
  423. if($get["older"] !== false){
  424. $params["before"] = date("Ymd", $get["before"]);
  425. }
  426. }
  427. try{
  428. $json =
  429. $this->get(
  430. $proxy,
  431. "https://api.mojeek.com/search",
  432. $params
  433. );
  434. }catch(Exception $error){
  435. throw new Exception("Failed to get JSON");
  436. }
  437. $json = json_decode($json, true);
  438. if($json === null){
  439. throw new Exception("Failed to decode JSON");
  440. }
  441. $out = [
  442. "status" => "ok",
  443. "spelling" => [
  444. "type" => "no_correction",
  445. "using" => null,
  446. "correction" => null
  447. ],
  448. "npt" => null,
  449. "answer" => [],
  450. "web" => [],
  451. "image" => [],
  452. "video" => [],
  453. "news" => [],
  454. "related" => []
  455. ];
  456. if(!isset($json["response"]["status"])){
  457. throw new Exception("API did not return a status field");
  458. }
  459. if($json["response"]["status"] != "OK"){
  460. throw new Exception("Mojeek API returned an error: " . $json["response"]["status"]);
  461. }
  462. if(!isset($json["response"]["results"])){
  463. throw new Exception("Mojeek API did not return a results object");
  464. }
  465. foreach($json["response"]["results"] as $entry){
  466. $out["web"][] = [
  467. "title" => html_entity_decode($entry["title"]),
  468. "description" => html_entity_decode($entry["desc"]),
  469. "url" => $entry["url"],
  470. "date" => $entry["timestamp"],
  471. "type" => "web",
  472. "thumb" => [
  473. "url" => null,
  474. "ratio" => null
  475. ],
  476. "sublink" => [],
  477. "table" => []
  478. ];
  479. }
  480. // get npt
  481. if($json["response"]["head"]["results"] > $json["response"]["head"]["start"] + $json["response"]["head"]["return"]){
  482. unset($params["api_key"]);
  483. if(!isset($params["s"])){
  484. $params["s"] = 11;
  485. }else{
  486. $params["s"] = $params["s"] + 10;
  487. }
  488. $out["npt"] =
  489. $this->backend->store(
  490. json_encode($params),
  491. "web",
  492. $proxy
  493. );
  494. }
  495. return $out;
  496. }
  497. public function web_browser($get){
  498. if($get["npt"]){
  499. [$token, $proxy] = $this->backend->get($get["npt"], "web");
  500. try{
  501. $html =
  502. $this->get(
  503. $proxy,
  504. "https://www.mojeek.com" . $token,
  505. []
  506. );
  507. }catch(Exception $error){
  508. throw new Exception("Failed to get HTML");
  509. }
  510. }else{
  511. $search = $get["s"];
  512. if(strlen($search) === 0){
  513. throw new Exception("Search term is empty!");
  514. }
  515. $proxy = $this->backend->get_ip();
  516. $lang = $get["lang"];
  517. $country = $get["country"];
  518. //$region = $get["region"];
  519. $domain = $get["domain"];
  520. $focus = $get["focus"];
  521. $params = [
  522. "q" => $search,
  523. "t" => 20, // number of results/page
  524. "tn" => 7, // number of news results/page
  525. "date" => 1, // show date
  526. "tlen" => 128, // max length of title
  527. //"arc" => ($country == "any" ? "none" : $country) // location. don't use autodetect!
  528. ];
  529. switch($focus){
  530. case "any": break;
  531. case "blogs":
  532. $params["fmt"] = "sst";
  533. $params["sst"] = "1";
  534. break;
  535. default:
  536. $params["foc_t"] = $focus;
  537. break;
  538. }
  539. if($lang != "any"){
  540. $params["lb"] = $lang;
  541. }
  542. /*
  543. if($region != "any"){
  544. $params["reg"] = $region;
  545. }*/
  546. if($domain != "1"){
  547. $params["si"] = $domain;
  548. }
  549. try{
  550. $html =
  551. $this->get(
  552. $proxy,
  553. "https://www.mojeek.com/search",
  554. $params
  555. );
  556. }catch(Exception $error){
  557. throw new Exception("Failed to get HTML");
  558. }
  559. }
  560. if(strlen($html) === 0){
  561. throw new Exception("Mojeek returned an empty page (probably a homepage redirect)");
  562. }
  563. $this->fuckhtml->load($html);
  564. $out = [
  565. "status" => "ok",
  566. "spelling" => [
  567. "type" => "no_correction",
  568. "using" => null,
  569. "correction" => null
  570. ],
  571. "npt" => null,
  572. "answer" => [],
  573. "web" => [],
  574. "image" => [],
  575. "video" => [],
  576. "news" => [],
  577. "related" => []
  578. ];
  579. $this->detect_block();
  580. $results =
  581. $this->fuckhtml
  582. ->getElementsByClassName("results-standard", "ul");
  583. if(count($results) === 0){
  584. return $out;
  585. }
  586. /*
  587. Get all search result divs
  588. */
  589. foreach($results as $container){
  590. $this->fuckhtml->load($container);
  591. $results =
  592. $this->fuckhtml
  593. ->getElementsByTagName("li");
  594. foreach($results as $result){
  595. $data = [
  596. "title" => null,
  597. "description" => null,
  598. "url" => null,
  599. "date" => null,
  600. "type" => "web",
  601. "thumb" => [
  602. "url" => null,
  603. "ratio" => null
  604. ],
  605. "sublink" => [],
  606. "table" => []
  607. ];
  608. $this->fuckhtml->load($result);
  609. $title =
  610. $this->fuckhtml
  611. ->getElementsByClassName("title", "a")[0];
  612. $data["title"] =
  613. html_entity_decode(
  614. $this->fuckhtml
  615. ->getTextContent(
  616. $title["innerHTML"]
  617. )
  618. );
  619. $data["url"] =
  620. html_entity_decode(
  621. $this->fuckhtml
  622. ->getTextContent(
  623. $title["attributes"]["href"]
  624. )
  625. );
  626. $description =
  627. $this->fuckhtml
  628. ->getElementsByClassName(
  629. "s", "p"
  630. );
  631. if(count($description) !== 0){
  632. $data["description"] =
  633. $this->titledots(
  634. html_entity_decode(
  635. $this->fuckhtml
  636. ->getTextContent(
  637. $description[0]
  638. )
  639. )
  640. );
  641. }
  642. $date =
  643. $this->fuckhtml
  644. ->getElementsByClassName(
  645. "mdate",
  646. "span"
  647. );
  648. if(count($date) !== 0){
  649. $data["date"] =
  650. strtotime(
  651. $this->fuckhtml
  652. ->getTextContent(
  653. $date[0]
  654. )
  655. );
  656. }
  657. $out["web"][] = $data;
  658. }
  659. }
  660. /*
  661. Get instant answers
  662. */
  663. $this->fuckhtml->load($html);
  664. $infoboxes =
  665. $this->fuckhtml
  666. ->getElementsByClassName(
  667. "infobox infobox-top",
  668. "div"
  669. );
  670. foreach($infoboxes as $infobox){
  671. $answer = [
  672. "title" => null,
  673. "description" => [],
  674. "url" => null,
  675. "thumb" => null,
  676. "table" => [],
  677. "sublink" => []
  678. ];
  679. // load first part with title + short definition
  680. $infobox_html =
  681. explode(
  682. "<hr>",
  683. $infobox["innerHTML"]
  684. );
  685. $this->fuckhtml->load($infobox_html[0]);
  686. // title
  687. $answer["title"] =
  688. $this->fuckhtml
  689. ->getTextContent(
  690. $this->fuckhtml
  691. ->getElementsByTagName("h1")[0]
  692. );
  693. // short definition
  694. $definition =
  695. $this->fuckhtml
  696. ->getElementsByTagName(
  697. "p"
  698. );
  699. if(count($definition) !== 0){
  700. $answer["description"][] = [
  701. "type" => "quote",
  702. "value" =>
  703. $this->fuckhtml
  704. ->getTextContent(
  705. $definition[0]
  706. )
  707. ];
  708. }
  709. // get thumbnail, if it exists
  710. $this->fuckhtml->load($infobox_html[1]);
  711. $thumb =
  712. $this->fuckhtml
  713. ->getElementsByClassName("float-right", "img");
  714. if(count($thumb) !== 0){
  715. preg_match(
  716. '/\/image\?img=([^&]+)/i',
  717. $thumb[0]["attributes"]["src"],
  718. $matches
  719. );
  720. if(count($matches) === 2){
  721. // for some reason, if we dont get the image from mojeek
  722. // it sometimes fail to fetch the right image URL
  723. $answer["thumb"] =
  724. "https://mojeek.com" .
  725. $this->fuckhtml
  726. ->getTextContent(
  727. $thumb[0]["attributes"]["src"]
  728. );
  729. }
  730. }
  731. // get description
  732. $ps =
  733. $this->fuckhtml
  734. ->getElementsByTagName("p");
  735. $first_tag = true;
  736. foreach($ps as $p){
  737. $this->fuckhtml->load($p);
  738. if(
  739. preg_match(
  740. '/^\s*<strong>/i',
  741. $p["innerHTML"]
  742. )
  743. ){
  744. /*
  745. Parse table
  746. */
  747. $strong =
  748. $this->fuckhtml
  749. ->getElementsByTagName("strong")[0];
  750. $p["innerHTML"] =
  751. str_replace($strong["innerHTML"], "", $p["innerHTML"]);
  752. $strong =
  753. preg_replace(
  754. '/:$/',
  755. "",
  756. ucfirst(
  757. $this->fuckhtml
  758. ->getTextContent(
  759. $strong
  760. )
  761. )
  762. );
  763. $answer["table"][trim($strong)] =
  764. trim(
  765. $this->fuckhtml
  766. ->getTextContent(
  767. $p
  768. )
  769. );
  770. continue;
  771. }
  772. $as =
  773. $this->fuckhtml
  774. ->getElementsByClassName("svg-icon");
  775. if(count($as) !== 0){
  776. /*
  777. Parse websites
  778. */
  779. foreach($as as $a){
  780. $answer["sublink"][
  781. ucfirst(explode(" ", $a["attributes"]["class"], 2)[1])
  782. ] =
  783. $this->fuckhtml
  784. ->getTextContent(
  785. $a["attributes"]["href"]
  786. );
  787. }
  788. continue;
  789. }
  790. /*
  791. Parse text content
  792. */
  793. $tags =
  794. $this->fuckhtml
  795. ->getElementsByTagName("*");
  796. $i = 0;
  797. foreach($tags as $tag){
  798. $c = count($answer["description"]);
  799. // remove tag from innerHTML
  800. $p["innerHTML"] =
  801. explode($tag["outerHTML"], $p["innerHTML"], 2);
  802. if(count($p["innerHTML"]) === 2){
  803. if(
  804. $i === 0 &&
  805. $c !== 0 &&
  806. $answer["description"][$c - 1]["type"] == "link"
  807. ){
  808. $append = "\n\n";
  809. }else{
  810. $append = "";
  811. }
  812. if($p["innerHTML"][0] != ""){
  813. $answer["description"][] = [
  814. "type" => "text",
  815. "value" => $append . trim($p["innerHTML"][0])
  816. ];
  817. }
  818. $p["innerHTML"] = $p["innerHTML"][1];
  819. }else{
  820. $p["innerHTML"] = $p["innerHTML"][0];
  821. }
  822. switch($tag["tagName"]){
  823. case "a":
  824. $value =
  825. $this->fuckhtml
  826. ->getTextContent(
  827. $tag
  828. );
  829. if(strtolower($value) == "wikipedia"){
  830. if($c !== 0){
  831. $answer["description"][$c - 1]["value"] =
  832. rtrim($answer["description"][$c - 1]["value"]);
  833. }
  834. break;
  835. }
  836. $answer["description"][] = [
  837. "type" => "link",
  838. "url" =>
  839. $this->fuckhtml
  840. ->getTextContent(
  841. $tag["attributes"]["href"]
  842. ),
  843. "value" =>
  844. $this->fuckhtml
  845. ->getTextContent(
  846. $tag
  847. )
  848. ];
  849. break;
  850. }
  851. $i++;
  852. }
  853. }
  854. // get URL
  855. $this->fuckhtml->load($infobox_html[2]);
  856. $answer["url"] =
  857. $this->fuckhtml
  858. ->getTextContent(
  859. $this->fuckhtml
  860. ->getElementsByTagName(
  861. "a"
  862. )[0]
  863. ["attributes"]
  864. ["href"]
  865. );
  866. // append answer
  867. $out["answer"][] = $answer;
  868. }
  869. /*
  870. Get news
  871. */
  872. $this->fuckhtml->load($html);
  873. $news =
  874. $this->fuckhtml
  875. ->getElementsByClassName(
  876. "results news-results",
  877. "div"
  878. );
  879. if(count($news) !== 0){
  880. $this->fuckhtml->load($news[0]);
  881. $lis =
  882. $this->fuckhtml
  883. ->getElementsByTagName("li");
  884. foreach($lis as $li){
  885. $this->fuckhtml->load($li);
  886. $a =
  887. $this->fuckhtml
  888. ->getElementsByClassName(
  889. "ob",
  890. "a"
  891. );
  892. if(count($a) === 0){
  893. continue;
  894. }
  895. $a = $a[0];
  896. $date =
  897. explode(
  898. " - ",
  899. $this->fuckhtml
  900. ->getTextContent(
  901. $this->fuckhtml
  902. ->getElementsByTagName(
  903. "span"
  904. )[0]
  905. )
  906. );
  907. $date =
  908. strtotime(
  909. $date[count($date) - 1]
  910. );
  911. $out["news"][] = [
  912. "title" =>
  913. html_entity_decode(
  914. $this->fuckhtml
  915. ->getTextContent(
  916. $a
  917. )
  918. ),
  919. "description" => null,
  920. "date" => $date,
  921. "thumb" => [
  922. "url" => null,
  923. "ratio" => null
  924. ],
  925. "url" =>
  926. $this->fuckhtml
  927. ->getTextContent(
  928. $a["attributes"]["href"]
  929. )
  930. ];
  931. }
  932. }
  933. /*
  934. Get next page
  935. */
  936. $this->fuckhtml->load($html);
  937. $pagination =
  938. $this->fuckhtml
  939. ->getElementsByClassName("pagination");
  940. if(count($pagination) !== false){
  941. $this->fuckhtml->load($pagination[0]);
  942. $as =
  943. $this->fuckhtml
  944. ->getElementsByTagName("a");
  945. foreach($as as $a){
  946. if($a["innerHTML"] == "Next"){
  947. $out["npt"] = $this->backend->store(
  948. $this->fuckhtml
  949. ->getTextContent(
  950. $a["attributes"]["href"]
  951. ),
  952. "web",
  953. $proxy
  954. );
  955. }
  956. }
  957. }
  958. return $out;
  959. }
  960. public function news(){
  961. throw new Exception("Currently unsupported");
  962. }
  963. public function news_browser($get){
  964. $search = $get["s"];
  965. if(strlen($search) === 0){
  966. throw new Exception("Search term is empty!");
  967. }
  968. $out = [
  969. "status" => "ok",
  970. "npt" => null,
  971. "news" => []
  972. ];
  973. try{
  974. $html =
  975. $this->get(
  976. $this->backend->get_ip(),
  977. "https://www.mojeek.com/search",
  978. [
  979. "q" => $search,
  980. "fmt" => "news"
  981. ]
  982. );
  983. }catch(Exception $error){
  984. throw new Exception("Failed to get HTML");
  985. }
  986. /*
  987. $handle = fopen("scraper/mojeek.html", "r");
  988. $html = fread($handle, filesize("scraper/mojeek.html"));
  989. fclose($handle);
  990. */
  991. $this->fuckhtml->load($html);
  992. $this->detect_block();
  993. $articles =
  994. $this->fuckhtml->getElementsByTagName("article");
  995. foreach($articles as $article){
  996. $this->fuckhtml->load($article);
  997. $data = [
  998. "title" => null,
  999. "author" => null,
  1000. "description" => null,
  1001. "date" => null,
  1002. "thumb" =>
  1003. [
  1004. "url" => null,
  1005. "ratio" => null
  1006. ],
  1007. "url" => null
  1008. ];
  1009. $a = $this->fuckhtml->getElementsByTagName("a")[0];
  1010. $data["title"] =
  1011. $this->fuckhtml
  1012. ->getTextContent(
  1013. $a["attributes"]["title"]
  1014. );
  1015. $data["url"] =
  1016. $this->fuckhtml
  1017. ->getTextContent(
  1018. $a["attributes"]["href"]
  1019. );
  1020. $p = $this->fuckhtml->getElementsByTagName("p");
  1021. $data["description"] =
  1022. $this->titledots(
  1023. $this->fuckhtml
  1024. ->getTextContent(
  1025. $this->fuckhtml
  1026. ->getElementsByClassName(
  1027. "s",
  1028. $p
  1029. )[0]
  1030. )
  1031. );
  1032. if($data["description"] == ""){
  1033. $data["description"] = null;
  1034. }
  1035. // get date from big node
  1036. $date =
  1037. $this->fuckhtml
  1038. ->getElementsByClassName(
  1039. "date",
  1040. $p
  1041. );
  1042. if(count($date) !== 0){
  1043. $data["date"] =
  1044. strtotime(
  1045. $this->fuckhtml
  1046. ->getTextContent(
  1047. $date[0]
  1048. )
  1049. );
  1050. }
  1051. // grep date + author
  1052. $s =
  1053. $this->fuckhtml
  1054. ->getElementsByClassName(
  1055. "i",
  1056. $p
  1057. )[0];
  1058. $this->fuckhtml->load($s);
  1059. $a =
  1060. $this->fuckhtml
  1061. ->getElementsByTagName("a");
  1062. if(count($a) !== 0){
  1063. // parse big node information
  1064. $data["author"] =
  1065. htmlspecialchars_decode(
  1066. $this->fuckhtml
  1067. ->getTextContent(
  1068. $a[0]["innerHTML"]
  1069. )
  1070. );
  1071. }else{
  1072. // parse smaller nodes
  1073. $replace =
  1074. $this->fuckhtml
  1075. ->getElementsByTagName("time")[0];
  1076. $data["date"] =
  1077. strtotime(
  1078. $this->fuckhtml
  1079. ->getTextContent(
  1080. $replace
  1081. )
  1082. );
  1083. $s["innerHTML"] =
  1084. str_replace(
  1085. $replace["outerHTML"],
  1086. "",
  1087. $s["innerHTML"]
  1088. );
  1089. $data["author"] =
  1090. preg_replace(
  1091. '/ &bull; $/',
  1092. "",
  1093. $s["innerHTML"]
  1094. );
  1095. }
  1096. $out["news"][] = $data;
  1097. }
  1098. return $out;
  1099. }
  1100. private function detect_block(){
  1101. $captcha =
  1102. $this->fuckhtml
  1103. ->getElementsByClassName(
  1104. "captcha-box",
  1105. "div"
  1106. );
  1107. if(count($captcha) !== 0){
  1108. throw new Exception("Mojeek returned a captcha");
  1109. }
  1110. $title =
  1111. $this->fuckhtml
  1112. ->getElementsByTagName(
  1113. "title"
  1114. );
  1115. if(
  1116. count($title) !== 0 &&
  1117. $this->fuckhtml
  1118. ->getTextContent(
  1119. $title[0]["innerHTML"]
  1120. ) == "403 - Forbidden"
  1121. ){
  1122. throw new Exception("Mojeek blocked this instance or request proxy.");
  1123. }
  1124. }
  1125. private function titledots($title){
  1126. return trim($title, ". \t\n\r\0\x0B");
  1127. }
  1128. }