4
1
Fork 1

Add TorrentAPI server abstract class

- Add TorrentFromServer type class
- Add TorrentListBuilder builder pattern class
- Add TorrentAPI abstract interface
This commit is contained in:
trueold89 2024-08-03 13:45:57 +03:00
parent ed324557c8
commit 6e4959e918
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
2 changed files with 88 additions and 1 deletions

View File

@ -6,7 +6,7 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from tubot.static.abc import IValidatable from tubot.static.abc import IValidatable
from tubot.torrent.types import TorrentTypes from tubot.torrent.types import TorrentTypes, ServerTypes
class TorrentObj(IValidatable, ABC): class TorrentObj(IValidatable, ABC):
@ -34,3 +34,49 @@ class TorrentObj(IValidatable, ABC):
""" """
self.content = content self.content = content
self.dest = destination 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

View File

@ -5,6 +5,7 @@
############################ ############################
# Imports # Imports
from collections.abc import Iterable
from enum import Enum from enum import Enum
@ -16,3 +17,43 @@ class TorrentTypes(Enum):
File = ".torrent file" File = ".torrent file"
Magnet = "torrent magnet link" Magnet = "torrent magnet link"
URL = "http(s) link to .torrent file" 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)