Skip to content

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

add bangumi calendar switcher

Predidit1cab0cd851ad
diff --git a/lib/modules/bangumi/bangumi_item.dart b/lib/modules/bangumi/bangumi_item.dart
index a09563d8..c73b3d55 100644
--- a/lib/modules/bangumi/bangumi_item.dart
+++ b/lib/modules/bangumi/bangumi_item.dart
@@ -1,4 +1,5 @@
import 'package:hive/hive.dart';
+import 'package:kazumi/utils/utils.dart';
part 'bangumi_item.g.dart';
@@ -49,7 +50,7 @@ class BangumiItem {
nameCn: (json['name_cn'] ?? '') == '' ? (json['name'] ?? '') : json['name_cn'],
summary: json['summary'] ?? '',
airDate: json['air_date'] ?? json ['date'],
- airWeekday: json['air_weekday'] ?? 1,
+ airWeekday: json['air_weekday'] ?? Utils.dateStringToWeekday(json ['date']) ?? 1,
// rating: Rating.fromJson(json['rating']),
rank: json['rank'] ?? 0,
images: Map<String, String>.from(json['images'] ?? {
diff --git a/lib/pages/timeline/timeline_controller.dart b/lib/pages/timeline/timeline_controller.dart
index 844ce364..acad3443 100644
--- a/lib/pages/timeline/timeline_controller.dart
+++ b/lib/pages/timeline/timeline_controller.dart
@@ -1,5 +1,6 @@
import 'package:kazumi/modules/bangumi/bangumi_item.dart';
import 'package:kazumi/request/bangumi.dart';
+import 'package:kazumi/utils/anime_season.dart';
import 'package:mobx/mobx.dart';
part 'timeline_controller.g.dart';
@@ -10,7 +11,13 @@ abstract class _TimelineController with Store {
@observable
List<List<BangumiItem>> bangumiCalendar = [];
+ DateTime selectedDate = DateTime.now();
+
Future getSchedules() async {
bangumiCalendar = await BangumiHTTP.getCalendar();
}
+
+ Future getSchedulesBySeason() async {
+ bangumiCalendar = await BangumiHTTP.getCalendarBySearch(AnimeSeason(selectedDate).toSeasonStartAndEnd());
+ }
}
\ No newline at end of file
diff --git a/lib/pages/timeline/timeline_page.dart b/lib/pages/timeline/timeline_page.dart
index 6ab220bc..dabe82fc 100644
--- a/lib/pages/timeline/timeline_page.dart
+++ b/lib/pages/timeline/timeline_page.dart
@@ -10,6 +10,8 @@ import 'package:kazumi/utils/utils.dart';
import 'package:kazumi/utils/constans.dart';
import 'package:provider/provider.dart';
import 'package:kazumi/bean/appbar/sys_app_bar.dart';
+import 'package:kazumi/utils/anime_season.dart';
+import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
class TimelinePage extends StatefulWidget {
const TimelinePage({super.key});
@@ -22,12 +24,14 @@ class _TimelinePageState extends State<TimelinePage>
with SingleTickerProviderStateMixin {
final TimelineController timelineController =
Modular.get<TimelineController>();
+ late String seasonString;
dynamic navigationBarState;
TabController? controller;
@override
void initState() {
super.initState();
+ seasonString = AnimeSeason(timelineController.selectedDate).toString();
int weekday = DateTime.now().weekday - 1;
controller =
TabController(vsync: this, length: tabs.length, initialIndex: weekday);
@@ -39,7 +43,6 @@ class _TimelinePageState extends State<TimelinePage>
Provider.of<SideNavigationBarState>(context, listen: false);
}
if (timelineController.bangumiCalendar.isEmpty) {
- // debugPrint('时间表缓存为空, 尝试重新加载');
timelineController.getSchedules();
}
}
@@ -76,39 +79,147 @@ class _TimelinePageState extends State<TimelinePage>
@override
Widget build(BuildContext context) {
- return OrientationBuilder(
- builder: (context, orientation) {
- return Observer(builder: (context) {
- return PopScope(
- canPop: false,
- onPopInvokedWithResult: (bool didPop, Object? result) {
- if (didPop) {
- return;
- }
- onBackPressed(context);
- },
- child: Scaffold(
- appBar: SysAppBar(
- toolbarHeight: 104,
- bottom: TabBar(
- controller: controller,
- tabs: tabs,
- indicatorColor: Theme.of(context).colorScheme.primary,
- ),
- title: InkWell(
- child: const Text('新番时间表'),
- onTap: () {},
- ),
+ return OrientationBuilder(builder: (context, orientation) {
+ return Observer(builder: (context) {
+ return PopScope(
+ canPop: false,
+ onPopInvokedWithResult: (bool didPop, Object? result) {
+ if (didPop) {
+ return;
+ }
+ onBackPressed(context);
+ },
+ child: Scaffold(
+ appBar: SysAppBar(
+ toolbarHeight: 104,
+ bottom: TabBar(
+ controller: controller,
+ tabs: tabs,
+ indicatorColor: Theme.of(context).colorScheme.primary,
+ ),
+ title: InkWell(
+ child: Text(seasonString),
+ onTap: () {
+ SmartDialog.show(
+ useAnimation: false,
+ builder: (context) {
+ return AlertDialog(
+ title: const Text("时间机器"),
+ content: StatefulBuilder(builder:
+ (BuildContext context, StateSetter setState) {
+ return SingleChildScrollView(
+ child: Wrap(
+ spacing: 8,
+ runSpacing: Utils.isCompact() ? 2 : 8,
+ children: [
+ for (final int i in List.generate(20,
+ (index) => DateTime.now().year - index))
+ for (final String selectedSeason in [
+ '秋',
+ '夏',
+ '春',
+ '冬'
+ ])
+ DateTime.now().isAfter(generateDateTime(
+ i, selectedSeason))
+ ? timelineController.selectedDate ==
+ generateDateTime(
+ i, selectedSeason)
+ ? FilledButton(
+ onPressed: () {
+ if (timelineController
+ .selectedDate !=
+ generateDateTime(i,
+ selectedSeason)) {
+ SmartDialog.dismiss();
+ timelineController
+ .selectedDate =
+ generateDateTime(i,
+ selectedSeason);
+ setState(() {
+ seasonString =
+ "加载中 ٩(◦`꒳´◦)۶";
+ });
+ if (AnimeSeason(timelineController
+ .selectedDate)
+ .toString() ==
+ AnimeSeason(DateTime
+ .now())
+ .toString()) {
+ timelineController
+ .getSchedules();
+ } else {
+ timelineController
+ .getSchedulesBySeason();
+ }
+ setState(() {
+ seasonString = AnimeSeason(
+ timelineController
+ .selectedDate)
+ .toString();
+ });
+ }
+ },
+ child: Text(i.toString() +
+ selectedSeason
+ .toString()),
+ )
+ : FilledButton.tonal(
+ onPressed: () {
+ if (timelineController
+ .selectedDate !=
+ generateDateTime(i,
+ selectedSeason)) {
+ SmartDialog.dismiss();
+ timelineController
+ .selectedDate =
+ generateDateTime(i,
+ selectedSeason);
+ setState(() {
+ seasonString =
+ "加载中 ٩(◦`꒳´◦)۶";
+ });
+ if (AnimeSeason(timelineController
+ .selectedDate)
+ .toString() ==
+ AnimeSeason(DateTime
+ .now())
+ .toString()) {
+ timelineController
+ .getSchedules();
+ } else {
+ timelineController
+ .getSchedulesBySeason();
+ }
+ setState(() {
+ seasonString = AnimeSeason(
+ timelineController
+ .selectedDate)
+ .toString();
+ });
+ }
+ },
+ child: Text(i.toString() +
+ selectedSeason
+ .toString()),
+ )
+ : Container(),
+ ],
+ ),
+ );
+ }),
+ );
+ });
+ },
),
- body: Padding(
- padding: const EdgeInsets.only(left: 5, right: 5, top: 10),
- child: renderBody(orientation)
- ),
),
- );
- });
- }
- );
+ body: Padding(
+ padding: const EdgeInsets.only(left: 5, right: 5, top: 10),
+ child: renderBody(orientation)),
+ ),
+ );
+ });
+ });
}
Widget renderBody(Orientation orientation) {
@@ -124,7 +235,8 @@ class _TimelinePageState extends State<TimelinePage>
}
}
- List<Widget> contentGrid(List<List<BangumiItem>> bangumiCalendar, Orientation orientation) {
+ List<Widget> contentGrid(
+ List<List<BangumiItem>> bangumiCalendar, Orientation orientation) {
List<Widget> gridViewList = [];
int crossCount = orientation != Orientation.portrait ? 6 : 3;
for (dynamic bangumiList in bangumiCalendar) {
diff --git a/lib/request/api.dart b/lib/request/api.dart
index af11010d..9a75e616 100644
--- a/lib/request/api.dart
+++ b/lib/request/api.dart
@@ -21,7 +21,7 @@ class Api {
// 番剧检索 (弃用)
static const String bangumiSearch = 'https://api.bgm.tv/search/subject/';
// 条目搜索
- static const String bangumiRankSearch = 'https://api.bgm.tv/v0/search/subjects';
+ static const String bangumiRankSearch = 'https://api.bgm.tv/v0/search/subjects?limit=100';
// 从条目ID获取详细信息
static const String bangumiInfoByID = 'https://api.bgm.tv/v0/subjects/';
// 弹弹Play
diff --git a/lib/request/bangumi.dart b/lib/request/bangumi.dart
index cea98739..1792acba 100644
--- a/lib/request/bangumi.dart
+++ b/lib/request/bangumi.dart
@@ -7,6 +7,8 @@ import 'package:kazumi/request/request.dart';
import 'package:kazumi/modules/bangumi/bangumi_item.dart';
class BangumiHTTP {
+ // why the api havn't been replaced by getCalendarBySearch?
+ // Beacause getCalendarBySearch is not stable, it will miss some bangumi items.
static Future getCalendar() async {
List<List<BangumiItem>> bangumiCalendar = [];
try {
@@ -35,6 +37,55 @@ class BangumiHTTP {
return bangumiCalendar;
}
+ // Get clander by search API, we need a list of strings (the start of the season and the end of the season) eg: ["2024-07-01", "2024-10-01"]
+ // because the air date is the launch date of the anime, it is usually a few days before the start of the season
+ // So we usually use the start of the season month -1 and the end of the season month -1
+ static Future getCalendarBySearch(List<String> dateRange) async {
+ List<BangumiItem> bangumiList = [];
+ List<List<BangumiItem>> bangumiCalendar = [];
+ var params = <String, dynamic>{
+ "keyword": "",
+ "filter": {
+ "type": [2],
+ "tag": ["日本"],
+ "air_date": [">=${dateRange[0]}", "<${dateRange[1]}"],
+ "rank": [">0", "<=99999"],
+ "nsfw": true
+ }
+ };
+ try {
+ final res = await Request().post(Api.bangumiRankSearch,
+ data: params,
+ options: Options(
+ headers: bangumiHTTPHeader, contentType: 'application/json'));
+ final jsonData = res.data;
+ final jsonList = jsonData['data'];
+ for (dynamic jsonItem in jsonList) {
+ if (jsonItem is Map<String, dynamic>) {
+ bangumiList.add(BangumiItem.fromJson(jsonItem));
+ }
+ }
+ } catch (e) {
+ KazumiLogger()
+ .log(Level.error, 'Resolve bangumi list failed ${e.toString()}');
+ }
+ try {
+ for (int weekday = 1; weekday <= 7; weekday++) {
+ List<BangumiItem> bangumiDayList = [];
+ for (BangumiItem bangumiItem in bangumiList) {
+ if (bangumiItem.airWeekday == weekday) {
+ bangumiDayList.add(bangumiItem);
+ }
+ }
+ bangumiCalendar.add(bangumiDayList);
+ }
+ } catch (e) {
+ KazumiLogger().log(
+ Level.error, 'Fetch bangumi item to calendar failed ${e.toString()}');
+ }
+ return bangumiCalendar;
+ }
+
static Future getBangumiList({int rank = 2, String tag = ''}) async {
List<BangumiItem> bangumiList = [];
late Map<String, dynamic> params;
@@ -63,7 +114,9 @@ class BangumiHTTP {
}
try {
final res = await Request().post(Api.bangumiRankSearch,
- data: params, options: Options(headers: bangumiHTTPHeader, contentType: 'application/json'));
+ data: params,
+ options: Options(
+ headers: bangumiHTTPHeader, contentType: 'application/json'));
final jsonData = res.data;
final jsonList = jsonData['data'];
for (dynamic jsonItem in jsonList) {
@@ -94,7 +147,9 @@ class BangumiHTTP {
try {
final res = await Request().post(Api.bangumiRankSearch,
- data: params, options: Options(headers: bangumiHTTPHeader, contentType: 'application/json'));
+ data: params,
+ options: Options(
+ headers: bangumiHTTPHeader, contentType: 'application/json'));
final jsonData = res.data;
final jsonList = jsonData['data'];
for (dynamic jsonItem in jsonList) {
diff --git a/lib/utils/anime_season.dart b/lib/utils/anime_season.dart
new file mode 100644
index 00000000..3c6b5ccf
--- /dev/null
+++ b/lib/utils/anime_season.dart
@@ -0,0 +1,53 @@
+/// This class asks for DateTime to get a string to indicate seasonal anime
+class AnimeSeason {
+ late DateTime _date;
+ final _seasons = ['冬季', '春季', '夏季', '秋季'];
+
+ AnimeSeason(DateTime date) {
+ _date = date;
+ }
+
+ List<int> _getYearAndSeason(DateTime dt) {
+ int year = dt.year;
+ int month = dt.month;
+
+ int season;
+ if ((month == 1) || (month == 2) || (month == 3)) {
+ season = 0;
+ } else if ((month == 4) || (month == 5) || (month == 6)) {
+ season = 1;
+ } else if ((month == 7) || (month == 8) || (month == 9)) {
+ season = 2;
+ } else {
+ season = 3;
+ }
+
+ return [year, season];
+ }
+
+ // Convert the DateTime to a List containing two strings (the start of the season -1 and the end of the season -1 ) eg: 2024-09-23 -> ['2024-06-01', '2024-09-01']
+ // why -1? because the air date is the launch date of the anime, it is usually a few days before the start of the season
+ List<String> toSeasonStartAndEnd() {
+ var yas = _getYearAndSeason(_date);
+ int year = yas[0];
+ int season = yas[1];
+
+ var end = DateTime(year, (season + 1) * 3, 1);
+
+ int startMonth = season * 3;
+ if (startMonth == 0) {
+ startMonth = 12;
+ year--;
+ }
+
+ var start = DateTime(year, season * 3 + 1, 1);
+ return [start.toString(), end.toString()];
+ }
+
+ @override
+ String toString() {
+ var yas = _getYearAndSeason(_date);
+
+ return '${yas[0]}年${_seasons[yas[1]]}新番';
+ }
+}
diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart
index bea8d840..8155a06e 100644
--- a/lib/utils/utils.dart
+++ b/lib/utils/utils.dart
@@ -310,6 +310,16 @@ class Utils {
return '$formattedHours:$formattedMinutes';
}
+ // 日期字符串转换为 weekday (eg: 2024-09-23 -> 1 (星期一))
+ static int dateStringToWeekday(String dateString) {
+ try {
+ DateTime date = DateTime.parse(dateString);
+ return date.weekday;
+ } catch (_) {
+ return 1;
+ }
+ }
+
static String appSign(
Map<String, dynamic> params, String appkey, String appsec) {
params['appkey'] = appkey;