vsco.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. class vsco{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("vsco");
  6. }
  7. public function getfilters($page){
  8. return [];
  9. }
  10. private function get($proxy, $url, $get = [], $bearer = null){
  11. $curlproc = curl_init();
  12. if($get !== []){
  13. $get_tmp = http_build_query($get);
  14. $url .= "?" . $get_tmp;
  15. }
  16. curl_setopt($curlproc, CURLOPT_URL, $url);
  17. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  18. if($bearer === null){
  19. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  20. ["User-Agent: " . config::USER_AGENT,
  21. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  22. "Accept-Language: en-US,en;q=0.5",
  23. "Accept-Encoding: gzip, deflate, br, zstd",
  24. "DNT: 1",
  25. "Sec-GPC: 1",
  26. "Connection: keep-alive",
  27. "Upgrade-Insecure-Requests: 1",
  28. "Sec-Fetch-Dest: document",
  29. "Sec-Fetch-Mode: navigate",
  30. "Sec-Fetch-Site: none",
  31. "Sec-Fetch-User: ?1",
  32. "Priority: u=0, i"]
  33. );
  34. }else{
  35. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  36. ["User-Agent: " . config::USER_AGENT,
  37. "Accept: */*",
  38. "Accept-Language: en-US",
  39. "Accept-Encoding: gzip",
  40. "Referer: https://vsco.co/search/images/" . urlencode($get["query"]),
  41. "authorization: Bearer " . $bearer,
  42. "content-type: application/json",
  43. "x-client-build: 1",
  44. "x-client-platform: web",
  45. "DNT: 1",
  46. "Sec-GPC: 1",
  47. "Connection: keep-alive",
  48. "Sec-Fetch-Dest: empty",
  49. "Sec-Fetch-Mode: cors",
  50. "Sec-Fetch-Site: same-origin",
  51. "Priority: u=0",
  52. "TE: trailers"]
  53. );
  54. }
  55. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  57. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  58. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  59. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  60. // http1 bypass?!?!?!
  61. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  62. $this->backend->assign_proxy($curlproc, $proxy);
  63. $data = curl_exec($curlproc);
  64. if(curl_errno($curlproc)){
  65. throw new Exception(curl_error($curlproc));
  66. }
  67. curl_close($curlproc);
  68. return $data;
  69. }
  70. public function image($get){
  71. if($get["npt"]){
  72. [$data, $proxy] =
  73. $this->backend->get(
  74. $get["npt"], "images"
  75. );
  76. $data = json_decode($data, true);
  77. }else{
  78. $search = $get["s"];
  79. if(strlen($search) === 0){
  80. throw new Exception("Search term is empty!");
  81. }
  82. $proxy = $this->backend->get_ip();
  83. // get bearer token
  84. try{
  85. $html =
  86. $this->get(
  87. $proxy,
  88. "https://vsco.co/feed"
  89. );
  90. }catch(Exception $error){
  91. throw new Exception("Failed to fetch feed page");
  92. }
  93. preg_match(
  94. '/"tkn":"([A-z0-9]+)"/',
  95. $html,
  96. $bearer
  97. );
  98. if(!isset($bearer[1])){
  99. throw new Exception("Failed to grep bearer token");
  100. }
  101. $data = [
  102. "pagination" => [
  103. "query" => $search,
  104. "page" => 0,
  105. "size" => 100
  106. ],
  107. "bearer" => $bearer[1]
  108. ];
  109. }
  110. try{
  111. $json =
  112. $this->get(
  113. $proxy,
  114. "https://vsco.co/api/2.0/search/images",
  115. $data["pagination"],
  116. $data["bearer"]
  117. );
  118. }catch(Exception $error){
  119. throw new Exception("Failed to fetch JSON");
  120. }
  121. $json = json_decode($json, true);
  122. if($json === null){
  123. throw new Exception("Failed to decode JSON");
  124. }
  125. $out = [
  126. "status" => "ok",
  127. "npt" => null,
  128. "image" => []
  129. ];
  130. if(!isset($json["results"])){
  131. throw new Exception("Failed to access results object");
  132. }
  133. foreach($json["results"] as $image){
  134. $image_domain = parse_url("https://" . $image["responsive_url"], PHP_URL_HOST);
  135. $thumbnail = explode($image_domain, $image["responsive_url"], 2)[1];
  136. if(substr($thumbnail, 0, 3) != "/1/"){
  137. $thumbnail =
  138. preg_replace(
  139. '/^\/[^\/]+/',
  140. "",
  141. $thumbnail
  142. );
  143. }
  144. $thumbnail = "https://img.vsco.co/cdn-cgi/image/width=480,height=360" . $thumbnail;
  145. $size =
  146. $this->image_ratio(
  147. (int)$image["dimensions"]["width"],
  148. (int)$image["dimensions"]["height"]
  149. );
  150. $out["image"][] = [
  151. "title" => $image["description"],
  152. "source" => [
  153. [
  154. "url" => "https://" . $image["responsive_url"],
  155. "width" => (int)$image["dimensions"]["width"],
  156. "height" => (int)$image["dimensions"]["height"]
  157. ],
  158. [
  159. "url" => $thumbnail,
  160. "width" => $size[0],
  161. "height" => $size[1]
  162. ]
  163. ],
  164. "url" => "https://" . $image["grid"]["domain"] . "/media/" . $image["imageId"]
  165. ];
  166. }
  167. // get NPT
  168. $max_page = ceil($json["total"] / 100);
  169. $data["pagination"]["page"]++;
  170. if($max_page > $data["pagination"]["page"]){
  171. $out["npt"] =
  172. $this->backend->store(
  173. json_encode($data),
  174. "images",
  175. $proxy
  176. );
  177. }
  178. return $out;
  179. }
  180. private function image_ratio($width, $height){
  181. $ratio = [
  182. 480 / $width,
  183. 360 / $height
  184. ];
  185. if($ratio[0] < $ratio[1]){
  186. $ratio = $ratio[0];
  187. }else{
  188. $ratio = $ratio[1];
  189. }
  190. return [
  191. floor($width * $ratio),
  192. floor($height * $ratio)
  193. ];
  194. }
  195. }