4
1
Fork 1
This repository has been archived on 2024-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
qBitDownload-Bot/Torrent.py

59 lines
1.3 KiB
Python
Raw Normal View History

2024-05-05 17:10:11 +00:00
# -*- coding: utf-8 -*-
from abc import ABCMeta, abstractmethod, abstractproperty
2024-05-09 14:47:57 +00:00
from typing import BinaryIO
2024-05-05 17:10:11 +00:00
from TYPES import TorrentTypes
from os.path import splitext as getext
from re import match
2024-05-09 14:47:57 +00:00
from Config import tmpDir
2024-05-05 17:10:11 +00:00
class Torrent(object):
__metaclass__ = ABCMeta
2024-05-09 14:47:57 +00:00
def __init__(self, url: str, path: str):
2024-05-05 17:10:11 +00:00
self.url = url
2024-05-09 14:47:57 +00:00
self.path = path
2024-05-05 17:10:11 +00:00
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):
2024-05-09 14:47:57 +00:00
def __init__(self, url: str, path: str):
super().__init__(url, path)
self.url = f"{tmpDir}/{url}"
2024-05-05 17:10:11 +00:00
@property
def torrent_type(self) -> TorrentTypes:
return TorrentTypes.FILE
def validate(self) -> bool:
if getext(self.url)[1].lower() == ".torrent":
return True