微博备份

炸号微博备份

目前为 2022-05-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         微博备份
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  炸号微博备份
// @author       fun
// @match        https://weibo.com
// @icon         https://www.google.com/s2/favicons?sz=64&domain=weibo.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  let btn = document.createElement("button");

  async function fetchContent(uid = 0, page = 1, since_id = null) {
    const req = await fetch(
      `https://weibo.com/ajax/statuses/mymblog?uid=${uid}&page=${page}&feature=0&` +
        (since_id ? "since_id=" + since_id : ""),
      {
        headers: {
          accept: "application/json, text/plain, */*",
          "accept-language": "zh-CN,zh;q=0.9,en-IN;q=0.8,en;q=0.7,ar;q=0.6",
          "sec-ch-ua":
            '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
          "sec-ch-ua-mobile": "?0",
          "sec-ch-ua-platform": '"macOS"',
          "sec-fetch-dest": "empty",
          "sec-fetch-mode": "cors",
          "sec-fetch-site": "same-origin",
          "x-requested-with": "XMLHttpRequest",
        },
        referrer: `https://weibo.com/u/${uid}`,
        referrerPolicy: "strict-origin-when-cross-origin",
        body: null,
        method: "GET",
        mode: "cors",
        credentials: "include",
      }
    );
    const data = await req.json();
    return data;
  }

  async function fetchAll() {
    var uid = $CONFIG.uid;
    let page = 1;
    let allPageData = [];
    let noMore = false;
    for (let index = 0; index < Infinity; index++) {
      console.log("scan", "page", page);
      printLog(`备份第 ${page} 页`);
      for (let index = 0; index < 10; index++) {
        const pageData = await fetchContent(uid, page);
        if (pageData.ok) {
          allPageData.push(pageData);
          if (pageData.data.list.length === 0) noMore = true;
          break;
        }
        await new Promise((resolve) => {
          setTimeout(resolve, 3000);
        });
        console.log("retry", index);
        printLog(`[重试]备份第 ${page} 页`);
      }
      page++;
      if (noMore) break;
      await new Promise((resolve) => {
        setTimeout(resolve, 3000);
      });
    }
    console.log("all done");
    printLog(`备份完毕`);
    const parsed = allPageData.reduce((all, item) => {
      item.data.list.forEach((c) => {
        const formatted = {
          images: c.pic_ids.map((d) => {
            return c.pic_infos[d].large.url;
          }),
          text: c.text,
          created_at: c.created_at,
          //   raw: c,
        };
        if (c.retweeted_status) {
          formatted.retweeted_status = {
            text: c.retweeted_status.text,
            images:
              c.retweeted_status.pic_ids &&
              c.retweeted_status.pic_ids.map((d) => {
                return c.retweeted_status.pic_infos[d].large.url;
              }),
          };
        }
        all.push(formatted);
      });
      return all;
    }, []);
    console.log("data", allPageData, parsed);
    document.body.innerText = JSON.stringify(parsed);
  }

  function printLog(msg) {
    btn.innerText = msg;
  }

  btn.innerHTML = "数据备份";
  document.body.appendChild(btn);
  btn.setAttribute(
    "style",
    `position: fixed;
    top: 20px;
    right: 20px;
    z-index: 100000;
    padding: 7px 15px;
    cursor: pointer;`
  );

  let started = false;

  btn.addEventListener("click", async () => {
    if (started) {
      alert("started");
      return;
    }
    started = true;
    await fetchAll();
    started = false;
  });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址