Pixiv Mio Rank

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

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

// ==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);
})();