Guest User

Untitled

a guest
Dec 1st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. const alis = require("alis")
  2. const path = require("path")
  3.  
  4. require("dotenv").config({ path: path.resolve(__dirname, "../.env") })
  5. const tip_divider = 1000000000000000000
  6.  
  7. // 数字を3桁に0で整形
  8. function pad_number(num) {
  9. let text = num.toString()
  10. if (text.length < 3) {
  11. let len = text.length
  12. for (let i = 0; i < 3 - len; i++) {
  13. text = "0" + text
  14. }
  15. }
  16. return text
  17. }
  18. // 文字列を任意の文字数にスペースで整形
  19. function pad(text, num) {
  20. text = text.toString()
  21. if (text.length < num) {
  22. let len = text.length
  23. for (let i = 0; i < num - len; i++) {
  24. text += " "
  25. }
  26. }
  27. return text
  28. }
  29. async function getTip() {
  30. let tips = {}
  31. let users = {}
  32. let total_amount = 0
  33. let max_user_id_length = 0
  34. // 通知APIから重複がないように投げ銭だけ記録(APIでページをまたいで重複通知が返ってくることがあったため)
  35. let json = await alis.p.me.notifications(
  36. { limit: 100 },
  37. {
  38. username: process.env.ALIS_USERNAME,
  39. password: process.env.ALIS_PASSWORD,
  40. getAllSync: json => {
  41. for (let v of json.Items || []) {
  42. if (
  43. v.type === "tip" &&
  44. typeof tips[v.notification_id] === "undefined"
  45. ) {
  46. let amount = v.tip_value / tip_divider
  47. tips[v.notification_id] = v
  48. if (typeof users[v.acted_user_id] === "undefined") {
  49. users[v.acted_user_id] = 0
  50. if (v.acted_user_id.length > max_user_id_length) {
  51. max_user_id_length = v.acted_user_id.length
  52. }
  53. }
  54. users[v.acted_user_id] += amount
  55. total_amount += amount
  56. }
  57. }
  58. return false
  59. }
  60. }
  61. )
  62. let users_array = []
  63. for (let k in users) {
  64. users_array.push({ user_id: k, tip: users[k] })
  65. }
  66.  
  67. // 投げ銭額順に並び替え
  68. users_array.sort((a, b) => {
  69. return b.tip - a.tip
  70. })
  71.  
  72. // 書き出し処理
  73. let index = 0
  74. // 同額の時にindexをプールする
  75. let index_pool = 0
  76. let last_amount = null
  77. console.log(`${pad(`Rank`, 7)}${pad(`user_id`, max_user_id_length + 2)}ALIS`)
  78. console.log(`------------------------------------------`)
  79. for (let v of users_array) {
  80. if (last_amount === null || last_amount !== v.tip) {
  81. index += 1 + index_pool
  82. index_pool = 0
  83. last_amount = v.tip
  84. } else {
  85. index_pool += 1
  86. }
  87. console.log(
  88. `${pad(`[${pad_number(index)}]`, 7)}${pad(
  89. v.user_id,
  90. max_user_id_length + 2
  91. )}${v.tip}`
  92. )
  93. }
  94. console.log(`------------------------------------------`)
  95. console.log(`[Total] ${total_amount} ALIS`)
  96. }
  97.  
  98. getTip()
  99. .then(res => {
  100. process.exit()
  101. })
  102. .catch(e => {
  103. console.log(e)
  104. process.exit()
  105. })
Add Comment
Please, Sign In to add comment