TelegramPoster/poster.py

157 lines
6.2 KiB
Python
Raw Normal View History

2023-01-10 13:52:44 +02:00
from os import sep, remove, getpid
2022-08-31 15:31:24 +03:00
from shutil import move
2023-01-09 16:36:56 +02:00
from sys import exit
2022-11-03 13:10:59 +02:00
from argparse import ArgumentParser
from modules.logging import logWrite
2023-01-10 13:52:44 +02:00
from modules.scheduler import scheduler
from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale
# Args =====================================================================================================================================
2022-11-03 13:10:59 +02:00
parser = ArgumentParser(
prog = "Telegram Poster",
description = "Bot for posting some of your stuff and also receiving submissions."
)
parser.add_argument("-m", "--move-sent", action="store_true")
parser.add_argument("-c", "--cleanup", action="store_true")
parser.add_argument("--confirm", action="store_true")
parser.add_argument("-i", "--cleanup-index", action="store_true")
parser.add_argument("-n", "--norun", action="store_true")
args = parser.parse_args()
if args.move_sent:
for entry in jsonLoad(configGet("index", "locations"))["sent"]:
try:
2022-08-31 15:31:24 +03:00
move(configGet("queue", "locations")+sep+entry, configGet("sent", "locations")+sep+entry)
except FileNotFoundError:
logWrite(locale("move_sent_doesnt_exist", "console", locale=configGet("locale")).format(entry))
except Exception as exp:
logWrite(locale("move_sent_doesnt_exception", "console", locale=configGet("locale")).format(entry, exp))
logWrite(locale("move_sent_completed", "console", locale=configGet("locale")))
2022-11-03 13:10:59 +02:00
if args.cleanup:
if args.confirm:
index = jsonLoad(configGet("index", "locations"))
for entry in index["sent"]:
try:
try:
2022-08-31 15:31:24 +03:00
remove(configGet("queue", "locations")+sep+entry)
except FileNotFoundError:
pass
try:
2022-08-31 15:31:24 +03:00
remove(configGet("sent", "locations")+sep+entry)
except FileNotFoundError:
pass
except Exception as exp:
logWrite(locale("cleanup_exception", "console", locale=configGet("locale")).format(entry, exp))
jsonSave(index, jsonLoad(configGet("index", "locations")))
logWrite(locale("cleanup_completed", "console", locale=configGet("locale")))
else:
logWrite(locale("cleanup_unathorized", "console", locale=configGet("locale")))
2022-11-03 13:10:59 +02:00
if args.cleanup_index:
if args.confirm:
index = jsonLoad(configGet("index", "locations"))
index["sent"] = []
jsonSave(index, jsonLoad(configGet("index", "locations")))
logWrite(locale("cleanup_index_completed", "console", locale=configGet("locale")))
else:
logWrite(locale("cleanup_index_unathorized", "console", locale=configGet("locale")))
2022-11-03 13:10:59 +02:00
if args.norun:
2022-11-03 13:13:02 +02:00
logWrite(locale("passed_norun", "console", locale=configGet("locale_log")))
2022-08-31 15:31:24 +03:00
exit()
#===========================================================================================================================================
# Import ===================================================================================================================================
try:
2023-01-10 13:52:44 +02:00
from modules.app import app
from pyrogram import idle
except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
2022-08-31 15:31:24 +03:00
exit()
#===========================================================================================================================================
2022-08-31 15:31:24 +03:00
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"):
2023-01-10 13:52:44 +02:00
# 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)
2023-01-10 13:52:44 +02:00
# Imports =====================================================================================================================================
2023-01-10 13:52:44 +02:00
from modules.commands.general import *
from modules.commands_register import register_commands
2022-08-30 15:10:05 +03:00
if configGet("submit", "mode"):
2023-01-10 13:52:44 +02:00
from modules.callbacks.submission import *
from modules.commands.mode_submit import *
from modules.handlers.submission import *
#===========================================================================================================================================
2022-08-30 15:01:12 +03:00
# 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)
if __name__ == "__main__":
logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid)))
2023-01-10 13:52:44 +02:00
app.start()
if configGet("startup", "reports"):
2023-01-10 13:52:44 +02:00
app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid)))
2023-01-09 19:01:07 +02:00
if configGet("post", "mode"):
scheduler.start()
2023-01-10 13:52:44 +02:00
register_commands(app)
idle()
2023-01-10 13:52:44 +02:00
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)