generated from dellevin/template
支持番茄阅读链接添加
This commit is contained in:
@@ -3,11 +3,22 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import '../../widgets/app_overlay.dart';
|
||||
|
||||
/// 豆瓣影视WebView页面 - 用于抓取影视信息
|
||||
/// 豆瓣WebView页面 - 用于抓取 影视/书籍/游戏 信息
|
||||
class DoubanWebViewPage extends StatefulWidget {
|
||||
final String url;
|
||||
|
||||
const DoubanWebViewPage({super.key, required this.url});
|
||||
/// 分类:movie / book / game
|
||||
final String category;
|
||||
|
||||
/// 来源:douban / fanqie(番茄小说)
|
||||
final String source;
|
||||
|
||||
const DoubanWebViewPage({
|
||||
super.key,
|
||||
required this.url,
|
||||
this.category = 'movie',
|
||||
this.source = 'douban',
|
||||
});
|
||||
|
||||
@override
|
||||
State<DoubanWebViewPage> createState() => _DoubanWebViewPageState();
|
||||
@@ -18,13 +29,22 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
bool _isLoading = true;
|
||||
bool _isExtracting = false; // 防止重复提取
|
||||
|
||||
String _titleFor() {
|
||||
if (widget.source == 'fanqie') return '番茄阅读';
|
||||
return switch (widget.category) {
|
||||
'book' => '豆瓣书籍',
|
||||
'game' => '豆瓣游戏',
|
||||
_ => '豆瓣影视',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
return Scaffold(
|
||||
backgroundColor: colors.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('豆瓣影视'),
|
||||
title: Text(_titleFor()),
|
||||
leading: _buildBackButton(),
|
||||
actions: [
|
||||
// 提取按钮 - 始终显示
|
||||
@@ -135,8 +155,14 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
/// 显示提取的信息对话框
|
||||
Future<void> _showExtractedInfo() async {
|
||||
// 先提取信息
|
||||
final movieInfo = await _extractMovieInfo();
|
||||
if (movieInfo == null) return;
|
||||
final info = await _extractInfo();
|
||||
if (info == null) return;
|
||||
|
||||
final secondaryLabel = switch (widget.category) {
|
||||
'book' => '作者',
|
||||
'game' => '开发商',
|
||||
_ => '导演',
|
||||
};
|
||||
|
||||
// 显示提取的信息
|
||||
if (mounted) {
|
||||
@@ -144,11 +170,29 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
context: context,
|
||||
builder: (ctx) {
|
||||
final colors = Theme.of(ctx).colorScheme;
|
||||
final isFanqie = widget.source == 'fanqie';
|
||||
final rows = isFanqie
|
||||
? <Widget>[
|
||||
_buildInfoRow(colors, '书名', info['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '作者', info['author']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '类型', info['genres']?.toString() ?? '未提取到'),
|
||||
if (info['summary'] != null)
|
||||
_buildInfoRow(colors, '简介', _truncate(info['summary'])),
|
||||
]
|
||||
: <Widget>[
|
||||
_buildInfoRow(colors, '标题', info['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '评分', info['rating']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, secondaryLabel, info['director']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '类型', info['genres']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '日期', info['releaseDate']?.toString() ?? '未提取到'),
|
||||
if (info['summary'] != null)
|
||||
_buildInfoRow(colors, '简介', _truncate(info['summary'])),
|
||||
];
|
||||
return AlertDialog(
|
||||
backgroundColor: colors.surface,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: Text(
|
||||
'提取的影视信息',
|
||||
'提取的信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
@@ -159,22 +203,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInfoRow(colors, '标题', movieInfo['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '导演', movieInfo['director']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '类型', movieInfo['genres']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow(colors, '上映日期', movieInfo['releaseDate']?.toString() ?? '未提取到'),
|
||||
if (movieInfo['summary'] != null)
|
||||
_buildInfoRow(
|
||||
colors,
|
||||
'简介',
|
||||
movieInfo['summary'].toString().substring(
|
||||
0,
|
||||
movieInfo['summary'].toString().length > 100
|
||||
? 100
|
||||
: movieInfo['summary'].toString().length) +
|
||||
'...'),
|
||||
],
|
||||
children: rows,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
@@ -188,7 +217,7 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(ctx);
|
||||
Navigator.pop(context, movieInfo);
|
||||
Navigator.pop(context, info);
|
||||
},
|
||||
child: Text(
|
||||
'使用此信息',
|
||||
@@ -234,8 +263,8 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 提取影视信息
|
||||
Future<Map<String, dynamic>?> _extractMovieInfo() async {
|
||||
/// 提取信息(按分类选择抓取脚本)
|
||||
Future<Map<String, dynamic>?> _extractInfo() async {
|
||||
// 检查是否已提取过,避免重复点击
|
||||
if (_isExtracting || _controller == null) return null;
|
||||
|
||||
@@ -252,7 +281,56 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
);
|
||||
|
||||
// 执行JavaScript代码提取页面信息
|
||||
final result = await _controller!.evaluateJavascript(source: r'''
|
||||
final result = await _controller!.evaluateJavascript(source: _scriptFor(widget.source));
|
||||
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
|
||||
// 解析提取的信息
|
||||
// evaluateJavascript 返回 JS 值,JSON.stringify 的结果是字符串
|
||||
final String jsonStr = result?.toString() ?? '';
|
||||
if (jsonStr.isEmpty) return null;
|
||||
|
||||
// result 是 JSON.stringify 的输出,可能带外层引号
|
||||
final String cleanJson = jsonStr.startsWith('"') && jsonStr.endsWith('"')
|
||||
? jsonDecode(jsonStr) as String
|
||||
: jsonStr;
|
||||
final Map<String, dynamic> movieInfo = jsonDecode(cleanJson) as Map<String, dynamic>;
|
||||
|
||||
return movieInfo;
|
||||
} catch (e) {
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('提取信息失败: $e')),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
_isExtracting = false;
|
||||
}
|
||||
}
|
||||
/// 截断长文本用于信息预览
|
||||
String _truncate(Object? v) {
|
||||
final s = v?.toString() ?? '';
|
||||
return s.length > 100 ? '${s.substring(0, 100)}...' : s;
|
||||
}
|
||||
}
|
||||
|
||||
/// 按来源/分类返回对应的抓取脚本
|
||||
String _scriptFor(String source) {
|
||||
return switch (source) {
|
||||
'fanqie' => _fanqieScript,
|
||||
'book' => _bookScript,
|
||||
'game' => _gameScript,
|
||||
_ => _movieScript,
|
||||
};
|
||||
}
|
||||
|
||||
/// 影视抓取脚本(移动版豆瓣 subject 页)
|
||||
const String _movieScript = r'''
|
||||
(function() {
|
||||
const info = {};
|
||||
|
||||
@@ -283,7 +361,9 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
}
|
||||
|
||||
// 获取评分 - 移动版可能在 mark-item 中
|
||||
const ratingEl = document.querySelector('.rating-num') || document.querySelector('.score');
|
||||
const ratingEl = document.querySelector('.score-num')
|
||||
|| document.querySelector('.rating-num')
|
||||
|| document.querySelector('.score');
|
||||
info.rating = ratingEl ? ratingEl.textContent.trim() : '';
|
||||
|
||||
// 获取导演 - 从演职员列表中找
|
||||
@@ -365,35 +445,213 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
|
||||
return JSON.stringify(info);
|
||||
})()
|
||||
''');
|
||||
''';
|
||||
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
/// 书籍抓取脚本(豆瓣 subject 页,兼容移动版/网页版)
|
||||
const String _bookScript = r'''
|
||||
(function() {
|
||||
const info = {};
|
||||
|
||||
// 解析提取的信息
|
||||
// evaluateJavascript 返回 JS 值,JSON.stringify 的结果是字符串
|
||||
final String jsonStr = result?.toString() ?? '';
|
||||
if (jsonStr.isEmpty) return null;
|
||||
// 标题(多种结构回退)
|
||||
const titleEl = document.querySelector('.sub-title')
|
||||
|| document.querySelector('h1[property="v:itemreviewed"]')
|
||||
|| document.querySelector('.title h1')
|
||||
|| document.querySelector('h1');
|
||||
info.title = titleEl ? titleEl.textContent.trim() : '';
|
||||
|
||||
// result 是 JSON.stringify 的输出,可能带外层引号
|
||||
final String cleanJson = jsonStr.startsWith('"') && jsonStr.endsWith('"')
|
||||
? jsonDecode(jsonStr) as String
|
||||
: jsonStr;
|
||||
final Map<String, dynamic> movieInfo = jsonDecode(cleanJson) as Map<String, dynamic>;
|
||||
// 封面(多种结构回退)
|
||||
const coverEl = document.querySelector('.sub-cover img')
|
||||
|| document.querySelector('#mainpic img')
|
||||
|| document.querySelector('.nbg img')
|
||||
|| document.querySelector('.pic img');
|
||||
if (coverEl) {
|
||||
let coverUrl = coverEl.src;
|
||||
if (coverUrl && coverUrl.includes('.webp')) {
|
||||
coverUrl = coverUrl.replace('.webp', '.jpg');
|
||||
}
|
||||
info.coverUrl = coverUrl;
|
||||
} else {
|
||||
info.coverUrl = '';
|
||||
}
|
||||
|
||||
return movieInfo;
|
||||
} catch (e) {
|
||||
// 关闭加载提示
|
||||
if (mounted) Navigator.pop(context);
|
||||
// 评分
|
||||
const ratingEl = document.querySelector('.score-num')
|
||||
|| document.querySelector('.rating-num')
|
||||
|| document.querySelector('.score')
|
||||
|| document.querySelector('.rating_self strong')
|
||||
|| document.querySelector('.ll.rating_num');
|
||||
info.rating = ratingEl ? ratingEl.textContent.trim() : '';
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('提取信息失败: $e')),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
_isExtracting = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 简介(网页版为 section-intro_desc,另加多级回退)
|
||||
const summaryEl = document.querySelector('.section-intro_desc')
|
||||
|| document.querySelector('.subject-intro p')
|
||||
|| document.querySelector('#link-report .intro')
|
||||
|| document.querySelector('.intro');
|
||||
info.summary = summaryEl ? summaryEl.textContent.trim().substring(0, 1000) : '';
|
||||
|
||||
// 副标题/别名
|
||||
const originalTitleEl = document.querySelector('.sub-original-title')
|
||||
|| document.querySelector('h2');
|
||||
info.alternateTitles = originalTitleEl ? [originalTitleEl.textContent.trim()] : [];
|
||||
|
||||
// 元信息:作者 / 译者 / 出版社 / 出版日期 / ISBN / 类型
|
||||
const metaEl = document.querySelector('.sub-meta')
|
||||
|| document.querySelector('#info')
|
||||
|| document.querySelector('.pub');
|
||||
const metaText = metaEl ? metaEl.textContent.replace(/\s+/g, ' ').trim() : '';
|
||||
info.director = metaText; // 暂存整行,供解析
|
||||
info.author = '';
|
||||
info.translator = '';
|
||||
info.publisher = '';
|
||||
info.releaseDate = '';
|
||||
info.isbn = '';
|
||||
info.genres = '';
|
||||
|
||||
if (metaText) {
|
||||
// 作者
|
||||
const authorMatch = metaText.match(/作者[:\s]*([^\/\n]+?)(?:\s*译者|\s*出版社|\s*出版年|\s*页数|\s*定价|\s*装帧|\s*丛书|\s*ISBN|$)/);
|
||||
if (authorMatch) info.author = authorMatch[1].trim();
|
||||
|
||||
// 译者
|
||||
const translatorMatch = metaText.match(/译者[:\s]*([^\/\n]+?)(?:\s*出版社|\s*出版年|\s*页数|\s*定价|\s*装帧|\s*丛书|\s*ISBN|$)/);
|
||||
if (translatorMatch) info.translator = translatorMatch[1].trim();
|
||||
|
||||
// 出版社
|
||||
const publisherMatch = metaText.match(/出版社[:\s]*([^\/\n]+?)(?:\s*出版年|\s*页数|\s*定价|\s*装帧|\s*丛书|\s*ISBN|$)/);
|
||||
if (publisherMatch) info.publisher = publisherMatch[1].trim();
|
||||
|
||||
// 出版日期 / 年份
|
||||
const dateMatch = metaText.match(/\d{4}-\d{1,2}(-\d{1,2})?/);
|
||||
if (dateMatch) info.releaseDate = dateMatch[0];
|
||||
|
||||
// ISBN
|
||||
const isbnMatch = metaText.match(/ISBN[:\s]*([\dXx-]+)/);
|
||||
if (isbnMatch) info.isbn = isbnMatch[1].trim();
|
||||
|
||||
// 类型标签
|
||||
const tagEls = document.querySelectorAll('.sub-tags a, .tags a, .tagCrumb a');
|
||||
const tags = [];
|
||||
tagEls.forEach(el => {
|
||||
const t = el.textContent.trim();
|
||||
if (t) tags.push(t);
|
||||
});
|
||||
info.genres = tags.join(',');
|
||||
}
|
||||
|
||||
return JSON.stringify(info);
|
||||
})()
|
||||
''';
|
||||
|
||||
/// 游戏抓取脚本(豆瓣 game subject 页 card 结构)
|
||||
const String _gameScript = r'''
|
||||
(function() {
|
||||
const info = {};
|
||||
|
||||
// 标题:card 内的 title
|
||||
const titleEl = document.querySelector('.card h1.title')
|
||||
|| document.querySelector('h1.title')
|
||||
|| document.querySelector('.sub-title')
|
||||
|| document.querySelector('h1[property="v:itemreviewed"]');
|
||||
info.title = titleEl ? titleEl.textContent.trim() : '';
|
||||
|
||||
// 封面:subject-info 内的 cover 图
|
||||
const coverEl = document.querySelector('.subject-info .cover')
|
||||
|| document.querySelector('.sub-cover img')
|
||||
|| document.querySelector('#mainpic img');
|
||||
if (coverEl) {
|
||||
let coverUrl = coverEl.src;
|
||||
if (coverUrl && coverUrl.includes('.webp')) {
|
||||
coverUrl = coverUrl.replace('.webp', '.jpg');
|
||||
}
|
||||
info.coverUrl = coverUrl;
|
||||
} else {
|
||||
info.coverUrl = '';
|
||||
}
|
||||
|
||||
// 评分:subject-info 内 rating 的 strong
|
||||
const ratingEl = document.querySelector('.subject-info .rating strong')
|
||||
|| document.querySelector('.score-num')
|
||||
|| document.querySelector('.rating-num');
|
||||
info.rating = ratingEl ? ratingEl.textContent.trim() : '';
|
||||
|
||||
// 简介:subject-intro 内 bd 的 p
|
||||
const summaryEl = document.querySelector('.subject-intro .bd p')
|
||||
|| document.querySelector('.section-intro_desc')
|
||||
|| document.querySelector('.subject-intro p')
|
||||
|| document.querySelector('.intro');
|
||||
info.summary = summaryEl ? summaryEl.textContent.trim().substring(0, 1000) : '';
|
||||
|
||||
// 元信息:subject-info 内 meta(斜杠分隔:类型 / 平台 / 发行日期)
|
||||
const metaEl = document.querySelector('.subject-info .meta')
|
||||
|| document.querySelector('.sub-meta');
|
||||
const metaText = metaEl ? metaEl.textContent.replace(/\s+/g, ' ').trim() : '';
|
||||
info.director = metaText; // 暂存整行,供解析
|
||||
info.developer = '';
|
||||
info.platforms = '';
|
||||
info.releaseDate = '';
|
||||
info.genres = '';
|
||||
|
||||
if (metaText) {
|
||||
// 发行日期(通常在最末尾,如 "2020-07-08 发行")
|
||||
const dateMatch = metaText.match(/\d{4}-\d{1,2}(-\d{1,2})?/);
|
||||
if (dateMatch) info.releaseDate = dateMatch[0];
|
||||
|
||||
// 按 / 切分,过滤空段和日期段
|
||||
const parts = metaText.split('/').map(s => s.trim()).filter(s => s && !s.match(/^\d{4}/));
|
||||
const platformNames = ['pc','ps4','ps5','psp','psv','ps3','ps2','xbox one','xbox series','xsx','xss','xbox 360','switch','wii u','wii','3ds','nds','nes','snes','steam','epic','itunes','ios','android','google play','itunes store','web','街机','街机盒'];
|
||||
const genres = [];
|
||||
const platforms = [];
|
||||
parts.forEach(p => {
|
||||
const lower = p.toLowerCase();
|
||||
if (platformNames.some(n => lower.includes(n))) {
|
||||
platforms.push(p);
|
||||
} else {
|
||||
genres.push(p);
|
||||
}
|
||||
});
|
||||
|
||||
// 孤立的日期年份(如 "2020")单列给 releaseDate
|
||||
if (!info.releaseDate) {
|
||||
const yearMatch = metaText.match(/\b(19|20)\d{2}\b/);
|
||||
if (yearMatch) info.releaseDate = yearMatch[0] + '-01-01';
|
||||
}
|
||||
|
||||
info.genres = genres.join(',');
|
||||
info.platforms = platforms.join(',');
|
||||
}
|
||||
|
||||
return JSON.stringify(info);
|
||||
})()
|
||||
''';
|
||||
|
||||
/// 番茄小说抓取脚本(fanjienovel.com 书籍页)
|
||||
const String _fanqieScript = r'''
|
||||
(function() {
|
||||
const info = {};
|
||||
const q = (s) => { const el = document.querySelector(s); return el ? el.textContent.trim() : ''; };
|
||||
|
||||
// 书名
|
||||
info.title = q('h1.info-name');
|
||||
|
||||
// 作者(如 "骁骑校 / 著")
|
||||
info.author = q('div.info-author');
|
||||
|
||||
// 封面
|
||||
const coverEl = document.querySelector('img.page-header-img');
|
||||
info.coverUrl = coverEl ? coverEl.src : '';
|
||||
|
||||
// 简介
|
||||
const summaryEl = document.querySelector('.abstract-content-text p')
|
||||
|| document.querySelector('.abstract-content-text')
|
||||
|| document.querySelector('.abstract-content');
|
||||
info.summary = summaryEl ? summaryEl.textContent.trim().substring(0, 1000) : '';
|
||||
|
||||
// 标签
|
||||
const tagEls = document.querySelectorAll('.category-item');
|
||||
const genres = [];
|
||||
tagEls.forEach(el => { const t = el.textContent.trim(); if (t) genres.push(t); });
|
||||
info.genres = genres.join(',');
|
||||
|
||||
return JSON.stringify(info);
|
||||
})()
|
||||
''';
|
||||
|
||||
Reference in New Issue
Block a user