You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
from database import Model
from const import *
from .yt_dlp_logger import *
import wget
import yt_dlp
Album = Model('album')
def download_album(album):
"""
Take a list of albums and process the downloads
:param album: Dict of album data
:return:
"""
print('------')
print('Album Data')
print(album)
artist = album.get('artist')
artist_path = MEDIA_FOLDER + '/%s' % artist
if not os.path.exists(artist_path):
os.mkdir(artist_path)
print('---')
# Create album folder
album_title = album.get('album')
album_path = artist_path + '/%s' % album_title
if not os.path.exists(album_path):
os.mkdir(album_path)
# Save album cover
if album.get('cover'):
try:
download_file(album.get('cover'), album_path)
except Exception as e:
print("Warning: %s" % e)
# Download album
ydl_opts = {
'logger': YtDlpLogger(),
'progress_hooks': [yt_dlp_log_hook],
'format': 'mp3/bestaudio/best',
'outtmpl': album_path + '/%(title)s.%(ext)s',
# See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
'postprocessors': [{ # Extract audio using ffmpeg
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
}]
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
error_code = ydl.download('https://youtube.com' + album.get('link'))
except Exception as e:
print('!!!!!!!!!')
print(e)
Album.write(album['id'], {'downloaded': True, 'downloading': False})
def download_file(url, output):
filename = wget.download(url, out=output)
os.rename(filename, output + '/album.jpg')
return filename