from os import getpid, path from datetime import datetime from sys import exit from time import time from modules.cli import * from modules.logger import logWrite from modules.scheduler import scheduler from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale # Import =================================================================================================================================== try: from pyrogram.sync import idle from pyrogram.errors import bad_request_400 from dateutil.relativedelta import relativedelta from modules.app import app except ModuleNotFoundError: print(locale("deps_missing", "console", locale=configGet("locale")), flush=True) exit() # =========================================================================================================================================== pid = getpid() # Work in progress # def check_forwards(app): # try: # index = jsonLoad(configGet("index", "locations")) # channel = app.get_chat(configGet("channel", "posting")) # peer = app.resolve_peer(configGet("channel", "posting")) # print(peer, flush=True) # posts_list = [i for i in range(index["last_id"]-100,index["last_id"])] # last_posts = app.get_messages(configGet("channel", "posting"), message_ids=posts_list) # for post in last_posts: # post_forwards = GetMessagePublicForwards(channel=peer, msg_id=post.id, offset_peer=peer, offset_rate=0, offset_id=0, limit=100) # print(post_forwards, flush=True) # for forward in post_forwards: # print(forward, flush=True) # except Exception as exp: # logWrite("Could not get last posts forwards due to {0} with traceback {1}".format(str(exp), traceback.format_exc()), debug=True) # if configGet("error", "reports"): # app.send_message(configGet("admin"), traceback.format_exc()) # pass # Work in progress # @app.on_message(~ filters.scheduled & filters.command(["forwards"], prefixes="/")) # def cmd_forwards(app, msg): # check_forwards(app) # Imports ================================================================================================================================== from plugins.commands.general import * from plugins.callbacks.shutdown import * if configGet("submit", "mode"): from plugins.callbacks.nothing import * from plugins.callbacks.submission import * from plugins.commands.mode_submit import * from plugins.handlers.submission import * if configGet("post", "mode"): from plugins.commands.photos import * # if configGet("api_based", "mode"): # from modules.api_client import authorize # =========================================================================================================================================== # Work in progress # Handle new forwards # @app.on_raw_update() # def fwd_got(app, update, users, chats): # if isinstance(update, UpdateChannelMessageForwards): # logWrite(f'Forward count increased to {update["forwards"]} on post {update["id"]} in channel {update["channel_id"]}') # logWrite(str(users), debug=True) # logWrite(str(chats), debug=True) # # else: # # logWrite(f"Got raw update of type {type(update)} with contents {update}", debug=True) # async def main(): # await app.start() # logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid))) # if configGet("startup", "reports"): # await app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid))) # if configGet("post", "mode"): # scheduler.start() # if configGet("api_based", "mode"): # token = authorize() # if len(get(f'{configGet("address", "posting", "api")}/albums?q={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}).json()["results"]) == 0: # post(f'{configGet("address", "posting", "api")}/albums?name={configGet("queue", "posting", "api", "albums")}&title={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}) # await idle() # await app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) # logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) # killProc(pid) # if __name__ == "__main__": # if find_spec("uvloop") is not None: # uvloop.install() # asyncio.run(main()) if __name__ == "__main__": logWrite(locale("startup", "console").format(str(pid))) app.start() if configGet("startup", "reports"): try: if path.exists(path.join(configGet("cache", "locations"), "shutdown_time")): downtime = relativedelta( datetime.now(), datetime.fromtimestamp( jsonLoad( path.join(configGet("cache", "locations"), "shutdown_time") )["timestamp"] ), ) if downtime.days >= 1: app.send_message( configGet("owner"), locale( "startup_downtime_days", "message", ).format(pid, downtime.days), ) elif downtime.hours >= 1: app.send_message( configGet("owner"), locale( "startup_downtime_hours", "message", ).format(pid, downtime.hours), ) else: app.send_message( configGet("owner"), locale( "startup_downtime_minutes", "message", locale=configGet("locale"), ).format(pid, downtime.minutes), ) else: app.send_message( configGet("owner"), locale("startup", "message").format(pid), ) except bad_request_400.PeerIdInvalid: logWrite( f"Could not send startup message to bot owner. Perhaps user has not started the bot yet." ) if configGet("post", "mode"): scheduler.start() # if configGet("api_based", "mode"): # token = await authorize() # if len(get(f'{configGet("address", "posting", "api")}/albums?q={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}).json()["results"]) == 0: # post(f'{configGet("address", "posting", "api")}/albums?name={configGet("queue", "posting", "api", "albums")}&title={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}) idle() try: app.send_message( configGet("owner"), locale("shutdown", "message").format(pid), ) except bad_request_400.PeerIdInvalid: logWrite( f"Could not send shutdown message to bot owner. Perhaps user has not started the bot yet." ) makedirs(configGet("cache", "locations"), exist_ok=True) jsonSave( {"timestamp": time()}, path.join(configGet("cache", "locations"), "shutdown_time"), ) logWrite(locale("shutdown", "console").format(str(pid))) scheduler.shutdown() killProc(pid)