This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2670445
Showing
44 changed files
with
21,230 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# WebStorm | ||
.idea/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
Version 2, December 2004 | ||
|
||
Copyright (C) 2020 kdxcxs <[email protected]> | ||
|
||
Everyone is permitted to copy and distribute verbatim or modified | ||
copies of this license document, and changing it is allowed as long | ||
as the name is changed. | ||
|
||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
|
||
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Bilibili Live Lottery | ||
|
||
Bilibili直播弹幕抽奖 | ||
|
||
## :bookmark_tabs:内容列表 | ||
|
||
- [背景](#背景) | ||
- [安装](#安装) | ||
- [使用说明](#使用说明) | ||
- [使用许可](#使用许可) | ||
|
||
## :triangular_flag_on_post:背景 | ||
|
||
许多主播在直播抽奖时都是通过截图来抽奖的,而我从没中过,想来想去越想越气,这肯定是因为截图这种方法不公平! | ||
|
||
于是就有了这个项目…… | ||
|
||
(然而我还是没中过) | ||
|
||
## :hourglass:安装 | ||
|
||
不需要安装~ | ||
|
||
就简单地用 [mdui](https://mdui.org/) 随便写了写,所有依赖都在 [libs](libs/) 文件夹中。 | ||
|
||
浏览器打开 [main.html](main.html) 即开即用:+1: | ||
|
||
## :bell:使用说明 | ||
|
||
1. 输入房间号后点击 `点击开始` 按钮开始记录发送弹幕的用户 uid 和昵称,不会重复记录。 | ||
2. 点击 `停止记录` 按钮后停止记录发送的弹幕。 | ||
3. 点击 `随机抽奖` 按钮在刚才记录过程中所有发送弹幕的用户里面随机抽取一位。 | ||
4. 关闭弹窗后点击中间粉色的中奖用户用户名可跳转到该用户的B站主页方便私信。 | ||
|
||
## :page_with_curl:使用许可 | ||
|
||
[WTFPL](LICENSE) :copyright: kdxcxs |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// cx@kdxcxs.com | ||
// |/ |¯\ \/ |¯ \/ |¯ | ||
// |¯\ |_/ /\ |_ /\ ¯| | ||
// ¯ | ||
|
||
var viewers = []; | ||
var viewerNames = []; | ||
var running = false; | ||
var allViewers = 0 | ||
function appendViewer(vd) { // viewer data | ||
if(running && !viewers.includes(vd[2][0])){ | ||
console.log(vd[2][0]); | ||
viewers.push(vd[2][0]); | ||
viewerNames.push(vd[2][1]); | ||
allViewers ++; | ||
$('#viewers-count')[0].innerText = allViewers; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// cx@kdxcxs.com | ||
// |/ |¯\ \/ |¯ \/ |¯ | ||
// |¯\ |_/ /\ |_ /\ ¯| | ||
// ¯ | ||
// https://github.com/lovelyyoshino/Bilibili-Live-API/blob/master/API.WebSocket.md | ||
|
||
const textEncoder = new TextEncoder('utf-8'); | ||
const textDecoder = new TextDecoder('utf-8'); | ||
|
||
const readInt = function (buffer, start, len) { | ||
let result = 0 | ||
for (let i = len - 1; i >= 0; i--) { | ||
result += Math.pow(256, len - i - 1) * buffer[start + i] | ||
} | ||
return result | ||
} | ||
|
||
const writeInt = function (buffer, start, len, value) { | ||
let i = 0 | ||
while (i < len) { | ||
buffer[start + i] = value / Math.pow(256, len - i - 1) | ||
i++ | ||
} | ||
} | ||
|
||
const encode = function (str, op) { | ||
let data = textEncoder.encode(str); | ||
let packetLen = 16 + data.byteLength; | ||
let header = [0, 0, 0, 0, 0, 16, 0, 1, 0, 0, 0, op, 0, 0, 0, 1] | ||
writeInt(header, 0, 4, packetLen) | ||
return (new Uint8Array(header.concat(...data))).buffer | ||
} | ||
const decode = function (blob) { | ||
return new Promise(function (resolve, reject) { | ||
let reader = new FileReader(); | ||
reader.onload = function (e) { | ||
let buffer = new Uint8Array(e.target.result) | ||
let result = {} | ||
result.packetLen = readInt(buffer, 0, 4) | ||
result.headerLen = readInt(buffer, 4, 2) | ||
result.ver = readInt(buffer, 6, 2) | ||
result.op = readInt(buffer, 8, 4) | ||
result.seq = readInt(buffer, 12, 4) | ||
if (result.op === 5) { | ||
result.body = [] | ||
let offset = 0; | ||
while (offset < buffer.length) { | ||
let packetLen = readInt(buffer, offset + 0, 4) | ||
let headerLen = 16// readInt(buffer,offset + 4,4) | ||
let data = buffer.slice(offset + headerLen, offset + packetLen); | ||
let body = textDecoder.decode(data); | ||
if (body) { | ||
result.body.push(JSON.parse(body)); | ||
} | ||
offset += packetLen; | ||
} | ||
} else if (result.op === 3) { | ||
result.body = { | ||
count: readInt(buffer, 16, 4) | ||
}; | ||
} | ||
resolve(result) | ||
} | ||
reader.readAsArrayBuffer(blob); | ||
}); | ||
} | ||
|
||
function connect(roomid) { | ||
window.ws = new WebSocket('wss://broadcastlv.chat.bilibili.com:2245/sub'); | ||
ws.onopen = function () { | ||
ws.send(encode(JSON.stringify({ | ||
roomid: roomid | ||
}), 7)); | ||
}; | ||
setInterval(function () { | ||
ws.send(encode('', 2)); | ||
}, 30000); | ||
ws.onmessage = async function (msgEvent) { | ||
const packet = await decode(msgEvent.data); | ||
switch (packet.op) { | ||
case 8: | ||
console.log('已加入房间'+roomid.toString()); | ||
break; | ||
case 5: | ||
packet.body.forEach((body) => { | ||
if (body.cmd === 'DANMU_MSG') { | ||
appendViewer(body.info); | ||
} | ||
}) | ||
break; | ||
} | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// cx@kdxcxs.com | ||
// |/ |¯\ \/ |¯ \/ |¯ | ||
// |¯\ |_/ /\ |_ /\ ¯| | ||
// ¯ | ||
|
||
console.log('%[email protected]\n|/ |¯\\ \\/ |¯ \\/ |¯ \n|¯\\ |_/ /\\ |_ /\\ ¯| \n ¯ ', 'color: blue; font-size: 20px'); | ||
|
||
// https://www.cnblogs.com/starof/p/4988516.html | ||
function randomNum(minNum, maxNum) { | ||
switch (arguments.length) { | ||
case 1: | ||
return parseInt(Math.random() * minNum + 1, 10); | ||
break; | ||
case 2: | ||
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); | ||
break; | ||
default: | ||
return 0; | ||
break; | ||
} | ||
} | ||
|
||
function enterRoom() { | ||
$('#room-choosing').hide(); | ||
$('#running-container').show(); | ||
$('#control-button')[0].onclick = startCatching; | ||
connect(parseInt($('#roomid-input')[0].value)); | ||
startCatching(); | ||
} | ||
|
||
function startCatching() { | ||
console.clear(); | ||
console.log('%[email protected]\n|/ |¯\\ \\/ |¯ \\/ |¯ \n|¯\\ |_/ /\\ |_ /\\ ¯| \n ¯ ', 'color: blue; font-size: 20px'); | ||
running = true; | ||
viewers = []; | ||
allViewers = 0; | ||
$('#viewers-count')[0].innerText = 0; | ||
$('#lottery-done').hide(); | ||
$('#running-container').show(); | ||
$('#running-status')[0].innerText = '开始记录!'; | ||
$('#control-button')[0].innerText = '停止记录'; | ||
$('#control-button')[0].onclick = stopCatching; | ||
} | ||
|
||
function stopCatching() { | ||
running = false; | ||
$('#running-status')[0].innerText = '准备抽奖'; | ||
$('#control-button')[0].innerText = '随机抽奖'; | ||
$('#control-button')[0].onclick = lottery; | ||
} | ||
|
||
function lottery() { | ||
$('#running-container').hide(); | ||
$('#lottery-done').show(); | ||
let luckyIndex = randomNum(1, viewers.length) - 1; | ||
$('#control-button')[0].innerText = '再抽一次'; | ||
$('#control-button')[0].onclick = startCatching; | ||
$('#lucky-user')[0].innerText = viewerNames[luckyIndex] + '(' + viewers[luckyIndex].toString() + ')'; | ||
$('#lucky-user')[0].onclick = () => { | ||
window.open('https://space.bilibili.com/' + viewers[luckyIndex].toString()); | ||
}; | ||
mdui.dialog({ | ||
title: '幸运儿出来了!', | ||
content: viewerNames[luckyIndex] + '(' + viewers[luckyIndex].toString() + ')', | ||
buttons: [ | ||
{ | ||
text: '确认' | ||
} | ||
] | ||
}); | ||
} |
Oops, something went wrong.