mojeek.php 24 KB

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