基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
fix(danmaku): use search episodes for manual danmaku lookup
Prediditc32db78c6a6c
diff --git a/lib/modules/danmaku/danmaku_search_response.dart b/lib/modules/danmaku/danmaku_search_response.dartindex 6481b8d9..86b8cfa8 100644--- a/lib/modules/danmaku/danmaku_search_response.dart+++ b/lib/modules/danmaku/danmaku_search_response.dart@@ -1,87 +1,42 @@-class DanmakuAnime {- int animeId;- String animeTitle;- String type;- String typeDescription;- String imageUrl;- DateTime startDate;- int episodeCount;- double rating;- bool isFavorited;-- DanmakuAnime({+/// Response of `/api/v2/search/episodes`.+///+/// Entries carry no cover, rating or air date, unlike `/api/v2/search/anime`.+class DanmakuSearchAnime {+ final int animeId;+ final String animeTitle;+ final String typeDescription;++ const DanmakuSearchAnime({required this.animeId,required this.animeTitle,- required this.type,required this.typeDescription,- required this.imageUrl,- required this.startDate,- required this.episodeCount,- required this.rating,- required this.isFavorited,});- factory DanmakuAnime.fromJson(Map<String, dynamic> json) {- return DanmakuAnime(+ factory DanmakuSearchAnime.fromJson(Map<String, dynamic> json) {+ return DanmakuSearchAnime(animeId: json['animeId'],animeTitle: json['animeTitle'],- type: json['type'],- typeDescription: json['typeDescription'],- imageUrl: json['imageUrl'],- startDate: DateTime.parse(json['startDate']),- episodeCount: json['episodeCount'],- rating: json['rating'].toDouble(),- isFavorited: json['isFavorited'],+ typeDescription: json['typeDescription'] ?? '',);}-- Map<String, dynamic> toJson() {- return {- 'animeId': animeId,- 'animeTitle': animeTitle,- 'type': type,- 'typeDescription': typeDescription,- 'imageUrl': imageUrl,- 'startDate': startDate.toIso8601String(),- 'episodeCount': episodeCount,- 'rating': rating,- 'isFavorited': isFavorited,- };- }}class DanmakuSearchResponse {- List<DanmakuAnime> animes;- int errorCode;- bool success;- String errorMessage;+ final List<DanmakuSearchAnime> animes;++ /// Result set was truncated; the user should narrow the keyword.+ final bool hasMore;- DanmakuSearchResponse({+ const DanmakuSearchResponse({required this.animes,- required this.errorCode,- required this.success,- required this.errorMessage,+ required this.hasMore,});factory DanmakuSearchResponse.fromJson(Map<String, dynamic> json) {var list = json['animes'] as List;- List<DanmakuAnime> animeList =- list.map((i) => DanmakuAnime.fromJson(i)).toList();-return DanmakuSearchResponse(- animes: animeList,- errorCode: json['errorCode'],- success: json['success'],- errorMessage: json['errorMessage'],+ animes: list.map((i) => DanmakuSearchAnime.fromJson(i)).toList(),+ hasMore: json['hasMore'] ?? false,);}-- Map<String, dynamic> toJson() {- return {- 'animes': animes.map((anime) => anime.toJson()).toList(),- 'errorCode': errorCode,- 'success': success,- 'errorMessage': errorMessage,- };- }}diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dartindex f8c52602..6749c125 100644--- a/lib/pages/player/player_item.dart+++ b/lib/pages/player/player_item.dart@@ -1175,10 +1175,8 @@ class _PlayerItemState extends State<PlayerItem>KazumiDialog.dismiss();KazumiDialog.showLoading(msg: '弹幕检索中');DanmakuSearchResponse danmakuSearchResponse;- DanmakuEpisodeResponse danmakuEpisodeResponse;try {- danmakuSearchResponse =- await DanmakuApi.getDanmakuSearchResponse(keyword);+ danmakuSearchResponse = await DanmakuApi.searchAnimes(keyword);} catch (e) {KazumiDialog.dismiss();KazumiDialog.showToast(message: '弹幕检索错误: ${e.toString()}');@@ -1195,70 +1193,90 @@ class _PlayerItemState extends State<PlayerItem>constraints: const BoxConstraints(maxWidth: 560),child: ListView(shrinkWrap: true,- children: danmakuSearchResponse.animes.map((danmakuInfo) {- return ListTile(- title: Text(danmakuInfo.animeTitle),- onTap: () async {- KazumiDialog.dismiss();- KazumiDialog.showLoading(msg: '弹幕检索中');- try {- danmakuEpisodeResponse =- await DanmakuApi.getDanDanEpisodesByDanDanBangumiID(- danmakuInfo.animeId);- } catch (e) {+ children: [+ if (danmakuSearchResponse.hasMore)+ Padding(+ padding: const EdgeInsets.fromLTRB(24, 16, 24, 8),+ child: Text(+ '结果较多,仅显示部分条目,可补充更完整的番剧名缩小范围',+ style: Theme.of(context).textTheme.bodySmall?.copyWith(+ color: Theme.of(context).colorScheme.outline,+ ),+ ),+ ),+ ...danmakuSearchResponse.animes.map((danmakuInfo) {+ return ListTile(+ title: Text(danmakuInfo.animeTitle),+ subtitle: danmakuInfo.typeDescription.isEmpty+ ? null+ : Text(danmakuInfo.typeDescription),+ onTap: () async {KazumiDialog.dismiss();- KazumiDialog.showToast(message: '弹幕检索错误: ${e.toString()}');- return;- }- KazumiDialog.dismiss();- if (danmakuEpisodeResponse.episodes.isEmpty) {- KazumiDialog.showToast(message: '未找到匹配结果');- return;- }- KazumiDialog.show(builder: (context) {- return Dialog(- child: ConstrainedBox(- constraints: const BoxConstraints(maxWidth: 560),- child: ListView(- shrinkWrap: true,- children:- danmakuEpisodeResponse.episodes.map((episode) {- return ListTile(- title: Text(episode.episodeTitle),- onTap: () async {- KazumiDialog.dismiss();- try {- videoPageController- .cancelAutomaticDanmakuLoad();- final hasDanmakus = await playerController- .danmaku- .getDanDanmakuByEpisodeID(- episode.episodeId);- if (!mounted) {- return;- }- if (hasDanmakus) {- playerController.danmaku- .setDanmakuEnabled(true);- KazumiDialog.showToast(message: '弹幕切换成功');- } else {- playerController.danmaku- .setDanmakuEnabled(false);- KazumiDialog.showToast(message: '未找到弹幕内容');+ KazumiDialog.showLoading(msg: '弹幕检索中');+ final DanmakuEpisodeResponse danmakuEpisodeResponse;+ try {+ danmakuEpisodeResponse =+ await DanmakuApi.getDanDanEpisodesByDanDanBangumiID(+ danmakuInfo.animeId);+ } catch (e) {+ KazumiDialog.dismiss();+ KazumiDialog.showToast(+ message: '弹幕检索错误: ${e.toString()}');+ return;+ }+ KazumiDialog.dismiss();+ if (danmakuEpisodeResponse.episodes.isEmpty) {+ KazumiDialog.showToast(message: '未找到匹配结果');+ return;+ }+ KazumiDialog.show(builder: (context) {+ return Dialog(+ child: ConstrainedBox(+ constraints: const BoxConstraints(maxWidth: 560),+ child: ListView.builder(+ shrinkWrap: true,+ itemCount: danmakuEpisodeResponse.episodes.length,+ itemBuilder: (context, index) {+ final episode =+ danmakuEpisodeResponse.episodes[index];+ return ListTile(+ title: Text(episode.episodeTitle),+ onTap: () async {+ KazumiDialog.dismiss();+ try {+ videoPageController+ .cancelAutomaticDanmakuLoad();+ final hasDanmakus = await playerController+ .danmaku+ .getDanDanmakuByEpisodeID(+ episode.episodeId);+ if (!mounted) {+ return;+ }+ if (hasDanmakus) {+ playerController.danmaku+ .setDanmakuEnabled(true);+ KazumiDialog.showToast(message: '弹幕切换成功');+ } else {+ playerController.danmaku+ .setDanmakuEnabled(false);+ KazumiDialog.showToast(+ message: '未找到弹幕内容');+ }+ } catch (e) {+ KazumiDialog.showToast(message: '弹幕切换失败');}- } catch (e) {- KazumiDialog.showToast(message: '弹幕切换失败');- }- },- );- }).toList(),+ },+ );+ },+ ),),- ),- );- });- },- );- }).toList(),+ );+ });+ },+ );+ }),+ ],),),);diff --git a/lib/request/apis/danmaku_api.dart b/lib/request/apis/danmaku_api.dartindex b2a61bd5..20cd509b 100644--- a/lib/request/apis/danmaku_api.dart+++ b/lib/request/apis/danmaku_api.dart@@ -4,7 +4,6 @@ import 'package:kazumi/services/logging/logger.dart';import 'package:kazumi/modules/danmaku/danmaku_module.dart';import 'package:kazumi/modules/danmaku/danmaku_search_response.dart';import 'package:kazumi/modules/danmaku/danmaku_episode_response.dart';-import 'package:kazumi/utils/string_similarity.dart';class DanmakuApi {static final DanmakuClient _client = DanmakuClient.instance;@@ -20,50 +19,6 @@ class DanmakuApi {return danmakuEpisodeResponse.bangumiId;}- // 从标题获取DanDanBangumiID- static Future<int> getBangumiIDByTitle(String title) async {- DanmakuSearchResponse danmakuSearchResponse =- await getDanmakuSearchResponse(title);-- int bestAnimeId = 0;- double maxSimilarity = 0;-- for (var anime in danmakuSearchResponse.animes) {- int animeId = anime.animeId;- if (animeId >= 100000 || animeId < 2) {- continue;- }-- String animeTitle = anime.animeTitle;- double similarity = calculateSimilarity(animeTitle, title);- if (similarity == 1) {- KazumiLogger().i('Danmaku: total match $title');- return animeId;- }-- if (similarity > maxSimilarity) {- maxSimilarity = similarity;- bestAnimeId = animeId;- KazumiLogger().i(- 'Danmaku: match anime danmaku $title --- $animeTitle similarity: $similarity');- }- }-- return bestAnimeId;- }-- // 从BangumiID获取分集ID- static Future<DanmakuEpisodeResponse> getDanmakuEpisodesByBangumiID(- int bangumiID) async {- var path = ApiEndpoints.formatUrl(- ApiEndpoints.dandanAPIInfoByBgmBangumiId, [bangumiID]);- var endPoint = ApiEndpoints.dandanAPIDomain + path;- final jsonData = await _client.get(endPoint);- DanmakuEpisodeResponse danmakuEpisodeResponse =- DanmakuEpisodeResponse.fromJson(jsonData);- return danmakuEpisodeResponse;- }-// 从DanDanBangumiID获取分集IDstatic Future<DanmakuEpisodeResponse> getDanDanEpisodesByDanDanBangumiID(int bangumiID) async {@@ -75,20 +30,23 @@ class DanmakuApi {return danmakuEpisodeResponse;}- // 从标题检索DanDan番剧数据库- static Future<DanmakuSearchResponse> getDanmakuSearchResponse(- String title) async {- var path = ApiEndpoints.dandanAPISearch;+ /// Manual search entry point.+ ///+ /// `/api/v2/search/anime` caps results at 25 with no paging parameter, which+ /// drops the main series of large franchises (Detective Conan has 48 entries).+ /// This endpoint is uncapped, but only under `v2`: the legacy engine collapses+ /// a keyword to a single anime. Its inline episode lists are truncated, so+ /// episodes still come from [getDanDanEpisodesByDanDanBangumiID].+ static Future<DanmakuSearchResponse> searchAnimes(String title) async {+ var path = ApiEndpoints.dandanAPISearchEpisodes;var endPoint = ApiEndpoints.dandanAPIDomain + path;Map<String, String> keywordMap = {- 'keyword': title,+ 'anime': title,'v2': 'true',};final jsonData = await _client.get(endPoint, queryParameters: keywordMap);- DanmakuSearchResponse danmakuSearchResponse =- DanmakuSearchResponse.fromJson(jsonData);- return danmakuSearchResponse;+ return DanmakuSearchResponse.fromJson(jsonData);}static Future<List<DanmakuEntry>> getDanDanmaku(diff --git a/lib/request/config/api_endpoints.dart b/lib/request/config/api_endpoints.dartindex eef0b2e8..219606d8 100644--- a/lib/request/config/api_endpoints.dart+++ b/lib/request/config/api_endpoints.dart@@ -122,7 +122,7 @@ class ApiEndpoints {static const String dandanAPIComment = "/api/v2/comment/";/// 检索弹弹番剧元数据- static const String dandanAPISearch = "/api/v2/search/anime";+ static const String dandanAPISearchEpisodes = "/api/v2/search/episodes";/// 获取弹弹番剧元数据static const String dandanAPIInfo = "/api/v2/bangumi/";diff --git a/lib/utils/string_similarity.dart b/lib/utils/string_similarity.dartdeleted file mode 100644index 4e97bf38..00000000--- a/lib/utils/string_similarity.dart+++ /dev/null@@ -1,36 +0,0 @@-// 计算两个字符串的编辑距离, 曾用于弹幕标题匹配-// 由于 DanDanPlay 现在直接提供基于 bgmBangumiID 的弹幕反查,此方法已不再使用--import 'dart:math';--int levenshteinDistance(String s1, String s2) {- if (s1 == s2) return 0;- if (s1.isEmpty) return s2.length;- if (s2.isEmpty) return s1.length;-- List<int> v0 = List<int>.generate(s2.length + 1, (i) => i);- List<int> v1 = List<int>.filled(s2.length + 1, 0);-- for (int i = 0; i < s1.length; i++) {- v1[0] = i + 1;-- for (int j = 0; j < s2.length; j++) {- int cost = (s1[i] == s2[j]) ? 0 : 1;- v1[j + 1] = min(v1[j] + 1, min(v0[j + 1] + 1, v0[j] + cost));- }-- for (int j = 0; j < v0.length; j++) {- v0[j] = v1[j];- }- }-- return v1[s2.length];-}--// 计算相似度百分比-double calculateSimilarity(String s1, String s2) {- int maxLength = max(s1.length, s2.length);- if (maxLength == 0) return 1.0;- if (s1 == s2) return 1.0;- return (1.0 - levenshteinDistance(s1, s2) / maxLength);-}