not-lain commited on
Commit
8001649
·
1 Parent(s): e67568c

cache songs

Browse files
Files changed (1) hide show
  1. app.py +27 -2
app.py CHANGED
@@ -35,6 +35,7 @@ class BotConfig:
35
  HF_REPO: str = "not-lain/assets"
36
  HF_SPACE: str = "https://not-lain-ytdlp.hf.space/"
37
  MAX_RETRY_ATTEMPTS: int = 3
 
38
 
39
  @property
40
  def song_path(self) -> str:
@@ -64,6 +65,15 @@ class MusicBot:
64
  self.current_song: Optional[QueueItem] = None
65
  self.queue: list[QueueItem] = []
66
  self.hf_client = Client(config.HF_SPACE, hf_token=None)
 
 
 
 
 
 
 
 
 
67
 
68
  async def ensure_voice_state(self, ctx: commands.Context) -> None:
69
  """Validate voice state and raise appropriate errors"""
@@ -74,15 +84,29 @@ class MusicBot:
74
  raise VoiceStateError("You must be in the same voice channel as the bot!")
75
 
76
  async def download_song(self, queue_item: QueueItem) -> None:
77
- """Download song from URL and update queue item with file path"""
78
  try:
 
 
 
 
 
 
 
 
 
79
  job = self.hf_client.submit(url=queue_item.url, api_name="/predict")
80
  while not job.done():
81
  time.sleep(0.1)
82
- print("job.outputs() = ", job.outputs())
83
  title, file_path = job.outputs()[0]
 
 
 
 
 
84
  queue_item.title = title
85
  queue_item.file_path = file_path
 
86
  except Exception as e:
87
  logger.error(f"Error downloading song: {e}")
88
  raise
@@ -166,6 +190,7 @@ class MusicBot:
166
  self.loop_mode = LoopMode.NONE
167
  self.current_song = None
168
  self.queue.clear()
 
169
 
170
 
171
  async def handle_voice_command(
 
35
  HF_REPO: str = "not-lain/assets"
36
  HF_SPACE: str = "https://not-lain-ytdlp.hf.space/"
37
  MAX_RETRY_ATTEMPTS: int = 3
38
+ MAX_CACHE_SIZE: int = 100 # Maximum number of songs to cache
39
 
40
  @property
41
  def song_path(self) -> str:
 
65
  self.current_song: Optional[QueueItem] = None
66
  self.queue: list[QueueItem] = []
67
  self.hf_client = Client(config.HF_SPACE, hf_token=None)
68
+ self.song_cache: dict[str, tuple[str, str]] = {} # url -> (title, file_path)
69
+
70
+ def _manage_cache(self) -> None:
71
+ """Remove oldest songs if cache exceeds maximum size"""
72
+ if len(self.song_cache) > self.config.MAX_CACHE_SIZE:
73
+ # Convert to list to get oldest entries (assuming insertion order is maintained)
74
+ oldest_urls = list(self.song_cache.keys())[: -self.config.MAX_CACHE_SIZE]
75
+ for url in oldest_urls:
76
+ del self.song_cache[url]
77
 
78
  async def ensure_voice_state(self, ctx: commands.Context) -> None:
79
  """Validate voice state and raise appropriate errors"""
 
84
  raise VoiceStateError("You must be in the same voice channel as the bot!")
85
 
86
  async def download_song(self, queue_item: QueueItem) -> None:
87
+ """Download song from URL and update queue item with file path, using cache when available"""
88
  try:
89
+ # Check if song is in cache
90
+ if queue_item.url in self.song_cache:
91
+ title, file_path = self.song_cache[queue_item.url]
92
+ queue_item.title = title
93
+ queue_item.file_path = file_path
94
+ logger.info(f"Found song in cache: {title}")
95
+ return
96
+
97
+ # Download if not in cache
98
  job = self.hf_client.submit(url=queue_item.url, api_name="/predict")
99
  while not job.done():
100
  time.sleep(0.1)
 
101
  title, file_path = job.outputs()[0]
102
+
103
+ # Update cache
104
+ self.song_cache[queue_item.url] = (title, file_path)
105
+ self._manage_cache()
106
+
107
  queue_item.title = title
108
  queue_item.file_path = file_path
109
+ logger.info(f"Downloaded and cached new song: {title}")
110
  except Exception as e:
111
  logger.error(f"Error downloading song: {e}")
112
  raise
 
190
  self.loop_mode = LoopMode.NONE
191
  self.current_song = None
192
  self.queue.clear()
193
+ # Note: We don't clear the cache when resetting state
194
 
195
 
196
  async def handle_voice_command(