some title of radio is html format, need to convert to human readable format
Closed this issue · 0 comments
dongfengweixiao commented
Search 'Taiwan' in 'Country', then play the content in red box.
1766 Radio 百家知識頻道 - 音樂時間
try fix this issue with those code, but missing content in red box.
diff --git a/lib/common/data/mpv_meta_data.dart b/lib/common/data/mpv_meta_data.dart
index 0c7bd6b..bd9e597 100644
--- a/lib/common/data/mpv_meta_data.dart
+++ b/lib/common/data/mpv_meta_data.dart
@@ -66,7 +66,9 @@ class MpvMetaData {
icyAudioInfo: map['icy-audio-info'] ?? '',
icyPub: map['icy-pub'] ?? '',
icyDescription: map['icy-description'] ?? '',
- icyTitle: map['icy-title'] ?? '',
+ icyTitle: map['icy-title'] != null
+ ? map['icy-title'].parseHtmlEntities()
+ : '',
);
}
diff --git a/lib/extensions/string_x.dart b/lib/extensions/string_x.dart
index e57c5fc..a09159a 100644
--- a/lib/extensions/string_x.dart
+++ b/lib/extensions/string_x.dart
@@ -31,6 +31,39 @@ extension StringExtension on String {
}
}
+extension ParseHtmlEntities on String {
+ String get parseHtmlEntities {
+ final regex = RegExp(r'&[#\w]+;');
+ return replaceAllMapped(regex, (match) {
+ String entity = match.group(0)!;
+ return _decodeHtmlEntity(entity);
+ });
+ }
+
+ String _decodeHtmlEntity(String entity) {
+ const htmlEntities = {
+ '&': '&',
+ '<': '<',
+ '>': '>',
+ '"': '"',
+ ''': "'",
+ };
+
+ if (htmlEntities.containsKey(entity)) {
+ return htmlEntities[entity]!;
+ }
+
+ if (entity.startsWith('&#')) {
+ int? codePoint = int.tryParse(entity.substring(2, entity.length - 1));
+ if (codePoint != null) {
+ return String.fromCharCode(codePoint);
+ }
+ }
+
+ return entity;
+ }
+}
+
extension NullableStringX on String? {
Duration? get parsedDuration {
final durationAsString = this;