1
0

pinterest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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($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->proxy->assign_proxy($curlproc);
  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. $search = $get["s"];
  46. $out = [
  47. "status" => "ok",
  48. "npt" => null,
  49. "image" => []
  50. ];
  51. $filter = [
  52. "source_url" => "/search/pins/?q=" . urlencode($search),
  53. "rs" => "typed",
  54. "data" =>
  55. json_encode(
  56. [
  57. "options" => [
  58. "article" => null,
  59. "applied_filters" => null,
  60. "appliedProductFilters" => "---",
  61. "auto_correction_disabled" => false,
  62. "corpus" => null,
  63. "customized_rerank_type" => null,
  64. "filters" => null,
  65. "query" => $search,
  66. "query_pin_sigs" => null,
  67. "redux_normalize_feed" => true,
  68. "rs" => "typed",
  69. "scope" => "pins", // pins, boards, videos,
  70. "source_id" => null
  71. ],
  72. "context" => []
  73. ]
  74. ),
  75. "_" => substr(str_replace(".", "", (string)microtime(true)), 0, -1)
  76. ];
  77. try{
  78. $json =
  79. json_decode(
  80. $this->get(
  81. "https://www.pinterest.ca/resource/BaseSearchResource/get/",
  82. $filter
  83. ),
  84. true
  85. );
  86. }catch(Exception $error){
  87. throw new Exception("Failed to fetch JSON");
  88. }
  89. if($json === null){
  90. throw new Exception("Failed to decode JSON");
  91. }
  92. //print_r($json);
  93. foreach(
  94. $json
  95. ["resource_response"]
  96. ["data"]
  97. ["results"]
  98. as $item
  99. ){
  100. switch($item["type"]){
  101. case "pin":
  102. /*
  103. Handle image object
  104. */
  105. $images = array_values($item["images"]);
  106. $image = &$images[count($images) - 1]; // original
  107. $thumb = &$images[1]; // 236x
  108. $title = [];
  109. if(
  110. isset($item["grid_title"]) &&
  111. trim($item["grid_title"]) != ""
  112. ){
  113. $title[] = $item["grid_title"];
  114. }
  115. if(
  116. isset($item["description"]) &&
  117. trim($item["description"]) != ""
  118. ){
  119. $title[] = $item["description"];
  120. }
  121. $title = implode(": ", $title);
  122. if(
  123. $title == "" &&
  124. isset($item["board"]["name"]) &&
  125. trim($item["board"]["name"]) != ""
  126. ){
  127. $title = $item["board"]["name"];
  128. }
  129. if($title == ""){
  130. $title = null;
  131. }
  132. $out["image"][] = [
  133. "title" => $title,
  134. "source" => [
  135. [
  136. "url" => $image["url"],
  137. "width" => (int)$image["width"],
  138. "height" => (int)$image["height"]
  139. ],
  140. [
  141. "url" => $thumb["url"],
  142. "width" => (int)$thumb["width"],
  143. "height" => (int)$thumb["height"]
  144. ]
  145. ],
  146. "url" => "https://www.pinterest.com/pin/" . $item["id"]
  147. ];
  148. break;
  149. case "board":
  150. if(isset($item["cover_pin"]["image_url"])){
  151. $image = [
  152. "url" => $item["cover_pin"]["image_url"],
  153. "width" => (int)$item["cover_pin"]["size"][0],
  154. "height" => (int)$item["cover_pin"]["size"][1]
  155. ];
  156. }elseif(isset($item["image_cover_url_hd"])){
  157. /*
  158. $image = [
  159. "url" =>
  160. "width" => null,
  161. "height" => null
  162. ];*/
  163. }
  164. break;
  165. }
  166. }
  167. return $out;
  168. }
  169. private function getfullresimage($image, $has_og){
  170. $has_og = $has_og ? "1200x" : "originals";
  171. return
  172. preg_replace(
  173. '/https:\/\/i\.pinimg\.com\/[^\/]+\//',
  174. "https://i.pinimg.com/" . $has_og . "/",
  175. $image
  176. );
  177. }
  178. }