1
0

pinterest.php 4.7 KB

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