Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some title of radio is html format, need to convert to human readable format #866

Closed
dongfengweixiao opened this issue Aug 19, 2024 · 0 comments · Fixed by #870, #861 or #878
Closed

some title of radio is html format, need to convert to human readable format #866

dongfengweixiao opened this issue Aug 19, 2024 · 0 comments · Fixed by #870, #861 or #878
Assignees

Comments

@dongfengweixiao
Copy link
Contributor

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 = {
+      '&': '&',
+      '&lt;': '<',
+      '&gt;': '>',
+      '&quot;': '"',
+      '&apos;': "'",
+    };
+
+    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;

图片

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment