1
0

mojeek.php 24 KB

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