From 6e4959e91844370924224a652d3d2716711a0b43 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Sat, 3 Aug 2024 13:45:57 +0300 Subject: [PATCH] Add TorrentAPI server abstract class - Add TorrentFromServer type class - Add TorrentListBuilder builder pattern class - Add TorrentAPI abstract interface --- tubot/torrent/abc.py | 48 +++++++++++++++++++++++++++++++++++++++++- tubot/torrent/types.py | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/tubot/torrent/abc.py b/tubot/torrent/abc.py index 1eb13f0..15089a8 100644 --- a/tubot/torrent/abc.py +++ b/tubot/torrent/abc.py @@ -6,7 +6,7 @@ from abc import ABC, abstractmethod from tubot.static.abc import IValidatable -from tubot.torrent.types import TorrentTypes +from tubot.torrent.types import TorrentTypes, ServerTypes class TorrentObj(IValidatable, ABC): @@ -34,3 +34,49 @@ class TorrentObj(IValidatable, ABC): """ self.content = content self.dest = destination + + +class TorrentAPI(IValidatable, ABC): + """ + Abstract class of torrent-server API's + """ + + _atype: ServerTypes # Server type propery + + def __init__(self) -> None: + if self._atype is None: + raise NotImplementedError("Torrent Server type not implemented") + + async def upload(self, torrent: TorrentObj) -> None: + """ + Adds the torrent to a queue on the server + + :param torrent: TorrenObject type (file, magnet, etc.) + """ + match torrent.torrent_type: + case TorrentTypes.File: + await self.upload_file(torrent) + case TorrentTypes.Magnet: + await self.upload_magnet(torrent) + case TorrentTypes.URL: + await self.upload_url(torrent) + + @abstractmethod + async def upload_file(self, torrent) -> None: + raise NotImplementedError + + @abstractmethod + async def upload_magnet(self, torrent) -> None: + raise NotImplementedError + + @abstractmethod + async def upload_url(self, torrent) -> None: + raise NotImplementedError + + @property + @abstractmethod + async def torrent_list(self) -> str: + """ + Returns PlainString with current torrent queue + """ + raise NotImplementedError diff --git a/tubot/torrent/types.py b/tubot/torrent/types.py index 9a28f87..d38a168 100644 --- a/tubot/torrent/types.py +++ b/tubot/torrent/types.py @@ -5,6 +5,7 @@ ############################ # Imports +from collections.abc import Iterable from enum import Enum @@ -16,3 +17,43 @@ class TorrentTypes(Enum): File = ".torrent file" Magnet = "torrent magnet link" URL = "http(s) link to .torrent file" + + +class ServerTypes(Enum): + """ + Types of Torrent servers API's + """ + + +class TorrentFromServer(object): + + name: str + state: str + percent: float + + def __init__(self, name: str, state: str, percent: float) -> None: + self.name = name + self.state = state + self.percent = percent + + def __str__(self) -> str: + return f"*Torrent:* {self.name}\n*State:* {self.state}\n*Progress:* {self.percent}" + + +class TorrentListBuilder(object): + """ + Torrent list type + """ + + collection: list + + def __init__(self) -> None: + self.collection = [] + + def append(self, torrent_data: Iterable) -> "TorrentListBuilder": + item = TorrentFromServer(*torrent_data) + self.collection.append(item) + return self + + def __str__(self) -> str: + return "\n---\n".join(self.collection)