imgur.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. class imgur{
  3. public function __construct(){
  4. include "lib/fuckhtml.php";
  5. $this->fuckhtml = new fuckhtml();
  6. include "lib/backend.php";
  7. $this->backend = new backend("imgur");
  8. }
  9. public function getfilters($page){
  10. return [
  11. "sort" => [ // /score/
  12. "display" => "Sort by",
  13. "option" => [
  14. "score" => "Highest scoring",
  15. "relevance" => "Most relevant",
  16. "time" => "Newest first"
  17. ]
  18. ],
  19. "time" => [ // /score/day/
  20. "display" => "Time posted",
  21. "option" => [
  22. "all" => "All time",
  23. "day" => "Today",
  24. "week" => "This week",
  25. "month" => "This month",
  26. "year" => "This year"
  27. ]
  28. ],
  29. "format" => [ // q_type
  30. "display" => "Format",
  31. "option" => [
  32. "any" => "Any format",
  33. "jpg" => "JPG",
  34. "png" => "PNG",
  35. "gif" => "GIF",
  36. "anigif" => "Animated GIF",
  37. "album" => "Albums"
  38. ]
  39. ],
  40. "size" => [ // q_size_px
  41. "display" => "Size",
  42. "option" => [
  43. "any" => "Any size",
  44. "small" => "Small (500px or less)",
  45. "med" => "Medium (500px to 2000px)",
  46. "big" => "Big (2000px to 5000px)",
  47. "lrg" => "Large (5000px to 10000px)",
  48. "huge" => "Huge (10000px and above)"
  49. ]
  50. ]
  51. ];
  52. }
  53. private function get($proxy, $url, $get = []){
  54. $curlproc = curl_init();
  55. if($get !== []){
  56. $get = http_build_query($get);
  57. $url .= "?scrolled&" . $get;
  58. }
  59. curl_setopt($curlproc, CURLOPT_URL, $url);
  60. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  61. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  62. ["User-Agent: " . config::USER_AGENT,
  63. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  64. "Accept-Language: en-US,en;q=0.5",
  65. "Accept-Encoding: gzip",
  66. "DNT: 1",
  67. "Referer: https://imgur.com/search/",
  68. "Connection: keep-alive",
  69. "Sec-Fetch-Dest: empty",
  70. "Sec-Fetch-Mode: cors",
  71. "Sec-Fetch-Site: same-origin",
  72. "TE: trailers",
  73. "X-Requested-With: XMLHttpRequest"]
  74. );
  75. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  77. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  78. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  79. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  80. $this->backend->assign_proxy($curlproc, $proxy);
  81. $data = curl_exec($curlproc);
  82. if(curl_errno($curlproc)){
  83. throw new Exception(curl_error($curlproc));
  84. }
  85. curl_close($curlproc);
  86. return $data;
  87. }
  88. public function image($get){
  89. if($get["npt"]){
  90. [$filter, $proxy] =
  91. $this->backend->get(
  92. $get["npt"],
  93. "images"
  94. );
  95. $filter = json_decode($filter, true);
  96. $search = $filter["s"];
  97. unset($filter["s"]);
  98. $sort = $filter["sort"];
  99. unset($filter["sort"]);
  100. $time = $filter["time"];
  101. unset($filter["time"]);
  102. $format = $filter["format"];
  103. unset($filter["format"]);
  104. $size = $filter["size"];
  105. unset($filter["size"]);
  106. $page = $filter["page"];
  107. unset($filter["page"]);
  108. }else{
  109. $search = $get["s"];
  110. if(strlen($search) === 0){
  111. throw new Exception("Search term is empty!");
  112. }
  113. $proxy = $this->backend->get_ip();
  114. $sort = $get["sort"];
  115. $time = $get["time"];
  116. $format = $get["format"];
  117. $size = $get["size"];
  118. $page = 0;
  119. $filter = [
  120. "q" => $search
  121. ];
  122. if($format != "any"){
  123. $filter["q_type"] = $format;
  124. }
  125. if($size != "any"){
  126. $filter["q_size_px"] = $size;
  127. $filter["q_size_is_mpx"] = "off";
  128. }
  129. }
  130. $out = [
  131. "status" => "ok",
  132. "npt" => null,
  133. "image" => []
  134. ];
  135. try{
  136. $html =
  137. $this->get(
  138. $proxy,
  139. "https://imgur.com/search/$sort/$time/page/$page",
  140. $filter
  141. );
  142. }catch(Exception $error){
  143. throw new Exception("Failed to fetch HTML");
  144. }
  145. $this->fuckhtml->load($html);
  146. $posts =
  147. $this->fuckhtml
  148. ->getElementsByClassName(
  149. "post",
  150. "div"
  151. );
  152. foreach($posts as $post){
  153. $this->fuckhtml->load($post);
  154. $image =
  155. $this->fuckhtml
  156. ->getElementsByTagName("img")[0];
  157. $image_url = "https:" . substr($this->fuckhtml->getTextContent($image["attributes"]["src"]), 0, -5);
  158. $out["image"][] = [
  159. "title" =>
  160. $this->fuckhtml
  161. ->getTextContent(
  162. $image["attributes"]["alt"]
  163. ),
  164. "source" => [
  165. [
  166. "url" => $image_url . ".jpg",
  167. "width" => null,
  168. "height" => null
  169. ],
  170. [
  171. "url" => $image_url . "m.jpg",
  172. "width" => null,
  173. "height" => null
  174. ]
  175. ],
  176. "url" =>
  177. "https://imgur.com" .
  178. $this->fuckhtml
  179. ->getTextContent(
  180. $this->fuckhtml
  181. ->getElementsByClassName(
  182. "image-list-link",
  183. "a"
  184. )
  185. [0]
  186. ["attributes"]
  187. ["href"]
  188. )
  189. ];
  190. }
  191. if(isset($out["image"][0])){
  192. // store nextpage
  193. $filter["s"] = $search;
  194. $filter["sort"] = $sort;
  195. $filter["time"] = $time;
  196. $filter["format"] = $format;
  197. $filter["size"] = $size;
  198. $filter["page"] = $page + 1;
  199. $out["npt"] =
  200. $this->backend->store(
  201. json_encode($filter),
  202. "images",
  203. $proxy
  204. );
  205. }
  206. return $out;
  207. }
  208. }