1
0

mojeek.php 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. "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($proxy, $url, $get = []){
  367. $headers = [
  368. "User-Agent: " . config::USER_AGENT,
  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.9",
  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. $this->backend->assign_proxy($curlproc, $proxy);
  394. $data = curl_exec($curlproc);
  395. if(curl_errno($curlproc)){
  396. throw new Exception(curl_error($curlproc));
  397. }
  398. curl_close($curlproc);
  399. return $data;
  400. }
  401. public function web($get){
  402. if($get["npt"]){
  403. [$token, $proxy] = $this->backend->get($get["npt"], "web");
  404. try{
  405. $html =
  406. $this->get(
  407. $proxy,
  408. "https://www.mojeek.com" . $token,
  409. []
  410. );
  411. }catch(Exception $error){
  412. throw new Exception("Failed to get HTML");
  413. }
  414. }else{
  415. $search = $get["s"];
  416. if(strlen($search) === 0){
  417. throw new Exception("Search term is empty!");
  418. }
  419. $proxy = $this->backend->get_ip();
  420. $lang = $get["lang"];
  421. $country = $get["country"];
  422. //$region = $get["region"];
  423. $domain = $get["domain"];
  424. $focus = $get["focus"];
  425. $params = [
  426. "q" => $search,
  427. "t" => 20, // number of results/page
  428. "tn" => 7, // number of news results/page
  429. "date" => 1, // show date
  430. "tlen" => 128, // max length of title
  431. //"dlen" => 511, // max length of description
  432. //"arc" => ($country == "any" ? "none" : $country) // location. don't use autodetect!
  433. ];
  434. switch($focus){
  435. case "any": break;
  436. case "blogs":
  437. $params["fmt"] = "sst";
  438. $params["sst"] = "1";
  439. break;
  440. default:
  441. $params["foc_t"] = $focus;
  442. break;
  443. }
  444. if($lang != "any"){
  445. $params["lb"] = $lang;
  446. }
  447. /*
  448. if($region != "any"){
  449. $params["reg"] = $region;
  450. }*/
  451. if($domain != "1"){
  452. $params["si"] = $domain;
  453. }
  454. try{
  455. $html =
  456. $this->get(
  457. $proxy,
  458. "https://www.mojeek.com/search",
  459. $params
  460. );
  461. }catch(Exception $error){
  462. throw new Exception("Failed to get HTML");
  463. }
  464. }
  465. if(strlen($html) === 0){
  466. throw new Exception("Mojeek returned an empty page (probably a homepage redirect)");
  467. }
  468. $this->fuckhtml->load($html);
  469. $out = [
  470. "status" => "ok",
  471. "spelling" => [
  472. "type" => "no_correction",
  473. "using" => null,
  474. "correction" => null
  475. ],
  476. "npt" => null,
  477. "answer" => [],
  478. "web" => [],
  479. "image" => [],
  480. "video" => [],
  481. "news" => [],
  482. "related" => []
  483. ];
  484. $this->detect_block();
  485. $results =
  486. $this->fuckhtml
  487. ->getElementsByClassName("results-standard", "ul");
  488. if(count($results) === 0){
  489. return $out;
  490. }
  491. /*
  492. Get all search result divs
  493. */
  494. foreach($results as $container){
  495. $this->fuckhtml->load($container);
  496. $results =
  497. $this->fuckhtml
  498. ->getElementsByTagName("li");
  499. foreach($results as $result){
  500. $data = [
  501. "title" => null,
  502. "description" => null,
  503. "url" => null,
  504. "date" => null,
  505. "type" => "web",
  506. "thumb" => [
  507. "url" => null,
  508. "ratio" => null
  509. ],
  510. "sublink" => [],
  511. "table" => []
  512. ];
  513. $this->fuckhtml->load($result);
  514. $title =
  515. $this->fuckhtml
  516. ->getElementsByClassName("title", "a")[0];
  517. $data["title"] =
  518. html_entity_decode(
  519. $this->fuckhtml
  520. ->getTextContent(
  521. $title["innerHTML"]
  522. )
  523. );
  524. $data["url"] =
  525. html_entity_decode(
  526. $this->fuckhtml
  527. ->getTextContent(
  528. $title["attributes"]["href"]
  529. )
  530. );
  531. $description =
  532. $this->fuckhtml
  533. ->getElementsByClassName(
  534. "s", "p"
  535. );
  536. if(count($description) !== 0){
  537. $data["description"] =
  538. $this->titledots(
  539. html_entity_decode(
  540. $this->fuckhtml
  541. ->getTextContent(
  542. $description[0]
  543. )
  544. )
  545. );
  546. }
  547. $date =
  548. $this->fuckhtml
  549. ->getElementsByClassName(
  550. "mdate",
  551. "span"
  552. );
  553. if(count($date) !== 0){
  554. $data["date"] =
  555. strtotime(
  556. $this->fuckhtml
  557. ->getTextContent(
  558. $date[0]
  559. )
  560. );
  561. }
  562. $out["web"][] = $data;
  563. }
  564. }
  565. /*
  566. Get instant answers
  567. */
  568. $this->fuckhtml->load($html);
  569. $infoboxes =
  570. $this->fuckhtml
  571. ->getElementsByClassName(
  572. "infobox infobox-top",
  573. "div"
  574. );
  575. foreach($infoboxes as $infobox){
  576. $answer = [
  577. "title" => null,
  578. "description" => [],
  579. "url" => null,
  580. "thumb" => null,
  581. "table" => [],
  582. "sublink" => []
  583. ];
  584. // load first part with title + short definition
  585. $infobox_html =
  586. explode(
  587. "<hr>",
  588. $infobox["innerHTML"]
  589. );
  590. $this->fuckhtml->load($infobox_html[0]);
  591. // title
  592. $answer["title"] =
  593. $this->fuckhtml
  594. ->getTextContent(
  595. $this->fuckhtml
  596. ->getElementsByTagName("h1")[0]
  597. );
  598. // short definition
  599. $definition =
  600. $this->fuckhtml
  601. ->getElementsByTagName(
  602. "p"
  603. );
  604. if(count($definition) !== 0){
  605. $answer["description"][] = [
  606. "type" => "quote",
  607. "value" =>
  608. $this->fuckhtml
  609. ->getTextContent(
  610. $definition[0]
  611. )
  612. ];
  613. }
  614. // get thumbnail, if it exists
  615. $this->fuckhtml->load($infobox_html[1]);
  616. $thumb =
  617. $this->fuckhtml
  618. ->getElementsByClassName("float-right", "img");
  619. if(count($thumb) !== 0){
  620. preg_match(
  621. '/\/image\?img=([^&]+)/i',
  622. $thumb[0]["attributes"]["src"],
  623. $matches
  624. );
  625. if(count($matches) === 2){
  626. // for some reason, if we dont get the image from mojeek
  627. // it sometimes fail to fetch the right image URL
  628. $answer["thumb"] =
  629. "https://mojeek.com" .
  630. $this->fuckhtml
  631. ->getTextContent(
  632. $thumb[0]["attributes"]["src"]
  633. );
  634. }
  635. }
  636. // get description
  637. $ps =
  638. $this->fuckhtml
  639. ->getElementsByTagName("p");
  640. $first_tag = true;
  641. foreach($ps as $p){
  642. $this->fuckhtml->load($p);
  643. if(
  644. preg_match(
  645. '/^\s*<strong>/i',
  646. $p["innerHTML"]
  647. )
  648. ){
  649. /*
  650. Parse table
  651. */
  652. $strong =
  653. $this->fuckhtml
  654. ->getElementsByTagName("strong")[0];
  655. $p["innerHTML"] =
  656. str_replace($strong["innerHTML"], "", $p["innerHTML"]);
  657. $strong =
  658. preg_replace(
  659. '/:$/',
  660. "",
  661. ucfirst(
  662. $this->fuckhtml
  663. ->getTextContent(
  664. $strong
  665. )
  666. )
  667. );
  668. $answer["table"][trim($strong)] =
  669. trim(
  670. $this->fuckhtml
  671. ->getTextContent(
  672. $p
  673. )
  674. );
  675. continue;
  676. }
  677. $as =
  678. $this->fuckhtml
  679. ->getElementsByClassName("svg-icon");
  680. if(count($as) !== 0){
  681. /*
  682. Parse websites
  683. */
  684. foreach($as as $a){
  685. $answer["sublink"][
  686. ucfirst(explode(" ", $a["attributes"]["class"], 2)[1])
  687. ] =
  688. $this->fuckhtml
  689. ->getTextContent(
  690. $a["attributes"]["href"]
  691. );
  692. }
  693. continue;
  694. }
  695. /*
  696. Parse text content
  697. */
  698. $tags =
  699. $this->fuckhtml
  700. ->getElementsByTagName("*");
  701. $i = 0;
  702. foreach($tags as $tag){
  703. $c = count($answer["description"]);
  704. // remove tag from innerHTML
  705. $p["innerHTML"] =
  706. explode($tag["outerHTML"], $p["innerHTML"], 2);
  707. if(count($p["innerHTML"]) === 2){
  708. if(
  709. $i === 0 &&
  710. $c !== 0 &&
  711. $answer["description"][$c - 1]["type"] == "link"
  712. ){
  713. $append = "\n\n";
  714. }else{
  715. $append = "";
  716. }
  717. if($p["innerHTML"][0] != ""){
  718. $answer["description"][] = [
  719. "type" => "text",
  720. "value" => $append . trim($p["innerHTML"][0])
  721. ];
  722. }
  723. $p["innerHTML"] = $p["innerHTML"][1];
  724. }else{
  725. $p["innerHTML"] = $p["innerHTML"][0];
  726. }
  727. switch($tag["tagName"]){
  728. case "a":
  729. $value =
  730. $this->fuckhtml
  731. ->getTextContent(
  732. $tag
  733. );
  734. if(strtolower($value) == "wikipedia"){
  735. if($c !== 0){
  736. $answer["description"][$c - 1]["value"] =
  737. rtrim($answer["description"][$c - 1]["value"]);
  738. }
  739. break;
  740. }
  741. $answer["description"][] = [
  742. "type" => "link",
  743. "url" =>
  744. $this->fuckhtml
  745. ->getTextContent(
  746. $tag["attributes"]["href"]
  747. ),
  748. "value" =>
  749. $this->fuckhtml
  750. ->getTextContent(
  751. $tag
  752. )
  753. ];
  754. break;
  755. }
  756. $i++;
  757. }
  758. }
  759. // get URL
  760. $this->fuckhtml->load($infobox_html[2]);
  761. $answer["url"] =
  762. $this->fuckhtml
  763. ->getTextContent(
  764. $this->fuckhtml
  765. ->getElementsByTagName(
  766. "a"
  767. )[0]
  768. ["attributes"]
  769. ["href"]
  770. );
  771. // append answer
  772. $out["answer"][] = $answer;
  773. }
  774. /*
  775. Get news
  776. */
  777. $this->fuckhtml->load($html);
  778. $news =
  779. $this->fuckhtml
  780. ->getElementsByClassName(
  781. "results news-results",
  782. "div"
  783. );
  784. if(count($news) !== 0){
  785. $this->fuckhtml->load($news[0]);
  786. $lis =
  787. $this->fuckhtml
  788. ->getElementsByTagName("li");
  789. foreach($lis as $li){
  790. $this->fuckhtml->load($li);
  791. $a =
  792. $this->fuckhtml
  793. ->getElementsByClassName(
  794. "ob",
  795. "a"
  796. );
  797. if(count($a) === 0){
  798. continue;
  799. }
  800. $a = $a[0];
  801. $date =
  802. explode(
  803. " - ",
  804. $this->fuckhtml
  805. ->getTextContent(
  806. $this->fuckhtml
  807. ->getElementsByTagName(
  808. "span"
  809. )[0]
  810. )
  811. );
  812. $date =
  813. strtotime(
  814. $date[count($date) - 1]
  815. );
  816. $out["news"][] = [
  817. "title" =>
  818. html_entity_decode(
  819. $this->fuckhtml
  820. ->getTextContent(
  821. $a
  822. )
  823. ),
  824. "description" => null,
  825. "date" => $date,
  826. "thumb" => [
  827. "url" => null,
  828. "ratio" => null
  829. ],
  830. "url" =>
  831. $this->fuckhtml
  832. ->getTextContent(
  833. $a["attributes"]["href"]
  834. )
  835. ];
  836. }
  837. }
  838. /*
  839. Get next page
  840. */
  841. $this->fuckhtml->load($html);
  842. $pagination =
  843. $this->fuckhtml
  844. ->getElementsByClassName("pagination");
  845. if(count($pagination) !== false){
  846. $this->fuckhtml->load($pagination[0]);
  847. $as =
  848. $this->fuckhtml
  849. ->getElementsByTagName("a");
  850. foreach($as as $a){
  851. if($a["innerHTML"] == "Next"){
  852. $out["npt"] = $this->backend->store(
  853. $this->fuckhtml
  854. ->getTextContent(
  855. $a["attributes"]["href"]
  856. ),
  857. "web",
  858. $proxy
  859. );
  860. }
  861. }
  862. }
  863. return $out;
  864. }
  865. public function news($get){
  866. $search = $get["s"];
  867. if(strlen($search) === 0){
  868. throw new Exception("Search term is empty!");
  869. }
  870. $out = [
  871. "status" => "ok",
  872. "npt" => null,
  873. "news" => []
  874. ];
  875. try{
  876. $html =
  877. $this->get(
  878. $this->backend->get_ip(),
  879. "https://www.mojeek.com/search",
  880. [
  881. "q" => $search,
  882. "fmt" => "news"
  883. ]
  884. );
  885. }catch(Exception $error){
  886. throw new Exception("Failed to get HTML");
  887. }
  888. /*
  889. $handle = fopen("scraper/mojeek.html", "r");
  890. $html = fread($handle, filesize("scraper/mojeek.html"));
  891. fclose($handle);
  892. */
  893. $this->fuckhtml->load($html);
  894. $this->detect_block();
  895. $articles =
  896. $this->fuckhtml->getElementsByTagName("article");
  897. foreach($articles as $article){
  898. $this->fuckhtml->load($article);
  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. $a = $this->fuckhtml->getElementsByTagName("a")[0];
  912. $data["title"] =
  913. $this->fuckhtml
  914. ->getTextContent(
  915. $a["attributes"]["title"]
  916. );
  917. $data["url"] =
  918. $this->fuckhtml
  919. ->getTextContent(
  920. $a["attributes"]["href"]
  921. );
  922. $p = $this->fuckhtml->getElementsByTagName("p");
  923. $data["description"] =
  924. $this->titledots(
  925. $this->fuckhtml
  926. ->getTextContent(
  927. $this->fuckhtml
  928. ->getElementsByClassName(
  929. "s",
  930. $p
  931. )[0]
  932. )
  933. );
  934. if($data["description"] == ""){
  935. $data["description"] = null;
  936. }
  937. // get date from big node
  938. $date =
  939. $this->fuckhtml
  940. ->getElementsByClassName(
  941. "date",
  942. $p
  943. );
  944. if(count($date) !== 0){
  945. $data["date"] =
  946. strtotime(
  947. $this->fuckhtml
  948. ->getTextContent(
  949. $date[0]
  950. )
  951. );
  952. }
  953. // grep date + author
  954. $s =
  955. $this->fuckhtml
  956. ->getElementsByClassName(
  957. "i",
  958. $p
  959. )[0];
  960. $this->fuckhtml->load($s);
  961. $a =
  962. $this->fuckhtml
  963. ->getElementsByTagName("a");
  964. if(count($a) !== 0){
  965. // parse big node information
  966. $data["author"] =
  967. htmlspecialchars_decode(
  968. $this->fuckhtml
  969. ->getTextContent(
  970. $a[0]["innerHTML"]
  971. )
  972. );
  973. }else{
  974. // parse smaller nodes
  975. $replace =
  976. $this->fuckhtml
  977. ->getElementsByTagName("time")[0];
  978. $data["date"] =
  979. strtotime(
  980. $this->fuckhtml
  981. ->getTextContent(
  982. $replace
  983. )
  984. );
  985. $s["innerHTML"] =
  986. str_replace(
  987. $replace["outerHTML"],
  988. "",
  989. $s["innerHTML"]
  990. );
  991. $data["author"] =
  992. preg_replace(
  993. '/ &bull; $/',
  994. "",
  995. $s["innerHTML"]
  996. );
  997. }
  998. $out["news"][] = $data;
  999. }
  1000. return $out;
  1001. }
  1002. private function detect_block(){
  1003. $captcha =
  1004. $this->fuckhtml
  1005. ->getElementsByClassName(
  1006. "captcha-box",
  1007. "div"
  1008. );
  1009. if(count($captcha) !== 0){
  1010. throw new Exception("Mojeek returned a captcha");
  1011. }
  1012. $title =
  1013. $this->fuckhtml
  1014. ->getElementsByTagName(
  1015. "title"
  1016. );
  1017. if(
  1018. count($title) !== 0 &&
  1019. $this->fuckhtml
  1020. ->getTextContent(
  1021. $title[0]["innerHTML"]
  1022. ) == "403 - Forbidden"
  1023. ){
  1024. throw new Exception("Mojeek blocked this instance or request proxy.");
  1025. }
  1026. }
  1027. private function titledots($title){
  1028. return trim($title, ". \t\n\r\0\x0B");
  1029. }
  1030. }