Last active
October 14, 2022 06:44
-
-
Save int64ago/19ccf185dccb984ed6c2e0ab637e2574 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Juejin Lucky | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description 从关注 网易云音乐大前端团队 并且在 https://juejin.im/pin/6871042321472421902 沸点下回复的用户中去重后随机抽出 25 个,并分 3 组 | |
// @author Cody | |
// @match https://juejin.im/pin/6871042321472421902 | |
// @grant none | |
// ==/UserScript== | |
const UID = '4265760847567016'; | |
const ITEM_ID = '6871042321472421902'; | |
const CLIENT_TYPE = 2608; | |
const ITEM_TYPE = 4; | |
const COUNT = 25; | |
const HOT_TOPIC_COMMENT_URL = 'https://apinew.juejin.im/interact_api/v1/comment/list'; | |
const FOLLOWER_URL = 'https://apinew.juejin.im/user_api/v1/follow/followers'; | |
/** | |
* Fisher–Yates shuffle | |
*/ | |
Array.prototype.shuffle = function () { | |
const input = this; | |
for (let i = input.length - 1; i >= 0; i--) { | |
const randomIndex = Math.floor(Math.random() * (i + 1)); | |
const itemAtIndex = input[randomIndex]; | |
input[randomIndex] = input[i]; | |
input[i] = itemAtIndex; | |
} | |
return input; | |
}; | |
async function getAllComments(cur = '0') { | |
const { count, cursor, has_more, data } = await fetch(HOT_TOPIC_COMMENT_URL, { | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
cursor: cur, | |
client_type: CLIENT_TYPE, | |
item_id: ITEM_ID, | |
item_type: ITEM_TYPE, | |
limit: 20, | |
}), | |
}).then(res => res.json()); | |
console.log(`获取沸点评论进度:${cursor}/${count}`); | |
if (!has_more) return data; | |
const others = await getAllComments(cursor); | |
return [...data, ...others]; | |
} | |
async function getAllFollowers(cur = '0') { | |
const { data: { count, cursor, hasMore, data } } = await fetch(`${FOLLOWER_URL}?user_id=${UID}&cursor=${cur}&limit=20`).then(res => res.json()); | |
console.log(`获取关注者进度:${cursor}/${count}`); | |
if (!hasMore) return data; | |
const others = await getAllFollowers(cursor); | |
return [...data, ...others]; | |
} | |
(async () => { | |
const t = new Date(); | |
console.log(`当前时间:${t.toLocaleDateString()} ${t.toLocaleTimeString()}`); | |
console.log('正在获取沸点评论……'); | |
const comments = await getAllComments(); | |
console.log('沸点评论数:', comments.length); | |
const commentUsers = comments.map(comment => comment.user_info.user_name); | |
console.log('正在获取关注者……'); | |
const followers = await getAllFollowers(); | |
console.log('关注者数:', followers.length); | |
const followerUsers = followers.map(follow => follow.user_name); | |
const validUsers = Array.from(new Set(commentUsers)).filter(user => followerUsers.includes(user)); | |
console.log('有效参与抽奖用户数:', validUsers.length); | |
const luckyUsers = validUsers.shuffle().slice(0, COUNT); | |
console.log(` | |
----- 中奖用户 A 组 ------ | |
${luckyUsers.slice(0, 10).join('\n')} | |
----- 中奖用户 B 组 ------ | |
${luckyUsers.slice(10, 15).join('\n')} | |
----- 中奖用户 C 组 ------ | |
${luckyUsers.slice(15, 25).join('\n')} | |
`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个代码写得也太好看了吧,什么神仙代码,心情都好了