pinterest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 = [], &$cookies, $header_data_post = null){
  11. $curlproc = curl_init();
  12. if($header_data_post === null){
  13. // handling GET
  14. // extract cookies
  15. $cookies_tmp = [];
  16. curl_setopt($curlproc, CURLOPT_HEADERFUNCTION, function($curlproc, $header) use (&$cookies_tmp){
  17. $length = strlen($header);
  18. $header = explode(":", $header, 2);
  19. if(trim(strtolower($header[0])) == "set-cookie"){
  20. $cookie_tmp = explode("=", trim($header[1]), 2);
  21. $cookies_tmp[trim($cookie_tmp[0])] =
  22. explode(";", $cookie_tmp[1], 2)[0];
  23. }
  24. return $length;
  25. });
  26. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  27. ["User-Agent: " . config::USER_AGENT,
  28. "Accept: application/json, text/javascript, */*, q=0.01",
  29. "Accept-Language: en-US,en;q=0.5",
  30. "Accept-Encoding: gzip",
  31. "Referer: https://ca.pinterest.com/",
  32. "X-Requested-With: XMLHttpRequest",
  33. "X-APP-VERSION: 78f8764",
  34. "X-Pinterest-AppState: active",
  35. "X-Pinterest-Source-Url: /",
  36. "X-Pinterest-PWS-Handler: www/index.js",
  37. "screen-dpr: 1",
  38. "is-preload-enabled: 1",
  39. "DNT: 1",
  40. "Sec-GPC: 1",
  41. "Sec-Fetch-Dest: empty",
  42. "Sec-Fetch-Mode: cors",
  43. "Sec-Fetch-Site: same-origin",
  44. "Connection: keep-alive",
  45. "Alt-Used: ca.pinterest.com",
  46. "Priority: u=0",
  47. "TE: trailers"]
  48. );
  49. if($get !== []){
  50. $get = http_build_query($get);
  51. $url .= "?" . $get;
  52. }
  53. }else{
  54. // handling POST (pagination)
  55. $get = http_build_query($get);
  56. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  57. ["User-Agent: " . config::USER_AGENT,
  58. "Accept: application/json, text/javascript, */*, q=0.01",
  59. "Accept-Language: en-US,en;q=0.5",
  60. "Accept-Encoding: gzip",
  61. "Content-Type: application/x-www-form-urlencoded",
  62. "Content-Length: " . strlen($get),
  63. "Referer: https://ca.pinterest.com/",
  64. "X-Requested-With: XMLHttpRequest",
  65. "X-APP-VERSION: 78f8764",
  66. "X-CSRFToken: " . $cookies["csrf"],
  67. "X-Pinterest-AppState: active",
  68. "X-Pinterest-Source-Url: /search/pins/?rs=ac&len=2&q=" . urlencode($header_data_post) . "&eq=" . urlencode($header_data_post),
  69. "X-Pinterest-PWS-Handler: www/search/[scope].js",
  70. "screen-dpr: 1",
  71. "is-preload-enabled: 1",
  72. "Origin: https://ca.pinterest.com",
  73. "DNT: 1",
  74. "Sec-GPC: 1",
  75. "Sec-Fetch-Dest: empty",
  76. "Sec-Fetch-Mode: cors",
  77. "Sec-Fetch-Site: same-origin",
  78. "Connection: keep-alive",
  79. "Alt-Used: ca.pinterest.com",
  80. "Cookie: " . $cookies["cookie"],
  81. "TE: trailers"]
  82. );
  83. curl_setopt($curlproc, CURLOPT_POST, true);
  84. curl_setopt($curlproc, CURLOPT_POSTFIELDS, $get);
  85. }
  86. curl_setopt($curlproc, CURLOPT_URL, $url);
  87. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  88. // http2 bypass
  89. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  90. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  91. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  92. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  93. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  94. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  95. $this->backend->assign_proxy($curlproc, $proxy);
  96. $data = curl_exec($curlproc);
  97. if(curl_errno($curlproc)){
  98. throw new Exception(curl_error($curlproc));
  99. }
  100. if($header_data_post === null){
  101. if(!isset($cookies_tmp["csrftoken"])){
  102. throw new Exception("Failed to grep CSRF token");
  103. }
  104. $cookies = "";
  105. foreach($cookies_tmp as $cookie_name => $cookie_value){
  106. $cookies .= $cookie_name . "=" . $cookie_value . "; ";
  107. }
  108. $cookies = [
  109. "csrf" => $cookies_tmp["csrftoken"],
  110. "cookie" => rtrim($cookies, " ;")
  111. ];
  112. }
  113. curl_close($curlproc);
  114. return $data;
  115. }
  116. public function image($get){
  117. if($get["npt"]){
  118. [$data, $proxy] =
  119. $this->backend->get(
  120. $get["npt"], "images"
  121. );
  122. $data = json_decode($data, true);
  123. $search = $data["q"];
  124. $cookies = $data["cookies"];
  125. try{
  126. $json =
  127. $this->get(
  128. $proxy,
  129. "https://ca.pinterest.com/resource/BaseSearchResource/get/",
  130. [
  131. "source_url" => "/search/pins/?q=" . urlencode($search) . "&rs=typed",
  132. "data" => json_encode(
  133. [
  134. "options" => [
  135. "applied_unified_filters" => null,
  136. "appliedProductFilters" => "---",
  137. "article" => null,
  138. "auto_correction_disabled" => false,
  139. "corpus" => null,
  140. "customized_rerank_type" => null,
  141. "domains" => null,
  142. "dynamicPageSizeExpGroup" => null,
  143. "filters" => null,
  144. "journey_depth" => null,
  145. "page_size" => null,
  146. "price_max" => null,
  147. "price_min" => null,
  148. "query_pin_sigs" => null,
  149. "query" => $data["q"],
  150. "redux_normalize_feed" => true,
  151. "request_params" => null,
  152. "rs" => "typed",
  153. "scope" => "pins",
  154. "selected_one_bar_modules" => null,
  155. "source_id" => null,
  156. "source_module_id" => null,
  157. "source_url" => "/search/pins/?q=" . urlencode($search) . "&rs=typed",
  158. "top_pin_id" => null,
  159. "top_pin_ids" => null,
  160. "bookmarks" => [
  161. $data["bookmark"]
  162. ]
  163. ],
  164. "context" => []
  165. ],
  166. JSON_UNESCAPED_SLASHES
  167. )
  168. ],
  169. $cookies,
  170. $search
  171. );
  172. }catch(Exception $error){
  173. throw new Exception("Failed to fetch JSON");
  174. }
  175. }else{
  176. $search = $get["s"];
  177. if(strlen($search) === 0){
  178. throw new Exception("Search term is empty!");
  179. }
  180. // https://ca.pinterest.com/resource/BaseSearchResource/get/?source_url=%2Fsearch%2Fpins%2F%3Feq%3Dhigurashi%26etslf%3D5966%26len%3D2%26q%3Dhigurashi%2520when%2520they%2520cry%26rs%3Dac&data=%7B%22options%22%3A%7B%22applied_unified_filters%22%3Anull%2C%22appliedProductFilters%22%3A%22---%22%2C%22article%22%3Anull%2C%22auto_correction_disabled%22%3Afalse%2C%22corpus%22%3Anull%2C%22customized_rerank_type%22%3Anull%2C%22domains%22%3Anull%2C%22dynamicPageSizeExpGroup%22%3Anull%2C%22filters%22%3Anull%2C%22journey_depth%22%3Anull%2C%22page_size%22%3Anull%2C%22price_max%22%3Anull%2C%22price_min%22%3Anull%2C%22query_pin_sigs%22%3Anull%2C%22query%22%3A%22higurashi%20when%20they%20cry%22%2C%22redux_normalize_feed%22%3Atrue%2C%22request_params%22%3Anull%2C%22rs%22%3A%22ac%22%2C%22scope%22%3A%22pins%22%2C%22selected_one_bar_modules%22%3Anull%2C%22source_id%22%3Anull%2C%22source_module_id%22%3Anull%2C%22source_url%22%3A%22%2Fsearch%2Fpins%2F%3Feq%3Dhigurashi%26etslf%3D5966%26len%3D2%26q%3Dhigurashi%2520when%2520they%2520cry%26rs%3Dac%22%2C%22top_pin_id%22%3Anull%2C%22top_pin_ids%22%3Anull%7D%2C%22context%22%3A%7B%7D%7D&_=1736116313987
  181. // source_url=%2Fsearch%2Fpins%2F%3Feq%3Dhigurashi%26etslf%3D5966%26len%3D2%26q%3Dhigurashi%2520when%2520they%2520cry%26rs%3Dac
  182. // &data=%7B%22options%22%3A%7B%22applied_unified_filters%22%3Anull%2C%22appliedProductFilters%22%3A%22---%22%2C%22article%22%3Anull%2C%22auto_correction_disabled%22%3Afalse%2C%22corpus%22%3Anull%2C%22customized_rerank_type%22%3Anull%2C%22domains%22%3Anull%2C%22dynamicPageSizeExpGroup%22%3Anull%2C%22filters%22%3Anull%2C%22journey_depth%22%3Anull%2C%22page_size%22%3Anull%2C%22price_max%22%3Anull%2C%22price_min%22%3Anull%2C%22query_pin_sigs%22%3Anull%2C%22query%22%3A%22higurashi%20when%20they%20cry%22%2C%22redux_normalize_feed%22%3Atrue%2C%22request_params%22%3Anull%2C%22rs%22%3A%22ac%22%2C%22scope%22%3A%22pins%22%2C%22selected_one_bar_modules%22%3Anull%2C%22source_id%22%3Anull%2C%22source_module_id%22%3Anull%2C%22source_url%22%3A%22%2Fsearch%2Fpins%2F%3Feq%3Dhigurashi%26etslf%3D5966%26len%3D2%26q%3Dhigurashi%2520when%2520they%2520cry%26rs%3Dac%22%2C%22top_pin_id%22%3Anull%2C%22top_pin_ids%22%3Anull%7D%2C%22context%22%3A%7B%7D%7D
  183. // &_=1736116313987
  184. $source_url = "/search/pins/?q=" . urlencode($search) . "&rs=" . urlencode($search);
  185. $filter = [
  186. "source_url" => $source_url,
  187. "rs" => "typed",
  188. "data" =>
  189. json_encode(
  190. [
  191. "options" => [
  192. "applied_unified_filters" => null,
  193. "appliedProductFilters" => "---",
  194. "article" => null,
  195. "corpus" => null,
  196. "customized_rerank_type" => null,
  197. "domains" => null,
  198. "dynamicPageSizeExpGroup" => null,
  199. "filters" => null,
  200. "journey_depth" => null,
  201. "page_size" => null,
  202. "price_max" => null,
  203. "price_min" => null,
  204. "query_pin_sigs" => null,
  205. "query" => $search,
  206. "redux_normalize_feed" => true,
  207. "request_params" => null,
  208. "rs" => "ac",
  209. "scope" => "pins", // pins, boards, videos,
  210. "selected_one_bar_modules" => null,
  211. "source_id" => null,
  212. "source_module_id" => null,
  213. "source_url" => $source_url,
  214. "top_pin_id" => null,
  215. "top_pin_ids" => null
  216. ],
  217. "context" => []
  218. ]
  219. ),
  220. "_" => substr(str_replace(".", "", (string)microtime(true)), 0, -1)
  221. ];
  222. $proxy = $this->backend->get_ip();
  223. $cookies = [];
  224. try{
  225. $json =
  226. $this->get(
  227. $proxy,
  228. "https://ca.pinterest.com/resource/BaseSearchResource/get/",
  229. $filter,
  230. $cookies,
  231. null
  232. );
  233. }catch(Exception $error){
  234. throw new Exception("Failed to fetch JSON");
  235. }
  236. }
  237. $json = json_decode($json, true);
  238. if($json === null){
  239. throw new Exception("Failed to decode JSON");
  240. }
  241. $out = [
  242. "status" => "ok",
  243. "npt" => null,
  244. "image" => []
  245. ];
  246. if(
  247. !isset(
  248. $json["resource_response"]
  249. ["status"]
  250. )
  251. ){
  252. throw new Exception("Unknown API failure");
  253. }
  254. if($json["resource_response"]["status"] != "success"){
  255. $status = "Got non-OK response: " . $json["resource_response"]["status"];
  256. if(
  257. isset(
  258. $json["resource_response"]["message"]
  259. )
  260. ){
  261. $status .= " - " . $json["resource_response"]["message"];
  262. }
  263. throw new Exception($status);
  264. }
  265. if(
  266. isset(
  267. $json["resource_response"]["sensitivity"]
  268. ["notices"][0]["description"]["text"]
  269. )
  270. ){
  271. throw new Exception(
  272. "Pinterest returned a notice: " .
  273. $json["resource_response"]["sensitivity"]["notices"][0]["description"]["text"]
  274. );
  275. }
  276. // get NPT
  277. if(isset($json["resource_response"]["bookmark"])){
  278. $out["npt"] =
  279. $this->backend->store(
  280. json_encode([
  281. "q" => $search,
  282. "bookmark" => $json["resource_response"]["bookmark"],
  283. "cookies" => $cookies
  284. ]),
  285. "images",
  286. $proxy
  287. );
  288. }
  289. foreach(
  290. $json
  291. ["resource_response"]
  292. ["data"]
  293. ["results"]
  294. as $item
  295. ){
  296. switch($item["type"]){
  297. case "pin":
  298. case "board":
  299. /*
  300. Handle image object
  301. */
  302. $images = array_values($item["images"]);
  303. $image = &$images[count($images) - 1]; // original
  304. $thumb = &$images[1]; // 236x
  305. $title = [];
  306. if(
  307. isset($item["grid_title"]) &&
  308. trim($item["grid_title"]) != ""
  309. ){
  310. $title[] = $item["grid_title"];
  311. }
  312. if(
  313. isset($item["description"]) &&
  314. trim($item["description"]) != ""
  315. ){
  316. $title[] = $item["description"];
  317. }
  318. $title = implode(": ", $title);
  319. if(
  320. $title == "" &&
  321. isset($item["board"]["name"]) &&
  322. trim($item["board"]["name"]) != ""
  323. ){
  324. $title = $item["board"]["name"];
  325. }
  326. if($title == ""){
  327. $title = null;
  328. }
  329. $out["image"][] = [
  330. "title" => $title,
  331. "source" => [
  332. [
  333. "url" => $image["url"],
  334. "width" => (int)$image["width"],
  335. "height" => (int)$image["height"]
  336. ],
  337. [
  338. "url" => $thumb["url"],
  339. "width" => (int)$thumb["width"],
  340. "height" => (int)$thumb["height"]
  341. ]
  342. ],
  343. "url" =>
  344. $item["link"] === null ?
  345. "https://ca.pinterest.com/pin/" . $item["id"] :
  346. $item["link"]
  347. ];
  348. break;
  349. }
  350. }
  351. return $out;
  352. }
  353. }