marginalia.php 5.1 KB

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