Merge branch 'main' into feature-cookies

This commit is contained in:
guozhigq
2024-12-01 21:13:49 +08:00
168 changed files with 2481 additions and 1772 deletions

View File

@ -619,4 +619,11 @@ class Api {
/// 获取空降区间
static const String getSkipSegments =
'${HttpString.sponsorBlockBaseUrl}/api/skipSegments';
/// 视频标签
static const String videoTag = '/x/tag/archive/tags';
/// 修复标题和海报
// /api/view?id=${aid} /all/video/av${aid} /video/av${aid}/
static const String fixTitleAndPic = '${HttpString.biliplusBaseUrl}/api/view';
}

View File

@ -1,8 +1,15 @@
import 'package:pilipala/models/common/invalid_video.dart';
import 'dart:convert';
import 'dart:math';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:pilipala/models/sponsor_block/segment.dart';
import 'index.dart';
class CommonHttp {
static final RegExp spmPrefixExp =
RegExp(r'<meta name="spm_prefix" content="([^"]+?)">');
static Future unReadDynamic() async {
var res = await Request().get(Api.getUnreadDynamic,
data: {'alltype_offset': 0, 'video_offset': '', 'article_offset': 0});
@ -18,10 +25,9 @@ class CommonHttp {
}
static Future querySkipSegments({required String bvid}) async {
var res = await Request().get(Api.getSkipSegments, data: {
var res = await Request().getWithoutCookie(Api.getSkipSegments, data: {
'videoID': bvid,
});
print(res.data);
if (res.data is List && res.data.isNotEmpty) {
try {
return {
@ -39,9 +45,73 @@ class CommonHttp {
}
} else {
return {
'status': true,
'status': false,
'data': [],
};
}
}
static Future fixVideoPicAndTitle({required int aid}) async {
var res = await Request().getWithoutCookie(Api.fixTitleAndPic, data: {
'id': aid,
});
if (res != null) {
if (res.data['code'] == -404) {
return {
'status': false,
'data': null,
'msg': '没有相关信息',
};
} else {
return {
'status': true,
'data': InvalidVideoModel.fromJson(res.data),
};
}
} else {
return {
'status': false,
'data': null,
'msg': '没有相关信息',
};
}
}
static Future buvidActivate() async {
try {
// 获取 HTML 数据
var html = await Request().get(Api.dynamicSpmPrefix);
// 提取 spmPrefix
String spmPrefix = spmPrefixExp.firstMatch(html.data)?.group(1) ?? '';
// 生成随机 PNG 结束部分
Random rand = Random();
String randPngEnd = base64.encode(
List<int>.generate(32, (_) => rand.nextInt(256))
..addAll(List<int>.filled(4, 0))
..addAll([73, 69, 78, 68])
..addAll(List<int>.generate(4, (_) => rand.nextInt(256))),
);
// 构建 JSON 数据
String jsonData = json.encode({
'3064': 1,
'39c8': '$spmPrefix.fp.risk',
'3c43': {
'adca': 'Linux',
'bfe9': randPngEnd.substring(randPngEnd.length - 50),
},
});
// 发送 POST 请求
await Request().post(
Api.activateBuvidApi,
data: {'payload': jsonData},
options: Options(contentType: 'application/json'),
);
} catch (err) {
debugPrint('buvidActivate error: $err');
}
}
}

View File

@ -8,6 +8,8 @@ class HttpString {
static const String messageBaseUrl = 'https://message.bilibili.com';
static const String bangumiBaseUrl = 'https://bili.meark.me';
static const String sponsorBlockBaseUrl = 'https://www.bsbsb.top';
static const String biliplusBaseUrl = 'https://www.biliplus.com';
static const List<int> validateStatusCodes = [
302,
304,

View File

@ -17,7 +17,9 @@ class DanmakaHttp {
var response = await Request().get(
Api.webDanmaku,
data: params,
extra: {'resType': ResponseType.bytes},
options: Options(
responseType: ResponseType.bytes,
),
);
return DmSegMobileReply.fromBuffer(response.data);
}

View File

@ -92,7 +92,7 @@ class DynamicsHttp {
//
static Future dynamicDetail({
String? id,
required String id,
}) async {
var res = await Request().get(Api.dynamicDetail, data: {
'timezone_offset': -480,

View File

@ -1,9 +1,6 @@
// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:math' show Random;
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
@ -12,7 +9,6 @@ import 'package:hive/hive.dart';
import 'package:pilipala/utils/id_utils.dart';
import '../utils/storage.dart';
import '../utils/utils.dart';
import 'api.dart';
import 'constants.dart';
import 'interceptor.dart';
@ -21,19 +17,17 @@ class Request {
static late CookieManager cookieManager;
static late final Dio dio;
factory Request() => _instance;
Box setting = GStrorage.setting;
static Box localCache = GStrorage.localCache;
Box setting = GStorage.setting;
static Box localCache = GStorage.localCache;
late bool enableSystemProxy;
late String systemProxyHost;
late String systemProxyPort;
static final RegExp spmPrefixExp =
RegExp(r'<meta name="spm_prefix" content="([^"]+?)">');
static String? buvid;
/// 设置cookie
static setCookie() async {
Box userInfoCache = GStrorage.userInfo;
Box setting = GStrorage.setting;
Box userInfoCache = GStorage.userInfo;
Box setting = GStorage.setting;
final String cookiePath = await Utils.getCookiePath();
final PersistCookieJar cookieJar = PersistCookieJar(
ignoreExpires: true,
@ -50,11 +44,6 @@ class Request {
baseUrlType = 'bangumi';
}
setBaseUrl(type: baseUrlType);
try {
await buvidActivate();
} catch (e) {
log("setCookie, ${e.toString()}");
}
final String cookieString = cookie
.map((Cookie cookie) => '${cookie.name}=${cookie.value}')
@ -80,9 +69,12 @@ class Request {
}
final List<Cookie> cookies = await cookieManager.cookieJar
.loadForRequest(Uri.parse(HttpString.baseUrl));
buvid = cookies.firstWhere((cookie) => cookie.name == 'buvid3').value;
if (buvid == null) {
.loadForRequest(Uri.parse(HttpString.apiBaseUrl));
buvid = cookies
.firstWhere((cookie) => cookie.name == 'buvid3',
orElse: () => Cookie('buvid3', ''))
.value;
if (buvid == null || buvid!.isEmpty) {
try {
var result = await Request().get(
"${HttpString.apiBaseUrl}/x/frontend/finger/spi",
@ -110,30 +102,6 @@ class Request {
dio.options.headers['referer'] = 'https://www.bilibili.com/';
}
static Future buvidActivate() async {
var html = await Request().get(Api.dynamicSpmPrefix);
String spmPrefix = spmPrefixExp.firstMatch(html.data)!.group(1)!;
Random rand = Random();
String rand_png_end = base64.encode(
List<int>.generate(32, (_) => rand.nextInt(256)) +
List<int>.filled(4, 0) +
[73, 69, 78, 68] +
List<int>.generate(4, (_) => rand.nextInt(256)));
String jsonData = json.encode({
'3064': 1,
'39c8': '${spmPrefix}.fp.risk',
'3c43': {
'adca': 'Linux',
'bfe9': rand_png_end.substring(rand_png_end.length - 50),
},
});
await Request().post(Api.activateBuvidApi,
data: {'payload': jsonData},
options: Options(contentType: 'application/json'));
}
/*
* config it and create
*/
@ -198,17 +166,13 @@ class Request {
*/
get(url, {data, Options? options, cancelToken, extra}) async {
Response response;
options ??= Options(); // 如果 options 为 null则初始化一个新的 Options 对象
ResponseType resType = ResponseType.json;
if (extra != null) {
resType = extra['resType'] ?? ResponseType.json;
if (extra['ua'] != null) {
options.headers = {'user-agent': headerUa(type: extra['ua'])};
options ??= Options();
options.headers ??= <String, dynamic>{};
options.headers?['user-agent'] = headerUa(type: extra['ua']);
}
}
options.responseType = resType;
try {
response = await dio.get(
url,

View File

@ -4,6 +4,7 @@ import 'package:hive/hive.dart';
import 'package:html/parser.dart';
import 'package:pilipala/models/member/article.dart';
import 'package:pilipala/models/member/like.dart';
import 'package:pilipala/models/user/info.dart';
import 'package:pilipala/utils/global_data_cache.dart';
import '../common/constants.dart';
import '../models/dynamics/result.dart';
@ -25,7 +26,7 @@ class MemberHttp {
}) async {
String? wWebid;
if ((await getWWebid(mid: mid))['status']) {
wWebid = GlobalDataCache().wWebid;
wWebid = GlobalDataCache.wWebid;
}
Map params = await WbiSign().makSign({
@ -470,11 +471,11 @@ class MemberHttp {
SmartDialog.dismiss();
if (res.data['code'] == 0) {
String accessKey = res.data['data']['access_token'];
Box localCache = GStrorage.localCache;
Box userInfoCache = GStrorage.userInfo;
var userInfo = userInfoCache.get('userInfoCache');
Box localCache = GStorage.localCache;
Box userInfoCache = GStorage.userInfo;
final UserInfoData? userInfo = userInfoCache.get('userInfoCache');
localCache.put(
LocalCacheKey.accessKey, {'mid': userInfo.mid, 'value': accessKey});
LocalCacheKey.accessKey, {'mid': userInfo!.mid, 'value': accessKey});
return {'status': true, 'data': [], 'msg': '操作成功'};
} else {
return {
@ -573,7 +574,7 @@ class MemberHttp {
}
static Future getWWebid({required int mid}) async {
String? wWebid = GlobalDataCache().wWebid;
String? wWebid = GlobalDataCache.wWebid;
if (wWebid != null) {
return {'status': true, 'data': wWebid};
}
@ -587,7 +588,7 @@ class MemberHttp {
final content = match.group(1);
String decodedString = Uri.decodeComponent(content!);
Map<String, dynamic> map = jsonDecode(decodedString);
GlobalDataCache().wWebid = map['access_id'];
GlobalDataCache.wWebid = map['access_id'];
return {'status': true, 'data': map['access_id']};
} else {
return {'status': false, 'data': '请检查登录状态'};
@ -604,7 +605,7 @@ class MemberHttp {
}) async {
String? wWebid;
if ((await getWWebid(mid: mid))['status']) {
wWebid = GlobalDataCache().wWebid;
wWebid = GlobalDataCache.wWebid;
}
Map params = await WbiSign().makSign({
'host_mid': mid,

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:html/parser.dart';
import 'package:pilipala/models/read/opus.dart';
import 'package:pilipala/models/read/read.dart';
@ -65,6 +66,11 @@ class ReadHttp {
var res = await Request().get(
'https://www.bilibili.com/read/cv$id',
extra: {'ua': 'pc'},
options: Options(
headers: {
'cookie': 'opus-goback=1',
},
),
);
String scriptContent =
extractScriptContents(parse(res.data).body!.outerHtml)[0];

View File

@ -11,7 +11,7 @@ import '../utils/storage.dart';
import 'index.dart';
class SearchHttp {
static Box setting = GStrorage.setting;
static Box setting = GStorage.setting;
static Future hotSearchList() async {
var res = await Request().get(Api.hotSearchList);
if (res.data is String) {

View File

@ -3,6 +3,7 @@ import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/utils/id_utils.dart';
import 'package:pilipala/models/video/tags.dart';
import '../common/constants.dart';
import '../models/common/reply_type.dart';
import '../models/home/rcmd/result.dart';
@ -25,11 +26,11 @@ import 'init.dart';
/// 返回{'status': bool, 'data': List}
/// view层根据 status 判断渲染逻辑
class VideoHttp {
static Box localCache = GStrorage.localCache;
static Box setting = GStrorage.setting;
static Box localCache = GStorage.localCache;
static Box setting = GStorage.setting;
static bool enableRcmdDynamic =
setting.get(SettingBoxKey.enableRcmdDynamic, defaultValue: true);
static Box userInfoCache = GStrorage.userInfo;
static Box userInfoCache = GStorage.userInfo;
// 首页推荐视频
static Future rcmdVideoList({required int ps, required int freshIdx}) async {
@ -607,4 +608,19 @@ class VideoHttp {
};
}
}
// 获取视频标签
static Future getVideoTag({required String bvid}) async {
var res = await Request().get(Api.videoTag, data: {'bvid': bvid});
if (res.data['code'] == 0) {
return {
'status': true,
'data': res.data['data'].map<VideoTagItem>((e) {
return VideoTagItem.fromJson(e);
}).toList()
};
} else {
return {'status': false, 'data': [], 'msg': res.data['message']};
}
}
}