add stats

This commit is contained in:
xtaodada 2023-09-14 23:09:42 +08:00
parent dfd93d89e2
commit 0b8505080f
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 303 additions and 1 deletions

View File

@ -1 +1,176 @@
main.svelte-cd7t1.svelte-cd7t1{margin:1em}button.svelte-cd7t1.svelte-cd7t1{white-space:nowrap}#searchbox.svelte-cd7t1.svelte-cd7t1{display:flex;margin-left:1px}#searchbox.svelte-cd7t1>*{margin-left:-1px}#searchbox.svelte-cd7t1 input[type="search"].svelte-cd7t1{flex-grow:1}@media(max-width: 700px){#searchbox.svelte-cd7t1.svelte-cd7t1{flex-direction:column}}.error.svelte-cd7t1.svelte-cd7t1{color:red;text-align:center}.info.svelte-cd7t1.svelte-cd7t1{display:flex;justify-content:center}.info.svelte-cd7t1>.svelte-cd7t1{padding:0.5em 1em;margin:1em 0}.info.svelte-cd7t1>p.svelte-cd7t1{border:1px #bfbfbf solid;border-radius:2em}input,button,select{border-radius:0;border:1px solid var(--color-inactive);height:2.3em}input:focus,button:focus,select:focus{border-color:var(--color-active);outline:1px solid var(--color-active);z-index:10}:root{--color-inactive:#bfbfbf;--color-active:#add8e6}.message.svelte-15hb0c4.svelte-15hb0c4{margin:1em 0;padding:0.5em;max-width:max-content;border:1px #eeeeee solid;box-shadow:0 0 3px gray;border-radius:5px;display:flex}.avatar.svelte-15hb0c4.svelte-15hb0c4{padding-right:0.5em}.content.svelte-15hb0c4.svelte-15hb0c4{overflow:hidden}.name.svelte-15hb0c4.svelte-15hb0c4{white-space:nowrap;color:#1e90ff;overflow:hidden;text-overflow:ellipsis}.text.svelte-15hb0c4.svelte-15hb0c4{white-space:pre-wrap;margin:0.2em 0}.text.svelte-15hb0c4>.keyword{background-color:#ffffab}.time.svelte-15hb0c4.svelte-15hb0c4{font-size:0.75em;float:right;margin-right:2px}.time.svelte-15hb0c4.svelte-15hb0c4,.time.svelte-15hb0c4>a.svelte-15hb0c4{color:gray}div.svelte-179qc5o.svelte-179qc5o{position:relative}ul.svelte-179qc5o.svelte-179qc5o{list-style:none;padding:0;margin:0;position:absolute;left:0;background-color:white;box-shadow:0 0 4px var(--color-inactive);clip-path:polygon(-100% 0, 200% 0, 200% 200%, -100% 200%)}li.svelte-179qc5o.svelte-179qc5o{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:pointer;line-height:2em}li.svelte-179qc5o>img.svelte-179qc5o{height:calc(2.3em - 2px);width:calc(2.3em - 2px);vertical-align:middle;padding-right:0.5em;display:inline-block;overflow:hidden}ul.svelte-179qc5o:not(:hover)>li.selected.svelte-179qc5o,li.svelte-179qc5o.svelte-179qc5o:hover{background-color:#d9f5ff}input.svelte-179qc5o.svelte-179qc5o{padding-left:2.5em;width:100%}input.svelte-179qc5o.svelte-179qc5o,ul.svelte-179qc5o.svelte-179qc5o{border-radius:0;border:1px solid var(--color-inactive)}input.svelte-179qc5o.svelte-179qc5o:focus,input.svelte-179qc5o:focus~ul.svelte-179qc5o{border-color:var(--color-active);box-shadow:0 0 4px var(--color-active);outline:1px solid var(--color-active)}input.svelte-179qc5o:focus~ul.svelte-179qc5o{border-top-color:var(--color-inactive)}.selected-avatar.svelte-179qc5o.svelte-179qc5o{position:absolute;pointer-events:none;top:1px;left:1px;height:calc(2.3em - 2px);width:calc(2.3em - 2px)}.hidden.svelte-179qc5o.svelte-179qc5o{display:none}
main.svelte-cd7t1.svelte-cd7t1 {
margin: 1em
}
button.svelte-cd7t1.svelte-cd7t1 {
white-space: nowrap
}
#searchbox.svelte-cd7t1.svelte-cd7t1 {
display: flex;
margin-left: 1px
}
#searchbox.svelte-cd7t1 > * {
margin-left: -1px
}
#searchbox.svelte-cd7t1 input[type="search"].svelte-cd7t1 {
flex-grow: 1
}
@media (max-width: 700px) {
#searchbox.svelte-cd7t1.svelte-cd7t1 {
flex-direction: column
}
}
.error.svelte-cd7t1.svelte-cd7t1 {
color: red;
text-align: center
}
.info.svelte-cd7t1.svelte-cd7t1 {
display: flex;
justify-content: center
}
.info.svelte-cd7t1 > .svelte-cd7t1 {
padding: 0.5em 1em;
margin: 1em 0
}
.info.svelte-cd7t1 > p.svelte-cd7t1 {
border: 1px #bfbfbf solid;
border-radius: 2em
}
input, button, select {
border-radius: 0;
border: 1px solid var(--color-inactive);
height: 2.3em
}
input:focus, button:focus, select:focus {
border-color: var(--color-active);
outline: 1px solid var(--color-active);
z-index: 10
}
:root {
--color-inactive: #bfbfbf;
--color-active: #add8e6
}
.message.svelte-15hb0c4.svelte-15hb0c4 {
margin: 1em 0;
padding: 0.5em;
max-width: max-content;
border: 1px #eeeeee solid;
box-shadow: 0 0 3px gray;
border-radius: 5px;
display: flex
}
.avatar.svelte-15hb0c4.svelte-15hb0c4 {
padding-right: 0.5em
}
.content.svelte-15hb0c4.svelte-15hb0c4 {
overflow: hidden
}
.name.svelte-15hb0c4.svelte-15hb0c4 {
white-space: nowrap;
color: #1e90ff;
overflow: hidden;
text-overflow: ellipsis
}
.text.svelte-15hb0c4.svelte-15hb0c4 {
white-space: pre-wrap;
margin: 0.2em 0
}
.text.svelte-15hb0c4 > .keyword {
background-color: #ffffab
}
.time.svelte-15hb0c4.svelte-15hb0c4 {
font-size: 0.75em;
float: right;
margin-right: 2px
}
.time.svelte-15hb0c4.svelte-15hb0c4, .time.svelte-15hb0c4 > a.svelte-15hb0c4 {
color: gray
}
div.svelte-179qc5o.svelte-179qc5o {
position: relative
}
ul.svelte-179qc5o.svelte-179qc5o {
list-style: none;
padding: 0;
margin: 0;
position: absolute;
left: 0;
background-color: white;
box-shadow: 0 0 4px var(--color-inactive);
clip-path: polygon(-100% 0, 200% 0, 200% 200%, -100% 200%)
}
li.svelte-179qc5o.svelte-179qc5o {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
line-height: 2em
}
li.svelte-179qc5o > img.svelte-179qc5o {
height: calc(2.3em - 2px);
width: calc(2.3em - 2px);
vertical-align: middle;
padding-right: 0.5em;
display: inline-block;
overflow: hidden
}
ul.svelte-179qc5o:not(:hover) > li.selected.svelte-179qc5o, li.svelte-179qc5o.svelte-179qc5o:hover {
background-color: #d9f5ff
}
input.svelte-179qc5o.svelte-179qc5o {
padding-left: 2.5em;
width: 100%
}
input.svelte-179qc5o.svelte-179qc5o, ul.svelte-179qc5o.svelte-179qc5o {
border-radius: 0;
border: 1px solid var(--color-inactive)
}
input.svelte-179qc5o.svelte-179qc5o:focus, input.svelte-179qc5o:focus ~ ul.svelte-179qc5o {
border-color: var(--color-active);
box-shadow: 0 0 4px var(--color-active);
outline: 1px solid var(--color-active)
}
input.svelte-179qc5o:focus ~ ul.svelte-179qc5o {
border-top-color: var(--color-inactive)
}
.selected-avatar.svelte-179qc5o.svelte-179qc5o {
position: absolute;
pointer-events: none;
top: 1px;
left: 1px;
height: calc(2.3em - 2px);
width: calc(2.3em - 2px)
}
.hidden.svelte-179qc5o.svelte-179qc5o {
display: none
}

113
html/stats.html Normal file
View File

@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>发言排行榜 - 星穹铁道</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.1/css/bootstrap.min.css"
integrity="sha512-Z/def5z5u2aR89OuzYcxmDJ0Bnd5V1cKqBEbvLOiUNWdg9PQeXVvXLI90SE4QOHGlfLqUnDNVAYyZi8UwUTmWQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<style>
body {
background-color: #222;
}
.container {
text-align: center;
color: #fff;
margin-top: 50px;
}
.avatar {
width: 50px;
height: 50px;
border-radius: 50%;
margin-right: 10px;
}
.user-info {
display: flex;
align-items: center;
justify-content: flex-start;
margin-bottom: 10px;
padding: 10px;
background-color: #333;
}
.user-info .user-name {
display: flex;
flex-grow: 1;
align-items: center;
margin-right: 10px;
}
.user-info .user-name span {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
.username {
color: #1e90ff;
}
</style>
</head>
<body>
<div class="container">
<h1>发言排行榜</h1>
<br/>
<h6 id="gname"></h6>
<br/>
<div class="row">
<div class="col" id="col">
</div>
</div>
</div>
<script>
const gdata = {
"-1001797471403": "崩坏:星穹铁道 · 仙舟:罗浮",
}
const gname = document.getElementById("gname")
const gid = location.search.split("=")[1]
gname.innerText = gdata[gid]
const col = document.getElementById("col")
function createUserDom(data) {
let userDom = document.createElement("div")
userDom.className = "user-info"
let avatar = document.createElement("img")
avatar.className = "avatar"
avatar.src = `luoxu/avatar/${data.user_id}.jpg`
let userInfo = document.createElement("div")
let userName = document.createElement("div")
userName.className = "user-name username"
let userNameSpan = document.createElement("span")
userNameSpan.innerText = data.name
let userMessage = document.createElement("div")
userMessage.className = "user-name"
let userMessageSpan = document.createElement("span")
userMessageSpan.innerText = `${data.messages} 条消息,平均每条消息 ${data.avg_chars} 个字符`
userName.appendChild(userNameSpan)
userMessage.appendChild(userMessageSpan)
userInfo.appendChild(userName)
userInfo.appendChild(userMessage)
userDom.appendChild(avatar)
userDom.appendChild(userInfo)
return userDom
}
fetch(`luoxu/stats/${gid}`)
.then(res => res.json())
.then(data => {
data.forEach(user => {
col.appendChild(createUserDom(user))
})
})
</script>
</body>
</html>

14
main.py
View File

@ -7,6 +7,7 @@ from starlette.staticfiles import StaticFiles
app = FastAPI()
local_api = "http://127.0.0.1:9008/luoxu/"
local_stats_api = "http://127.0.0.1:9010/api/"
allowed_origins = [
"search-pub.xtaolabs.com",
"127.0.0.1:9009",
@ -19,6 +20,7 @@ app.add_middleware(
allow_headers=["*"],
)
groups_id = [1366383997, 1797471403, 1936247070]
groups_stats_id = [-1001797471403]
groups = {
"groups": [
{
@ -76,6 +78,18 @@ async def read_avatar(avatar: str) -> StreamingResponse:
return StreamingResponse(r.aiter_bytes(), media_type="image/png")
@app.get("/luoxu/stats/{gid}")
async def read_avatar(gid: str):
try:
if int(gid) not in groups_stats_id:
return []
except Exception:
return []
async with AsyncClient(timeout=60) as client:
r = await client.get(f"{local_stats_api}group_analytics?cid={gid}")
return r.json()
@app.get("/")
async def redirect_to_html():
return RedirectResponse(url="/index.html")