Skip to content

Commit

Permalink
feat: add more information
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Apr 25, 2024
1 parent 2bb74a9 commit 5d823b9
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 41 deletions.
40 changes: 25 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions scripts/run.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Client } from '../src/client';
import { printUsers } from '../src/output';
import { printUsers, toCSV } from '../src/output';

const ruid = '477317922';
const roomid = '21672023';

async function main() {
async function main(csv: boolean) {
const client = new Client(roomid, ruid);
const list = await client.get();
printUsers(list);

if (csv) {
console.log(toCSV(list));
} else {
printUsers(list);
}
}

main();
main(true);
37 changes: 31 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import * as core from '@actions/core';
import axios from 'axios';

import { User } from './types';
import type { User, Captain } from './types';

import { retry } from './utils';

type RawCaptain = Omit<Captain, 'level' | 'medal'> & {
guard_level: number;
medal_info: {
medal_name: string;
medal_level: number;
medal_color_start: number;
medal_color_end: number;
medal_color_border: number;
};
};

export class Client {
private readonly roomid: string;
private readonly ruid: string;
Expand Down Expand Up @@ -32,10 +44,10 @@ export class Client {
}
}

private async fetch(page: number): Promise<Array<User & { guard_level: number }>> {
private async fetch(page: number): Promise<RawCaptain[]> {
try {
const { data } = await retry(() =>
axios.get('https://api.live.bilibili.com/guard/topList', {
axios.get('https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList', {
params: {
roomid: this.roomid,
ruid: this.ruid,
Expand All @@ -54,7 +66,7 @@ export class Client {
}
}

async get(): Promise<User[]> {
async get(): Promise<Captain[]> {
const ans = [];
for (let i = 1; ; i++) {
const res = await this.fetch(i);
Expand All @@ -65,7 +77,20 @@ export class Client {
}

return ans
.map((u) => ({ uid: u.uid, username: u.username, level: u.guard_level }))
.sort((lhs, rhs) => (lhs.level ?? 3) - (rhs.level ?? 3));
.map((u) => ({
rank: u.rank,
uid: u.uid,
username: u.username,
level: u.guard_level,
accompany: u.accompany,
medal: {
name: u.medal_info.medal_name,
level: u.medal_info.medal_level,
colorStart: u.medal_info.medal_color_start,
colorEnd: u.medal_info.medal_color_end,
colorBorder: u.medal_info.medal_color_border
}
}))
.sort((lhs, rhs) => lhs.rank - rhs.rank);
}
}
27 changes: 13 additions & 14 deletions src/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';

import type { User } from './types';
import type { Captain } from './types';

import { padLeft } from './utils';

Expand All @@ -11,34 +11,33 @@ function getType(level?: number): string {
return '舰长';
}

export function printUsers(list: User[]) {
let cnt = 1;
export function printUsers(list: Captain[]) {
const width = String(list.length).length;
for (const user of list) {
const index = padLeft(String(cnt++), width);
const index = padLeft(String(user.rank), width);
const type = getType(user.level);
const accompany = user.accompany ? `, accompany: ${user.accompany}` : '';
core.info(`${index}. ${type} ${user.username} (uid: ${user.uid}${accompany})`);
const medal = `${padLeft(String(user.medal.level), 2)}${user.medal.name}`;
const accompany = `陪伴了主播 ${user.accompany} 天`;
core.info(`${index}. ${type} ${medal} ${user.username} (uid: ${user.uid}) ${accompany}`);
}
}

export function toCSV(list: User[]): string {
const text = ['rank,uid,username,type,accompany'];
let cnt = 1;
export function toCSV(list: Captain[]): string {
const text = ['rank,uid,username,type,accompany,medal_name,medal_level'];
for (const user of list) {
const type = getType(user.level);
text.push(
`${cnt++},${user.uid},${user.username},${getType(user.level)},${user.accompany ?? ''}`
`${user.rank},${user.uid},${user.username},${type},${user.accompany},${user.medal.name},${user.medal.level}`
);
}
return text.join('\n');
}

export function toMarkdown(list: User[]): string {
const text = ['|序号|UID|用户名|大航海|陪伴天数|', '|:-:|:-:|:-:|:-:|:-:|'];
let cnt = 1;
export function toMarkdown(list: Captain[]): string {
const text = ['|序号|UID|用户名|大航海|陪伴天数|粉丝牌|等级|', '|:-:|:-:|:-:|:-:|:-:|:-:|:-:|'];
for (const user of list) {
text.push(
`|${cnt++}|${user.uid}|${user.username}|${getType(user.level)}|${user.accompany ?? ''}|`
`|${user.rank}|${user.uid}|${user.username}|${getType(user.level)}|${user.accompany}|${user.medal.name}|${user.medal.level}|`
);
}
return text.join('\n');
Expand Down
32 changes: 30 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,44 @@ export interface User {
* Captain username (at fetched time)
*/
username: string;
}

export interface Captain {
/**
* Rank
*/
rank: number;

/**
* Captain uid
*/
uid: number;

/**
* Captain username (at fetched time)
*/
username: string;

/**
* Captain level: 3 for 舰长, 2 for 提督, 1 for 总督
*
* @default 3
*/
level?: number;
level: number;

/**
* Captain accompany length (number of days)
*/
accompany?: number;
accompany: number;

/**
* Medal information
*/
medal: {
name: string;
level: number;
colorStart: number;
colorEnd: number;
colorBorder: number;
};
}

0 comments on commit 5d823b9

Please sign in to comment.