@aswless_854771076/ai_short_studio_cli 0.1.156 → 0.1.157
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.
- package/README.md +1 -1
- package/dist/commands/canvas/document/get.js +3 -3
- package/dist/commands/canvas/document/save.js +8 -2
- package/dist/commands/canvas/documents.js +1 -1
- package/dist/commands/canvas/workflow/status.js +1 -1
- package/dist/domain/canvas-schema-key.d.ts +2 -0
- package/dist/domain/canvas-schema-key.js +21 -0
- package/dist/domain/vvicat-api.js +1 -1
- package/package.json +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/SKILL.md +13 -13
- package/skills/using-vvicat-ai-short-studio-cli/references/commands/canvas.md +21 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/humanizer/SOURCE.md +9 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/humanizer/humanizer.md +408 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/SKILL.md +16 -30
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-character-images.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-character-profiles.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-drama-art-style.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-prop-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-scene-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/manage-drama-art-styles.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/manage-drama-assets.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/ideate-drama-from-market/prompts/market_inspiration.en.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/ideate-drama-from-market/prompts/market_inspiration.zh.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/plan-drama-production/prompts/production_plan.en.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/plan-drama-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/codex-contracts.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/analyze-drama-source.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/define-drama-brief.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/design-drama-bible.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/humanizer.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/outline-drama-series.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/short-drama.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/write-drama-director-book.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/write-drama-episode.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/edit-deliver-drama.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/edit-drama-timeline.md +4 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/remotion-best-practices.md +4 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing-workflow.md +8 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/feature-completeness.md +25 -25
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/configure-generation-providers.md +5 -11
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/design-drama-audio.md +4 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/drama-generation-service.md +6 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/generate-drama-videos.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/generate-storyboard-images.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/monitor-drama-tasks.md +9 -7
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/transform-drama-media.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/write-drama-video-prompts.md +9 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/analyze-drama-market.md +8 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/ideate-drama-from-market/market-inspiration.schema.json +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/ideate-drama-from-market.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/README.md +59 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/analyze.mjs +517 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/analyze.test.mjs +354 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/juchacha-client.mjs +188 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/juchacha-client.test.mjs +218 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/normalize.mjs +458 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/normalize.test.mjs +236 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/report.mjs +69 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/store.mjs +341 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/store.test.mjs +302 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/module-map.json +43 -133
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/music-catalog-providers.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/manage-drama-projects.md +5 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/orchestrate-short-drama.md +7 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/recover-drama-pipeline.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/short-drama-skill-index.md +7 -7
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/use-short-drama-studio.md +4 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/pipeline.md +40 -65
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/project-spec-v1.md +91 -131
- package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/h3-prompt-writing.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt/prompt-craft.md +185 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt/seedance-prompting.md +64 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt-skill-index.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/recreation/recreation-qa.md +87 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/review/review-drama-script.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/review/review-drama-shots.md +5 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/build-drama-storyboard.md +1 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/design-fight-video.md +0 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/direct-blender-previz/direction-contract.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/direct-blender-previz.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/generate-blender-previz.md +5 -10
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/plan-drama-production.md +2 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/plan-shot-continuity.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/usage-guide.md +28 -70
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/SKILL.md +0 -447
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/brief.md +0 -93
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/project-files.md +0 -246
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/reference-video.md +0 -266
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/script-and-time.md +0 -253
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/transformations.md +0 -141
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/distribution.md +0 -176
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/local-tools.md +0 -411
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/model-and-provider.md +0 -275
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/profile.md +0 -286
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/asset-and-shot-preproduction.md +0 -193
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/b-roll.md +0 -94
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/caption-tracking.md +0 -125
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/captions.md +0 -252
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/examples/conversation-images.md +0 -218
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/examples/image-direction.md +0 -123
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/generated-dependencies.md +0 -38
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/graphic-compositions.md +0 -85
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/image-direction.md +0 -95
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/motion-graphics.md +0 -68
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/recreation-policy.md +0 -51
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/screen-demonstrations.md +0 -51
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/shot-production-contracts.md +0 -161
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/sound-mix.md +0 -76
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/video-direction.md +0 -433
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/voice-and-performance.md +0 -224
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/voice-direction.md +0 -99
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/narration-led-demo.md +0 -87
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/presenter-led-explainer.md +0 -160
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/ranking-listicle.md +0 -71
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/reference-recreation-canvas.md +0 -19
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/short-drama-workflow.md +0 -165
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/short-drama.md +0 -88
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/street-interview.md +0 -92
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/talking-head.md +0 -82
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/two-person-podcast.md +0 -101
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/index.md +0 -77
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/audio-presentation.md +0 -49
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/authoring.md +0 -228
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/browser-capture.md +0 -112
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/builds.md +0 -330
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/caption-authoring.md +0 -156
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/caption-presentation.md +0 -66
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-design.md +0 -217
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-sharing.md +0 -150
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-visuals.md +0 -152
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/detail.svrun +0 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/direction.svs +0 -14
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/look.svs +0 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/material.svrun +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/production.svml +0 -73
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/production.svrun +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/visuals.ts +0 -62
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/fonts-and-text.md +0 -138
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/image-operations.md +0 -102
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/index.md +0 -73
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/media-presentation.md +0 -111
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/media.md +0 -246
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/performance.md +0 -114
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/prompt-kits.md +0 -165
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/rendering.md +0 -141
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/review.md +0 -211
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/runs.md +0 -151
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/script-syntax.md +0 -236
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/snapshots.md +0 -90
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/sound.md +0 -75
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/source-syntax.md +0 -163
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/spatial.md +0 -189
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/studio-companions.md +0 -186
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/studio.md +0 -249
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/system.md +0 -169
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/timeline.md +0 -74
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/timing.md +0 -96
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/track-authoring.md +0 -215
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/tracks.md +0 -67
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/video-downloads.md +0 -47
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/vocabulary.md +0 -119
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/README.md +0 -11
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/comfly-provider.md +0 -12
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/seedance-community-skills.md +0 -23
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/recreation/publish-drama-references/litterbox.md +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/runninghub-provider.md +0 -33
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/starrouter-provider.md +0 -53
- package/skills/using-vvicat-ai-short-studio-cli/references/video-model-routing.md +0 -10
- package/skills/using-vvicat-ai-short-studio-cli/references/video-prompt-spec.md +0 -32
- /package/skills/using-vvicat-ai-short-studio-cli/references/{short-drama/assets/modules/humanizer → humanizer}/LICENSE +0 -0
- /package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/examples/h3-base-prompt-writing.md +0 -0
- /package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/examples/h3-reference-prompt-writing.md +0 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import {
|
|
4
|
+
analyzeMarket,
|
|
5
|
+
companyConcentration,
|
|
6
|
+
compareSnapshots,
|
|
7
|
+
percentileRanks,
|
|
8
|
+
platformMatrix,
|
|
9
|
+
topicMetrics,
|
|
10
|
+
} from './analyze.mjs'
|
|
11
|
+
|
|
12
|
+
const fixedFilters = { generatedAt: '2026-09-16T00:00:00.000Z' }
|
|
13
|
+
|
|
14
|
+
test('机会分可解释且高需求低拥挤题材排名更高', () => {
|
|
15
|
+
const items = [
|
|
16
|
+
{ key: 'a', title: '谜案', topics: ['悬疑探案'], rankingType: 'douyin', ranking: 1, heatValue: 1000, growthValue: 800, isNew: true, persistenceDays: 3, provenance: { topicSource: 'source-tag' }, companies: {} },
|
|
17
|
+
{ key: 'b', title: '情感一', topics: ['都市情感'], rankingType: 'hot', ranking: 20, heatValue: 10, growthValue: 0, isNew: false, persistenceDays: 1, provenance: { topicSource: 'title-keyword' }, companies: {} },
|
|
18
|
+
{ key: 'c', title: '情感二', topics: ['都市情感'], rankingType: 'douyin', ranking: 25, heatValue: 5, growthValue: 0, isNew: false, persistenceDays: 1, provenance: { topicSource: 'source-tag' }, companies: {} },
|
|
19
|
+
]
|
|
20
|
+
const report = analyzeMarket({ items, successfulRankingTypes: ['hot', 'douyin'], snapshotIds: ['s1'], filters: fixedFilters })
|
|
21
|
+
assert.equal(report.topic_metrics[0].topic, '悬疑探案')
|
|
22
|
+
assert.deepEqual(report.topic_metrics[0].ranking_types, ['douyin'])
|
|
23
|
+
assert.ok(report.topic_metrics[0].opportunity_score > report.topic_metrics[1].opportunity_score)
|
|
24
|
+
assert.equal(report.summary.formula_version, 'topic-opportunity-v1')
|
|
25
|
+
assert.match(report.limitations.join(' '), /单次快照/)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('百分位稳健处理全零、并列、单项及非法数值', () => {
|
|
29
|
+
assert.deepEqual(percentileRanks([0, 0, 0]), [0.5, 0.5, 0.5])
|
|
30
|
+
assert.deepEqual(percentileRanks([5]), [0.5])
|
|
31
|
+
assert.deepEqual(percentileRanks([1, 3, 3, null, NaN, Infinity]), [0, 0.75, 0.75, null, null, null])
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('重复 observation 不重复供给且持久度截尾归一化', () => {
|
|
35
|
+
const items = [
|
|
36
|
+
{ key: 'same', title: '推理谜案', topics: ['悬疑探案'], rankingType: 'hot', ranking: 1, heatValue: 100, growthValue: 10, isNew: true, persistenceDays: 60, provenance: { topicSource: 'title-keyword' } },
|
|
37
|
+
{ key: 'same', title: '推理谜案', topics: ['悬疑探案'], rankingType: 'douyin', ranking: 2, heatValue: 300, growthValue: 30, isNew: true, persistenceDays: 60, provenance: { topicSource: 'title-keyword' } },
|
|
38
|
+
]
|
|
39
|
+
const metric = topicMetrics(items, ['hot', 'douyin'])[0]
|
|
40
|
+
assert.equal(metric.supply_count, 1)
|
|
41
|
+
assert.equal(metric.persistence_strength, 1)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('缺失热度增长新剧和持久度不按零计分,并公开指标覆盖', () => {
|
|
45
|
+
const metrics = topicMetrics([
|
|
46
|
+
{ key: 'a', topics: ['甲'], rankingType: 'hot', ranking: 1, heatValue: 100, provenance: { topicSource: 'source-tag' } },
|
|
47
|
+
{ key: 'b', topics: ['乙'], rankingType: 'hot', ranking: 2, heatValue: 0, growthValue: 0, isNew: false, persistenceDays: 0, provenance: { topicSource: 'source-tag' } },
|
|
48
|
+
], ['hot'])
|
|
49
|
+
const metric = metrics.find(({ topic }) => topic === '甲')
|
|
50
|
+
|
|
51
|
+
assert.equal(metric.demand_strength, 1)
|
|
52
|
+
assert.equal(metric.growth_strength, null)
|
|
53
|
+
assert.equal(metric.new_title_rate, null)
|
|
54
|
+
assert.equal(metric.persistence_strength, null)
|
|
55
|
+
assert.deepEqual(metric.metric_coverage, {
|
|
56
|
+
rank_strength: 1,
|
|
57
|
+
demand_strength: 1,
|
|
58
|
+
growth_strength: 0,
|
|
59
|
+
new_title_rate: 0,
|
|
60
|
+
platform_coverage: 1,
|
|
61
|
+
persistence_strength: 0,
|
|
62
|
+
})
|
|
63
|
+
assert.equal(metric.opportunity_score, 90)
|
|
64
|
+
assert.equal(metric.confidence, 'low')
|
|
65
|
+
assert.equal(metric.confidence_detail.metric_coverage, 0.25)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('榜内热度摘要忽略缺失值,全部缺失时返回 null', () => {
|
|
69
|
+
const metric = topicMetrics([
|
|
70
|
+
{ key: 'a', topics: ['题材'], rankingType: 'hot', ranking: 1 },
|
|
71
|
+
{ key: 'b', topics: ['题材'], rankingType: 'hot', ranking: 2 },
|
|
72
|
+
], ['hot'])[0]
|
|
73
|
+
|
|
74
|
+
assert.deepEqual(metric.ranking_heat, [{ ranking_type: 'hot', demand_strength: null, sample_count: 2 }])
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('跨平台同剧在平台矩阵中合并且证据只保留引用', () => {
|
|
78
|
+
const matrix = platformMatrix([
|
|
79
|
+
{ key: 'a', playletId: 9, title: '同剧', topics: ['悬疑探案'], rankingType: 'hot', ranking: 2, snapshotId: 's1' },
|
|
80
|
+
{ key: 'a', playletId: 9, title: '同剧', topics: ['悬疑探案'], rankingType: 'douyin', ranking: 1, snapshotId: 's1' },
|
|
81
|
+
])
|
|
82
|
+
assert.equal(matrix.length, 1)
|
|
83
|
+
assert.deepEqual(matrix[0].ranking_types, ['douyin', 'hot'])
|
|
84
|
+
assert.equal(matrix[0].platform_coverage, 2)
|
|
85
|
+
assert.deepEqual(matrix[0].topic_sources, [])
|
|
86
|
+
assert.deepEqual(matrix[0].evidence[0], { snapshot_id: 's1', ranking_type: 'douyin', playlet_id: 9, key: 'playlet-id:9' })
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('公司集中度以有归属去重作品为分母,空分母为零', () => {
|
|
90
|
+
const concentration = companyConcentration([
|
|
91
|
+
{ key: 'a', companies: { producer: ['甲', '甲'] } },
|
|
92
|
+
{ key: 'b', companies: ['甲', '乙'] },
|
|
93
|
+
{ key: 'c', companies: null },
|
|
94
|
+
])
|
|
95
|
+
assert.equal(concentration.total_with_company, 2)
|
|
96
|
+
assert.equal(concentration.cr3, 1)
|
|
97
|
+
assert.equal(companyConcentration([{ key: 'x', companies: {} }]).cr3, 0)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('公司集中度按平台、承制、版权和制作角色分开计算', () => {
|
|
101
|
+
const result = companyConcentration([
|
|
102
|
+
{ key: 'a', companies: { platform: ['平台甲'], contractor: ['承制甲'], copyrightHolder: ['版权甲'], producer: ['制作甲'] } },
|
|
103
|
+
{ key: 'b', companies: { platform: ['平台甲'], contractor: ['承制乙'], copyrightHolder: [], producer: [] } },
|
|
104
|
+
])
|
|
105
|
+
assert.deepEqual(Object.keys(result.by_role), ['platform', 'contractor', 'copyrightHolder', 'producer'])
|
|
106
|
+
assert.equal(result.by_role.platform.total_with_company, 2)
|
|
107
|
+
assert.equal(result.by_role.platform.cr3, 1)
|
|
108
|
+
assert.equal(result.by_role.copyrightHolder.total_with_company, 1)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('筛选后空结果仍返回完整零值报告且输入不变', () => {
|
|
112
|
+
const items = [{ key: 'a', title: '剧', topics: ['悬疑探案'], audience: '女频', era: '现代', format: '短剧', rankingType: 'hot', ranking: 1 }]
|
|
113
|
+
const before = structuredClone(items)
|
|
114
|
+
const report = analyzeMarket({ items, successfulRankingTypes: ['hot'], snapshotIds: ['s1'], filters: { ...fixedFilters, audience: '男频' } })
|
|
115
|
+
assert.deepEqual(items, before)
|
|
116
|
+
assert.equal(report.coverage.sample_count, 0)
|
|
117
|
+
assert.deepEqual(report.topic_metrics, [])
|
|
118
|
+
assert.equal(report.company_concentration.cr3, 0)
|
|
119
|
+
assert.equal(report.new_title_watch.length, 0)
|
|
120
|
+
assert.equal(Object.keys(report).join(','), 'schema_version,report_id,generated_at,snapshot_ids,coverage,filters,summary,topic_metrics,topic_cooccurrence,supply_demand_quadrants,platform_preferences,platform_matrix,company_concentration,new_title_watch,evidence,limitations')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('单榜筛选保留采集榜单口径,但平台覆盖按当前分析榜单计算', () => {
|
|
124
|
+
const report = analyzeMarket({
|
|
125
|
+
items: [
|
|
126
|
+
{ key: 'hot', topics: ['题材'], rankingType: 'hot', ranking: 1 },
|
|
127
|
+
{ key: 'douyin', topics: ['题材'], rankingType: 'douyin', ranking: 1 },
|
|
128
|
+
],
|
|
129
|
+
successfulRankingTypes: ['hot', 'douyin'],
|
|
130
|
+
snapshotIds: ['s1'],
|
|
131
|
+
filters: { ...fixedFilters, rankingTypes: ['hot'] },
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
assert.deepEqual(report.coverage.successful_ranking_types, ['douyin', 'hot'])
|
|
135
|
+
assert.deepEqual(report.coverage.analysis_ranking_types, ['hot'])
|
|
136
|
+
assert.equal(report.topic_metrics[0].platform_coverage, 1)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('部分榜单失败、历史口径不同不生成趋势', () => {
|
|
140
|
+
const current = { items: [{ key: 'a', rankingType: 'hot' }], successfulRankingTypes: ['hot', 'douyin'] }
|
|
141
|
+
const previous = { items: [{ key: 'old', rankingType: 'hot' }], successfulRankingTypes: ['hot'] }
|
|
142
|
+
assert.equal(compareSnapshots(current, previous).trend, null)
|
|
143
|
+
const report = analyzeMarket({
|
|
144
|
+
items: current.items,
|
|
145
|
+
successfulRankingTypes: current.successfulRankingTypes,
|
|
146
|
+
snapshotIds: ['new', 'old'],
|
|
147
|
+
previousItems: previous.items,
|
|
148
|
+
filters: { ...fixedFilters, previousSuccessfulRankingTypes: previous.successfulRankingTypes },
|
|
149
|
+
})
|
|
150
|
+
assert.deepEqual(report.coverage.failed_ranking_types, ['motion', 'motion-ai', 'motion-comedy', 'kuaishou', 'hongguo', 'income'])
|
|
151
|
+
assert.equal(report.summary.trend, null)
|
|
152
|
+
assert.match(report.limitations.join(' '), /不可比/)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('新剧观察稳定排序且机会分位于零到一百', () => {
|
|
156
|
+
const report = analyzeMarket({
|
|
157
|
+
items: [
|
|
158
|
+
{ key: 'b', title: '乙', topics: ['题材'], rankingType: 'hot', ranking: 2, isNew: true, provenance: { topicSource: 'source-tag' } },
|
|
159
|
+
{ key: 'a', title: '甲', topics: ['题材'], rankingType: 'douyin', ranking: 1, isNew: true, provenance: { topicSource: 'source-tag' } },
|
|
160
|
+
],
|
|
161
|
+
successfulRankingTypes: ['hot', 'douyin'],
|
|
162
|
+
snapshotIds: ['s1'],
|
|
163
|
+
filters: fixedFilters,
|
|
164
|
+
})
|
|
165
|
+
assert.deepEqual(report.new_title_watch.map((item) => item.key), ['a', 'b'])
|
|
166
|
+
assert.ok(report.topic_metrics.every((item) => item.opportunity_score >= 0 && item.opportunity_score <= 100))
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('没有历史成功榜单口径时,不会把当前集合复用为趋势口径', () => {
|
|
170
|
+
const report = analyzeMarket({
|
|
171
|
+
items: [{ key: 'now', rankingType: 'hot' }],
|
|
172
|
+
successfulRankingTypes: ['hot'],
|
|
173
|
+
snapshotIds: ['now', 'before'],
|
|
174
|
+
previousItems: [{ key: 'before', rankingType: 'douyin' }],
|
|
175
|
+
filters: fixedFilters,
|
|
176
|
+
})
|
|
177
|
+
assert.equal(report.summary.trend, null)
|
|
178
|
+
assert.match(report.limitations.join(' '), /不可比/)
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('两个 snapshot id 但没有两份实际内容时明确标记趋势不可用', () => {
|
|
182
|
+
const report = analyzeMarket({
|
|
183
|
+
items: [{ key: 'now', rankingType: 'hot' }],
|
|
184
|
+
successfulRankingTypes: ['hot'],
|
|
185
|
+
snapshotIds: ['now', 'before'],
|
|
186
|
+
filters: fixedFilters,
|
|
187
|
+
})
|
|
188
|
+
assert.equal(report.summary.trend, null)
|
|
189
|
+
assert.match(report.limitations.join(' '), /趋势不可用/)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('快照成功榜单集合忽略输入顺序,并要求两边都有实际 observation', () => {
|
|
193
|
+
const sameCoverage = compareSnapshots(
|
|
194
|
+
{ items: [{ key: 'a', rankingType: 'hot' }], successfulRankingTypes: ['hot', 'douyin'] },
|
|
195
|
+
{ items: [{ key: 'b', rankingType: 'douyin' }], successfulRankingTypes: ['douyin', 'hot'] },
|
|
196
|
+
)
|
|
197
|
+
assert.equal(sameCoverage.comparable, true)
|
|
198
|
+
assert.notEqual(sameCoverage.trend, null)
|
|
199
|
+
assert.equal(compareSnapshots({ items: [], successfulRankingTypes: ['hot'] }, { items: [{ key: 'b', rankingType: 'hot' }], successfulRankingTypes: ['hot'] }).trend, null)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test('趋势报告间隔小时数,少于二十四小时标记为瞬时快照', () => {
|
|
203
|
+
const result = compareSnapshots(
|
|
204
|
+
{ items: [{ key: 'now', rankingType: 'hot', observedAt: '2026-09-16T10:00:00.000Z' }], successfulRankingTypes: ['hot'] },
|
|
205
|
+
{ items: [{ key: 'before', rankingType: 'hot', observedAt: '2026-09-16T09:30:00.000Z' }], successfulRankingTypes: ['hot'] },
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
assert.equal(result.trend.elapsed_hours, 0.5)
|
|
209
|
+
assert.equal(result.trend.period_kind, 'instant')
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test('报告的平台矩阵用快照 id 回填无 observation 快照字段的证据', () => {
|
|
213
|
+
const report = analyzeMarket({
|
|
214
|
+
items: [{ key: 'a', playletId: 1, rankingType: 'hot', ranking: 1 }],
|
|
215
|
+
successfulRankingTypes: ['hot'],
|
|
216
|
+
snapshotIds: ['snapshot-current'],
|
|
217
|
+
filters: fixedFilters,
|
|
218
|
+
})
|
|
219
|
+
assert.equal(report.platform_matrix[0].evidence[0].snapshot_id, 'snapshot-current')
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
test('同一 playletId 即使 key 不同也只计一部供给、平台作品和公司归属', () => {
|
|
223
|
+
const items = [
|
|
224
|
+
{ key: 'different-a', playletId: 7, topics: ['题材'], rankingType: 'hot', ranking: 1, companies: ['甲'] },
|
|
225
|
+
{ key: 'different-b', playletId: 7, topics: ['题材'], rankingType: 'douyin', ranking: 2, companies: ['甲'] },
|
|
226
|
+
]
|
|
227
|
+
assert.equal(topicMetrics(items, ['hot', 'douyin'])[0].supply_count, 1)
|
|
228
|
+
assert.equal(platformMatrix(items).length, 1)
|
|
229
|
+
assert.equal(companyConcentration(items).total_with_company, 1)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('来源标签在相同样本量和平台覆盖下比标题推断有更高置信度', () => {
|
|
233
|
+
const makeItems = (topicSource) => Array.from({ length: 5 }, (_, index) => ({
|
|
234
|
+
key: `${topicSource}-${index}`,
|
|
235
|
+
topics: ['题材'],
|
|
236
|
+
rankingType: index % 2 === 0 ? 'hot' : 'douyin',
|
|
237
|
+
heatValue: index,
|
|
238
|
+
growthValue: index,
|
|
239
|
+
isNew: index % 2 === 0,
|
|
240
|
+
persistenceDays: index,
|
|
241
|
+
provenance: { topicSource },
|
|
242
|
+
}))
|
|
243
|
+
const sourceTag = topicMetrics(makeItems('source-tag'), ['hot', 'douyin'])[0]
|
|
244
|
+
const titleKeyword = topicMetrics(makeItems('title-keyword'), ['hot', 'douyin'])[0]
|
|
245
|
+
assert.equal(sourceTag.confidence, 'high')
|
|
246
|
+
assert.equal(titleKeyword.confidence, 'low')
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('报告 ID 覆盖趋势、评分输入和筛选,并在相同输入下稳定', () => {
|
|
250
|
+
const base = {
|
|
251
|
+
items: [{ key: 'now', topics: ['题材'], rankingType: 'hot', ranking: 1, isNew: false, persistenceDays: 1 }],
|
|
252
|
+
successfulRankingTypes: ['hot'],
|
|
253
|
+
snapshotIds: ['now', 'before'],
|
|
254
|
+
previousItems: [{ key: 'before', topics: ['题材'], rankingType: 'hot', ranking: 1 }],
|
|
255
|
+
filters: fixedFilters,
|
|
256
|
+
}
|
|
257
|
+
const same = analyzeMarket(base)
|
|
258
|
+
assert.equal(analyzeMarket(base).report_id, same.report_id)
|
|
259
|
+
assert.notEqual(analyzeMarket({ ...base, previousItems: [] }).report_id, same.report_id)
|
|
260
|
+
assert.notEqual(analyzeMarket({ ...base, items: [{ ...base.items[0], isNew: true, persistenceDays: 2 }] }).report_id, same.report_id)
|
|
261
|
+
assert.notEqual(analyzeMarket({ ...base, filters: { ...fixedFilters, rankingTypes: ['hot'] } }).report_id, same.report_id)
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
test('历史成功榜单从筛选前 observation 推导,筛选不改变两期采集口径', () => {
|
|
265
|
+
const report = analyzeMarket({
|
|
266
|
+
items: [
|
|
267
|
+
{ key: 'now-hot', audience: '女频', rankingType: 'hot' },
|
|
268
|
+
{ key: 'now-douyin', audience: '男频', rankingType: 'douyin' },
|
|
269
|
+
],
|
|
270
|
+
previousItems: [
|
|
271
|
+
{ key: 'old-hot', audience: '女频', rankingType: 'hot' },
|
|
272
|
+
{ key: 'old-douyin', audience: '男频', rankingType: 'douyin' },
|
|
273
|
+
],
|
|
274
|
+
successfulRankingTypes: ['hot', 'douyin'],
|
|
275
|
+
snapshotIds: ['now', 'old'],
|
|
276
|
+
filters: { ...fixedFilters, audience: '女频' },
|
|
277
|
+
})
|
|
278
|
+
assert.notEqual(report.summary.trend, null)
|
|
279
|
+
assert.doesNotMatch(report.limitations.join(' '), /不可比/)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('固定样本精确遵循机会分权重', () => {
|
|
283
|
+
const metrics = topicMetrics([
|
|
284
|
+
{ key: 'a', topics: ['甲'], rankingType: 'hot', ranking: 1, heatValue: 100, growthValue: 100, isNew: true, persistenceDays: 15 },
|
|
285
|
+
{ key: 'b', topics: ['乙'], rankingType: 'hot', ranking: 2, heatValue: 0, growthValue: 0, isNew: false, persistenceDays: 0 },
|
|
286
|
+
], ['hot'])
|
|
287
|
+
assert.deepEqual(metrics.map(({ topic, rank_strength, demand_strength, growth_strength, new_title_rate, platform_coverage, persistence_strength, crowding, opportunity_score }) => ({ topic, rank_strength, demand_strength, growth_strength, new_title_rate, platform_coverage, persistence_strength, crowding, opportunity_score })), [
|
|
288
|
+
{ topic: '甲', rank_strength: 1, demand_strength: 1, growth_strength: 1, new_title_rate: 1, platform_coverage: 1, persistence_strength: 0.5, crowding: 0.5, opportunity_score: 82 },
|
|
289
|
+
{ topic: '乙', rank_strength: 0, demand_strength: 0, growth_strength: 0, new_title_rate: 0, platform_coverage: 1, persistence_strength: 0, crowding: 0.5, opportunity_score: 8 },
|
|
290
|
+
])
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
test('深冻结的非空输入可分析且保持不变', () => {
|
|
294
|
+
const deepFreeze = (value) => {
|
|
295
|
+
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
296
|
+
Object.freeze(value)
|
|
297
|
+
for (const entry of Object.values(value)) deepFreeze(entry)
|
|
298
|
+
}
|
|
299
|
+
return value
|
|
300
|
+
}
|
|
301
|
+
const input = deepFreeze({
|
|
302
|
+
items: [{ key: 'a', topics: ['题材'], rankingType: 'hot', companies: { producer: ['甲'] }, provenance: { topicSource: 'source-tag' } }],
|
|
303
|
+
successfulRankingTypes: ['hot'],
|
|
304
|
+
snapshotIds: ['s1'],
|
|
305
|
+
filters: fixedFilters,
|
|
306
|
+
})
|
|
307
|
+
const before = structuredClone(input)
|
|
308
|
+
assert.doesNotThrow(() => analyzeMarket(input))
|
|
309
|
+
assert.deepEqual(input, before)
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
test('snapshotIds 首项始终作为当前快照回填各类当前证据', () => {
|
|
313
|
+
const currentSnapshot = '20260916-current'
|
|
314
|
+
const report = analyzeMarket({
|
|
315
|
+
items: [{ key: 'a', playletId: 1, title: '新剧', topics: ['题材'], rankingType: 'hot', ranking: 1, isNew: true }],
|
|
316
|
+
previousItems: [{ key: 'old', rankingType: 'hot' }],
|
|
317
|
+
successfulRankingTypes: ['hot'],
|
|
318
|
+
snapshotIds: [currentSnapshot, '20260915-history'],
|
|
319
|
+
filters: fixedFilters,
|
|
320
|
+
})
|
|
321
|
+
assert.equal(report.snapshot_ids[0], '20260915-history')
|
|
322
|
+
assert.equal(report.evidence[0].snapshot_id, currentSnapshot)
|
|
323
|
+
assert.equal(report.platform_matrix[0].evidence[0].snapshot_id, currentSnapshot)
|
|
324
|
+
assert.equal(report.new_title_watch[0].evidence[0].snapshot_id, currentSnapshot)
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
test('报告派生题材共现、供需象限和平台偏好,并为每项保留证据引用', () => {
|
|
328
|
+
const report = analyzeMarket({
|
|
329
|
+
items: [
|
|
330
|
+
{ key: 'a', title: '甲乙', topics: ['甲', '乙'], rankingType: 'hot', ranking: 1, heatValue: 100 },
|
|
331
|
+
{ key: 'b', title: '甲', topics: ['甲'], rankingType: 'hot', ranking: 2, heatValue: 0 },
|
|
332
|
+
{ key: 'c', title: '乙', topics: ['乙'], rankingType: 'douyin', ranking: 1, heatValue: 50 },
|
|
333
|
+
],
|
|
334
|
+
successfulRankingTypes: ['hot', 'douyin'],
|
|
335
|
+
snapshotIds: ['snapshot-current'],
|
|
336
|
+
filters: fixedFilters,
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
assert.deepEqual(report.topic_cooccurrence, [{
|
|
340
|
+
topics: ['甲', '乙'],
|
|
341
|
+
cooccurrence_count: 1,
|
|
342
|
+
evidence: [{ snapshot_id: 'snapshot-current', ranking_type: 'hot', playlet_id: 'a', key: 'a' }],
|
|
343
|
+
}])
|
|
344
|
+
assert.ok(report.supply_demand_quadrants.every((entry) => entry.evidence.length > 0))
|
|
345
|
+
assert.deepEqual(report.platform_preferences[0], {
|
|
346
|
+
ranking_type: 'douyin',
|
|
347
|
+
topic_preferences: [{
|
|
348
|
+
topic: '乙',
|
|
349
|
+
sample_count: 1,
|
|
350
|
+
share: 1,
|
|
351
|
+
evidence: [{ snapshot_id: 'snapshot-current', ranking_type: 'douyin', playlet_id: 'c', key: 'c' }],
|
|
352
|
+
}],
|
|
353
|
+
})
|
|
354
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
const definitions = {
|
|
4
|
+
hot: { title: '热力榜', dateEndpoint: '/playlet/getHotRankingDate', listEndpoint: '/playlet/listHotRanking', params: {} },
|
|
5
|
+
motion: { title: '动态漫榜', dateEndpoint: '/playlet/motionComicDate', listEndpoint: '/playlet/motionComic', params: { rankType: 1 } },
|
|
6
|
+
'motion-ai': { title: '真人AI榜', dateEndpoint: '/playlet/motionComicDate', listEndpoint: '/playlet/motionComic', params: { rankType: 2 } },
|
|
7
|
+
'motion-comedy': { title: '沙雕漫榜', dateEndpoint: '/playlet/motionComicDate', listEndpoint: '/playlet/motionComic', params: { rankType: 3 } },
|
|
8
|
+
douyin: { title: '抖音热播榜', dateEndpoint: '/playlet/getNativePlayCountDate', listEndpoint: '/playlet/selectNativePlayletPlayCountListByDate', params: {} },
|
|
9
|
+
kuaishou: { title: '快手热播榜', dateEndpoint: '/playlet/getKuaishouNativePlayCountDate', listEndpoint: '/playlet/selectKuaishouNativePlayletPlayCountListByDate', params: {} },
|
|
10
|
+
hongguo: { title: '红果榜', dateEndpoint: '/playlet/listHongGuoRankingDate', listEndpoint: '/playlet/listHongGuoRanking', params: {} },
|
|
11
|
+
income: { title: '短剧收入榜', dateEndpoint: null, listEndpoint: '/playlet/getPlayletRevenueRankData', params: {} },
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
for (const definition of Object.values(definitions)) {
|
|
15
|
+
Object.freeze(definition.params)
|
|
16
|
+
Object.freeze(definition)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const rankingDefinitions = Object.freeze(definitions)
|
|
20
|
+
|
|
21
|
+
const PERIOD_KEYS = ['day', 'week', 'month']
|
|
22
|
+
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/
|
|
23
|
+
|
|
24
|
+
class JuchachaError extends Error {
|
|
25
|
+
constructor(code, endpoint, message) {
|
|
26
|
+
super(message)
|
|
27
|
+
this.code = code
|
|
28
|
+
this.endpoint = endpoint
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
toJSON() {
|
|
32
|
+
return { code: this.code, endpoint: this.endpoint, message: this.message }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function error(code, endpoint, message) {
|
|
37
|
+
return new JuchachaError(code, endpoint, message)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isRecord(value) {
|
|
41
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function shanghaiDate(now) {
|
|
45
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) throw new TypeError('now 必须是有效日期')
|
|
46
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
47
|
+
timeZone: 'Asia/Shanghai',
|
|
48
|
+
year: 'numeric',
|
|
49
|
+
month: '2-digit',
|
|
50
|
+
day: '2-digit',
|
|
51
|
+
}).formatToParts(now)
|
|
52
|
+
const values = Object.fromEntries(parts.filter(({ type }) => type !== 'literal').map(({ type, value }) => [type, value]))
|
|
53
|
+
return `${values.year}-${values.month}-${values.day}`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function dateSignature(now = new Date()) {
|
|
57
|
+
return createHash('md5').update(shanghaiDate(now)).digest('hex')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function selectedPeriod(periods) {
|
|
61
|
+
if (!isRecord(periods)) throw error('JUCHACHA_INVALID_RESPONSE', null, '周期参数必须是对象')
|
|
62
|
+
const selected = PERIOD_KEYS.filter((key) => periods[key] !== undefined && periods[key] !== null && periods[key] !== '')
|
|
63
|
+
if (selected.length > 1) throw error('JUCHACHA_INVALID_RESPONSE', null, 'day、week、month 只能选择一个周期')
|
|
64
|
+
return selected[0] ? { key: selected[0], value: periods[selected[0]] } : null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function publicErrorMessage(value, fallback) {
|
|
68
|
+
return typeof value === 'string' && value.trim() ? value.trim() : fallback
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isTimeout(errorValue) {
|
|
72
|
+
return errorValue?.name === 'TimeoutError' || errorValue?.name === 'AbortError' || errorValue?.code === 'ABORT_ERR'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function createJuchachaClient({
|
|
76
|
+
baseUrl = 'https://playlet-applet.dataeye.com',
|
|
77
|
+
fetchImpl = fetch,
|
|
78
|
+
now = () => new Date(),
|
|
79
|
+
timeoutMs = 20_000,
|
|
80
|
+
} = {}) {
|
|
81
|
+
const rootUrl = String(baseUrl).replace(/\/+$/, '')
|
|
82
|
+
|
|
83
|
+
function requestUrl(endpoint, params = {}) {
|
|
84
|
+
const url = new URL(`${rootUrl}${endpoint}`)
|
|
85
|
+
for (const [key, value] of Object.entries(params)) {
|
|
86
|
+
if (value !== undefined && value !== null && value !== '') url.searchParams.set(key, String(value))
|
|
87
|
+
}
|
|
88
|
+
return url
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function request(endpoint, params, contentKind) {
|
|
92
|
+
const url = requestUrl(endpoint, params)
|
|
93
|
+
let response
|
|
94
|
+
try {
|
|
95
|
+
response = await fetchImpl(url, {
|
|
96
|
+
method: 'GET',
|
|
97
|
+
headers: {
|
|
98
|
+
S: dateSignature(now()),
|
|
99
|
+
authentication: '',
|
|
100
|
+
},
|
|
101
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
102
|
+
})
|
|
103
|
+
} catch (caught) {
|
|
104
|
+
if (isTimeout(caught)) throw error('JUCHACHA_TIMEOUT', endpoint, '剧查查请求超时')
|
|
105
|
+
throw error('JUCHACHA_HTTP_ERROR', endpoint, '剧查查请求失败')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!response || response.ok === false || (Number.isInteger(response.status) && response.status >= 400)) {
|
|
109
|
+
throw error('JUCHACHA_HTTP_ERROR', endpoint, '剧查查返回 HTTP 错误')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let payload
|
|
113
|
+
try {
|
|
114
|
+
if (typeof response.json !== 'function') throw new TypeError('response.json 不可用')
|
|
115
|
+
payload = await response.json()
|
|
116
|
+
} catch (caught) {
|
|
117
|
+
if (isTimeout(caught)) throw error('JUCHACHA_TIMEOUT', endpoint, '剧查查请求超时')
|
|
118
|
+
throw error('JUCHACHA_INVALID_RESPONSE', endpoint, '剧查查返回内容不是有效 JSON')
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!isRecord(payload)) throw error('JUCHACHA_INVALID_RESPONSE', endpoint, '剧查查返回对象无效')
|
|
122
|
+
if (typeof payload.statusCode !== 'number' || payload.statusCode !== 200) {
|
|
123
|
+
throw error('JUCHACHA_API_ERROR', endpoint, publicErrorMessage(payload.message, '剧查查接口返回业务错误'))
|
|
124
|
+
}
|
|
125
|
+
if (contentKind === 'list' && !Array.isArray(payload.content)) {
|
|
126
|
+
throw error('JUCHACHA_INVALID_RESPONSE', endpoint, '剧查查榜单内容无效')
|
|
127
|
+
}
|
|
128
|
+
if (contentKind === 'date' && !isRecord(payload.content) && typeof payload.content !== 'string') {
|
|
129
|
+
throw error('JUCHACHA_INVALID_RESPONSE', endpoint, '剧查查日期内容无效')
|
|
130
|
+
}
|
|
131
|
+
return payload
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function fetchLatestDay(definition) {
|
|
135
|
+
const payload = await request(definition.dateEndpoint, definition.params, 'date')
|
|
136
|
+
const content = payload.content
|
|
137
|
+
const day = typeof content === 'string' ? content : content.day
|
|
138
|
+
if (typeof day !== 'string' || !DATE_PATTERN.test(day)) {
|
|
139
|
+
throw error('JUCHACHA_INVALID_RESPONSE', definition.dateEndpoint, '剧查查最新日期无效')
|
|
140
|
+
}
|
|
141
|
+
return day
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function fetchRanking(type, periods = {}) {
|
|
145
|
+
if (!Object.hasOwn(rankingDefinitions, type)) {
|
|
146
|
+
throw error('JUCHACHA_INVALID_RESPONSE', null, `未知榜单类型:${String(type)}`)
|
|
147
|
+
}
|
|
148
|
+
const definition = rankingDefinitions[type]
|
|
149
|
+
const period = selectedPeriod(periods)
|
|
150
|
+
let chosenPeriod = period
|
|
151
|
+
if (!chosenPeriod && definition.dateEndpoint) {
|
|
152
|
+
chosenPeriod = { key: 'day', value: await fetchLatestDay(definition) }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const params = {
|
|
156
|
+
...definition.params,
|
|
157
|
+
...(chosenPeriod ? { [chosenPeriod.key]: chosenPeriod.value } : {}),
|
|
158
|
+
pageId: 1,
|
|
159
|
+
pageSize: 30,
|
|
160
|
+
}
|
|
161
|
+
return request(definition.listEndpoint, params, 'list')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function fetchAllRankings(periods = {}) {
|
|
165
|
+
const types = Object.keys(rankingDefinitions)
|
|
166
|
+
const settled = await Promise.allSettled(types.map((type) => fetchRanking(type, periods)))
|
|
167
|
+
const rankings = {}
|
|
168
|
+
const failures = []
|
|
169
|
+
for (let index = 0; index < settled.length; index += 1) {
|
|
170
|
+
const result = settled[index]
|
|
171
|
+
const type = types[index]
|
|
172
|
+
if (result.status === 'fulfilled') {
|
|
173
|
+
rankings[type] = result.value
|
|
174
|
+
} else {
|
|
175
|
+
const reason = result.reason
|
|
176
|
+
failures.push({
|
|
177
|
+
type,
|
|
178
|
+
code: reason?.code || 'JUCHACHA_INVALID_RESPONSE',
|
|
179
|
+
endpoint: reason?.endpoint ?? rankingDefinitions[type].listEndpoint,
|
|
180
|
+
message: publicErrorMessage(reason?.message, '剧查查榜单请求失败'),
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { rankings, failures }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return Object.freeze({ fetchRanking, fetchAllRankings })
|
|
188
|
+
}
|