100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
import importlib.util
|
|
import json
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
from urllib.parse import urlparse
|
|
|
|
import yaml
|
|
|
|
|
|
class Service:
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
alias: str,
|
|
path: Path,
|
|
api: Path,
|
|
cache: Path,
|
|
quality: str,
|
|
config: Path,
|
|
profile: Path,
|
|
cookies: Path,
|
|
credentials: str,
|
|
) -> None:
|
|
self.name = name
|
|
self.alias = alias
|
|
self.path = path
|
|
self.api = api
|
|
self.cache = cache
|
|
self.quality = quality
|
|
self.config = config
|
|
self.profile = profile
|
|
self.cookies = cookies
|
|
self.credentials = credentials
|
|
|
|
def import_service(self):
|
|
spec = importlib.util.spec_from_file_location(self.name, str(self.path))
|
|
service_module = importlib.util.module_from_spec(spec)
|
|
sys.modules[self.name] = service_module
|
|
spec.loader.exec_module(service_module)
|
|
return getattr(service_module, self.name)
|
|
|
|
|
|
class ServiceManager:
|
|
def __init__(self):
|
|
self.settings = Path("utils") / "settings"
|
|
with open("config.yaml", "r") as f:
|
|
self.config = yaml.safe_load(f)
|
|
|
|
with open(self.settings / "services.json") as f:
|
|
data = json.load(f)
|
|
for service, details in data.items():
|
|
details["path"] = Path(details["path"])
|
|
details["api"] = Path(details["api"])
|
|
details["cache"] = Path(details["cache"])
|
|
details["config"] = Path(details["config"])
|
|
details["profile"] = Path(details["profile"])
|
|
details["cookies"] = Path(details["cookies"])
|
|
|
|
self.services = {url: Service(**data) for url, data in data.items()}
|
|
|
|
def get_service(self, url) -> tuple:
|
|
log = logging.getLogger()
|
|
service = self.services.get(urlparse(url).netloc)
|
|
if service is None:
|
|
log.error("URL did not match any supported service")
|
|
sys.exit(1)
|
|
|
|
log.info(f"\u001b[1m{service.alias[0]}\u001b[0m")
|
|
|
|
if service.config.exists():
|
|
log.info("+ Adding service config")
|
|
with open(service.config, "r") as f:
|
|
self.config.update(yaml.safe_load(f))
|
|
|
|
if service.profile.exists():
|
|
log.info("+ Adding service profile")
|
|
with open(service.profile, "r") as f:
|
|
self.config.update(yaml.safe_load(f))
|
|
|
|
if service.cookies.exists():
|
|
log.info("+ Adding cookie data")
|
|
self.config["cookies"] = service.cookies
|
|
|
|
if service.cache.exists():
|
|
self.config["download_cache"] = service.cache
|
|
else:
|
|
service.cache.touch()
|
|
with service.cache.open("w") as file:
|
|
json.dump({}, file)
|
|
self.config["download_cache"] = service.cache
|
|
|
|
with open(service.api, "r") as f:
|
|
self.config.update(yaml.safe_load(f))
|
|
|
|
return service.import_service(), self.config
|
|
|
|
|
|
service_manager = ServiceManager()
|