Playlist Download #547
Closed
AntonioHinds
started this conversation in
Ideas
Replies: 2 comments 11 replies
-
Perhaps this may be of assistance. import os
import requests
def download_track(track_url, save_path):
try:
response = requests.get(track_url, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"Downloaded {save_path}")
else:
print(f"Failed to download {track_url}")
except Exception as e:
print(f"Error downloading {track_url}: {e}")
def download_playlist(playlist_id, playlist_name):
# Fetch tracks in the playlist
tracks = fetch_playlist_tracks(playlist_id)
playlist_dir = f"local_playlists/{playlist_name}"
os.makedirs(playlist_dir, exist_ok=True)
track_url = track['url']
track_id = track['id']
save_path = f"{playlist_dir}/{track_id}.mp3"
print(f"Downloading {track_id}...")
download_track(track_url, save_path)
print(f"All tracks in playlist '{playlist_name}' have been downloaded to {playlist_dir}.")
---
```python
from flask import Flask, request, jsonify
import os
import requests
app = Flask(__name__)
def download_track(track_url, save_path):
try:
response = requests.get(track_url, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
return True
else:
return False
except Exception as e:
print(f"Error downloading {track_url}: {e}")
return False
@app.route('/download_playlist', methods=['POST'])
def download_playlist():
data = request.json
playlist_id = data.get('playlist_id')
playlist_name = data.get('playlist_name')
# Fetch tracks in the playlist
tracks = fetch_playlist_tracks(playlist_id)
# Create a directory for the playlist
playlist_dir = f"local_playlists/{playlist_name}"
os.makedirs(playlist_dir, exist_ok=True)
# Download each track
for track in tracks:
track_url = track['url']
track_id = track['id']
save_path = f"{playlist_dir}/{track_id}.mp3"
if not download_track(track_url, save_path):
return jsonify({"status": "error", "message": f"Failed to download track {track_id}"}), 500
return jsonify({"status": "success", "message": f"Playlist '{playlist_name}' downloaded successfully!"})
if __name__ == '__main__':
app.run(debug=True) |
Beta Was this translation helpful? Give feedback.
1 reply
-
It's not that I find it difficult to put an already existing function in a loop. It's just that the already restrictive server, which limits some people for no reason, might ban your IP for some time. |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Update:
There is currently no way to have an entire playlist offline but instead, one will have to manually make each song to be available offline. It would be a nice qol to being able to have an option to make an entire playlist have an offline option.
Below may be of assistance.
Beta Was this translation helpful? Give feedback.
All reactions