@dan-uni/dan-any 0.0.2 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,23 @@ export declare enum Pools {
128
128
  Ix = 3
129
129
  }
130
130
  export type ctime = string | number | bigint | Date;
131
+ export interface UniDMObj {
132
+ FCID: string;
133
+ progress: number;
134
+ mode: Modes;
135
+ fontsize: number;
136
+ color: number;
137
+ senderID: string;
138
+ content: string;
139
+ ctime: Date;
140
+ weight: number;
141
+ pool: Pools;
142
+ attr: DMAttr[];
143
+ platform: platfrom | string;
144
+ SPMO: string;
145
+ extra: string;
146
+ DMID: string;
147
+ }
131
148
  /**
132
149
  * 所有 number/bigint 值设为0自动转换为默认
133
150
  */
@@ -291,6 +308,7 @@ export declare class UniDM {
291
308
  * - Artplayer: style不为空时,将其JSON.stringify()存入
292
309
  */
293
310
  extraStr?: string | undefined, DMID?: string | undefined);
311
+ static create(args: Partial<UniDMObj>): UniDM;
294
312
  get extra(): Extra;
295
313
  get isFrom3rdPlatform(): boolean;
296
314
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dan-uni/dan-any",
3
- "version": "0.0.2",
4
- "description": "The toolbox of dan-uni.",
3
+ "version": "0.0.7",
4
+ "description": "A danmaku transformer lib, supporting danmaku from different platforms.",
5
5
  "keywords": [
6
6
  "bangumi",
7
7
  "danmaku"
package/src/index.test.ts CHANGED
@@ -3,9 +3,7 @@ import { describe, it } from 'vitest'
3
3
 
4
4
  import { UniPool } from './index'
5
5
 
6
- describe('转化自', () => {
7
- it('bili(xml)', () => {
8
- const xml = `<i>
6
+ const xml = `<i>
9
7
  <chatserver>chat.bilibili.com</chatserver>
10
8
  <chatid>1156756312</chatid>
11
9
  <mission>0</mission>
@@ -28,8 +26,10 @@ describe('转化自', () => {
28
26
  <d p="13.462,1,25,16777215,1686302133,0,3cab672c,1335558106620590080">哈哈哈</d>
29
27
  <d p="13.481,1,25,16777215,1686297342,0,ce67fafd,1335517923728804864">?</d>
30
28
  <d p="13.499,1,25,16777215,1686301548,3,2848bf1c,1335553202649003264">不喜欢</d>
31
- </i>`,
32
- pool = UniPool.fromBiliXML(xml)
29
+ </i>`
30
+ describe('转化自', () => {
31
+ it('bili(xml)', () => {
32
+ const pool = UniPool.fromBiliXML(xml)
33
33
  console.info(xml)
34
34
  console.info(pool)
35
35
  })
@@ -49,3 +49,13 @@ describe('转化自', () => {
49
49
  console.info(pool)
50
50
  })
51
51
  })
52
+
53
+ describe('共通值', () => {
54
+ const pool = UniPool.fromBiliXML(xml)
55
+ it('获取shared', () => {
56
+ console.info(pool.shared)
57
+ })
58
+ it('按pool分组', () => {
59
+ console.info(pool.split('pool'))
60
+ })
61
+ })
package/src/index.ts CHANGED
@@ -60,8 +60,79 @@ export interface DM_JSON_DDPlay {
60
60
  m: string
61
61
  }[]
62
62
  }
63
+
64
+ export type DM_format =
65
+ | 'danuni.json'
66
+ | 'danuni.bin'
67
+ | 'danuni.pb.zst'
68
+ | 'bili.xml'
69
+ | 'bili.bin'
70
+ | 'bili.cmd.bin'
71
+ | 'dplayer.json'
72
+ | 'artplayer.json'
73
+ | 'ddplay.json'
74
+
75
+ type shareItems = Partial<
76
+ Pick<
77
+ UniDMTools.UniDMObj,
78
+ 'FCID' | 'senderID' | 'platform' | 'SPMO' | 'pool' | 'mode'
79
+ >
80
+ >
81
+
63
82
  export class UniPool {
64
- constructor(public dans: UniDM[]) {}
83
+ readonly shared: shareItems = {}
84
+ constructor(public dans: UniDM[]) {
85
+ function isShared(key: keyof UniDMTools.UniDMObj) {
86
+ return new Set(dans.map((d) => d[key])).size === 1
87
+ }
88
+ if (isShared('FCID')) this.shared.FCID = dans[0].FCID
89
+ if (isShared('senderID')) this.shared.senderID = dans[0].senderID
90
+ if (isShared('platform')) this.shared.platform = dans[0].platform
91
+ if (isShared('SPMO')) this.shared.SPMO = dans[0].SPMO
92
+ if (isShared('pool')) this.shared.pool = dans[0].pool
93
+ if (isShared('mode')) this.shared.mode = dans[0].mode
94
+ }
95
+ static create() {
96
+ return new UniPool([])
97
+ }
98
+ assign(dans: UniPool | UniDM | UniDM[]) {
99
+ if (dans instanceof UniPool) {
100
+ return new UniPool([...this.dans, ...dans.dans])
101
+ } else if (dans instanceof UniDM) {
102
+ return new UniPool([...this.dans, dans])
103
+ } else if (Array.isArray(dans) && dans.every((d) => d instanceof UniDM)) {
104
+ return new UniPool([...this.dans, ...dans])
105
+ } else return this
106
+ }
107
+ split(key: keyof shareItems) {
108
+ if (this.shared[key]) return [this]
109
+ const set = new Set(this.dans.map((d) => d[key]))
110
+ return [...set].map((v) => {
111
+ return new UniPool(this.dans.filter((d) => d[key] === v))
112
+ })
113
+ }
114
+ convert2(format: DM_format) {
115
+ switch (format) {
116
+ case 'danuni.json':
117
+ return JSON.stringify(this.dans)
118
+ case 'danuni.bin':
119
+ return this.toPb()
120
+ // case 'bili.xml':
121
+ // return this.toBiliXML()
122
+ // case 'bili.bin':
123
+ // return this.toBiliBin()
124
+ // case 'bili.cmd.bin':
125
+ // return this.toBiliCmdBin()
126
+ case 'dplayer.json':
127
+ return this.toDplayer()
128
+ case 'artplayer.json':
129
+ return this.toArtplayer()
130
+ case 'ddplay.json':
131
+ return this.toDDplay()
132
+ default:
133
+ throw new Error('unknown format or unsupported now!')
134
+ }
135
+ }
65
136
  static fromPb(bin: Uint8Array | ArrayBuffer) {
66
137
  const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
67
138
  return new UniPool(
@@ -242,6 +242,24 @@ export type ctime = string | number | bigint | Date
242
242
  // dateStr = 'dateStr',
243
243
  // }
244
244
 
245
+ export interface UniDMObj {
246
+ FCID: string
247
+ progress: number
248
+ mode: Modes
249
+ fontsize: number
250
+ color: number
251
+ senderID: string
252
+ content: string
253
+ ctime: Date
254
+ weight: number
255
+ pool: Pools
256
+ attr: DMAttr[]
257
+ platform: platfrom | string
258
+ SPMO: string
259
+ extra: string
260
+ DMID: string
261
+ }
262
+
245
263
  /**
246
264
  * 所有 number/bigint 值设为0自动转换为默认
247
265
  */
@@ -342,6 +360,25 @@ export class UniDM {
342
360
  // if (attr < 0 || attr > 0b111) this.attr = 0
343
361
  if (!DMID) DMID = this.toDMID()
344
362
  }
363
+ static create(args: Partial<UniDMObj>) {
364
+ return new UniDM(
365
+ args.FCID || ID.fromNull().toString(),
366
+ args.progress,
367
+ args.mode,
368
+ args.fontsize,
369
+ args.color,
370
+ args.senderID,
371
+ args.content,
372
+ args.ctime,
373
+ args.weight,
374
+ args.pool,
375
+ args.attr,
376
+ args.platform,
377
+ args.SPMO,
378
+ args.extra,
379
+ args.DMID,
380
+ )
381
+ }
345
382
  get extra() {
346
383
  const extra = JSON.parse(this.extraStr || '{}')
347
384
  return (typeof extra === 'object' ? extra : {}) as Extra
@@ -476,46 +513,48 @@ export class UniDM {
476
513
  // if (args.mode === 7) extra.bili.adv = args.content
477
514
  // else if (args.mode === 8) extra.bili.code = args.content
478
515
  // else if (args.mode === 9) extra.bili.bas = args.content
479
- return new UniDM(
480
- FCID.toString(),
481
- args.progress,
516
+ return this.create({
517
+ ...args,
518
+ FCID: FCID.toString(),
519
+ // progress: args.progress,
482
520
  mode,
483
- args.fontsize,
484
- args.color,
485
- senderID.toString(),
486
- args.content,
487
- this.transCtime(args.ctime),
488
- args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
521
+ // fontsize: args.fontsize,
522
+ // color: args.color,
523
+ senderID: senderID.toString(),
524
+ // content: args.content,
525
+ ctime: this.transCtime(args.ctime),
526
+ weight: args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
489
527
  pool,
490
- DMAttrUtils.fromBin(args.attr, 'bili'),
491
- domainPreset.bili,
528
+ attr: DMAttrUtils.fromBin(args.attr, 'bili'),
529
+ platform: domainPreset.bili,
492
530
  SPMO,
493
531
  // 需改进,7=>advanced 8=>code 9=>bas 互动=>command
494
532
  // 同时塞进无法/无需直接解析的数据
495
533
  // 另开一个解析器,为大部分播放器(无法解析该类dm)做文本类型降级处理
496
- args.mode >= 7 ? JSON.stringify(extra, BigIntSerializer) : undefined,
497
- )
534
+ extra:
535
+ args.mode >= 7 ? JSON.stringify(extra, BigIntSerializer) : undefined,
536
+ })
498
537
  }
499
538
  static fromBiliCommand(args: DMBiliCommand, SPMO?: string, cid?: bigint) {
500
539
  if (args.oid && !cid) cid = args.oid
501
540
  const FCID = ID.fromBili({ cid }),
502
541
  senderID = ID.fromBili({ mid: args.mid })
503
- return new UniDM(
504
- FCID.toString(),
505
- args.progress,
506
- Modes.Ext,
507
- 0,
508
- 0,
509
- senderID.toString(),
510
- args.content,
511
- // BigInt(Date.parse(args.ctime + ' GMT+0800')), // 无视本地时区,按照B站的东8区计算时间
512
- new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
513
- 10,
514
- Pools.Adv,
515
- ['Protect'],
516
- domainPreset.bili,
542
+ return this.create({
543
+ ...args,
544
+ FCID: FCID.toString(),
545
+ // progress: args.progress,
546
+ mode: Modes.Ext,
547
+ // fontsize: args.fontsize,
548
+ // color: args.color,
549
+ senderID: senderID.toString(),
550
+ // content: args.content,
551
+ ctime: new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
552
+ weight: 10,
553
+ pool: Pools.Adv,
554
+ attr: ['Protect'],
555
+ platform: domainPreset.bili,
517
556
  SPMO,
518
- JSON.stringify(
557
+ extra: JSON.stringify(
519
558
  {
520
559
  bili: {
521
560
  command: args,
@@ -523,25 +562,22 @@ export class UniDM {
523
562
  },
524
563
  BigIntSerializer,
525
564
  ),
526
- )
565
+ })
527
566
  }
528
567
  static fromDplayer(args: DMDplayer, playerID: string, domain: string) {
529
568
  const FCID = ID.fromUnknown(playerID, domain),
530
569
  senderID = ID.fromUnknown(args.midHash, domain)
531
- return new UniDM(
532
- FCID.toString(),
533
- args.progress,
534
- this.transMode(args.mode, 'dplayer'),
535
- 0,
536
- args.color,
537
- senderID.toString(),
538
- args.content,
539
- new Date(),
540
- 0,
541
- 0,
542
- [],
543
- domain,
544
- )
570
+ return this.create({
571
+ ...args,
572
+ FCID: FCID.toString(),
573
+ // progress: args.progress,
574
+ mode: this.transMode(args.mode, 'dplayer'),
575
+ // fontsize: 25,
576
+ // color: args.color,
577
+ senderID: senderID.toString(),
578
+ // content: args.content,
579
+ platform: domain,
580
+ })
545
581
  }
546
582
  toDplayer(): DMDplayer {
547
583
  let mode = 0
@@ -569,22 +605,18 @@ export class UniDM {
569
605
  }
570
606
  else extra = { artplayer: { style: args.style } }
571
607
  }
572
- return new UniDM(
573
- FCID.toString(),
574
- args.progress,
575
- this.transMode(args.mode, 'artplayer'),
576
- 0,
577
- args.color,
578
- senderID.toString(),
579
- args.content,
580
- new Date(),
581
- 0,
582
- 0,
583
- [],
584
- domain,
585
- undefined,
586
- JSON.stringify(extra),
587
- )
608
+ return this.create({
609
+ ...args,
610
+ FCID: FCID.toString(),
611
+ // progress: args.progress,
612
+ mode: this.transMode(args.mode, 'artplayer'),
613
+ // fontsize: 25,
614
+ // color: args.color,
615
+ senderID: senderID.toString(),
616
+ // content: args.content,
617
+ platform: domain,
618
+ extra: JSON.stringify(extra, BigIntSerializer), //optional BigINt parser
619
+ })
588
620
  }
589
621
  toArtplayer(): DMArtplayer {
590
622
  let mode = 0
@@ -604,23 +636,18 @@ export class UniDM {
604
636
  domain = domainPreset.ddplay,
605
637
  ) {
606
638
  const FCID = ID.fromUnknown(episodeId, domain)
607
- return new UniDM(
608
- FCID.toString(),
609
- args.progress,
610
- this.transMode(args.mode, 'ddplay'),
611
- 0,
612
- args.color,
613
- args.uid,
614
- args.m,
615
- new Date(),
616
- 0,
617
- 0,
618
- [],
619
- domain,
620
- undefined,
621
- undefined,
622
- args.cid.toString(), //无需 new ID() 获取带suffix的ID
623
- )
639
+ return this.create({
640
+ ...args,
641
+ FCID: FCID.toString(),
642
+ // progress: args.progress,
643
+ mode: this.transMode(args.mode, 'ddplay'),
644
+ // fontsize: 25,
645
+ // color: args.color,
646
+ senderID: args.uid,
647
+ content: args.m,
648
+ platform: domain,
649
+ DMID: args.cid.toString(), //无需 new ID() 获取带suffix的ID
650
+ })
624
651
  }
625
652
  toDDplay(): DMDDplay {
626
653
  let mode = 1