marginalia.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. class marginalia{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("marginalia");
  6. }
  7. public function getfilters($page){
  8. switch($page){
  9. case "web":
  10. return [
  11. "profile" => [
  12. "display" => "Profile",
  13. "option" => [
  14. "any" => "Default",
  15. "modern" => "Modern"
  16. ]
  17. ],
  18. "format" => [
  19. "display" => "Format",
  20. "option" => [
  21. "any" => "Any",
  22. "html5" => "html5",
  23. "xhtml" => "xhtml",
  24. "html123" => "html123"
  25. ]
  26. ],
  27. "file" => [
  28. "display" => "File",
  29. "option" => [
  30. "any" => "Any",
  31. "nomedia" => "Deny media",
  32. "media" => "Contains media",
  33. "audio" => "Contains audio",
  34. "video" => "Contains video",
  35. "archive" => "Contains archive",
  36. "document" => "Contains document"
  37. ]
  38. ],
  39. "javascript" => [
  40. "display" => "Javascript",
  41. "option" => [
  42. "any" => "Allow JS",
  43. "deny" => "Deny JS",
  44. "require" => "Require JS"
  45. ]
  46. ],
  47. "trackers" => [
  48. "display" => "Trackers",
  49. "option" => [
  50. "any" => "Allow trackers",
  51. "deny" => "Deny trackers",
  52. "require" => "Require trackers"
  53. ]
  54. ],
  55. "cookies" => [
  56. "display" => "Cookies",
  57. "option" => [
  58. "any" => "Allow cookies",
  59. "deny" => "Deny cookies",
  60. "require" => "Require cookies"
  61. ]
  62. ],
  63. "affiliate" => [
  64. "display" => "Affiliate links in body",
  65. "option" => [
  66. "any" => "Allow affiliate links",
  67. "deny" => "Deny affiliate links",
  68. "require" => "Require affiliate links"
  69. ]
  70. ]
  71. ];
  72. }
  73. }
  74. private function get($proxy, $url, $get = []){
  75. $headers = [
  76. "User-Agent: " . config::USER_AGENT,
  77. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  78. "Accept-Language: en-US,en;q=0.5",
  79. "Accept-Encoding: gzip",
  80. "DNT: 1",
  81. "Connection: keep-alive",
  82. "Upgrade-Insecure-Requests: 1",
  83. "Sec-Fetch-Dest: document",
  84. "Sec-Fetch-Mode: navigate",
  85. "Sec-Fetch-Site: none",
  86. "Sec-Fetch-User: ?1"
  87. ];
  88. $curlproc = curl_init();
  89. if($get !== []){
  90. $get = http_build_query($get);
  91. $url .= "?" . $get;
  92. }
  93. curl_setopt($curlproc, CURLOPT_URL, $url);
  94. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  95. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  96. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  97. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  98. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  99. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  100. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  101. $this->backend->assign_proxy($curlproc, $proxy);
  102. $data = curl_exec($curlproc);
  103. if(curl_errno($curlproc)){
  104. throw new Exception(curl_error($curlproc));
  105. }
  106. curl_close($curlproc);
  107. return $data;
  108. }
  109. public function web($get){
  110. $search = [$get["s"]];
  111. if(strlen($get["s"]) === 0){
  112. throw new Exception("Search term is empty!");
  113. }
  114. $profile = $get["profile"];
  115. $format = $get["format"];
  116. $file = $get["file"];
  117. foreach(
  118. [
  119. "javascript" => $get["javascript"],
  120. "trackers" => $get["trackers"],
  121. "cookies" => $get["cookies"],
  122. "affiliate" => $get["affiliate"]
  123. ]
  124. as $key => $value
  125. ){
  126. if($value == "any"){ continue; }
  127. switch($key){
  128. case "javascript": $str = "js:true"; break;
  129. case "trackers": $str = "special:tracking"; break;
  130. case "cookies": $str = "special:cookies"; break;
  131. case "affiliate": $str = "special:affiliate"; break;
  132. }
  133. if($value == "deny"){
  134. $str = "-" . $str;
  135. }
  136. $search[] = $str;
  137. }
  138. if($format != "any"){
  139. $search[] = "format:$format";
  140. }
  141. switch($file){
  142. case "any": break;
  143. case "nomedia": $search[] = "-special:media"; break;
  144. case "media": $search[] = "special:media"; break;
  145. default:
  146. $search[] = "file:$file";
  147. }
  148. $search = implode(" ", $search);
  149. $params = [
  150. "count" => 20
  151. ];
  152. if($profile == "modern"){
  153. $params["index"] = 1;
  154. }
  155. try{
  156. $json =
  157. $this->get(
  158. $this->backend->get_ip(), // no nextpage
  159. "https://api.marginalia.nu/" . config::MARGINALIA_API_KEY . "/search/" . urlencode($search),
  160. $params
  161. );
  162. }catch(Exception $error){
  163. throw new Exception("Failed to get JSON");
  164. }
  165. if($json == "Slow down"){
  166. throw new Exception("The API key used is rate limited. Please try again in a few minutes.");
  167. }
  168. $json = json_decode($json, true);
  169. /*
  170. $handle = fopen("scraper/marginalia.json", "r");
  171. $json = json_decode(fread($handle, filesize("scraper/marginalia.json")), true);
  172. fclose($handle);*/
  173. $out = [
  174. "status" => "ok",
  175. "spelling" => [
  176. "type" => "no_correction",
  177. "using" => null,
  178. "correction" => null
  179. ],
  180. "npt" => null,
  181. "answer" => [],
  182. "web" => [],
  183. "image" => [],
  184. "video" => [],
  185. "news" => [],
  186. "related" => []
  187. ];
  188. foreach($json["results"] as $result){
  189. $out["web"][] = [
  190. "title" => $result["title"],
  191. "description" => str_replace("\n", " ", $result["description"]),
  192. "url" => $result["url"],
  193. "date" => null,
  194. "type" => "web",
  195. "thumb" => [
  196. "url" => null,
  197. "ratio" => null
  198. ],
  199. "sublink" => [],
  200. "table" => []
  201. ];
  202. }
  203. return $out;
  204. }
  205. }