added some url parsing

This commit is contained in:
Hellow2 2023-03-30 12:00:39 +02:00
parent de2f326218
commit 21e81c82a3
3 changed files with 46 additions and 3 deletions

View File

@ -192,6 +192,15 @@ class Page:
return music_object return music_object
@classmethod
def fetch_object_from_source(cls, source: Source):
obj_type = cls._get_type_of_url(source.url)
if obj_type is None:
return Artist()
return cls._fetch_object_from_source(source=source, obj_type=obj_type)
@classmethod @classmethod
def _fetch_object_from_source(cls, source: Source, obj_type: Union[Type[Song], Type[Album], Type[Artist], Type[Label]], stop_at_level: int = 1): def _fetch_object_from_source(cls, source: Source, obj_type: Union[Type[Song], Type[Album], Type[Artist], Type[Label]], stop_at_level: int = 1):
if obj_type == Artist: if obj_type == Artist:
@ -269,3 +278,7 @@ class Page:
@classmethod @classmethod
def _fetch_label_from_source(cls, source: Source, stop_at_level: int = 1) -> Label: def _fetch_label_from_source(cls, source: Source, stop_at_level: int = 1) -> Label:
return Label() return Label()
@classmethod
def _get_type_of_url(cls, url: str) -> Optional[Union[Type[Song], Type[Album], Type[Artist], Type[Label]]]:
return None

View File

@ -1,8 +1,9 @@
from collections import defaultdict from collections import defaultdict
from typing import List, Optional, Dict from typing import List, Optional, Dict, Type, Union
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import pycountry import pycountry
from urllib.parse import urlparse
from ..utils.shared import ( from ..utils.shared import (
ENCYCLOPAEDIA_METALLUM_LOGGER as LOGGER ENCYCLOPAEDIA_METALLUM_LOGGER as LOGGER
@ -635,3 +636,21 @@ class EncyclopaediaMetallum(Page):
cls._fetch_lyrics(song_id=song_id) cls._fetch_lyrics(song_id=song_id)
] ]
) )
@classmethod
def _get_type_of_url(cls, url: str) -> Optional[Union[Type[Song], Type[Album], Type[Artist], Type[Label]]]:
parsed_url = urlparse(url)
path: List[str] = parsed_url.path.split("/")
if "band" in path:
return Artist
if "bands" in path:
return Artist
if "albums" in path:
return Album
if "labels" in path:
return Label
return None

View File

@ -1,9 +1,8 @@
from collections import defaultdict from collections import defaultdict
from typing import List, Optional, Union from typing import List, Optional, Type, Union
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import pycountry import pycountry
import time
from urllib.parse import urlparse from urllib.parse import urlparse
from enum import Enum from enum import Enum
from dataclasses import dataclass from dataclasses import dataclass
@ -880,3 +879,15 @@ class Musify(Page):
album.update_tracksort() album.update_tracksort()
return album return album
@classmethod
def _get_type_of_url(cls, url: str) -> Optional[Union[Type[Song], Type[Album], Type[Artist], Type[Label]]]:
url: MusifyUrl = cls.parse_url(url)
if url.source_type == MusifyTypes.ARTIST:
return Artist
if url.source_type == MusifyTypes.RELEASE:
return Album
if url.source_type == MusifyTypes.SONG:
return Song
return None