-
Notifications
You must be signed in to change notification settings - Fork 66
/
web_analyzer.dart
417 lines (368 loc) · 13.1 KB
/
web_analyzer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:fast_gbk/fast_gbk.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:html/dom.dart' hide Text;
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart';
import 'package:http/io_client.dart';
abstract class InfoBase {
late DateTime _timeout;
}
/// Web Information
class WebInfo extends InfoBase {
final String? title;
final String? icon;
final String? description;
final String? image;
WebInfo({this.title, this.icon, this.description, this.image});
}
/// Image Information
class WebImageInfo extends InfoBase {
final String? image;
WebImageInfo({this.image});
}
/// Video Information
class WebVideoInfo extends WebImageInfo {
WebVideoInfo({String? image}) : super(image: image);
}
/// Web analyzer
class WebAnalyzer {
static final Map<String?, InfoBase> _map = {};
static final RegExp _bodyReg =
RegExp(r"<body[^>]*>([\s\S]*?)<\/body>", caseSensitive: false);
static final RegExp _htmlReg = RegExp(
r"(<head[^>]*>([\s\S]*?)<\/head>)|(<script[^>]*>([\s\S]*?)<\/script>)|(<style[^>]*>([\s\S]*?)<\/style>)|(<[^>]+>)|(<link[^>]*>([\s\S]*?)<\/link>)|(<[^>]+>)",
caseSensitive: false);
static final RegExp _metaReg = RegExp(
r"<(meta|link)(.*?)\/?>|<title(.*?)</title>",
caseSensitive: false,
dotAll: true);
static final RegExp _titleReg =
RegExp("(title|icon|description|image)", caseSensitive: false);
static final RegExp _lineReg = RegExp(r"[\n\r]| |>");
static final RegExp _spaceReg = RegExp(r"\s+");
/// Is it an empty string
static bool isNotEmpty(String? str) {
return str != null && str.isNotEmpty && str.trim().length > 0;
}
/// Get web information
/// return [InfoBase]
static InfoBase? getInfoFromCache(String? url) {
final InfoBase? info = _map[url];
if (info != null) {
if (!info._timeout.isAfter(DateTime.now())) {
_map.remove(url);
}
}
return info;
}
/// Get web information
/// return [InfoBase]
static Future<InfoBase?> getInfo(String? url,
{Duration cache = const Duration(hours: 24),
bool multimedia = true}) async {
// final start = DateTime.now();
InfoBase? info = getInfoFromCache(url);
if (info != null) return info;
try {
// info = await _getInfo(url, multimedia);
info = await _getInfoByIsolate(url, multimedia);
if (info != null) {
info._timeout = DateTime.now().add(cache);
_map[url] = info;
}
} catch (e) {
print("Get web error:$url, Error:$e");
}
// print("$url cost ${DateTime.now().difference(start).inMilliseconds}");
return info;
}
static Future<InfoBase?> _getInfo(String url, bool? multimedia) async {
final response = await _requestUrl(url);
if (response == null) return null;
// print("$url ${response.statusCode}");
if (multimedia!) {
final String? contentType = response.headers["content-type"];
if (contentType != null) {
if (contentType.contains("image/")) {
return WebImageInfo(image: url);
} else if (contentType.contains("video/")) {
return WebVideoInfo(image: url);
}
}
}
return _getWebInfo(response, url, multimedia);
}
static Future<InfoBase?> _getInfoByIsolate(
String? url, bool multimedia) async {
final sender = ReceivePort();
final Isolate isolate = await Isolate.spawn(_isolate, sender.sendPort);
final sendPort = await sender.first as SendPort;
final answer = ReceivePort();
sendPort.send([answer.sendPort, url, multimedia]);
final List<String?>? res = await answer.first;
InfoBase? info;
if (res != null) {
if (res[0] == "0") {
info = WebInfo(
title: res[1], description: res[2], icon: res[3], image: res[4]);
} else if (res[0] == "1") {
info = WebVideoInfo(image: res[1]);
} else if (res[0] == "2") {
info = WebImageInfo(image: res[1]);
}
}
sender.close();
answer.close();
isolate.kill(priority: Isolate.immediate);
return info;
}
static void _isolate(SendPort sendPort) {
final port = ReceivePort();
sendPort.send(port.sendPort);
port.listen((message) async {
final SendPort? sender = message[0];
final String url = message[1];
final bool? multimedia = message[2];
final info = await _getInfo(url, multimedia);
if (info is WebInfo) {
sender!
.send(["0", info.title, info.description, info.icon, info.image]);
} else if (info is WebVideoInfo) {
sender!.send(["1", info.image]);
} else if (info is WebImageInfo) {
sender!.send(["2", info.image]);
} else {
sender!.send(null);
}
port.close();
});
}
static bool _certificateCheck(X509Certificate cert, String host, int port) =>
true;
static Future<Response?> _requestUrl(String url,
{int count = 0, String? cookie}) async {
Response? res;
final Uri uri = Uri.parse(url);
final HttpClient ioClient = HttpClient()
..badCertificateCallback = _certificateCheck
..connectionTimeout = Duration(seconds: 2);
final IOClient client = IOClient(ioClient);
final BaseRequest request = Request('GET', uri)
..followRedirects = true
..maxRedirects = 3
..persistentConnection = true
..headers["accept-encoding"] = "gzip, deflate, br"
..headers["User-Agent"] =
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Mobile Safari/537.36"
..headers["cache-control"] = "no-cache"
..headers["accept"] =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
// print(request.headers);
final IOStreamedResponse stream =
await client.send(request).catchError((err) {
print("Error in getting the link => ${request.url}");
print("Error => $err");
});
if (stream.statusCode == HttpStatus.movedTemporarily ||
stream.statusCode == HttpStatus.movedPermanently) {
if (stream.isRedirect && count < 6) {
final String? location = stream.headers['location'];
if (location != null) {
url = location;
if (location.startsWith("/")) {
url = uri.origin + location;
}
}
if (stream.headers['set-cookie'] != null) {
cookie = stream.headers['set-cookie'];
}
count++;
client.close();
// print("Redirect ====> $url");
return _requestUrl(url, count: count, cookie: cookie);
}
} else if (stream.statusCode == HttpStatus.ok) {
res = await Response.fromStream(stream);
if (uri.host == "m.tb.cn") {
final match = RegExp(r"var url = \'(.*)\'").firstMatch(res.body);
if (match != null) {
final newUrl = match.group(1);
if (newUrl != null) {
return _requestUrl(newUrl, count: count, cookie: cookie);
}
}
}
}
client.close();
if (res == null) print("Get web info empty($url)");
return res;
}
static Future<InfoBase?> _getWebInfo(
Response response, String url, bool? multimedia) async {
if (response.statusCode == HttpStatus.ok) {
String? html;
try {
html = const Utf8Decoder().convert(response.bodyBytes);
if (url.contains("twitter.com")) {
String temp = html.replaceAll("\\", "");
temp = temp.replaceAll("u003C", "<");
temp = temp.replaceAll("u003E", ">");
// print(temp);
} else {
// print(html);
}
} catch (e) {
try {
html = gbk.decode(response.bodyBytes);
} catch (e) {
print("Web page resolution failure from:$url Error:$e");
}
}
if (html == null) {
print("Web page resolution failure from:$url");
return null;
}
// Improved performance
// final start = DateTime.now();
final headHtml = _getHeadHtml(html);
final document = parser.parse(headHtml);
// print("dom cost ${DateTime.now().difference(start).inMilliseconds}");
final uri = Uri.parse(url);
// get image or video
if (multimedia!) {
final gif = _analyzeGif(document, uri);
if (gif != null) return gif;
final video = _analyzeVideo(document, uri);
if (video != null) return video;
}
final info = WebInfo(
title: _analyzeTitle(document),
icon: _analyzeIcon(document, uri),
description: _analyzeDescription(document, html),
image: _analyzeImage(document, uri),
);
return info;
}
return null;
}
static String _getHeadHtml(String html) {
html = html.replaceFirst(_bodyReg, "<body></body>");
final matchs = _metaReg.allMatches(html);
final StringBuffer head = StringBuffer("<html><head>");
matchs.forEach((element) {
final String str = element.group(0)!;
if (str.contains(_titleReg)) head.writeln(str);
});
head.writeln("</head></html>");
return head.toString();
}
static InfoBase? _analyzeGif(Document document, Uri uri) {
if (_getMetaContent(document, "property", "og:image:type") == "image/gif") {
final gif = _getMetaContent(document, "property", "og:image");
if (gif != null) return WebImageInfo(image: _handleUrl(uri, gif));
}
return null;
}
static InfoBase? _analyzeVideo(Document document, Uri uri) {
final video = _getMetaContent(document, "property", "og:video");
if (video != null) return WebVideoInfo(image: _handleUrl(uri, video));
return null;
}
static String? _getMetaContent(
Document document, String property, String propertyValue) {
final meta = document.head!.getElementsByTagName("meta");
final ele =
meta.firstWhereOrNull((e) => e.attributes[property] == propertyValue);
if (ele != null) return ele.attributes["content"]?.trim();
return null;
}
static String _analyzeTitle(Document document, {bool isTwitter = false}) {
if (isTwitter) return "";
final title = _getMetaContent(document, "property", "og:title");
if (title != null) return title;
final list = document.head!.getElementsByTagName("title");
if (list.isNotEmpty) {
final tagTitle = list.first.text;
return tagTitle.trim();
}
return "";
}
static String? _analyzeDescription(Document document, String html) {
final desc = _getMetaContent(document, "property", "og:description");
if (desc != null &&
!desc.contains('JavaScript is disabled in your browser')) return desc;
final description = _getMetaContent(document, "name", "description") ??
_getMetaContent(document, "name", "Description");
if (!isNotEmpty(description)) {
// final DateTime start = DateTime.now();
String body = html.replaceAll(_htmlReg, "");
body = body.trim().replaceAll(_lineReg, " ").replaceAll(_spaceReg, " ");
if (body.length > 300) {
body = body.substring(0, 300);
}
// print("html cost ${DateTime.now().difference(start).inMilliseconds}");
if (body.contains('JavaScript is disabled in your browser')) return '';
return body;
}
if (description!.contains('JavaScript is disabled in your browser'))
return '';
return description;
}
static String? _analyzeIcon(Document document, Uri uri) {
final meta = document.head!.getElementsByTagName("link");
String? icon = "";
// get icon first
var metaIcon = meta.firstWhereOrNull((e) {
final rel = (e.attributes["rel"] ?? "").toLowerCase();
if (rel == "icon") {
icon = e.attributes["href"];
if (icon != null && !icon!.toLowerCase().contains(".svg")) {
return true;
}
}
return false;
});
metaIcon ??= meta.firstWhereOrNull((e) {
final rel = (e.attributes["rel"] ?? "").toLowerCase();
if (rel == "shortcut icon") {
icon = e.attributes["href"];
if (icon != null && !icon!.toLowerCase().contains(".svg")) {
return true;
}
}
return false;
});
if (metaIcon != null) {
icon = metaIcon.attributes["href"];
} else {
return "${uri.origin}/favicon.ico";
}
return _handleUrl(uri, icon);
}
static String? _analyzeImage(Document document, Uri uri) {
final image = _getMetaContent(document, "property", "og:image");
return uri.host.contains("twitter.com")
? "https://github.com/sur950/any_link_preview/blob/master/lib/assets/twitter.png?raw=true"
: uri.host.contains("facebook.com")
? "https://github.com/sur950/any_link_preview/blob/master/lib/assets/facebook.jpg?raw=true"
: _handleUrl(uri, image);
}
static String? _handleUrl(Uri uri, String? source) {
if (isNotEmpty(source) && !source!.startsWith("http")) {
if (source.startsWith("//")) {
source = "${uri.scheme}:$source";
} else {
if (source.startsWith("/")) {
source = "${uri.origin}$source";
} else {
source = "${uri.origin}/$source";
}
}
}
return source;
}
}