Compare commits

..

No commits in common. "6a7e7d5aded8e9c0e906ae58eb176e8ffff2fe9b" and "a87385db78989e9d894019d5a72c65fbaa8fdef7" have entirely different histories.

5 changed files with 31 additions and 62 deletions

View File

@ -33,11 +33,7 @@ def get_spotdl_client():
spot_settings = { spot_settings = {
"simple_tui": True, "simple_tui": True,
"log_level": "ERROR", "log_level": "ERROR",
"lyrics_providers": ["genius", "azlyrics", "musixmatch"], "lyrics_providers": ["genius", "musixmatch"],
"threads": 6,
"format": "mp3",
"ffmpeg": "ffmpeg",
"sponsor_block": True,
} }
thread_local.spotdl_client = Spotdl( thread_local.spotdl_client = Spotdl(
client_id=settings.MUSIC_SPOTIFY_ID, client_id=settings.MUSIC_SPOTIFY_ID,

View File

@ -10,7 +10,6 @@
from PIL import Image from PIL import Image
from pydub import AudioSegment from pydub import AudioSegment
from pytube import Search, YouTube from pytube import Search, YouTube
from spotdl.providers.audio import YouTubeMusic
from akarpov.music.models import Song from akarpov.music.models import Song
from akarpov.music.services.db import load_track from akarpov.music.services.db import load_track
@ -19,28 +18,22 @@
final_filename = None final_filename = None
ytmusic = YouTubeMusic() ydl_opts = {
"format": "m4a/bestaudio/best",
"postprocessors": [
{ # Extract audio using ffmpeg
"key": "FFmpegExtractAudio",
"preferredcodec": "m4a",
}
],
"outtmpl": f"{settings.MEDIA_ROOT}/%(uploader)s_%(title)s.%(ext)s",
}
def download_file(url): def download_file(url):
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": f"{settings.MEDIA_ROOT}/%(uploader)s_%(title)s.%(ext)s",
"postprocessors": [
{"key": "SponsorBlock"}, # Skip sponsor segments
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}, # Extract audio
{"key": "EmbedThumbnail"}, # Embed Thumbnail
{"key": "FFmpegMetadata"}, # Apply correct metadata
],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True) info = ydl.extract_info(url)
filename = ydl.prepare_filename(info) return info["requested_downloads"][0]["_filename"]
return os.path.splitext(filename)[0] + ".mp3"
def parse_description(description: str) -> list: def parse_description(description: str) -> list:
@ -74,7 +67,7 @@ def parse_description(description: str) -> list:
def download_from_youtube_link(link: str, user_id: int) -> Song: def download_from_youtube_link(link: str, user_id: int) -> Song:
song = None song = None
with yt_dlp.YoutubeDL({"ignoreerrors": True, "extract_flat": True}) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False) info_dict = ydl.extract_info(link, download=False)
title = info_dict.get("title", None) title = info_dict.get("title", None)
description = info_dict.get("description", None) description = info_dict.get("description", None)
@ -89,9 +82,6 @@ def download_from_youtube_link(link: str, user_id: int) -> Song:
+ slugify(orig_path.split("/")[-1].split(".")[0]) + slugify(orig_path.split("/")[-1].split(".")[0])
+ ".mp3" + ".mp3"
) )
if orig_path.endswith(".mp3"):
os.rename(orig_path, path)
else:
AudioSegment.from_file(orig_path).export(path) AudioSegment.from_file(orig_path).export(path)
if orig_path != path: if orig_path != path:
os.remove(orig_path) os.remove(orig_path)

View File

@ -3,13 +3,14 @@
import pylast import pylast
import spotipy import spotipy
import structlog import structlog
import ytmusicapi import yt_dlp
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from celery import shared_task from celery import shared_task
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from django.conf import settings from django.conf import settings
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import now from django.utils.timezone import now
from pytube import Channel, Playlist
from spotipy import SpotifyClientCredentials from spotipy import SpotifyClientCredentials
from akarpov.music.api.serializers import SongSerializer from akarpov.music.api.serializers import SongSerializer
@ -39,30 +40,19 @@ def list_tracks(url, user_id):
yandex.load_playlist(url, user_id) yandex.load_playlist(url, user_id)
if "youtube.com" in url: if "youtube.com" in url:
if "channel" in url or "/c/" in url: if "channel" in url or "/c/" in url:
ytmusic = ytmusicapi.YTMusic() channel = Channel(url)
channel_id = url.split("/")[-1] for video in channel.videos:
channel_songs = ytmusic.get_artist(channel_id)["songs"]["results"] with yt_dlp.YoutubeDL({}) as ydl:
print(channel_songs) info = ydl.extract_info(video, download=False)
if info.get("category") == "Music":
for song in channel_songs:
process_yb.apply_async( process_yb.apply_async(
kwargs={ kwargs={"url": video.watch_url, "user_id": user_id}
"url": f"https://youtube.com/watch?v={song['videoId']}",
"user_id": user_id,
}
) )
elif "playlist" in url or "&list=" in url: elif "playlist" in url or "&list=" in url:
ytmusic = ytmusicapi.YTMusic() playlist = Playlist(url)
playlist_id = url.split("=")[-1] for video in playlist.videos:
playlist_songs = ytmusic.get_playlist(playlist_id)["tracks"]["results"]
for song in playlist_songs:
process_yb.apply_async( process_yb.apply_async(
kwargs={ kwargs={"url": video.watch_url, "user_id": user_id}
"url": f"https://music.youtube.com/watch?v={song['videoId']}",
"user_id": user_id,
}
) )
else: else:
process_yb.apply_async(kwargs={"url": url, "user_id": user_id}) process_yb.apply_async(kwargs={"url": url, "user_id": user_id})

View File

@ -33,13 +33,6 @@ RUN apt-get update && \
apt-get purge -y --auto-remove -o APT:AutoRemove:RecommendsImportant=false && \ apt-get purge -y --auto-remove -o APT:AutoRemove:RecommendsImportant=false && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
# Make ssh dir
RUN mkdir -p /root/.ssh/
# Create known_hosts and add github to it
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
RUN pip install "poetry==$POETRY_VERSION" RUN pip install "poetry==$POETRY_VERSION"
RUN python -m venv /venv RUN python -m venv /venv

View File

@ -119,7 +119,7 @@ spotdl = "^4.2.4"
fuzzywuzzy = "^0.18.0" fuzzywuzzy = "^0.18.0"
python-levenshtein = "^0.23.0" python-levenshtein = "^0.23.0"
pylast = "^5.2.0" pylast = "^5.2.0"
textract = {git = "https://github.com/Alexander-D-Karpov/textract", branch = "master"} textract = {git = "https://github.com/Alexander-D-Karpov/textract.git", branch = "master"}
librosa = "^0.10.1" librosa = "^0.10.1"