pinterest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. class pinterest{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("pinterest");
  6. }
  7. public function getfilters($page){
  8. return [];
  9. }
  10. private function get($proxy, $url, $get = []){
  11. $curlproc = curl_init();
  12. if($get !== []){
  13. $get = http_build_query($get);
  14. $url .= "?" . $get;
  15. }
  16. curl_setopt($curlproc, CURLOPT_URL, $url);
  17. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  18. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  19. ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/110.0",
  20. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  21. "Accept-Language: en-US,en;q=0.5",
  22. "Accept-Encoding: gzip",
  23. "DNT: 1",
  24. "Connection: keep-alive",
  25. "Upgrade-Insecure-Requests: 1",
  26. "Sec-Fetch-Dest: document",
  27. "Sec-Fetch-Mode: navigate",
  28. "Sec-Fetch-Site: none",
  29. "Sec-Fetch-User: ?1"]
  30. );
  31. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  32. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  33. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  34. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  35. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  36. $this->backend->assign_proxy($curlproc, $proxy);
  37. $data = curl_exec($curlproc);
  38. if(curl_errno($curlproc)){
  39. throw new Exception(curl_error($curlproc));
  40. }
  41. curl_close($curlproc);
  42. return $data;
  43. }
  44. public function image($get){
  45. if($get["npt"]){
  46. // @TODO
  47. // post data for next page
  48. $data = [
  49. "source_url" => "/search/pins/?q=" . urlencode($search) . "&rs=typed",
  50. "data" =>
  51. json_encode(
  52. [
  53. // {"options":{"applied_filters":null,"appliedProductFilters":"---","article":null,"auto_correction_disabled":false,"corpus":null,"customized_rerank_type":null,"domains":null,"filters":null,"journey_depth":null,"page_size":null,"price_max":null,"price_min":null,"query_pin_sigs":null,"query":"higurashi","redux_normalize_feed":true,"rs":"typed","scope":"pins","selected_one_bar_modules":null,"source_id":null,"source_module_id":null,"top_pin_id":null,"bookmarks":["Y2JVSG81V2sxcmNHRlpWM1J5VFVad1ZsWlVRbXhpVmtreVZsZHpOV0pIU2tkV2FscFhVbXhhVkZreU1WSmtNREZWVjIxR1RrMXNTbEJXYlhSaFVtMVdjMVZ1U2xaaWEzQnpXVlJPVTJWV1pISlhhM1JYVm10V05sVldVbE5XVjBwMVVXMUdWVll6VFhoVWJYaFhWMVp3Ums1V1RsTmlSbGt5Vm10YWFtVkdWbkpOU0dSUFZsZG9XRmxzWkc5VlZscHlWbGhrYkdKR1NubFdWelZQWVVaYWRHVkVRbFppUmtwVVZrUktWMlJIVWtWV2JHaHBVakZLU0Zkc1pEUmtNVnBZVW10b2FsSXdXbkJXYlRWRFpHeGFSMWRzVG1oaGVrWllXV3RvVTFVeFpFaFZiRUpoVm5wRk1GbHFSbXRYVjA1R1YyczFWMVpHV2pSWFZtaDNVakZrY2sxWVRsaGlhM0JXV1ZSR1MyRkdiRlZTYm1SVVVteHdXbGxWVlRGVk1VbDVWRmhrVjAxdVVuWlVhMXBTWlVaT2MxcEhSbE5TTWswMVdtdGFWMU5YU2paVmJYaFRUVmhDUjFZeU5YZFVNVkY0VjJ0b1ZXRnJOVlpVVmxwTFVURndXR042VmxOV2ExcGFXVlZWTlZVeFNYZE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TFUWGx3U0ZWVmQzRm1SMWw1VFZSUk1WbDZUVEJhUjFGNVQxZFNhVnB0VlRGT1JFVXdXVlJuZVU1cVRUUk5hbU40VDBSSk1VNXFWVEZOYlZwcVdsUnJlRTFFVVhwWmVsVjNXbXBvYkU1dFJYbE9ha0Y2VDFSSk5VMTZWVEJaYWtJNFZHdFdXR1pCUFQwPXxOb25lfDg3NTcwOTAzODAxNDc0OTMqR1FMKnwzMjM3YjM3ZGNhMGU3YjYyYzYzYzAyZGJkNGU1MjdlNzMyMTExMTNlMmUyMzEyOWM2MDAzYmU1ZTlmZjkwYjAwfE5FV3w="]},"context":{}}
  54. ]
  55. );
  56. ];
  57. }else{
  58. $search = $get["s"];
  59. if(strlen($search) === 0){
  60. throw new Exception("Search term is empty!");
  61. }
  62. $filter = [
  63. "source_url" => "/search/pins/?q=" . urlencode($search),
  64. "rs" => "typed",
  65. "data" =>
  66. json_encode(
  67. [
  68. "options" => [
  69. "article" => null,
  70. "applied_filters" => null,
  71. "appliedProductFilters" => "---",
  72. "auto_correction_disabled" => false,
  73. "corpus" => null,
  74. "customized_rerank_type" => null,
  75. "filters" => null,
  76. "query" => $search,
  77. "query_pin_sigs" => null,
  78. "redux_normalize_feed" => true,
  79. "rs" => "typed",
  80. "scope" => "pins", // pins, boards, videos,
  81. "source_id" => null
  82. ],
  83. "context" => []
  84. ]
  85. ),
  86. "_" => substr(str_replace(".", "", (string)microtime(true)), 0, -1)
  87. ];
  88. $proxy = $this->backend->get_ip();
  89. }
  90. try{
  91. $json =
  92. json_decode(
  93. $this->get(
  94. $proxy,
  95. "https://www.pinterest.ca/resource/BaseSearchResource/get/",
  96. $filter
  97. ),
  98. true
  99. );
  100. }catch(Exception $error){
  101. throw new Exception("Failed to fetch JSON");
  102. }
  103. if($json === null){
  104. throw new Exception("Failed to decode JSON");
  105. }
  106. $out = [
  107. "status" => "ok",
  108. "npt" => null,
  109. "image" => []
  110. ];
  111. foreach(
  112. $json
  113. ["resource_response"]
  114. ["data"]
  115. ["results"]
  116. as $item
  117. ){
  118. switch($item["type"]){
  119. case "pin":
  120. /*
  121. Handle image object
  122. */
  123. $images = array_values($item["images"]);
  124. $image = &$images[count($images) - 1]; // original
  125. $thumb = &$images[1]; // 236x
  126. $title = [];
  127. if(
  128. isset($item["grid_title"]) &&
  129. trim($item["grid_title"]) != ""
  130. ){
  131. $title[] = $item["grid_title"];
  132. }
  133. if(
  134. isset($item["description"]) &&
  135. trim($item["description"]) != ""
  136. ){
  137. $title[] = $item["description"];
  138. }
  139. $title = implode(": ", $title);
  140. if(
  141. $title == "" &&
  142. isset($item["board"]["name"]) &&
  143. trim($item["board"]["name"]) != ""
  144. ){
  145. $title = $item["board"]["name"];
  146. }
  147. if($title == ""){
  148. $title = null;
  149. }
  150. $out["image"][] = [
  151. "title" => $title,
  152. "source" => [
  153. [
  154. "url" => $image["url"],
  155. "width" => (int)$image["width"],
  156. "height" => (int)$image["height"]
  157. ],
  158. [
  159. "url" => $thumb["url"],
  160. "width" => (int)$thumb["width"],
  161. "height" => (int)$thumb["height"]
  162. ]
  163. ],
  164. "url" => "https://www.pinterest.com/pin/" . $item["id"]
  165. ];
  166. break;
  167. case "board":
  168. if(isset($item["cover_pin"]["image_url"])){
  169. $image = [
  170. "url" => $item["cover_pin"]["image_url"],
  171. "width" => (int)$item["cover_pin"]["size"][0],
  172. "height" => (int)$item["cover_pin"]["size"][1]
  173. ];
  174. }elseif(isset($item["image_cover_url_hd"])){
  175. /*
  176. $image = [
  177. "url" =>
  178. "width" => null,
  179. "height" => null
  180. ];*/
  181. }
  182. break;
  183. }
  184. }
  185. return $out;
  186. }
  187. private function getfullresimage($image, $has_og){
  188. $has_og = $has_og ? "1200x" : "originals";
  189. return
  190. preg_replace(
  191. '/https:\/\/i\.pinimg\.com\/[^\/]+\//',
  192. "https://i.pinimg.com/" . $has_og . "/",
  193. $image
  194. );
  195. }
  196. }