diff options
| author | Bobby <[email protected]> | 2024-12-14 18:32:42 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-12-14 18:32:42 -0500 |
| commit | 944b2d923f2ba55d24e90daaef46f99d0e4a84b6 (patch) | |
| tree | dfd53521c515c9b1eb19d6dd10c00e9b85a0b3d5 | |
| parent | efb25b04c72b972c3eaf949d97ea81a38edacb21 (diff) | |
| download | thatcomputerscientist-944b2d923f2ba55d24e90daaef46f99d0e4a84b6.tar.xz thatcomputerscientist-944b2d923f2ba55d24e90daaef46f99d0e4a84b6.zip | |
numeric id based streaming
| -rw-r--r-- | apps/stream/songs.py | 45 | ||||
| -rw-r--r-- | apps/stream/urls.py | 2 | ||||
| -rw-r--r-- | apps/stream/views.py | 106 | ||||
| -rw-r--r-- | static/js/shared/kawaiiBeatsPlayer.js | 10 |
4 files changed, 47 insertions, 116 deletions
diff --git a/apps/stream/songs.py b/apps/stream/songs.py index 78fb79ca..a39e310f 100644 --- a/apps/stream/songs.py +++ b/apps/stream/songs.py @@ -1,90 +1,105 @@ MUSIC_FILES = [ { - "id": "Bling-Bang-Bang-Born.mp3", + "id": 1, + "songName": "Bling-Bang-Bang-Born.mp3", "title": "Bling Bang Bang Born", "artist": "CREEPY NUTS", "album": "Bling Bang Bang Born", }, { - "id": "DADDY ! DADDY ! DO !.mp3", + "id": 2, + "songName": "DADDY ! DADDY ! DO !.mp3", "title": "DADDY ! DADDY ! DO !", "artist": "Masayuki Suzuki", "album": "Kaguya-sama: Love Is War", }, { - "id": "Duvet.mp3", + "id": 3, + "songName": "Duvet.mp3", "title": "Duvet", "artist": "Boa", "album": "Serial Experiments Lain", }, { - "id": "Nandemonaiya - movie ver..mp3", + "id": 4, + "songName": "Nandemonaiya - movie ver..mp3", "title": "Nandemonaiya - movie ver.", "artist": "RADWIMPS", "album": "Your Name", }, { - "id": "Shinunoga E-Wa.mp3", + "id": 5, + "songName": "Shinunoga E-Wa.mp3", "title": "Shinunoga E-Wa", "artist": "Fujii Kaze", "album": "HELP EVER HURT NEVER", }, { - "id": "Sparkle - movie ver..mp3", + "id": 6, + "songName": "Sparkle - movie ver..mp3", "title": "Sparkle - movie ver.", "artist": "RADWIMPS", "album": "Your Name", }, { - "id": "Zenzenzense - movie ver..mp3", + "id": 7, + "songName": "Zenzenzense - movie ver..mp3", "title": "Zenzenzense - movie ver.", "artist": "RADWIMPS", "album": "Your Name", }, { - "id": "アイドル.mp3", + "id": 8, + "songName": "アイドル.mp3", "title": "アイドル", "artist": "YOASOBI", "album": "アイドル", }, { - "id": "ただ声一つ.mp3", + "id": 9, + "songName": "ただ声一つ.mp3", "title": "ただ声一つ", "artist": "ロクデナシ", "album": "ただ声一つ", }, { - "id": "好きだから。.mp3", + "id": 10, + "songName": "好きだから。.mp3", "title": "好きだから。", "artist": "『ユイカ』", "album": "好きだから。", }, { - "id": "忘れてください.mp3", + "id": 11, + "songName": "忘れてください.mp3", "title": "忘れてください", "artist": "Yorushika", "album": "忘れてください", }, { - "id": "恋愛サーキュレーション.mp3", + "id": 12, + "songName": "恋愛サーキュレーション.mp3", "title": "恋愛サーキュレーション", "artist": "物語シリーズ", "album": "Utamonogatari Special Edition", }, { - "id": "知らないままで.mp3", + "id": 13, + "songName": "知らないままで.mp3", "title": "知らないままで", "artist": "ロクデナシ", "album": "知らないままで", }, { - "id": "言って。.mp3", + "id": 14, + "songName": "言って。.mp3", "title": "言って。", "artist": "Yorushika", "album": "夏草が邪魔をする", }, { - "id": "青のすみか.mp3", + "id": 15, + "songName": "青のすみか.mp3", "title": "青のすみか", "artist": "Tatsuya Kitani", "album": "青のすみか", diff --git a/apps/stream/urls.py b/apps/stream/urls.py index 326c83e6..4fc57ee5 100644 --- a/apps/stream/urls.py +++ b/apps/stream/urls.py @@ -5,5 +5,5 @@ from . import views app_name = "stream" urlpatterns = [ path("random-song", views.random_song, name="random_song"), - path("song/<str:song_id>", views.stream_song, name="stream_song"), + path("song/<int:song_id>", views.stream_song, name="stream_song"), ] diff --git a/apps/stream/views.py b/apps/stream/views.py index 707a92af..5e341fb7 100644 --- a/apps/stream/views.py +++ b/apps/stream/views.py @@ -7,6 +7,7 @@ import requests from django.http import HttpResponse, JsonResponse CDN_URL = os.getenv("CDN_URL") +MUSIC_FILES_COUNT = len(MUSIC_FILES) def get_stream_url(filename: str) -> str: @@ -14,109 +15,20 @@ def get_stream_url(filename: str) -> str: def random_song(request) -> JsonResponse: - """Get a random song from the API.""" - song = random.choice(MUSIC_FILES) + next_song_id = request.GET.get("next") + if next_song_id: + song = MUSIC_FILES[int(next_song_id) % MUSIC_FILES_COUNT] + else: + song = random.choice(MUSIC_FILES) return JsonResponse(song) -def stream_song(request, song_id: str) -> HttpResponse: - """Stream a specific song by filename.""" - stream_url = get_stream_url(song_id) +def stream_song(request, song_id: int) -> HttpResponse: + song = MUSIC_FILES[song_id - 1] + stream_url = get_stream_url(song["songName"]) response = requests.get(stream_url, stream=True) return HttpResponse( response.raw.read(), content_type=response.headers.get("Content-Type", "audio/mpeg"), ) - - -# Constants from environment variables -# API_CONFIG = { -# "BASE_URL": os.getenv("MUSIC_API_BASE_URL", "http://music.shi.foo/rest"), -# "USERNAME": os.getenv("MUSIC_API_USERNAME"), -# "PASSWORD": os.getenv("MUSIC_API_PASSWORD"), -# "VERSION": os.getenv("MUSIC_API_VERSION", "1.16.1"), -# "CLIENT": os.getenv("MUSIC_API_CLIENT", "Shifoo"), -# } - - -# def get_auth_params() -> Dict[str, str]: -# """Generate authentication parameters for API requests.""" -# salt = "".join(random.choices("abcdefghijklmnopqrstuvwxyz0123456789", k=6)) -# token = md5((API_CONFIG["PASSWORD"] + salt).encode()).hexdigest() - -# return { -# "u": API_CONFIG["USERNAME"], -# "t": token, -# "s": salt, -# "v": API_CONFIG["VERSION"], -# "c": API_CONFIG["CLIENT"], -# } - - -# def make_api_request( -# endpoint: str, params: Dict = None, stream: bool = False -# ) -> Union[requests.Response, Dict]: -# """Make an API request with error handling.""" -# auth_params = get_auth_params() -# if params: -# auth_params.update(params) - -# url = f"{API_CONFIG['BASE_URL']}/{endpoint}" - -# try: -# response = requests.get(url, params=auth_params, stream=stream) -# response.raise_for_status() -# return response if stream else response.json() -# except requests.RequestException as e: -# # Log error here if you have logging configured -# return {"error": str(e)} - - -# def random_song(request) -> JsonResponse: -# """Get a random song from the API.""" -# # Try to get cached response first -# cache_key = "random_song_response" -# cached_response = cache.get(cache_key) -# if cached_response: -# return JsonResponse(cached_response) - -# response = make_api_request("getRandomSongs", {"size": 1, "f": "json"}) - -# if "error" in response: -# return JsonResponse({"error": response["error"]}, status=500) - -# try: -# subsonic_response = response.get("subsonic-response", {}) -# song = subsonic_response.get("randomSongs", {}).get("song", [{}])[0] - -# if not song: -# return JsonResponse({"error": "No song found"}, status=404) - -# # Construct response data -# response_data = { -# "song": song, -# "coverURL": f"{API_CONFIG['BASE_URL']}/getCoverArt", -# "coverParams": {"id": song.get("coverArt"), **get_auth_params()}, -# } - -# # Cache the response for a short period -# cache.set(cache_key, response_data, 30) # Cache for 30 seconds - -# return JsonResponse(response_data) -# except Exception as e: -# # Log error here if you have logging configured -# return JsonResponse({"error": str(e)}, status=500) - - -# def stream_song(request, song_id: str) -> HttpResponse: -# """Stream a specific song by ID.""" -# response = make_api_request("stream", {"id": song_id}, stream=True) - -# if isinstance(response, dict) and "error" in response: -# return JsonResponse({"error": response["error"]}, status=500) - -# return HttpResponse( -# response.raw.read(), -# content_type=response.headers.get("Content-Type", "audio/mpeg"), -# ) diff --git a/static/js/shared/kawaiiBeatsPlayer.js b/static/js/shared/kawaiiBeatsPlayer.js index 21666be1..d6f7674c 100644 --- a/static/js/shared/kawaiiBeatsPlayer.js +++ b/static/js/shared/kawaiiBeatsPlayer.js @@ -42,6 +42,7 @@ let isPlaying = false; let currentSong = null; let isLoading = true; let isDragging = false; +let Debugging = true; // DOM Elements const elements = { @@ -162,7 +163,8 @@ class SongStore { }; } // Get new song if we're at the end - const newSong = await this.fetchNewSong(); + const nextSongId = this.songs[this.currentIndex]?.id; + const newSong = await this.fetchNewSong(nextSongId); return this.addSong(newSong); } @@ -178,9 +180,10 @@ class SongStore { return null; } - async fetchNewSong() { + async fetchNewSong(nextSongId = null) { try { - const response = await fetch('/stream/random-song'); + const url = nextSongId ? `/stream/random-song?next=${nextSongId}` : '/stream/random-song'; + const response = await fetch(url); const data = await response.json(); return data; } catch (error) { @@ -370,6 +373,7 @@ function drawVisualizer() { // State Management Functions function savePlaybackState() { if (!currentSong) return; + if (Debugging) return; const currentTime = isPlaying ? audioContext.currentTime - startTime : pauseTime; const state = { |
