music-kraken-core/src/music_kraken/pages/youtube.py

232 lines
7.9 KiB
Python
Raw Normal View History

2023-06-12 19:53:40 +00:00
from typing import List, Optional, Type
2023-06-13 11:20:49 +00:00
from urllib.parse import urlparse, urlunparse, parse_qs
2023-06-12 19:53:40 +00:00
import logging
2023-06-13 11:20:49 +00:00
from dataclasses import dataclass
from enum import Enum
2023-02-06 08:44:11 +00:00
2023-06-12 19:53:40 +00:00
from ..objects import Source, DatabaseObject
2023-02-06 08:44:11 +00:00
from .abstract import Page
2023-06-12 19:53:40 +00:00
from ..objects import (
2023-02-06 08:44:11 +00:00
Artist,
Source,
SourcePages,
Song,
Album,
2023-06-12 19:53:40 +00:00
Label,
Target
2023-02-06 08:44:11 +00:00
)
2023-06-12 19:53:40 +00:00
from ..connection import Connection
from ..utils.support_classes import DownloadResult
2023-06-13 11:20:49 +00:00
from ..utils.shared import YOUTUBE_LOGGER, INVIDIOUS_INSTANCE
2023-06-12 19:53:40 +00:00
2023-02-06 08:44:11 +00:00
2023-06-12 19:53:40 +00:00
"""
2023-06-13 11:20:49 +00:00
- https://yt.artemislena.eu/api/v1/search?q=Zombiez+-+Topic&page=1&date=none&type=channel&duration=none&sort=relevance
- https://yt.artemislena.eu/api/v1/channels/playlists/UCV0Ntl3lVR7xDXKoCU6uUXA
- https://yt.artemislena.eu/api/v1/playlists/OLAK5uy_kcUBiDv5ATbl-R20OjNaZ5G28XFanQOmM
- https://yt.artemislena.eu/api/v1/videos/SULFl39UjgY
2023-06-12 19:53:40 +00:00
"""
2023-02-06 08:44:11 +00:00
2023-06-13 13:03:11 +00:00
def get_invidious_url(path: str = "", params: str = "", query: str = "", fragment: str = "") -> str:
return urlunparse((INVIDIOUS_INSTANCE.scheme, INVIDIOUS_INSTANCE.netloc, path, params, query, fragment))
2023-06-13 11:20:49 +00:00
class YouTubeUrlType(Enum):
CHANNEL = "channel"
PLAYLIST = "playlist"
VIDEO = "watch"
NONE = ""
class YouTubeUrl:
"""
Artist
https://yt.artemislena.eu/channel/UCV0Ntl3lVR7xDXKoCU6uUXA
https://www.youtube.com/channel/UCV0Ntl3lVR7xDXKoCU6uUXA
Release
https://yt.artemislena.eu/playlist?list=OLAK5uy_nEg5joAyFjHBPwnS_ADHYtgSqAjFMQKLw
https://www.youtube.com/playlist?list=OLAK5uy_nEg5joAyFjHBPwnS_ADHYtgSqAjFMQKLw
Track
https://yt.artemislena.eu/watch?v=SULFl39UjgY&list=OLAK5uy_nEg5joAyFjHBPwnS_ADHYtgSqAjFMQKLw&index=1
https://www.youtube.com/watch?v=SULFl39UjgY
"""
def __init__(self, url: str) -> None:
"""
Raises Index exception for wrong url, and value error for not found enum type
"""
self.id = ""
parsed = urlparse(url=url)
self.url_type: YouTubeUrlType
type_frag_list = parsed.path.split("/")
if len(type_frag_list) < 2:
self.url_type = YouTubeUrlType.NONE
else:
try:
self.url_type = YouTubeUrlType(type_frag_list[1].strip())
except ValueError:
self.url_type = YouTubeUrlType.NONE
if self.url_type == YouTubeUrlType.CHANNEL:
if len(type_frag_list) < 3:
self.couldnt_find_id(url)
else:
self.id = type_frag_list[2]
elif self.url_type == YouTubeUrlType.PLAYLIST:
query_stuff = parse_qs(parsed.query)
if "list" not in query_stuff:
self.couldnt_find_id(url)
else:
self.id = query_stuff["list"]
elif self.url_type == YouTubeUrlType.VIDEO:
query_stuff = parse_qs(parsed.query)
if "v" not in query_stuff:
self.couldnt_find_id(url)
else:
self.id = query_stuff["v"]
def couldnt_find_id(self, url: str):
YOUTUBE_LOGGER.warning(f"The id is missing: {url}")
self.url_type = YouTubeUrlType.NONE
@property
def api(self) -> str:
if self.url_type == YouTubeUrlType.CHANNEL:
return get_invidious_url(path=f"/api/v1/channels/playlists/{self.id}")
if self.url_type == YouTubeUrlType.PLAYLIST:
return get_invidious_url(path=f"/api/v1/playlists/{id}")
if self.url_type == YouTubeUrlType.VIDEO:
return get_invidious_url(path=f"/api/v1/videos/{self.id}")
return get_invidious_url()
@property
def normal(self) -> str:
if self.url_type.CHANNEL:
return get_invidious_url(path=f"/channel/{self.id}")
if self.url_type.PLAYLIST:
return get_invidious_url(path="/playlist", query=f"list={self.id}")
if self.url_type.VIDEO:
return get_invidious_url(path="/watch", query=f"v={self.id}")
2023-06-12 19:53:40 +00:00
class YouTube(Page):
# CHANGE
SOURCE_TYPE = SourcePages.YOUTUBE
LOGGER = YOUTUBE_LOGGER
2023-02-06 08:44:11 +00:00
2023-06-12 19:53:40 +00:00
def __init__(self, *args, **kwargs):
self.connection: Connection = Connection(
2023-06-13 11:29:24 +00:00
host=get_invidious_url(),
2023-06-12 19:53:40 +00:00
logger=self.LOGGER
)
2023-02-06 08:44:11 +00:00
2023-06-12 19:53:40 +00:00
super().__init__(*args, **kwargs)
2023-02-06 08:44:11 +00:00
2023-06-12 19:53:40 +00:00
def get_source_type(self, source: Source) -> Optional[Type[DatabaseObject]]:
2023-06-13 11:20:49 +00:00
_url_type = {
YouTubeUrlType.CHANNEL: Artist,
YouTubeUrlType.PLAYLIST: Album,
YouTubeUrlType.VIDEO: Song,
}
parsed = YouTubeUrl(source.url)
if parsed.url_type in _url_type:
return _url_type[parsed.url_type]
2023-06-12 19:53:40 +00:00
def general_search(self, search_query: str) -> List[DatabaseObject]:
2023-06-13 11:29:24 +00:00
2023-06-12 19:53:40 +00:00
return [Artist(name="works")]
def label_search(self, label: Label) -> List[Label]:
return []
2023-06-13 13:03:11 +00:00
def _json_to_artist(self, artist_json: dict) -> Artist:#
return Artist(
name=artist_json["author"].replace(" - Topic", ""),
source_list=[
Source(self.SOURCE_TYPE, get_invidious_url(path=artist_json["authorUrl"]))
]
)
2023-06-12 19:53:40 +00:00
def artist_search(self, artist: Artist) -> List[Artist]:
2023-06-13 11:29:24 +00:00
# https://yt.artemislena.eu/api/v1/search?q=Zombiez+-+Topic&page=1&date=none&type=channel&duration=none&sort=relevance
endpoint = get_invidious_url(path="/api/v1/search", query=f"q={artist.name.replace(' ', '+')}+-+Topic&page=1&date=none&type=channel&duration=none&sort=relevance")
2023-06-13 13:03:11 +00:00
artist_list = []
r = self.connection.get(endpoint)
for search_result in r.json():
if search_result["type"] != "channel":
continue
author: str = search_result["author"]
if not author.endswith(" - Topic"):
continue
artist_list.append(self._json_to_artist(search_result))
return artist_list
2023-06-12 19:53:40 +00:00
def album_search(self, album: Album) -> List[Album]:
return []
def song_search(self, song: Song) -> List[Song]:
return []
def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song:
return Song()
def fetch_album(self, source: Source, stop_at_level: int = 1) -> Album:
2023-06-13 13:03:11 +00:00
return Album()
2023-06-12 19:53:40 +00:00
def fetch_artist(self, source: Source, stop_at_level: int = 1) -> Artist:
2023-06-13 13:03:11 +00:00
parsed = YouTubeUrl(source.url)
if parsed.url_type != YouTubeUrlType.CHANNEL:
return Artist(source_list=[source])
artist_name = None
album_list = []
# playlist
# https://yt.artemislena.eu/api/v1/channels/playlists/UCV0Ntl3lVR7xDXKoCU6uUXA
r = self.connection.get(get_invidious_url(f"/api/v1/channels/playlists/{parsed.id}"))
for playlist_json in r.json()["playlists"]:
if playlist_json["type"] != "playlist":
continue
artist_name = playlist_json["author"].replace(" - Topic", "")
# /playlist?list=OLAK5uy_nbvQeskr8nbIuzeLxoceNLuCL_KjAmzVw
album_list.append(Album(
title=playlist_json["title"],
source_list=[Source(
self.SOURCE_TYPE, get_invidious_url(path="/playlist", query=f"list={playlist_json['playlistId']}")
)],
artist_list=[Artist(
name=artist_name,
source_list=[
Source(self.SOURCE_TYPE, get_invidious_url(path=playlist_json["authorUrl"]))
]
)]
))
return Artist(name=artist_name, main_album_list=album_list, source_list=[source])
2023-06-12 19:53:40 +00:00
def fetch_label(self, source: Source, stop_at_level: int = 1) -> Label:
return Label()
def download_song_to_target(self, source: Source, target: Target, desc: str = None) -> DownloadResult:
return DownloadResult()