File size: 5,822 Bytes
de1ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#    This file is part of the AutoAnime distribution.
#    Copyright (c) 2025 Kaif_00z
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3.
#
#    This program is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#    General Public License for more details.
#
# License can be found in <
# https://github.com/kaif-00z/AutoAnimeBot/blob/main/LICENSE > .

# if you are using this following code then don't forgot to give proper
# credit to t.me/kAiF_00z (github.com/kaif-00z)

import asyncio
import os
import secrets
import shutil
from glob import glob
from traceback import format_exc

from telethon import Button

from core.bot import LOGS, Bot, Var
from database import DataBase
from functions.info import AnimeInfo
from functions.tools import Tools
from libs.logger import Reporter


class Executors:
    def __init__(
        self,
        bot: Bot,
        dB: DataBase,
        configurations: dict,
        input_file: str,
        info: AnimeInfo,
        reporter: Reporter,
    ):
        self.is_original = configurations.get("original_upload")
        self.is_button = configurations.get("button_upload")
        self.anime_info = info
        self.bot = bot
        self.input_file = input_file
        self.tools = Tools()
        self.db = dB
        self.reporter = reporter
        self.msg_id = None
        self.output_file = None

    async def execute(self):
        try:
            rename = await self.anime_info.rename(self.is_original)
            self.output_file = f"encode/{rename}"
            thumb = await self.tools.cover_dl((await self.anime_info.get_poster()))
            if self.is_original:
                await self.reporter.started_renaming()
                succ, out = await self.tools.rename_file(
                    self.input_file, self.output_file
                )
                if not succ:
                    return False, out
            else:
                _log_msg = await self.reporter.started_compressing()
                succ, _new_msg = await self.tools.compress(
                    self.input_file, self.output_file, _log_msg
                )
                if not succ:
                    return False, _new_msg
                self.reporter.msg = _new_msg
            await self.reporter.started_uploading()
            if self.is_button:
                msg = await self.bot.upload_anime(
                    self.output_file, rename, thumb or "thumb.jpg", is_button=True
                )
                btn = Button.url(
                    f"{self.anime_info.data.get('video_resolution')}",
                    url=f"https://t.me/{((await self.bot.get_me()).username)}?start={msg.id}",
                )
                self.msg_id = msg.id
                return True, btn
            msg = await self.bot.upload_anime(
                self.output_file, rename, thumb or "thumb.jpg"
            )
            self.msg_id = msg.id
            return True, []
        except BaseException:
            await self.reporter.report_error(str(format_exc()), log=True)
            return False, str(format_exc())

    def run_further_work(self):
        asyncio.run(self.further_work())

    async def further_work(self):
        try:
            if self.msg_id:
                await self.reporter.started_gen_ss()
                msg = await self.bot.get_messages(
                    Var.BACKUP_CHANNEL if self.is_button else Var.MAIN_CHANNEL,
                    ids=self.msg_id,
                )
                btn = [
                    [],
                ]
                link_info = await self.tools.mediainfo(self.output_file, self.bot)
                if link_info:
                    btn.append(
                        [
                            Button.url(
                                "πŸ“œ MediaInfo",
                                url=link_info,
                            )
                        ]
                    )
                    await msg.edit(buttons=btn)
                _hash = secrets.token_hex(nbytes=7)
                ss_path, sp_path = await self.tools.gen_ss_sam(_hash, self.output_file)
                if ss_path and sp_path:
                    ss = await self.bot.send_message(
                        Var.CLOUD_CHANNEL,
                        file=glob(f"{ss_path}/*") or ["assest/poster_not_found.jpg"],
                    )
                    sp = await self.bot.send_message(
                        Var.CLOUD_CHANNEL,
                        file=sp_path,
                        thumb="thumb.jpg",
                        force_document=True,
                    )
                    await self.db.store_items(_hash, [[i.id for i in ss], [sp.id]])
                    btn.append(
                        [
                            Button.url(
                                "πŸ“Ί Sample & ScreenShots",
                                url=f"https://t.me/{((await self.bot.get_me()).username)}?start={_hash}",
                            )
                        ]
                    )
                    await msg.edit(buttons=btn)
                    await self.reporter.all_done()
                    try:
                        shutil.rmtree(_hash)
                        os.remove(sp_path)
                        os.remove(self.input_file)
                        os.remove(self.output_file)
                    except BaseException:
                        LOGS.error(str(format_exc()))
        except BaseException:
            await self.reporter.report_error(str(format_exc()), log=True)