getHtmlContent method
Implementation
Future<LinkPeekModel> getHtmlContent(String url) async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final htmlContent = response.body;
final parsedInfo = _parseHtml(htmlContent);
String favicon = parsedInfo['favicon'] ?? "";
Uri uri = Uri.parse(url);
if (favicon.isNotEmpty) {
if (favicon.startsWith("//")) {
favicon = "https:$favicon";
} else if (favicon.startsWith("/")) {
favicon = uri.origin + favicon;
}
}
final PaletteGenerator? palette = await _getIconPalette(favicon);
final String title = (parsedInfo['title'] ?? "").trim();
final String description = (parsedInfo['description'] ?? "").trim();
final String thumbnail = parsedInfo['thumbnail'] ?? "";
final String domain = uri.origin.split("/").last;
return LinkPeekModel(
title: title,
webIcon: favicon,
defaultColor: palette?.dominantColor?.color,
description: description,
thumbnail: thumbnail,
colorScheme: palette,
domain: domain,
url: url);
} else {
_logger.e("Server responsed very badly! package is upset",
error: response.statusCode);
throw Exception("Error fetching URL: ${response.statusCode}");
}
} catch (e) {
_logger.e("One more error in your code ☠️", error: e);
throw Exception("Error fetching URL: $e");
}
}