Last active
July 15, 2026 09:18
-
-
Save himanshubalani/aa13b07ab43c0720e0188bc65564e819 to your computer and use it in GitHub Desktop.
Code files for YT Music Tracker
This file contains hidden or 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
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
| <application | |
| android:label="yt_music_tracker" | |
| android:name="${applicationName}" | |
| android:icon="@mipmap/ic_launcher"> | |
| <service android:label="notifications" | |
| android:name="notification.listener.service.NotificationListener" | |
| android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" | |
| android:exported="true"> | |
| <intent-filter> | |
| <action android:name="android.service.notification.NotificationListenerService" /> | |
| </intent-filter> | |
| </service> | |
| <activity | |
| android:name=".MainActivity" | |
| android:exported="true" | |
| android:launchMode="singleTop" | |
| android:taskAffinity="" | |
| android:theme="@style/LaunchTheme" | |
| android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" | |
| android:hardwareAccelerated="true" | |
| android:windowSoftInputMode="adjustResize"> | |
| <!-- Specifies an Android theme to apply to this Activity as soon as | |
| the Android process has started. This theme is visible to the user | |
| while the Flutter UI initializes. After that, this theme continues | |
| to determine the Window background behind the Flutter UI. --> | |
| <meta-data | |
| android:name="io.flutter.embedding.android.NormalTheme" | |
| android:resource="@style/NormalTheme" | |
| /> | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN"/> | |
| <category android:name="android.intent.category.LAUNCHER"/> | |
| </intent-filter> | |
| </activity> | |
| <!-- Don't delete the meta-data below. | |
| This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> | |
| <meta-data | |
| android:name="flutterEmbedding" | |
| android:value="2" /> | |
| </application> | |
| <!-- Required to query activities that can process text, see: | |
| https://developer.android.com/training/package-visibility and | |
| https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT. | |
| In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. --> | |
| <queries> | |
| <intent> | |
| <action android:name="android.intent.action.PROCESS_TEXT"/> | |
| <data android:mimeType="text/plain"/> | |
| </intent> | |
| </queries> | |
| </manifest> |
This file contains hidden or 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:firebase_database/firebase_database.dart'; | |
| import 'package:flutter/material.dart'; | |
| void updateLiveMusicOnFirebase(String title, String artist, String? artUrl, bool isPlaying, Color albumColor) { | |
| // Reference to a specific node in Firebase | |
| DatabaseReference ref = FirebaseDatabase.instance.ref("live_music"); | |
| ref.set({ | |
| "title": title, | |
| "artist": artist, | |
| "albumArt": artUrl, | |
| "isPlaying": isPlaying, | |
| "updatedAt": DateTime.now().toIso8601String(), | |
| "color": albumColor, | |
| }); | |
| } |
This file contains hidden or 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
| // YT Music Tracker: An app that let's me listen to my notifications and send them live to realtime database on firebase. | |
| import 'dart:convert'; | |
| import 'package:firebase_core/firebase_core.dart'; | |
| import 'package:http/http.dart' as http; | |
| import 'dart:typed_data'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:notification_listener_service/notification_event.dart'; | |
| import 'package:notification_listener_service/notification_listener_service.dart'; | |
| import 'package:url_launcher/url_launcher.dart'; | |
| import 'package:firebase_database/firebase_database.dart'; | |
| import 'firebase_options.dart'; | |
| import 'package:firebase_auth/firebase_auth.dart'; | |
| void main() async { | |
| WidgetsFlutterBinding.ensureInitialized(); | |
| await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); | |
| await FirebaseAuth.instance.signInAnonymously(); // ← add this | |
| runApp(const YTMusicTrackerApp()); | |
| } | |
| class YTMusicTrackerApp extends StatelessWidget { | |
| const YTMusicTrackerApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'YT Music Tracker', | |
| theme: ThemeData.dark(useMaterial3: true), | |
| home: const TrackerScreen(), | |
| ); | |
| } | |
| } | |
| class TrackerScreen extends StatefulWidget { | |
| const TrackerScreen({super.key}); | |
| @override | |
| State<TrackerScreen> createState() => _TrackerScreenState(); | |
| } | |
| class _TrackerScreenState extends State<TrackerScreen> { | |
| bool _hasPermission = false; | |
| ServiceNotificationEvent? _currentTrack; | |
| String? _lastUploadedTitle; | |
| DateTime? _lastFirebaseUpdate; | |
| String? _highResArtUrl; | |
| Color? _dominantColor; | |
| final List<String> ytMusicPackages = [ | |
| 'com.google.android.apps.youtube.music', // Official | |
| 'app.revanced.android.apps.youtube.music', // ReVanced | |
| 'au.com.shiftyjelly.pocketcasts', //PocketCasts | |
| ]; | |
| // // The official package name for YouTube Music | |
| // final String ytMusicPackage = 'app.revanced.android.apps.youtube.music'; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _checkPermissionAndListen(); | |
| } | |
| Future<String?> fetchHighResAlbumArt(String title, String artist) async { | |
| try { | |
| // Clean up the string for search (e.g., removing "(feat. X)" often helps searches) | |
| final cleanTitle = title.split('(')[0].trim(); | |
| final query = Uri.encodeComponent('$cleanTitle $artist'); | |
| // Free iTunes API (No API key required) | |
| final url = Uri.parse( | |
| 'https://itunes.apple.com/search?term=$query&entity=song&limit=1', | |
| ); | |
| final response = await http.get(url); | |
| if (response.statusCode == 200) { | |
| final data = jsonDecode(response.body); | |
| if (data['results'] != null && data['results'].isNotEmpty) { | |
| String artUrl = data['results'][0]['artworkUrl100']; | |
| // THE MAGIC TRICK: Replace the 100x100 resolution with 1000x1000 for ultra high-res! | |
| return artUrl.replaceAll('100x100bb.jpg', '1000x1000bb.jpg'); | |
| } | |
| } | |
| } catch (e) { | |
| debugPrint("Failed to fetch high-res art: $e"); | |
| } | |
| return null; | |
| } | |
| Future<void> _getDominantColor(String imageUrl) async { | |
| try { | |
| final imageProvider = NetworkImage(imageUrl); | |
| final colorScheme = await ColorScheme.fromImageProvider( | |
| provider: imageProvider, | |
| ); | |
| setState(() { | |
| _dominantColor = colorScheme.inversePrimary; | |
| }); | |
| } catch (e) { | |
| debugPrint("Failed to get dominant color: $e"); | |
| setState(() { | |
| _dominantColor = Colors.redAccent; | |
| }); | |
| } | |
| } | |
| Future<void> _checkPermissionAndListen() async { | |
| // 1. Check if user already granted Notification Access | |
| bool isGranted = await NotificationListenerService.isPermissionGranted(); | |
| setState(() { | |
| _hasPermission = isGranted; | |
| }); | |
| if (isGranted) { | |
| _startListening(); | |
| } | |
| } | |
| Future<void> _requestPermission() async { | |
| // 2. Open Android Settings to grant access | |
| bool isGranted = await NotificationListenerService.requestPermission(); | |
| setState(() { | |
| _hasPermission = isGranted; | |
| }); | |
| if (isGranted) { | |
| _startListening(); | |
| } | |
| } | |
| void _startListening() { | |
| NotificationListenerService.notificationsStream.listen((event) async { | |
| if (ytMusicPackages.contains(event.packageName)) { | |
| if (event.hasRemoved ?? false) { | |
| setState(() { | |
| _currentTrack = null; | |
| _highResArtUrl = null; | |
| _dominantColor = Colors.redAccent; | |
| }); | |
| FirebaseDatabase.instance.ref("live_music").update({ | |
| "isPlaying": false, | |
| }); | |
| } else { | |
| // 1. Instantly update the UI with the track info and low-res icon | |
| setState(() { | |
| _currentTrack = event; | |
| _highResArtUrl = null; | |
| }); | |
| // 2. Fetch high-res art in the background | |
| if (event.title != null && event.content != null) { | |
| String? url = await fetchHighResAlbumArt( | |
| event.title!, | |
| event.content!, | |
| ); | |
| // In _startListening(), after you get the url, ADD this call: | |
| if (_currentTrack?.title == event.title) { | |
| setState(() { | |
| _highResArtUrl = url; | |
| }); | |
| if (url != null) { | |
| await _getDominantColor(url); // ← ADD THIS | |
| } else { | |
| setState(() { | |
| _dominantColor = Colors.redAccent; | |
| }); // ← ADD THIS | |
| } | |
| // ✅ ALWAYS update Firebase | |
| updateLiveMusicOnFirebase( | |
| event.title!, | |
| event.content!, | |
| url, | |
| true, | |
| _dominantColor!, | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| } | |
| void updateLiveMusicOnFirebase( | |
| String title, | |
| String artist, | |
| String? artUrl, | |
| bool isPlaying, | |
| Color? dominantColor, | |
| ) { | |
| final now = DateTime.now(); | |
| final isSameSong = _lastUploadedTitle == title; | |
| final isTooSoon = | |
| _lastFirebaseUpdate != null && | |
| now.difference(_lastFirebaseUpdate!).inMinutes < 2; | |
| // Skip only if it's the same song AND within the cooldown | |
| if (isSameSong && isTooSoon) return; | |
| _lastFirebaseUpdate = now; | |
| _lastUploadedTitle = title; | |
| final colorHex = dominantColor != null | |
| ? '#${dominantColor.toARGB32().toRadixString(16).toUpperCase()}' | |
| : null; | |
| DatabaseReference ref = FirebaseDatabase.instance.ref("live_music"); | |
| ref.set({ | |
| "title": title, | |
| "artist": artist, | |
| "albumArt": artUrl, | |
| "isPlaying": isPlaying, | |
| "updatedAt": now.toIso8601String(), | |
| "albumColor": colorHex, | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('YT Music Tracker'), | |
| backgroundColor: ThemeData.light().primaryColorDark, | |
| ), | |
| backgroundColor: ThemeData.dark().primaryColor, | |
| body: Center( | |
| child: Padding( | |
| padding: const EdgeInsets.all(24.0), | |
| child: _buildBody(), | |
| ), | |
| ), | |
| ); | |
| } | |
| Widget _buildBody() { | |
| if (!_hasPermission) { | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| const Text( | |
| 'We need permission to read notifications so we can see what YouTube Music is playing.', | |
| textAlign: TextAlign.center, | |
| style: TextStyle(fontSize: 16), | |
| ), | |
| const SizedBox(height: 20), | |
| ElevatedButton( | |
| onPressed: _requestPermission, | |
| child: const Text('Grant Permission'), | |
| ), | |
| ], | |
| ); | |
| } | |
| if (_currentTrack == null) { | |
| return const Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| Icon(Icons.music_off, size: 80, color: Colors.grey), | |
| SizedBox(height: 16), | |
| Text( | |
| 'Nothing is playing on YouTube Music', | |
| style: TextStyle(fontSize: 18, color: Colors.grey), | |
| ), | |
| ], | |
| ); | |
| } | |
| // Safely extract the Album Art if it exists | |
| // Safely extract the low-res Album Art if it exists | |
| Uint8List? lowResAlbumArt = _currentTrack!.largeIcon; | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| InkWell( | |
| onTap: () => launchUrl( | |
| Uri.parse(_highResArtUrl.toString()), | |
| mode: LaunchMode.externalNonBrowserApplication, | |
| ), | |
| child: SelectableText( | |
| _highResArtUrl.toString(), | |
| textAlign: TextAlign.center, | |
| ), | |
| ), | |
| const SizedBox(height: 24), | |
| // ALBUM ART LOGIC | |
| _highResArtUrl != null | |
| ? Image.network( | |
| _highResArtUrl!, | |
| width: 300, | |
| height: 300, | |
| fit: BoxFit.cover, | |
| // Show the low-res image while the high-res one is downloading | |
| loadingBuilder: (context, child, loadingProgress) { | |
| if (loadingProgress == null) return child; // Fully loaded | |
| return lowResAlbumArt != null | |
| ? Image.memory( | |
| lowResAlbumArt, | |
| width: 300, | |
| height: 300, | |
| fit: BoxFit.cover, | |
| ) | |
| : Container( | |
| width: 300, | |
| height: 300, | |
| color: Colors.grey[800], | |
| child: const Center( | |
| child: CircularProgressIndicator(), | |
| ), | |
| ); | |
| }, | |
| // Fallback to low-res if the network request fails | |
| errorBuilder: (context, error, stackTrace) { | |
| return lowResAlbumArt != null | |
| ? Image.memory( | |
| lowResAlbumArt, | |
| width: 300, | |
| height: 300, | |
| fit: BoxFit.cover, | |
| ) | |
| : Container( | |
| width: 300, | |
| height: 300, | |
| color: Colors.grey[800], | |
| child: const Icon(Icons.music_note, size: 100), | |
| ); | |
| }, | |
| ) | |
| // If we don't have a high-res URL (e.g. YouTube exclusive remix) use low-res | |
| : lowResAlbumArt != null | |
| ? Image.memory( | |
| lowResAlbumArt, | |
| width: 300, | |
| height: 300, | |
| fit: BoxFit.cover, | |
| ) | |
| // Absolute fallback if no art exists at all | |
| : Container( | |
| width: 300, | |
| height: 300, | |
| color: Colors.grey[800], | |
| child: const Icon( | |
| Icons.music_note, | |
| size: 100, | |
| color: Colors.grey, | |
| ), | |
| ), | |
| const SizedBox(height: 24), | |
| Text( | |
| _currentTrack!.title ?? 'Unknown Song', | |
| textAlign: TextAlign.center, | |
| style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold), | |
| ), | |
| const SizedBox(height: 8), | |
| Text( | |
| _currentTrack!.content ?? 'Unknown Artist', | |
| textAlign: TextAlign.center, | |
| style: const TextStyle(fontSize: 18, color: Colors.white70), | |
| ), | |
| const SizedBox(height: 24), | |
| if (_dominantColor != null) ...[ | |
| const SizedBox(height: 16), | |
| Container( | |
| width: 60, | |
| height: 60, | |
| decoration: BoxDecoration( | |
| color: _dominantColor, | |
| borderRadius: BorderRadius.circular(12), | |
| boxShadow: [ | |
| BoxShadow( | |
| color: _dominantColor!, | |
| blurRadius: 16, | |
| spreadRadius: 2, | |
| ), | |
| ], | |
| ), | |
| child: Center(child: Text('T', style: TextStyle(color: Colors.black, fontSize: 30, fontWeight: FontWeight.bold),)), | |
| ), | |
| ], | |
| ], | |
| ); | |
| } | |
| } |
This file contains hidden or 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
| dependencies: | |
| flutter: | |
| sdk: flutter | |
| cupertino_icons: ^1.0.8 | |
| notification_listener_service: ^0.3.5 | |
| http: ^1.6.0 | |
| url_launcher: ^6.3.2 | |
| firebase_core: ^4.7.0 | |
| firebase_database: ^12.3.0 | |
| firebase_auth: ^6.4.0 | |
| dev_dependencies: | |
| flutter_test: | |
| sdk: flutter | |
| flutter_lints: ^6.0.0 | |
| flutter: | |
| uses-material-design: true |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Now playing — neobrutal</title> | |
| <link rel="preconnect" href="https://fonts.googleapis.com"> | |
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | |
| <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@500;600;700&family=Roboto+Flex:wght@800;900&display=swap" rel="stylesheet"> | |
| <style> | |
| *{box-sizing:border-box;} | |
| body{ | |
| margin:0; | |
| min-height:100vh; | |
| background:#f2f0e9; | |
| font-family:'Quicksand',sans-serif; | |
| display:flex; | |
| flex-direction:column; | |
| align-items:center; | |
| justify-content:center; | |
| gap:48px; | |
| padding:56px 20px; | |
| } | |
| .group{ display:flex; flex-direction:column; align-items:center; gap:12px; } | |
| .group-label{ | |
| font-family:'Roboto Flex',sans-serif; | |
| font-weight:800; | |
| font-size:11px; | |
| letter-spacing:0.14em; | |
| text-transform:uppercase; | |
| color:#111; | |
| opacity:0.45; | |
| } | |
| .music-icon{ display:block; } | |
| .box-circles{ | |
| display:flex; | |
| align-items:right; | |
| gap:2px; | |
| } | |
| .box-circles span{ | |
| width:15px; | |
| height:15px; | |
| border-radius:50%; | |
| border:2px solid #111; | |
| background:rgba(255,255,255,0.8); | |
| flex-shrink:0; | |
| box-sizing:border-box; | |
| } | |
| /* ---- mobile card ---- */ | |
| .nb-card{ | |
| width:340px; | |
| height:104px; | |
| border-radius:18px; | |
| border:2.5px solid #111; | |
| display:flex; | |
| overflow:hidden; | |
| background:#1f7a8c; | |
| color:#fff; | |
| box-shadow:0px 0px 0 rgba(17,17,17,0.9); | |
| transition:background 0.3s ease, color 0.3s ease; | |
| } | |
| .nb-art{ | |
| width:104px; | |
| height:100%; | |
| object-fit:cover; | |
| flex:0 0 auto; | |
| background:#0003; | |
| border-right:2.5px solid #111; | |
| display:block; | |
| } | |
| .nb-body{ | |
| flex:1 1 auto; | |
| min-width:0; | |
| padding:9px 12px; | |
| display:flex; | |
| flex-direction:column; | |
| justify-content:space-between; | |
| } | |
| .nb-top-row{ display:flex; align-items:flex-start; justify-content:space-between; gap:8px; } | |
| .nb-eyebrow{ | |
| font-weight:700; | |
| font-size:11px; | |
| letter-spacing:0.02em; | |
| white-space:nowrap; | |
| overflow:hidden; | |
| text-overflow:ellipsis; | |
| } | |
| .nb-title{ | |
| font-weight:700; | |
| font-size:14px; | |
| line-height:1.25; | |
| margin:0; | |
| display:-webkit-box; | |
| -webkit-line-clamp:2; | |
| -webkit-box-orient:vertical; | |
| overflow:hidden; | |
| } | |
| .nb-artist{ | |
| font-weight:600; | |
| font-size:11px; | |
| margin:2px 0 0; | |
| white-space:nowrap; | |
| overflow:hidden; | |
| text-overflow:ellipsis; | |
| opacity:0.85; | |
| } | |
| .nb-bottom-row{ display:flex; align-items:center; justify-content:flex-end; gap:5px; } | |
| .nb-bottom-row .music-label{ | |
| font-family:'Roboto Flex',sans-serif; | |
| font-weight:900; | |
| font-size:12px; | |
| letter-spacing:0.02em; | |
| } | |
| /* ---- desktop pill ---- */ | |
| .nb-pill{ | |
| width:min(92vw,480px); | |
| border-radius:16px; | |
| border:2.5px solid #111; | |
| background:#1f7a8c; | |
| color:#111; | |
| display:flex; | |
| align-items:center; | |
| gap:6px; | |
| padding:4px 8px; | |
| box-shadow:0px 0px 0 rgba(17,17,17,0.9); | |
| transition:background 0.3s ease, color 0.3s ease; | |
| } | |
| .nb-pill .music-icon{ flex:0 0 auto; } | |
| .nb-listening{ | |
| font-weight:700; | |
| font-size:13px; | |
| white-space:nowrap; | |
| flex:0 0 auto; | |
| } | |
| .nb-thumb{ | |
| width:30px; height:30px; border-radius:8px; | |
| object-fit:cover; | |
| border:1.5px solid currentColor; | |
| flex:0 0 auto; | |
| } | |
| .nb-track{ | |
| font-weight:700; | |
| font-size:13px; | |
| white-space:nowrap; | |
| overflow:hidden; | |
| text-overflow:ellipsis; | |
| flex:1 1 auto; | |
| min-width:0; | |
| } | |
| .nb-pill .box-circles{ flex:0 0 auto; margin-left:4px; } | |
| .nb-card[data-state="loading"] .nb-title, | |
| .nb-pill[data-state="loading"] .nb-track{ | |
| background:rgba(255,255,255,0.3); | |
| border-radius:4px; | |
| color:transparent; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="group"> | |
| <span class="group-label">Mobile</span> | |
| <div class="nb-card" id="nbCard" data-state="loading"> | |
| <img class="nb-art" id="nbArt" src="" alt=""> | |
| <div class="nb-body"> | |
| <div class="nb-top-row"> | |
| <span class="nb-eyebrow" id="nbEyebrow">Listening to</span> | |
| <div class="box-circles"> | |
| <span></span> | |
| </div> | |
| </div> | |
| <div> | |
| <p class="nb-title" id="nbTitle">Loading…</p> | |
| <p class="nb-artist" id="nbArtist"> </p> | |
| </div> | |
| <div class="nb-bottom-row"> | |
| <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="20" height="20" viewBox="0 0 50 50"> | |
| <path d="M 25 3 C 12.85 3 3 12.85 3 25 C 3 37.15 12.85 47 25 47 C 37.15 47 47 37.15 47 25 C 47 12.85 37.15 3 25 3 z M 25 11 C 32.72 11 39 17.28 39 25 C 39 32.72 32.72 39 25 39 C 17.28 39 11 32.72 11 25 C 11 17.28 17.28 11 25 11 z M 25 13 C 18.383 13 13 18.383 13 25 C 13 31.617 18.383 37 25 37 C 31.617 37 37 31.617 37 25 C 37 18.383 31.617 13 25 13 z M 22.019531 18.501953 C 22.194031 18.505203 22.366984 18.552484 22.521484 18.646484 L 31.521484 24.146484 C 31.817484 24.327484 32 24.651 32 25 C 32 25.349 31.818484 25.671516 31.521484 25.853516 L 22.521484 31.353516 C 22.361484 31.450516 22.181 31.5 22 31.5 C 21.832 31.5 21.663719 31.456094 21.511719 31.371094 C 21.195719 31.194094 21 30.861 21 30.5 L 21 19.5 C 21 19.139 21.195719 18.805906 21.511719 18.628906 C 21.670219 18.540906 21.845031 18.498703 22.019531 18.501953 z"></path> | |
| </svg> | |
| <span class="music-label">Music</span> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="group"> | |
| <span class="group-label">Desktop</span> | |
| <div class="nb-pill" id="nbPill" data-state="loading"> | |
| <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="20" height="20" viewBox="0 0 50 50"> | |
| <path d="M 25 3 C 12.85 3 3 12.85 3 25 C 3 37.15 12.85 47 25 47 C 37.15 47 47 37.15 47 25 C 47 12.85 37.15 3 25 3 z M 25 11 C 32.72 11 39 17.28 39 25 C 39 32.72 32.72 39 25 39 C 17.28 39 11 32.72 11 25 C 11 17.28 17.28 11 25 11 z M 25 13 C 18.383 13 13 18.383 13 25 C 13 31.617 18.383 37 25 37 C 31.617 37 37 31.617 37 25 C 37 18.383 31.617 13 25 13 z M 22.019531 18.501953 C 22.194031 18.505203 22.366984 18.552484 22.521484 18.646484 L 31.521484 24.146484 C 31.817484 24.327484 32 24.651 32 25 C 32 25.349 31.818484 25.671516 31.521484 25.853516 L 22.521484 31.353516 C 22.361484 31.450516 22.181 31.5 22 31.5 C 21.832 31.5 21.663719 31.456094 21.511719 31.371094 C 21.195719 31.194094 21 30.861 21 30.5 L 21 19.5 C 21 19.139 21.195719 18.805906 21.511719 18.628906 C 21.670219 18.540906 21.845031 18.498703 22.019531 18.501953 z"></path> | |
| </svg> | |
| <span class="nb-listening">listening to</span> | |
| <img class="nb-thumb" id="nbThumb" src="" alt=""> | |
| <span class="nb-track" id="nbTrack">Loading…</span> | |
| <div class="box-circles"> | |
| <span></span> | |
| <span></span> | |
| <span></span> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| const FIREBASE_URL = "https://yt-music-tracker-default-rtdb.asia-southeast1.firebasedatabase.app/live_music.json"; //Replace this with your URL | |
| const POLL_MS = 15000; | |
| const nbCard = document.getElementById('nbCard'); | |
| const nbEyebrow = document.getElementById('nbEyebrow'); | |
| const nbArt = document.getElementById('nbArt'); | |
| const nbTitle = document.getElementById('nbTitle'); | |
| const nbArtist = document.getElementById('nbArtist'); | |
| const nbPill = document.getElementById('nbPill'); | |
| const nbThumb = document.getElementById('nbThumb'); | |
| const nbTrack = document.getElementById('nbTrack'); | |
| function parseArgb(hex){ | |
| if(!hex || typeof hex !== 'string') return null; | |
| const clean = hex.replace('#',''); | |
| if(clean.length === 8){ | |
| return { r: parseInt(clean.slice(2,4),16), g: parseInt(clean.slice(4,6),16), b: parseInt(clean.slice(6,8),16) }; | |
| } | |
| if(clean.length === 6){ | |
| return { r: parseInt(clean.slice(0,2),16), g: parseInt(clean.slice(2,4),16), b: parseInt(clean.slice(4,6),16) }; | |
| } | |
| return null; | |
| } | |
| function luminance(r,g,b){ return (0.299*r + 0.587*g + 0.114*b) / 255; } | |
| function applyBg(el, argbHex){ | |
| const c = parseArgb(argbHex) || { r:31, g:122, b:140 }; | |
| const bg = 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')'; | |
| const textColor = luminance(c.r,c.g,c.b) < 0.5 ? '#ffffff' : '#111111'; | |
| el.style.background = bg; | |
| el.style.color = textColor; | |
| } | |
| function escapeHtml(s){ | |
| return String(s).replace(/[&<>"']/g, c => ({ '&':'&','<':'<','>':'>','"':'"',"'":''' }[c])); | |
| } | |
| async function refresh(){ | |
| try{ | |
| const res = await fetch(FIREBASE_URL, {cache:'no-store'}); | |
| if(!res.ok) throw new Error('bad response'); | |
| const data = await res.json(); | |
| if(!data){ throw new Error('no data'); } | |
| const t = data.title || 'Untitled'; | |
| const a = data.artist || 'Unknown artist'; | |
| nbCard.dataset.state = 'ok'; | |
| applyBg(nbCard, data.albumColor); | |
| nbEyebrow.textContent = data.isPlaying ? 'Listening to' : 'Last played'; | |
| if(nbArt.src !== data.albumArt){ nbArt.src = data.albumArt || ''; } | |
| nbTitle.textContent = t; | |
| nbArtist.textContent = a; | |
| nbPill.dataset.state = 'ok'; | |
| applyBg(nbPill, data.albumColor); | |
| if(nbThumb.src !== data.albumArt){ nbThumb.src = data.albumArt || ''; } | |
| nbTrack.textContent = t + ' by ' + a; | |
| }catch(err){ | |
| nbCard.dataset.state = 'error'; | |
| nbTitle.textContent = 'Can\u2019t reach tracker'; | |
| nbArtist.textContent = 'Retrying\u2026'; | |
| nbPill.dataset.state = 'error'; | |
| nbTrack.textContent = 'Can\u2019t reach tracker \u2014 retrying\u2026'; | |
| } | |
| } | |
| refresh(); | |
| setInterval(refresh, POLL_MS); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment