Add Torrent class
This commit is contained in:
parent
7e17c71610
commit
e5857715e8
11
TYPES.py
11
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"
|
||||
|
|
|
@ -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
|
Reference in New Issue