-
-
Save melodysdreamj/5bd974a6647eb084595780301265729d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:html/parser.dart' as parser; | |
import 'package:http/http.dart' as http; | |
Future<Map<String, String>> _parseHtml(String htmlString, Uri url) async { | |
final document = parser.parse(htmlString); | |
Map<String, String> info = {}; | |
// Title parsing | |
final titleElement = document.querySelector('title'); | |
info['title'] = titleElement?.text ?? ''; | |
// Site name parsing | |
final siteNameElement = document.querySelector('meta[property="og:site_name"], meta[name="application-name"]'); | |
if (siteNameElement != null) { | |
info['site_name'] = siteNameElement.attributes['content'] ?? ''; | |
} else { | |
info['site_name'] = url.host; // Use the host as the site name if no other title is found | |
} | |
// Description parsing | |
final metaDescription = document.querySelector('meta[name="description"], meta[property="og:description"], meta[name="twitter:description"]'); | |
info['description'] = metaDescription?.attributes['content'] ?? ''; | |
// Favicon parsing | |
final iconElement = document.querySelector('link[rel="icon"], link[rel="shortcut icon"], link[rel="apple-touch-icon"]'); | |
if (iconElement != null) { | |
final href = iconElement.attributes['href']; | |
if (href != null && href.isNotEmpty) { | |
info['favicon'] = _resolveUrl(href, url).toString(); | |
} | |
} else { | |
// Check if favicon.ico exists in the root | |
final faviconUri = url.resolve('/favicon.ico'); | |
final faviconExists = await _checkFaviconExists(faviconUri); | |
if (faviconExists) { | |
info['favicon'] = faviconUri.toString(); | |
} | |
} | |
// Thumbnail parsing | |
final thumbnailElement = document.querySelector('meta[property="og:image"], meta[name="twitter:image"], meta[itemprop="image"]'); | |
if (thumbnailElement != null) { | |
final content = thumbnailElement.attributes['content']; | |
if (content != null && content.isNotEmpty) { | |
info['thumbnail'] = _resolveUrl(content, url).toString(); | |
} | |
} | |
return info; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment