Browse Source

feat: add `Next page` button (merge pull request #23 from qminlh/next-page)

Add next page button
Ahwx 1 year ago
parent
commit
45a9e8c62e
1 changed files with 95 additions and 7 deletions
  1. 95 7
      search.php

+ 95 - 7
search.php

@@ -28,13 +28,89 @@
 
 $query = $_GET['q'];
 
-$baseurl = "https://pinterest.com/resource/BaseSearchResource/get/?data=%7B%22options%22%3A%7B%22query%22%3A%22{}%22%7D%7D";
+$bookmark = null;
+if (array_key_exists("bookmark", $_GET)) {
+    $bookmark = urldecode($_GET["bookmark"]);
+}
 
-$search = function($query) use($baseurl)
+$csrftoken = null;
+if (array_key_exists("csrftoken", $_GET)) {
+    $csrftoken = $_GET["csrftoken"];
+}
+
+$url = "https://www.pinterest.com/resource/BaseSearchResource/get/";
+
+class SearchResult
+{
+    public $images;
+    public $bookmark;
+}
+
+$header_function = function($ch, $rawheader)
 {
-    $url = str_replace("{}", str_replace(" ", "%20", $query), $baseurl);
-    $json = file_get_contents($url);
-    $data = json_decode($json);
+    global $csrftoken;
+    $len = strlen($rawheader);
+
+    $header = explode(":", $rawheader, 2);
+    if (count($header) != 2)
+        return $len;
+
+    // we are only interested in set-cookie header
+    if (trim($header[0]) != "set-cookie")
+        return $len;
+
+    $cookie = explode(";", trim($header[1]), 2);
+    $cookie = explode("=", $cookie[0], 2);
+
+    switch ($cookie[0])
+    {
+        case "csrftoken":
+            $csrftoken = $cookie[1];
+    }
+
+    return $len;
+};
+
+$prepare_search_curl_obj = function($query, $bookmark) use ($url, $header_function, $csrftoken)
+{
+    $data_param_obj = array(
+        "options"=>array(
+            "query"=>$query
+        )
+    );
+    if ($bookmark != null)
+        $data_param_obj["options"]["bookmarks"] = array($bookmark);
+
+    $data_param = urlencode(json_encode($data_param_obj));
+
+    $headers = array();
+    if ($csrftoken != null)
+    {
+        $headers[] = "x-csrftoken: $csrftoken";
+        $headers[] = "cookie: csrftoken=$csrftoken";
+    }
+
+    $finalurl = $url;
+    if ($bookmark == null)
+        $finalurl = "$url?data=$data_param";
+
+    $ch = curl_init($finalurl);
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+    curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
+    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+    if ($bookmark != null)
+    {
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
+    }
+    return $ch;
+};
+
+$search = function($query, $bookmark) use($prepare_search_curl_obj)
+{
+    $ch = $prepare_search_curl_obj($query, $bookmark);
+    $response = curl_exec($ch);
+    $data = json_decode($response);
     $images = array();
     echo "<div class=img-container>";
         foreach ($data->{"resource_response"}->{"data"}->{"results"} as $result)
@@ -45,11 +121,23 @@ $search = function($query) use($baseurl)
             echo "<a class=img-result href='/image_proxy.php?url=", $url, "'>";
             echo "<img src='/image_proxy.php?url=", $url, "'></a>";
         }
-        return $images;
     echo "</div>";
+    $result = new SearchResult();
+    $result->images = $images;
+    if (property_exists($data->{"resource_response"}, "bookmark"))
+        $result->bookmark = $data->{"resource_response"}->{"bookmark"};
+    return $result;
 };
 
-$images = $search("$query");
+$result = $search($query, $bookmark);
+
+if ($result->bookmark != null)
+{
+    $query_encoded = urlencode($query);
+    $bookmark_encoded = urlencode($result->bookmark);
+    $csrftoken_encoded = urlencode($csrftoken);
+    echo "<h2 style=\"text-align: center;\"><a href=\"/search.php?q=$query_encoded&bookmark=$bookmark_encoded&csrftoken=$csrftoken_encoded\">Next page</a></h2><br><br><br>";
+}
 
 include "misc/footer.php";