Skip to content

Commit

Permalink
Merge pull request #56 from 31December99/0.x.x
Browse files Browse the repository at this point in the history
Add new features to the torrent page's description
  • Loading branch information
31December99 authored Dec 8, 2024
2 parents ceca8dd + 90ca215 commit 1877789
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 34 deletions.
52 changes: 36 additions & 16 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

service_filename = "Unit3Dbot_service.env"
n_of_uploaders=3

def create_default_env_file(path):
default_content = """
Expand All @@ -20,9 +21,10 @@ def create_default_env_file(path):
# TMDB
TMDB_APIKEY=
# IMGBB;FREE_IMAGE
# IMAGE UPLOADERS
IMGBB_KEY=
FREE_IMAGE_KEY=
LENSDUMP_KEY=
# QBITTORRENT CLIENT
QBIT_USER=
Expand All @@ -32,9 +34,10 @@ def create_default_env_file(path):
############################################## USER PREFERENCES ##############################################
# Image uploader priority. 1=first in list
IMGBB_PRIORITY=1
FREE_IMAGE_PRIORITY=2
# Image uploader priority. 0=first in list
IMGBB_PRIORITY=0
FREE_IMAGE_PRIORITY=1
LENSDUMP_PRIORITY=2
# Search for possible candidates for duplicate files
DUPLICATE_ON=False
Expand Down Expand Up @@ -113,6 +116,8 @@ class Config(BaseSettings):
TMDB_APIKEY: str | None = Field(default=None, env="TMDB_APIKEY")
IMGBB_KEY: str | None = Field(default=None, env="IMGBB_KEY")
FREE_IMAGE_KEY: str | None = Field(default=None, env="FREE_IMAGE_KEY")
LENSDUMP_KEY: str | None = Field(default=None, env="LENSDUMP_KEY")

PW_API_KEY: str | None = Field(default=None, env="PW_API_KEY")
PW_URL: str = Field(default="http://localhost:9696/api/v1", env="PW_URL")
FTPX_USER: str | None = Field(default=None, env="FTPX_USER")
Expand All @@ -129,8 +134,10 @@ class Config(BaseSettings):
QBIT_PORT: str | None = Field(default="8080", env="QBIT_PORT")

# USER PREFERENCES
IMGBB_PRIORITY: int = Field(default=0, env="IMGBB_PRIORITY")
FREE_IMAGE_PRIORITY: int = Field(default=1, env="FREE_IMAGE_PRIORITY")
IMGBB_PRIORITY: int = Field(default=2, env="IMGBB_PRIORITY")
LENSDUMP_PRIORITY: int = Field(default=2, env="IMGBB_PRIORITY")

DUPLICATE_ON: str = Field(default=False, env="DUPLICATE_ON")
NUMBER_OF_SCREENSHOTS: int = Field(default=6, env="NUMBER_OF_SCREENSHOTS")
COMPRESS_SCSHOT: int = Field(default=4, env="COMPRESS_SCSHOT")
Expand Down Expand Up @@ -218,6 +225,30 @@ def validate_freeimage_apikey(cls, value):
custom_console.bot_error_log("No FREE IMAGE API_KEY provided")
return value

@field_validator("LENSDUMP_KEY")
def validate_lensdump_apikey(cls, value):
if not value:
custom_console.bot_error_log("No LENSDUMP API_KEY provided")
return value

@field_validator("FREE_IMAGE_PRIORITY")
def validate_free_image_priority(cls, value):
if not isinstance(value, int) or not (0 <= value <= n_of_uploaders):
return cls.model_fields["FREE_IMAGE_PRIORITY"].default
return value

@field_validator("IMGBB_PRIORITY")
def validate_imgbb_priority(cls, value):
if not isinstance(value, int) or not (0 <= value <= n_of_uploaders):
return cls.model_fields["IMGBB_PRIORITY"].default
return value

@field_validator("LENSDUMP_PRIORITY")
def validate_lensdump_priority(cls, value):
if not isinstance(value, int) or not (0 <= value <= n_of_uploaders):
return cls.model_fields["LENSDUMP_PRIORITY"].default
return value

@field_validator("QBIT_USER")
def validate_qbit_user(cls, value):
if not value:
Expand All @@ -236,17 +267,6 @@ def validate_qbit_port(cls, value):
custom_console.bot_error_log("No QBIT_PORT provided")
return value

@field_validator("FREE_IMAGE_PRIORITY")
def validate_free_image_priority(cls, value):
if not isinstance(value, int) or not (1 <= value <= 2):
return cls.model_fields["FREE_IMAGE_PRIORITY"].default
return value

@field_validator("IMGBB_PRIORITY")
def validate_imgbb_priority(cls, value):
if not isinstance(value, int) or not (1 <= value <= 2):
return cls.model_fields["IMGBB_PRIORITY"].default
return value

@field_validator("DUPLICATE_ON")
def validate_duplicate_on(cls, value):
Expand Down
52 changes: 38 additions & 14 deletions common/external_services/imageHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ def __init__(self, image: bytes, key: str):

@abstractmethod
def get_endpoint(self):
return "https://api.imgbb.com/1/upload"
pass

@abstractmethod
def get_params(self):
return {
"key": self.key,
}
def get_data(self):
pass

@abstractmethod
def get_field_name(self):
pass

def upload(self):
params = self.get_params()
data = self.get_data()
files = {
"image": (None, self.image),
self.get_field_name(): (None, self.image),
}

upload_n = 0
while upload_n < 4:
try:
upload_n += 1
response = requests.post(
self.get_endpoint(), params=params, files=files, timeout=10
self.get_endpoint(), data = data, files = files, timeout = 10
)
response.raise_for_status()
return response.json()
Expand Down Expand Up @@ -73,31 +75,49 @@ def handle_http_error(self, error, attempt):
custom_console.bot_error_log(f"HTTPError received: {error}")


class ImgBB(ImageUploader):

priority= config.IMGBB_PRIORITY
def get_endpoint(self) -> str:
return "https://api.imgbb.com/1/upload"

def get_data(self) -> dict:
return {
"key": self.key,
}

def get_field_name(self) -> str:
return 'image'

class Freeimage(ImageUploader):

priority = config.FREE_IMAGE_PRIORITY

def get_endpoint(self) -> str:
return "https://freeimage.host/api/1/upload"

def get_params(self) -> dict:
def get_data(self) -> dict:
return {
"key": self.key,
"format": "json",
}

def get_field_name(self) -> str:
return 'image'

class ImgBB(ImageUploader):
class LensDump(ImageUploader):

priority= config.IMGBB_PRIORITY
priority= config.LENSDUMP_PRIORITY
def get_endpoint(self) -> str:
return "https://api.imgbb.com/1/upload"
return "https://lensdump.com/api/1/upload"

def get_params(self) -> dict:
def get_data(self) -> dict:
return {
"key": self.key,
}

def get_field_name(self) -> str:
return 'source'


class ImageUploaderFallback:
def __init__(self, uploader):
Expand Down Expand Up @@ -138,3 +158,7 @@ def result(response: dict, uploader_host: str) -> str:

if uploader_host == "ImgBB":
return response["data"]["display_url"]

if uploader_host == "LensDump":
return response['image']['url']

1 change: 1 addition & 0 deletions media_db/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ class Results:
date: str | None = None
not_resources: bool = False
keywords: bool = False
trailer_key: str | None = None
15 changes: 15 additions & 0 deletions media_db/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def start(self, file_name: str):
result = self.mytmdb.input_tmdb()

if result:
result.trailer_key = self.trailer(result.video_id)
backdrop_path = result.backdrop_path
poster_path = result.poster_path
overview = result.overview
Expand All @@ -59,6 +60,7 @@ def start(self, file_name: str):
custom_console.bot_log(f"[TMDB POSTER]............ {result.poster_path}")
custom_console.bot_log(f"[TMDB BACKDROP].......... {result.backdrop_path}")
# custom_console.bot_log(f"[TMDB KEYWORDS].......... {result.keywords}\n")
custom_console.bot_log(f"[TMDB TRAILER]........... https://www.youtube.com/watch?v={result.trailer_key}\n")

"""
# Print the list of search results
Expand All @@ -71,3 +73,16 @@ def start(self, file_name: str):
else:
custom_console.bot_log(f"Non trovo un ID valido per {file_name}")
sys.exit()


def trailer(self, video_id: int) -> str:
self.mytmdb.tmdb.language='it'
videos = self.mytmdb.tmdb.videos(video_id)
if not videos['results']:
self.mytmdb.tmdb.language = 'en'
videos = self.mytmdb.tmdb.videos(video_id)
trailer = next((video for video in videos['results'] if video['type'].lower() == 'trailer' and video['site'].lower() == 'youtube'), None)
if trailer:
return trailer['key']


10 changes: 7 additions & 3 deletions unit3dup/media_manager/VideoManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ def tmdb(self, content: Contents):

return tv_show_result

def _video_info(self):
return Video.info(self.file_name)
def _video_info(self, trailer_key):
# Add the trailer to the torrent page's description
video_info = Video.info(self.file_name)
video_info.description+=f'[center][youtube]{trailer_key}[/youtube][/center]'
return video_info


@staticmethod
def torrent(content: Contents):
Expand All @@ -127,7 +131,7 @@ def upload(self, content: Contents):

# Create a new payload
data = unit3d_up.payload(
tv_show=self.tv_show_result, video_info=self._video_info()
tv_show=self.tv_show_result, video_info=self._video_info(trailer_key=self.tv_show_result.trailer_key)
)

# Get a new tracker instance
Expand Down
4 changes: 3 additions & 1 deletion unit3dup/pvtVideo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from common.external_services.imageHost import ImgBB, Freeimage, ImageUploaderFallback
from common.external_services.imageHost import ImgBB, Freeimage, LensDump, ImageUploaderFallback
from common.mediainfo import MediaFile
from common.config import config
from common.frames import VideoFrame
Expand All @@ -22,6 +22,7 @@ def __init__(self, file_name: str):
# Host APi keys
self.IMGBB_KEY = config.IMGBB_KEY
self.FREE_IMAGE_KEY = config.FREE_IMAGE_KEY
self.LENSDUMP_KEY= config.LENSDUMP_KEY

# File name
self.file_name: str = file_name
Expand Down Expand Up @@ -83,6 +84,7 @@ def _description(self, extracted_frames: list) -> str:
master_uploaders = [
Freeimage(img_bytes, self.FREE_IMAGE_KEY),
ImgBB(img_bytes, self.IMGBB_KEY),
LensDump(img_bytes, self.LENSDUMP_KEY),
]

# Sorting list based on priority
Expand Down

0 comments on commit 1877789

Please sign in to comment.