Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const alis = require("alis")
- const path = require("path")
- require("dotenv").config({ path: path.resolve(__dirname, "../.env") })
- const tip_divider = 1000000000000000000
- // 数字を3桁に0で整形
- function pad_number(num) {
- let text = num.toString()
- if (text.length < 3) {
- let len = text.length
- for (let i = 0; i < 3 - len; i++) {
- text = "0" + text
- }
- }
- return text
- }
- // 文字列を任意の文字数にスペースで整形
- function pad(text, num) {
- text = text.toString()
- if (text.length < num) {
- let len = text.length
- for (let i = 0; i < num - len; i++) {
- text += " "
- }
- }
- return text
- }
- async function getTip() {
- let tips = {}
- let users = {}
- let total_amount = 0
- let max_user_id_length = 0
- // 通知APIから重複がないように投げ銭だけ記録(APIでページをまたいで重複通知が返ってくることがあったため)
- let json = await alis.p.me.notifications(
- { limit: 100 },
- {
- username: process.env.ALIS_USERNAME,
- password: process.env.ALIS_PASSWORD,
- getAllSync: json => {
- for (let v of json.Items || []) {
- if (
- v.type === "tip" &&
- typeof tips[v.notification_id] === "undefined"
- ) {
- let amount = v.tip_value / tip_divider
- tips[v.notification_id] = v
- if (typeof users[v.acted_user_id] === "undefined") {
- users[v.acted_user_id] = 0
- if (v.acted_user_id.length > max_user_id_length) {
- max_user_id_length = v.acted_user_id.length
- }
- }
- users[v.acted_user_id] += amount
- total_amount += amount
- }
- }
- return false
- }
- }
- )
- let users_array = []
- for (let k in users) {
- users_array.push({ user_id: k, tip: users[k] })
- }
- // 投げ銭額順に並び替え
- users_array.sort((a, b) => {
- return b.tip - a.tip
- })
- // 書き出し処理
- let index = 0
- // 同額の時にindexをプールする
- let index_pool = 0
- let last_amount = null
- console.log(`${pad(`Rank`, 7)}${pad(`user_id`, max_user_id_length + 2)}ALIS`)
- console.log(`------------------------------------------`)
- for (let v of users_array) {
- if (last_amount === null || last_amount !== v.tip) {
- index += 1 + index_pool
- index_pool = 0
- last_amount = v.tip
- } else {
- index_pool += 1
- }
- console.log(
- `${pad(`[${pad_number(index)}]`, 7)}${pad(
- v.user_id,
- max_user_id_length + 2
- )}${v.tip}`
- )
- }
- console.log(`------------------------------------------`)
- console.log(`[Total] ${total_amount} ALIS`)
- }
- getTip()
- .then(res => {
- process.exit()
- })
- .catch(e => {
- console.log(e)
- process.exit()
- })
Add Comment
Please, Sign In to add comment