feat: 专栏文章渲染

This commit is contained in:
guozhigq
2023-09-16 21:47:27 +08:00
parent 380ada9ae0
commit 3daa06a198
8 changed files with 649 additions and 169 deletions

View File

@ -3,8 +3,12 @@ import 'package:html/parser.dart';
import 'package:pilipala/http/index.dart';
class HtmlHttp {
static Future reqHtml(id) async {
var response = await Request().get("https://www.bilibili.com/opus/$id");
// article
static Future reqHtml(id, dynamicType) async {
var response = await Request().get(
"https://www.bilibili.com/opus/$id",
extra: {'ua': 'pc'},
);
Document rootTree = parse(response.data);
Element body = rootTree.body!;
Element appDom = body.querySelector('#app')!;
@ -34,7 +38,46 @@ class HtmlHttp {
'uname': uname,
'updateTime': updateTime,
'content': opusContent,
'commentId': commentId
'commentId': int.parse(commentId)
};
}
// read
static Future reqReadHtml(id, dynamicType) async {
var response = await Request().get(
"https://www.bilibili.com/$dynamicType/$id/",
extra: {'ua': 'pc'},
);
Document rootTree = parse(response.data);
Element body = rootTree.body!;
Element appDom = body.querySelector('#app')!;
Element authorHeader = appDom.querySelector('.up-left')!;
// 头像
// String avatar =
// authorHeader.querySelector('.bili-avatar-img')!.attributes['data-src']!;
// print(avatar);
// avatar = 'https:${avatar.split('@')[0]}';
String uname = authorHeader.querySelector('.up-name')!.text.trim();
// 动态详情
Element opusDetail = appDom.querySelector('.article-content')!;
// 发布时间
// String updateTime =
// opusDetail.querySelector('.opus-module-author__pub__text')!.text;
// print(updateTime);
//
String opusContent =
opusDetail.querySelector('#read-article-holder')!.innerHtml;
RegExp digitRegExp = RegExp(r'\d+');
Iterable<Match> matches = digitRegExp.allMatches(id);
String number = matches.first.group(0)!;
return {
'status': true,
'avatar': '',
'uname': uname,
'updateTime': '',
'content': opusContent,
'commentId': int.parse(number)
};
}
}

View File

@ -130,11 +130,19 @@ class Request {
Response response;
Options options;
ResponseType resType = ResponseType.json;
if (extra != null) {
resType = extra!['resType'] ?? ResponseType.json;
}
options = Options();
options.responseType = resType;
if (extra != null) {
options.responseType = extra!['resType'] ?? ResponseType.json;
if (extra['ua'] != null) {
print(options.headers);
options.headers = {'user-agent': headerUa(type: extra['ua'])};
// options.headers!['user-agent'] = headerUa(type: extra['ua']);
}
}
try {
response = await dio.get(
url,
@ -201,14 +209,19 @@ class Request {
token.cancel("cancelled");
}
String headerUa() {
String headerUa({type = 'mob'}) {
String headerUa = '';
if (Platform.isIOS) {
headerUa =
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1';
if (type == 'mob') {
if (Platform.isIOS) {
headerUa =
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1';
} else {
headerUa =
'Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36';
}
} else {
headerUa =
'Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36';
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15';
}
return headerUa;
}