Skip to content

Commit

Permalink
opt: url scheme优化 issues #581
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Mar 3, 2024
1 parent c4b3446 commit 8109314
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
4 changes: 4 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
android:pathPattern="/mobile/video/.*" />
<data android:scheme="https" android:host="www.bilibili.com"
android:pathPattern="/mobile/video/.*" />
<data android:scheme="https" android:host="b23.tv"
android:pathPattern="/*" />
<data android:scheme="https" android:host="space.bilibili.com"
android:pathPattern="/*" />

</intent-filter>
</activity>
Expand Down
118 changes: 81 additions & 37 deletions lib/utils/app_scheme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:get/get.dart';
import '../http/search.dart';
import '../models/common/search_type.dart';
import 'id_utils.dart';
import 'url_utils.dart';
import 'utils.dart';

class PiliSchame {
Expand Down Expand Up @@ -38,23 +39,16 @@ class PiliSchame {
final String path = value.path;

if (scheme == 'bilibili') {
// bilibili://root
if (host == 'root') {
Navigator.popUntil(
Get.context!, (Route<dynamic> route) => route.isFirst);
}

// bilibili://space/{uid}
else if (host == 'space') {
} else if (host == 'space') {
final String mid = path.split('/').last;
Get.toNamed<dynamic>(
'/member?mid=$mid',
arguments: <String, dynamic>{'face': null},
);
}

// bilibili://video/{aid}
else if (host == 'video') {
} else if (host == 'video') {
String pathQuery = path.split('/').last;
final numericRegex = RegExp(r'^[0-9]+$');
if (numericRegex.hasMatch(pathQuery)) {
Expand All @@ -68,24 +62,16 @@ class PiliSchame {
} else {
SmartDialog.showToast('投稿匹配失败');
}
}

// bilibili://live/{roomid}
else if (host == 'live') {
} else if (host == 'live') {
final String roomId = path.split('/').last;
Get.toNamed<dynamic>('/liveRoom?roomid=$roomId',
arguments: <String, String?>{'liveItem': null, 'heroTag': roomId});
}

// bilibili://bangumi/season/${ssid}
else if (host == 'bangumi') {
} else if (host == 'bangumi') {
if (path.startsWith('/season')) {
final String seasonId = path.split('/').last;
_bangumiPush(int.parse(seasonId));
_bangumiPush(int.parse(seasonId), null);
}
}
// 专栏 bilibili://opus/detail/883089655985078289
else if (host == 'opus') {
} else if (host == 'opus') {
if (path.startsWith('/detail')) {
var opusId = path.split('/').last;
Get.toNamed(
Expand All @@ -101,6 +87,9 @@ class PiliSchame {
Get.toNamed('/searchResult', parameters: {'keyword': ''});
}
}
if (scheme == 'https') {
_fullPathPush(value);
}
}

// 投稿跳转
Expand Down Expand Up @@ -131,10 +120,10 @@ class PiliSchame {
}

// 番剧跳转
static Future<void> _bangumiPush(int seasonId) async {
static Future<void> _bangumiPush(int? seasonId, int? epId) async {
SmartDialog.showLoading<dynamic>(msg: '获取中...');
try {
var result = await SearchHttp.bangumiInfo(seasonId: seasonId, epId: null);
var result = await SearchHttp.bangumiInfo(seasonId: seasonId, epId: epId);
if (result['status']) {
var bangumiDetail = result['data'];
final int cid = bangumiDetail.episodes!.first.cid;
Expand All @@ -151,6 +140,8 @@ class PiliSchame {
},
),
);
} else {
SmartDialog.showToast(result['msg']);
}
} catch (e) {
SmartDialog.showToast('番剧获取失败:$e');
Expand All @@ -163,29 +154,67 @@ class PiliSchame {
// final String scheme = value.scheme!;
final String host = value.host!;
final String? path = value.path;
// Map<String, String> query = value.query!;
if (host.startsWith('live.bilibili')) {
Map<String, String>? query = value.query;
RegExp regExp = RegExp(r'^(www\.)?m?\.(bilibili\.com)$');
if (regExp.hasMatch(host)) {
print('bilibili.com');
} else if (host.contains('live')) {
int roomId = int.parse(path!.split('/').last);
// print('直播');
Get.toNamed('/liveRoom?roomid=$roomId',
arguments: {'liveItem': null, 'heroTag': roomId.toString()});
return;
}
if (host.startsWith('space.bilibili')) {
print('个人空间');
Get.toNamed(
'/liveRoom?roomid=$roomId',
arguments: {'liveItem': null, 'heroTag': roomId.toString()},
);
} else if (host.contains('space')) {
var mid = path!.split('/').last;
Get.toNamed('/member?mid=$mid', arguments: {'face': ''});
return;
} else if (host == 'b23.tv') {
final String fullPath = 'https://$host$path';
final String redirectUrl = await UrlUtils.parseRedirectUrl(fullPath);
final String pathSegment = Uri.parse(redirectUrl).path;
final String lastPathSegment = pathSegment.split('/').last;
final RegExp avRegex = RegExp(r'^[aA][vV]\d+', caseSensitive: false);
if (avRegex.hasMatch(lastPathSegment)) {
final Map<String, dynamic> map =
IdUtils.matchAvorBv(input: lastPathSegment);
if (map.containsKey('AV')) {
_videoPush(map['AV']! as int, null);
} else if (map.containsKey('BV')) {
_videoPush(null, map['BV'] as String);
} else {
SmartDialog.showToast('投稿匹配失败');
}
} else if (lastPathSegment.startsWith('ep')) {
_handleEpisodePath(lastPathSegment, redirectUrl);
} else if (lastPathSegment.startsWith('ss')) {
_handleSeasonPath(lastPathSegment, redirectUrl);
} else if (lastPathSegment.startsWith('BV')) {
UrlUtils.matchUrlPush(
lastPathSegment,
'',
redirectUrl,
);
} else {
Get.toNamed(
'/webview',
parameters: {'url': redirectUrl, 'type': 'url', 'pageTitle': ''},
);
}
}

if (path != null) {
final String area = path.split('/')[1];
final String area = path.split('/').last;
switch (area) {
case 'bangumi':
// print('番剧');
final String seasonId = path.split('/').last;
_bangumiPush(matchNum(seasonId).first);
print('番剧');
if (area.startsWith('ep')) {
_bangumiPush(null, matchNum(area).first);
} else if (area.startsWith('ss')) {
_bangumiPush(matchNum(area).first, null);
}
break;
case 'video':
// print('投稿');
print('投稿');
final Map<String, dynamic> map = IdUtils.matchAvorBv(input: path);
if (map.containsKey('AV')) {
_videoPush(map['AV']! as int, null);
Expand All @@ -200,6 +229,7 @@ class PiliSchame {
break;
case 'space':
print('个人空间');
Get.toNamed('/member?mid=$area', arguments: {'face': ''});
break;
}
}
Expand All @@ -211,4 +241,18 @@ class PiliSchame {

return matches.map((Match match) => int.parse(match.group(0)!)).toList();
}

static void _handleEpisodePath(String lastPathSegment, String redirectUrl) {
final String seasonId = _extractIdFromPath(lastPathSegment);
_bangumiPush(null, matchNum(seasonId).first);
}

static void _handleSeasonPath(String lastPathSegment, String redirectUrl) {
final String seasonId = _extractIdFromPath(lastPathSegment);
_bangumiPush(matchNum(seasonId).first, null);
}

static String _extractIdFromPath(String lastPathSegment) {
return lastPathSegment.split('/').last;
}
}
5 changes: 3 additions & 2 deletions lib/utils/id_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class IdUtils {
if (input == null || input.isEmpty) {
return result;
}
final RegExp bvRegex = RegExp(r'BV[0-9A-Za-z]{10}', caseSensitive: false);
final RegExp avRegex = RegExp(r'AV\d+', caseSensitive: false);
final RegExp bvRegex =
RegExp(r'[bB][vV][0-9A-Za-z]{10}', caseSensitive: false);
final RegExp avRegex = RegExp(r'[aA][vV]\d+', caseSensitive: false);

final Iterable<Match> bvMatches = bvRegex.allMatches(input);
final Iterable<Match> avMatches = avRegex.allMatches(input);
Expand Down

0 comments on commit 8109314

Please sign in to comment.