Coder Social home page Coder Social logo

weibosyncfollow's Introduction

微博同步关注列表

A:待同步账号。B:需要同步的账号

起因

微博账号因为不当转发被封号,需要将关注列表导出到新的账号。

思路

比对A账号与B账号的关注,过滤出B的未关注账号列表,添加关注。

中间的一些坑

  1. 关注列表限制

    博主自保护策略,可能只能看到账号的部分关注列表。需要待同步账号登录账号获取。

  2. 微博关注接口并发限制

    接口需要设置延时,频率太快会触发微博的限流。

  3. 每日最大关注数量

    目测是150,超出后每次关注需要输入验证码

  4. 关注接口的请求头

    新版的微博关注接口`https://www.weibo.com/ajax/friendships/create 请求头必须增加xsrf token,否则会报403。

    如果使用旧版的微博关注接口https://weibo.com/aj/f/followed,则不需要。

操作步骤

有两种方案对应两种方法

方案1

  • 优点:不需要登录A账号,操作步骤少

  • 缺点:由于接口限制,可能无法获得完整的关注列表进行关注。

方案2:

  • 优点:可以获得完整的关注列表关注。

  • 缺点:需要登录A账号,操作步骤多。

方案1

  1. 前往微博首页,登录B账号
  2. 进入A的首页。
  3. F12打开开发者工具,在网络中选择任意一个请求,找到请求头中的x-xsrf-token,复制后的内容,
  4. 在控制台粘贴以下代码,并填入上一步得到的token,回车执行代码,等待控制台提示成功。

由于接口限制可能无法获得完整的关注列表

let list = []
let token = ''
let key = 'followList'
const getFollowed = async (page = 1, list = []) => {
  const uid = window.location.pathname.split('/').pop()
  const res = await fetch(`/ajax/friendships/friends?page=${page}&uid=${uid}`, {
    method: 'GET',
    headers: {
      "content-type": 'application/json'
    }
  }).then(res => res.json())
  if (res.users.length > 0) {
    list.push(...res.users)
    await getFollowed(++page, list)
  }
  return list
}

function sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

const batchFollow = async (list = [], index = 0) => {
  const followUser = list[index]
  await fetch(`/ajax/friendships/create`, {
    method: 'POST',
    headers: {
      "content-type": 'application/json;charset=UTF-8',
      "accept": "application/json, text/plain, */*",
      "x-xsrf-token": token
    },
    body: JSON.stringify({
      friend_uid: followUser.id,
      lpage: "profile",
      page: "profile"
    }
    )
  }).then(res => res.json()).then((res) => {
    console.log(`[${++index}/${list.length}],${res.name},关注成功`)
  })
  if (list.length > 0) {
    await sleep(1000)
    await batchFollow(list, index)
  }
  return list
}

const start = async (t) => {
  token = t
  list = (await getFollowed()).filter(item => !item.following)
  console.log(`获取列表成功,共${list.length}个`);
  batchFollow(list)
}
start("(复制的token)") //例如:start("ApMq0KHPGo3SBclIGe6dMpn7")

方案2

todo:过滤关注过的用户

  1. 前往微博首页,登录A账号
  2. F12打开开发者工具,在控制台输入以下代码,等待控制台提示成功。
const key = 'followList'
const getOwnFollowed = async (page = 1, list = []) => {
  const res = await fetch(`/ajax/profile/followContent?page=${page}`, {
    method: 'GET',
    headers: {
      "content-type": 'application/json'
    }
  }).then(res => res.json())
  const users = res.data.follows.users
  if (users.length > 0) {
    list.push(...users)
    // await sleep(500)
    await getOwnFollowed(++page, list)
  }
  return list
}
const start = async () => {
  const list = await getOwnFollowed()
  localStorage.setItem('followList', JSON.stringify(list))
  console.log(`获取列表成功,共${list.length}个`);
}
start()
  1. 退出 A 账号,登录 B 账号
  2. F12打开开发者工具,在网络中选择任意一个请求,找到请求头中的x-xsrf-token,复制后的内容,
  3. 在控制台粘贴以下代码,并填入上一步得到的token,回车执行代码,等待控制台提示成功。
let token = ''
function sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

const getOwnFollowed = async (page = 1, list = []) => {
  const res = await fetch(`/ajax/profile/followContent?page=${page}`, {
    method: 'GET',
    headers: {
      "content-type": 'application/json'
    }
  }).then(res => res.json())
  const users = res.data.follows.users
  if (users.length > 0) {
    list.push(...users)
    await getOwnFollowed(++page, list)
  }
  return list
}

const batchFollow = async (list = [], index = 0) => {
  const followUser = list[index++]
  await fetch(`/ajax/friendships/create`, {
    method: 'POST',
    headers: {
      "content-type": 'application/json;charset=UTF-8',
      "accept": "application/json, text/plain, */*",
      "x-xsrf-token": token
    },
    body: JSON.stringify({
      friend_uid: followUser.id,
      lpage: "profile",
      page: "profile"
    }
    )
  }).then(res => res.json()).then(async (res) => {
    console.log(`[${index + 1}/${list.length}],${res.name},关注成功`)
    if (list.length > 0) {
      await sleep(2000)
      await batchFollow(list, index)
    }
  })
  return list
}

const start = async (t) => {
  token = t
  const list = await getOwnFollowed()
  let unFollowList = JSON.parse(localStorage.getItem('followList')).filter(unFollowItem => list.findIndex(item => item.id === unFollowItem.id) === -1)
  console.log(`获取待关注列表成功,共${unFollowList.length}个`);
  await batchFollow(unFollowList)
}

start("(复制的token)") //例如:start("ApMq0KHPGo3SBclIGe6dMpn7")

weibosyncfollow's People

Contributors

lxw15337674 avatar nondanee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.