diff --git a/lib/http/common.dart b/lib/http/common.dart
index 2f5f0e84..9644d142 100644
--- a/lib/http/common.dart
+++ b/lib/http/common.dart
@@ -1,8 +1,14 @@
+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'');
static Future unReadDynamic() async {
var res = await Request().get(Api.getUnreadDynamic,
data: {'alltype_offset': 0, 'video_offset': '', 'article_offset': 0});
@@ -43,4 +49,42 @@ class CommonHttp {
};
}
}
+
+ 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.generate(32, (_) => rand.nextInt(256))
+ ..addAll(List.filled(4, 0))
+ ..addAll([73, 69, 78, 68])
+ ..addAll(List.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');
+ }
+ }
}
diff --git a/lib/http/init.dart b/lib/http/init.dart
index 8a11034c..03de43b7 100644
--- a/lib/http/init.dart
+++ b/lib/http/init.dart
@@ -1,9 +1,7 @@
// 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';
@@ -13,7 +11,6 @@ import 'package:pilipala/models/user/info.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';
@@ -27,8 +24,6 @@ class Request {
late bool enableSystemProxy;
late String systemProxyHost;
late String systemProxyPort;
- static final RegExp spmPrefixExp =
- RegExp(r'');
static String? buvid;
/// 设置cookie
@@ -62,11 +57,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}')
@@ -122,30 +112,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.generate(32, (_) => rand.nextInt(256)) +
- List.filled(4, 0) +
- [73, 69, 78, 68] +
- List.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
*/
diff --git a/lib/main.dart b/lib/main.dart
index 1ec86c8e..fcb29fad 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -10,6 +10,7 @@ import 'package:flutter/material.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/common/widgets/custom_toast.dart';
+import 'package:pilipala/http/common.dart';
import 'package:pilipala/http/init.dart';
import 'package:pilipala/models/common/color_type.dart';
import 'package:pilipala/models/common/theme_type.dart';
@@ -66,6 +67,7 @@ void main() async {
PiliSchame.init();
await GlobalDataCache().initialize();
+ CommonHttp.buvidActivate();
}
class MyApp extends StatelessWidget {