2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

User:机智的小鱼君/APIsandbox

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索

执行自动作业测试。

请求间隔:5 秒

Source
const nodemw = require('nodemw')
const secret = require('./secret')

const bot = new nodemw({
  ...secret.sites[0],
  ...secret.users[0],
  userAgent: secret.userAgent,
})

async function getToken(type = 'csrf') {
  return new Promise((res, rej) => {
    bot.getToken('', type, (err, token) => {
      if (err) {
        rej(err)
        return
      }
      res(token)
    })
  })
}

class mwApi {
  get(params) {
    return this.query(params, 'GET')
  }
  post(params) {
    return this.query(params, 'POST')
  }
  async postWithToken(token, params) {
    return this.query({ token: await getToken(token), ...params }, 'POST')
  }
  query(params, method = 'GET') {
    return new Promise((resolve, reject) => {
      bot.api.call(
        { format: 'json', ...params },
        (err, data) => {
          if (err) {
            reject(err)
            return
          }
          resolve(data)
        },
        method
      )
    })
  }
}

function login() {
  return new Promise((res) => bot.logIn(res))
}

function sleep(t = 0) {
  return new Promise((n) => setTimeout(n, t))
}

async function loopActions(total = 10, index = 0) {
  const count = index + 1
  const api = new mwApi()
  console.log(`[${count}/${total}] start`)
  const data = await api.postWithToken('csrf', {
    action: 'edit',
    title: 'User:机智的小鱼君/APIsandbox',
    appendtext: `\n\n这是第 '''${count}/${total}''' 次执行操作,时间是 ''${new Date().toISOString()}''`,
    summary: `[${count}/${total}] 自动程序测试`,
    tags: 'test',
  })
  console.log(`[${count}/${total}] done`, data)
  if (count >= total) return console.log('All done')
  console.log('sleep...')
  await sleep(5000)
  console.log('next loop')
  return loopActions(total, count)
}

!(async () => {
  await login()
  loopActions(15)
})()

这是第 1/15 次执行操作,时间是 2021-04-03T20:49:36.486Z

这是第 2/15 次执行操作,时间是 2021-04-03T20:50:02.531Z

这是第 3/15 次执行操作,时间是 2021-04-03T20:50:28.173Z

这是第 4/15 次执行操作,时间是 2021-04-03T20:50:53.870Z

这是第 5/15 次执行操作,时间是 2021-04-03T20:51:19.528Z

这是第 6/15 次执行操作,时间是 2021-04-03T20:51:45.073Z

这是第 7/15 次执行操作,时间是 2021-04-03T20:52:10.798Z

这是第 8/15 次执行操作,时间是 2021-04-03T20:52:36.559Z

这是第 9/15 次执行操作,时间是 2021-04-03T20:53:02.197Z

这是第 10/15 次执行操作,时间是 2021-04-03T20:53:28.030Z

这是第 11/15 次执行操作,时间是 2021-04-03T20:53:53.773Z

这是第 12/15 次执行操作,时间是 2021-04-03T20:54:19.422Z

这是第 13/15 次执行操作,时间是 2021-04-03T20:54:45.168Z

这是第 14/15 次执行操作,时间是 2021-04-03T20:55:10.775Z

这是第 15/15 次执行操作,时间是 2021-04-03T20:55:36.415Z