From e5857715e843d1fe4289c917a3030e6e31ae6dd8 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Sun, 5 May 2024 20:10:11 +0300 Subject: [PATCH] Add Torrent class --- TYPES.py | 11 ++++++++--- Torrent.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 Torrent.py diff --git a/TYPES.py b/TYPES.py index fed91d6..c502a5d 100644 --- a/TYPES.py +++ b/TYPES.py @@ -7,6 +7,11 @@ class DirParserTypes(Enum): class LogTypes(Enum): - LOG = "Log" - ERROR = "Error" - WARN = "Warning" + LOG = "LOG" + ERROR = "ERROR" + WARN = "WARNING" + + +class TorrentTypes(Enum): + MAGNET = "Magnet Link" + FILE = "Torrent File" diff --git a/Torrent.py b/Torrent.py new file mode 100644 index 0000000..f406e66 --- /dev/null +++ b/Torrent.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +from abc import ABCMeta, abstractmethod, abstractproperty +from TYPES import TorrentTypes +from os.path import splitext as getext +from re import match + + +class Torrent(object): + __metaclass__ = ABCMeta + + def __init__(self, url: str): + self.url = url + if not (self.validate()): + raise ValueError + + @property + @abstractmethod + def torrent_type(self) -> TorrentTypes: + pass + + @abstractmethod + def validate(self) -> bool: + pass + + @property + def value(self) -> str: + return self.value + + +class MagnetLink(Torrent): + + @property + def torrent_type(self) -> TorrentTypes: + return TorrentTypes.MAGNET + + def validate(self) -> bool: + pattern = r"^magnet:\?xt=urn:btih:[a-fA-F0-9]{40}.*$" + if match(pattern, self.url): + return True + + +class TorrentFile(Torrent): + + @property + def torrent_type(self) -> TorrentTypes: + return TorrentTypes.FILE + + def validate(self) -> bool: + if getext(self.url)[1].lower() == ".torrent": + return True