Pixiv Mio Rank

获取pixiv的排行数据,并新增到mio

目前為 2024-03-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Pixiv Mio Rank
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @license      MIT
// @description  获取pixiv的排行数据,并新增到mio
// @author       kasuie
// @match        https://www.pixiv.net/ranking.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @grant       GM.xmlHttpRequest
// @run-at      document-end
// ==/UserScript==

(function () {
  "use strict";

  const format = (v, date, mode, uid, uploadName) => {
    let tags = v?.tags || [];
    let pageCount = +v.illust_page_count;
    let pathDate = null,
      pixAvatar = null,
      exts = [];
    if (v.attr == "original" && !tags.includes("original")) {
      tags.push("原创");
      tags.push("original");
    }
    if (tags?.length) {
      tags = tags.filter((vv) => {
        return (
          vv &&
          !vv.includes("收藏") &&
          !vv.includes("users") &&
          !vv.includes("bookmarks") &&
          !vv.includes("Bookmarks") &&
          !vv.includes("R-18")
        );
      });
    }
    const matches = v.url.match(
      /\/(\d{4}\/\d{2}\/\d{2}\/\d{2}\/\d{2}\/\d{2})\//
    );
    if (matches && matches[1]) {
      pathDate = matches[1];
    }
    const extArr = v.url?.split(".");
    if (extArr?.length) {
      const ext = extArr[extArr.length - 1];
      for (let index = 0; index < pageCount; index++) {
        exts.push(ext);
      }
    }
    pixAvatar = v.profile_img
      ?.replace("https://i.pximg.net", "")
      ?.replace("_50", "");

    return {
      pid: v.illust_id,
      uid: v.user_id,
      author:
        v.user_name
          ?.replace(/@(.*)/, "")
          ?.replace(/@(.*)/, "")
          ?.replace(/❤(.*)/, "")
          ?.replace(/■(.*)/, "")
          ?.replace(/▶(.*)/, "") || v.user_name,
      rankType: mode,
      tags: tags?.join(","),
      exts: exts?.join(","),
      pageCount: pageCount,
      title: v.title,
      datePath: pathDate,
      pixAvatar,
      width: v.width,
      height: v.height,
      aspectRatio: Math.round((v.width / v.height) * 1000) / 1000,
      createDate: new Date(
        new Date(v.illust_upload_timestamp * 1000).toLocaleString("chinese", {
          hour12: false,
        })
      ),
      viewCount: v.view_count,
      ratingCount: v.rating_count,
      illusType: +v.illust_type,
      uploadName: uploadName,
      uploadUid: uid,
      status: v?.is_bookmarked ? v.yes_rank - 101 : v.yes_rank,
      startDate: v.yes_rank == 0 ? `${date}_${v.yes_rank}:${v.rank}` : null,
      endDate: v.yes_rank > 0 ? `${date}_${v.yes_rank}:${v.rank}` : null,
    };
  };

  const btn = document.createElement("button");
  btn.style.position = "fixed";
  btn.style.right = 0;
  btn.style.top = "45%";
  btn.addEventListener("click", (_e) => {
    const urlParams = new URLSearchParams(window.location.search);
    const mode = urlParams.get("mode");
    const date = urlParams.get("date");
    let p = 1;
    let data = [];
    let url = `/ranking.php?format=json`;
    if (mode) {
      url = `${url}&mode=${mode}`;
    }
    if (date) {
      url = `${url}&date=${date}`;
    }
    const userDom = document.querySelector("div.sc-1asno00-0");
    const uploadName = userDom.getAttribute("title");
    const uid = null;
    GM.xmlHttpRequest({
      method: "GET",
      url: `${url}&p=${p}`,
      headers: {
        referer: "https://www.pixiv.net/",
        "Accept-Language:":
          "zh-CN,zh-CN;q=0.9,zh;q=0.8,en-US;q=0.7,en,en-CN;q=0.6",
      },
      onload({ responseText }) {
        const { contents: page1, date, mode } = JSON.parse(responseText) || {};
        console.log(`第${p}页数据:`, JSON.parse(responseText));
        if (page1?.length) {
          ++p;
          page1.forEach((ele) => {
            if (+ele?.illust_type == 0) {
              data.push(format(ele, date, mode, uid, uploadName));
            }
          });
          GM.xmlHttpRequest({
            method: "GET",
            url: `${url}&p=${p}`,
            headers: {
              referer: "https://www.pixiv.net/",
              "Accept-Language:":
                "zh-CN,zh-CN;q=0.9,zh;q=0.8,en-US;q=0.7,en,en-CN;q=0.6",
            },
            onload({ responseText }) {
              const {
                contents: page2,
                date,
                mode,
                prev_date,
                next_date,
              } = JSON.parse(responseText) || {};
              console.log(`第${p}页数据:`, JSON.parse(responseText));
              if (page2?.length) {
                page2.forEach((ele) => {
                  if (+ele?.illust_type == 0) {
                    data.push(format(ele, date, mode, uid, uploadName));
                  }
                });
                console.log("请求参数:", {
                  rankDate: date,
                  prevDate: prev_date,
                  nextDate: next_date || "",
                  rankType: mode,
                  uploadName: uploadName,
                  rankList: data,
                });
                GM.xmlHttpRequest({
                  method: "POST",
                  url: "https://kasuie.cc/apis/prank/newDate",
                  headers: { "Content-Type": "application/json" },
                  data: JSON.stringify({
                    rankDate: date,
                    prevDate: prev_date,
                    nextDate: next_date || "",
                    rankType: mode,
                    uploadName: uploadName,
                    rankList: data,
                  }),
                  onload({ responseText }) {
                    console.log("数据保存结果:", JSON.parse(responseText));
                  },
                });
              }
            },
          });
        }
      },
    });
  });
  btn.innerHTML = "开始执行";
  document.querySelector("body").appendChild(btn);
})();