爱发电赞助名单生成脚本

2019-5-28 22:35:02

自从bmclapi赞助开始用起爱发电,就一直在手动维护赞助名单,随着名单越来越长,每次维护名单的时候都要手动添加然后人肉去重。问了一下官方,也暂时没有打算做webhook。
每次手动维护名单实在好烦,于是花几分钟研究了一下赞助名单接口,又花了几分钟糊了个脚本,来自动生成赞助名单的markdown。
以下代码只要打开爱发电的网页,登陆后打开开发者工具,粘贴执行即可得到对应的赞助名单

!async function(){const s="https://afdian.net/api/my/sponsored-bill?type=old",t=[];let a=s;for(;;){console.log(t.length);const n=await fetch(a,{method:"get"}),e=await n.json();for(const s of e.data.list)t.unshift({name:s.user.name,url:s.user.url_slug?`https://afdian.net/@${s.user.url_slug}`:`https://afdian.net/u/${s.user.user_id}`});if(a=`${s}&pay_success_sn=${e.data.list[e.data.list.length-1].pay_success_sn}`,0===e.data.has_more)break}const n=t.map(s=>`- [${s.name}](${s.url})`).join("\n");console.log(n)}();

附上typescript源码

interface Response {
  ec: number
  em: string
  data: {
    has_more: 0 | 1
    list: Array<{
      pay_success_sn: number,
      user: {
        name: string,
        user_id: string,
        url_slug: string,
      }
    }>
  }
}

interface Result {
  name: string
  url: string
}

(async function main() {
  const defaultUrl = 'https://afdian.net/api/my/sponsored-bill?type=old'
  const list: Result[] = []

  let url = defaultUrl
  while (true) {
    console.log(list.length)
    const res = await fetch(url, {
      method: 'get',
    })
    const body = await res.json() as Response
    for (const trade of body.data.list) {
      list.unshift({
        name: trade.user.name,
        url: trade.user.url_slug ? `https://afdian.net/@${trade.user.url_slug}` : `https://afdian.net/u/${trade.user.user_id}`
      })
    }
    url = `${defaultUrl}&pay_success_sn=${body.data.list[body.data.list.length - 1].pay_success_sn}`
    if (body.data.has_more === 0) break
  }
  const data = list.map((e) => `- [${e.name}](${e.url})`).join('\n')
  console.log(data)
})()