mckabue commited on
Commit
4c64cef
·
1 Parent(s): 2f4074b

Enhance CSV parsing utility to support optional shuffling; update HTML table generation for improved playback handling

Browse files
Files changed (3) hide show
  1. app.py +7 -4
  2. index.html +20 -10
  3. requirements.txt +2 -1
app.py CHANGED
@@ -6,6 +6,7 @@ import logging
6
  import pandas as pd
7
  from pytube import Search, YouTube
8
  from flask import Flask, request, send_from_directory
 
9
 
10
  # https://github.com/pytube/pytube/issues/1270#issuecomment-2100372834
11
  pytube_logger = logging.getLogger('pytube')
@@ -48,7 +49,7 @@ def parse_csv_test():
48
  try:
49
  # Construct the path to the CSV file
50
  csv_path = Path(__file__).parent / 'shazamlibrary.test.csv'
51
- return parse_csv_util(pd.read_csv(csv_path, header=1))
52
  except Exception as e:
53
  return str(e)
54
 
@@ -57,9 +58,11 @@ def get_youtube_song(title: str, artist: str) -> Optional[YouTube]:
57
  search_result = Search(f'{title} by {artist}')
58
  return search_result.results[0] if search_result.results else None
59
 
60
- def parse_csv_util(shazamlibrary_df):
61
  try:
62
- shazamlibrary_df = shazamlibrary_df.drop_duplicates(subset=['TrackKey'])[['Title', 'Artist']]
63
- return shazamlibrary_df.to_json(orient="records")
 
 
64
  except Exception as e:
65
  return str(e)
 
6
  import pandas as pd
7
  from pytube import Search, YouTube
8
  from flask import Flask, request, send_from_directory
9
+ from sklearn.utils import shuffle as shuffle_fn
10
 
11
  # https://github.com/pytube/pytube/issues/1270#issuecomment-2100372834
12
  pytube_logger = logging.getLogger('pytube')
 
49
  try:
50
  # Construct the path to the CSV file
51
  csv_path = Path(__file__).parent / 'shazamlibrary.test.csv'
52
+ return parse_csv_util(pd.read_csv(csv_path, header=1), True)
53
  except Exception as e:
54
  return str(e)
55
 
 
58
  search_result = Search(f'{title} by {artist}')
59
  return search_result.results[0] if search_result.results else None
60
 
61
+ def parse_csv_util(df: pd.DataFrame, shuffle = False):
62
  try:
63
+ df = df.drop_duplicates(subset=['TrackKey'])[['Title', 'Artist']]
64
+ if shuffle:
65
+ df = shuffle_fn(df)
66
+ return df.to_json(orient="records")
67
  except Exception as e:
68
  return str(e)
index.html CHANGED
@@ -136,7 +136,7 @@
136
  function generateTable(playlist) {
137
  try {
138
  songsPlaylist = playlist
139
- tableBody = songsPlaylist.map((i, index) => `
140
  <tr data-index="${index}">
141
  <th>${index + 1}</th>
142
  <th>${i.Title}</th>
@@ -154,6 +154,11 @@
154
  </thead>
155
  <tbody>${tableBody}</tbody>
156
  </table>`
 
 
 
 
 
157
  } catch (error) {
158
  playlistTable.innerHTML = error;
159
  }
@@ -207,16 +212,21 @@
207
  }, newIndex);
208
  }
209
  }
210
- const setIntervalId = setInterval(() => {
211
- if (YT.Player) {
212
- if (!youtubePlayer) {
213
- initiatePlayer();
214
- } else if (songsPlaylist.length > 0) {
215
- clearInterval(setIntervalId);
216
- onContinue();
 
 
 
217
  }
218
- }
219
- }, 300);
 
 
220
  </script>
221
  </body>
222
 
 
136
  function generateTable(playlist) {
137
  try {
138
  songsPlaylist = playlist
139
+ const tableBody = songsPlaylist.map((i, index) => `
140
  <tr data-index="${index}">
141
  <th>${index + 1}</th>
142
  <th>${i.Title}</th>
 
154
  </thead>
155
  <tbody>${tableBody}</tbody>
156
  </table>`
157
+
158
+ tryToPlay(() => {
159
+ videoIndex = -1;
160
+ onContinue();
161
+ }, 100)
162
  } catch (error) {
163
  playlistTable.innerHTML = error;
164
  }
 
212
  }, newIndex);
213
  }
214
  }
215
+
216
+ function tryToPlay(playCallback, timeout) {
217
+ setTimeout(() => {
218
+ if (YT.Player) {
219
+ if (!youtubePlayer) {
220
+ initiatePlayer();
221
+ tryToPlay?.();
222
+ } else if (songsPlaylist.length > 0) {
223
+ playCallback();
224
+ }
225
  }
226
+ }, timeout);
227
+ }
228
+
229
+ tryToPlay(null, 300);
230
  </script>
231
  </body>
232
 
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  pytube==15.0.0
2
  pandas==2.2.2
3
  flask==3.0.3
4
- gunicorn==22.0.0
 
 
1
  pytube==15.0.0
2
  pandas==2.2.2
3
  flask==3.0.3
4
+ gunicorn==22.0.0
5
+ scikit-learn==1.5.2