Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

refactor(staff): switch to v0 persons API with role priority (#2430)

ChouChiu9bf832acaacf
diff --git a/lib/bean/card/staff_card.dart b/lib/bean/card/staff_card.dart
index be90ac11..cc574afd 100644
--- a/lib/bean/card/staff_card.dart
+++ b/lib/bean/card/staff_card.dart
@@ -24,8 +24,8 @@ class StaffCard extends StatelessWidget {
subtitle: staffFullItem.staff.nameCN.isNotEmpty
? Text(staffFullItem.staff.nameCN)
: null,
- trailing: Text(staffFullItem.positions.isNotEmpty
- ? (staffFullItem.positions[0].type.cn)
+ trailing: Text(staffFullItem.relations.isNotEmpty
+ ? staffFullItem.relations.first
: ''),
);
}
diff --git a/lib/modules/staff/staff_item.dart b/lib/modules/staff/staff_item.dart
index a2a4626c..9650811a 100644
--- a/lib/modules/staff/staff_item.dart
+++ b/lib/modules/staff/staff_item.dart
@@ -1,51 +1,23 @@
class StaffFullItem {
final Staff staff;
- final List<Position> positions;
+ final List<String> relations;
StaffFullItem({
required this.staff,
- required this.positions,
+ required this.relations,
});
-
- factory StaffFullItem.fromJson(Map<String, dynamic> json) {
- return StaffFullItem(
- staff: json['staff'] != null
- ? Staff.fromJson(json['staff'] as Map<String, dynamic>)
- : Staff.fromTemplate(),
- positions: (json['positions'] as List<dynamic>? ?? [])
- .map((item) => Position.fromJson(item as Map<String, dynamic>))
- .toList(),
- );
- }
-
- Map<String, dynamic> toJson() {
- return {
- 'staff': staff.toJson(),
- 'positions': positions.map((item) => item.toJson()).toList(),
- };
- }
}
class Staff {
final int id;
final String name;
final String nameCN;
- final int type;
- final String info;
- final int comment;
- final bool lock;
- final bool nsfw;
final Images? images;
Staff({
required this.id,
required this.name,
required this.nameCN,
- required this.type,
- required this.info,
- required this.comment,
- required this.lock,
- required this.nsfw,
this.images,
});
@@ -54,47 +26,11 @@ class Staff {
id: json['id'] is int ? json['id'] as int : 0,
name: json['name'] as String? ?? '',
nameCN: json['nameCN'] as String? ?? '',
- type: json['type'] is int ? json['type'] as int : 0,
- info: json['info'] as String? ?? '',
- comment: json['comment'] is int ? json['comment'] as int : 0,
- lock: json['lock'] as bool? ?? false,
- nsfw: json['nsfw'] as bool? ?? false,
images: json['images'] != null
? Images.fromJson(json['images'] as Map<String, dynamic>)
: null,
);
}
-
- factory Staff.fromTemplate() {
- return Staff(
- id: 0,
- name: '',
- nameCN: '',
- type: 0,
- info: '',
- comment: 0,
- lock: false,
- nsfw: false,
- images: null,
- );
- }
-
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{
- 'id': id,
- 'name': name,
- 'nameCN': nameCN,
- 'type': type,
- 'info': info,
- 'comment': comment,
- 'lock': lock,
- 'nsfw': nsfw,
- };
- if (images != null) {
- data['images'] = images!.toJson();
- }
- return data;
- }
}
class Images {
@@ -118,84 +54,4 @@ class Images {
grid: json['grid'] as String? ?? '',
);
}
-
- Map<String, dynamic> toJson() {
- return {
- 'large': large,
- 'medium': medium,
- 'small': small,
- 'grid': grid,
- };
- }
-}
-
-class Position {
- final PositionType type;
- final String summary;
- final String appearEps;
-
- Position({
- required this.type,
- required this.summary,
- required this.appearEps,
- });
-
- factory Position.fromJson(Map<String, dynamic> json) {
- return Position(
- type: json['type'] != null
- ? PositionType.fromJson(json['type'] as Map<String, dynamic>)
- : PositionType.fromTemplate(),
- summary: json['summary'] as String? ?? '',
- appearEps: json['appearEps'] as String? ?? '',
- );
- }
-
- Map<String, dynamic> toJson() {
- return {
- 'type': type.toJson(),
- 'summary': summary,
- 'appearEps': appearEps,
- };
- }
-}
-
-class PositionType {
- final int id;
- final String en;
- final String cn;
- final String jp;
-
- PositionType({
- required this.id,
- required this.en,
- required this.cn,
- required this.jp,
- });
-
- factory PositionType.fromJson(Map<String, dynamic> json) {
- return PositionType(
- id: json['id'] is int ? json['id'] as int : 0,
- en: json['en'] as String? ?? '',
- cn: json['cn'] as String? ?? '',
- jp: json['jp'] as String? ?? '',
- );
- }
-
- factory PositionType.fromTemplate() {
- return PositionType(
- id: 0,
- en: '',
- cn: '',
- jp: '',
- );
- }
-
- Map<String, dynamic> toJson() {
- return {
- 'id': id,
- 'en': en,
- 'cn': cn,
- 'jp': jp,
- };
- }
}
diff --git a/lib/modules/staff/staff_response.dart b/lib/modules/staff/staff_response.dart
index a1dbda07..09fe538c 100644
--- a/lib/modules/staff/staff_response.dart
+++ b/lib/modules/staff/staff_response.dart
@@ -2,33 +2,56 @@ import 'package:kazumi/modules/staff/staff_item.dart';
class StaffResponse {
final List<StaffFullItem> data;
- final int total;
StaffResponse({
required this.data,
- required this.total,
});
- factory StaffResponse.fromJson(Map<String, dynamic> json) {
- return StaffResponse(
- data: (json['data'] as List<dynamic>? ?? [])
- .map((item) => StaffFullItem.fromJson(item as Map<String, dynamic>))
- .toList(),
- total: json['total'] is int ? json['total'] as int : 0,
- );
- }
+ /// Parses `GET /v0/subjects/{id}/persons`, which returns a flat
+ /// (person × relation) list, so entries are merged by person id.
+ factory StaffResponse.fromJson(List list) {
+ final Map<int, Staff> staffById = {};
+ final Map<int, List<String>> relationsById = {};
+ for (final item in list) {
+ final json = item as Map<String, dynamic>;
+ final staff = Staff.fromJson(json);
+ staffById.putIfAbsent(staff.id, () => staff);
+ relationsById
+ .putIfAbsent(staff.id, () => [])
+ .add(json['relation'] as String? ?? '');
+ }
+
+ final data = staffById.entries.map((entry) {
+ final relations = relationsById[entry.key]!
+ ..sort((a, b) => _priorityOf(a).compareTo(_priorityOf(b)));
+ return StaffFullItem(staff: entry.value, relations: relations);
+ }).toList()
+ ..sort((a, b) => _priorityOf(a.relations.first)
+ .compareTo(_priorityOf(b.relations.first)));
- factory StaffResponse.fromTemplate() {
- return StaffResponse(
- data: [],
- total: 0,
- );
+ return StaffResponse(data: data);
}
- Map<String, dynamic> toJson() {
- return {
- 'data': data.map((item) => item.toJson()).toList(),
- 'total': total,
- };
+ static int _priorityOf(String relation) {
+ final index = _staffRelationPriority.indexOf(relation);
+ return index < 0 ? _staffRelationPriority.length : index;
}
+
+ /// Key roles pinned to the top of the staff list, in this order.
+ static const List<String> _staffRelationPriority = [
+ '动画制作',
+ '原作',
+ '总导演',
+ '导演',
+ '系列构成',
+ '脚本',
+ '人物设定',
+ '总作画监督',
+ '作画监督',
+ '美术监督',
+ '色彩设计',
+ '音乐',
+ '音响监督',
+ '摄影监督',
+ ];
}
diff --git a/lib/request/apis/bangumi_api.dart b/lib/request/apis/bangumi_api.dart
index 1b48f0f8..b271c3c0 100644
--- a/lib/request/apis/bangumi_api.dart
+++ b/lib/request/apis/bangumi_api.dart
@@ -489,8 +489,7 @@ class BangumiApi {
static Future<StaffResponse> getBangumiStaffByID(int id) async {
final jsonData = await _client.get(
ApiEndpoints.formatUrl(
- ApiEndpoints.bangumiAPINextDomain + ApiEndpoints.bangumiStaffByIDNext,
- [id]),
+ ApiEndpoints.bangumiAPIDomain + ApiEndpoints.bangumiStaffByID, [id]),
);
return StaffResponse.fromJson(jsonData);
}
diff --git a/lib/request/config/api_endpoints.dart b/lib/request/config/api_endpoints.dart
index 3126a3d0..5ce586f8 100644
--- a/lib/request/config/api_endpoints.dart
+++ b/lib/request/config/api_endpoints.dart
@@ -61,6 +61,9 @@ class ApiEndpoints {
/// 从条目ID获取角色信息
static const String bangumiCharacterByID = '/v0/subjects/{0}/characters';
+ /// 从条目ID获取工作人员信息
+ static const String bangumiStaffByID = '/v0/subjects/{0}/persons';
+
/// 从条目ID获取剧集ID
static const String bangumiEpisodeByID = '/v0/episodes';
@@ -110,9 +113,6 @@ class ApiEndpoints {
static const String bangumiCharacterCommentsByIDNext =
'/p1/characters/{0}/comments';
- /// 番剧工作人员信息
- static const String bangumiStaffByIDNext = '/p1/subjects/{0}/staffs/persons';
-
/// DanDanPlay API Domain
static const String dandanAPIDomain = 'https://api.dandanplay.net';