From f3b9c956394f1803ffe855118c7cf7ae6ec7c087 Mon Sep 17 00:00:00 2001 From: L-ING Date: Fri, 29 Dec 2023 19:05:24 +0800 Subject: [PATCH] refactor: capitalize global variable, seperate res --- modules/client.py | 6 +- modules/global_var.py | 143 ++---------------------------------- modules/handlers/auth.py | 4 +- modules/handlers/dir.py | 2 +- modules/handlers/help.py | 2 +- modules/handlers/links.py | 2 +- modules/handlers/logs.py | 7 +- modules/handlers/start.py | 2 +- modules/handlers/url.py | 2 +- modules/onedrive/session.py | 4 +- modules/res.py | 132 +++++++++++++++++++++++++++++++++ modules/utils.py | 7 +- 12 files changed, 160 insertions(+), 153 deletions(-) create mode 100644 modules/res.py diff --git a/modules/client.py b/modules/client.py index 6b9b6b1..8e36f32 100644 --- a/modules/client.py +++ b/modules/client.py @@ -9,12 +9,12 @@ from telethon import TelegramClient from modules.onedrive import Onedrive from modules.env import tg_api_id, tg_api_hash, tg_bot_token, od_client_id, od_client_secret, server_uri -from modules.global_var import tg_bot_session_path, tg_client_session_path +from modules.global_var import TG_BOT_SESSION_PATH, TG_CLIENT_SESSION_PATH def init_tg_client(): - return TelegramClient(tg_client_session_path, tg_api_id, tg_api_hash, sequential_updates=True) + return TelegramClient(TG_CLIENT_SESSION_PATH, tg_api_id, tg_api_hash, sequential_updates=True) -tg_bot = TelegramClient(tg_bot_session_path, tg_api_id, tg_api_hash, sequential_updates=True).start( +tg_bot = TelegramClient(TG_BOT_SESSION_PATH, tg_api_id, tg_api_hash, sequential_updates=True).start( bot_token=tg_bot_token ) diff --git a/modules/global_var.py b/modules/global_var.py index 48c6e50..cf2160c 100644 --- a/modules/global_var.py +++ b/modules/global_var.py @@ -5,19 +5,19 @@ :license: MIT, see LICENSE for more details. """ -tg_login_max_attempts = 3 +TG_LOGIN_MAX_ATTEMPTS = 3 PART_SIZE = 2 * 1024 * 1024 -file_param_name_list = ['name', 'filename', 'file_name', 'title', 'file'] +FILE_PARAM_NAME_LIST = ['name', 'filename', 'file_name', 'title', 'file'] -logs_lines_per_page = 50 +LOGS_LINES_PER_PAGE = 50 -tg_bot_session_path = 'session/bot.session' -tg_client_session_path = 'session/user.session' -od_session_path = 'session/onedrive.session' +TG_BOT_SESSION_PATH = 'session/bot.session' +TG_CLIENT_SESSION_PATH = 'session/user.session' +OD_SESSION_PATH = 'session/onedrive.session' -base_headers = { +BASE_HEADERS = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15' } @@ -50,131 +50,4 @@ 'LPT8', 'LPT9', 'desktop.ini' -] - - -start_res = ''' -Transfer files to Onedrive. - -Forward or upload files to me, or pass message link to transfer restricted content from group or channel. - -- /help: Ask for help. -''' - - -help_res = ''' -- /auth to authorize for Telegram and OneDrive. -- /clear to clear all history except status message. -- /autoDelete to toggle whether bot should auto delete message. -- /logs to show all logs. -- /drive to list all OneDrive accounts. -- /dir to show current OneDrive directory. - -```/links $message_link $range``` -To transfer sequential restricted content. -```/url $file_url``` -To upload file through url. -```/logs $range``` -To show the most recent logs for the specified page number. -```/logs clear``` -To clear logs. -```/drive add``` -To add a OneDrive account. -```/drive $index``` -To change the OneDrive account. -```/drive logout``` -To logout current OneDrive account. -```/drive logout $index``` -To logout specified OneDrive account. -```/dir $remote_path``` -To set OneDrive directory. -```/dir temp $remote_path``` -To set temporary OneDrive directory. -```/dir temp cancel``` -To restore OneDrive directory to the previous one. -```/dir reset``` -To reset OneDrive directory to default. - -- To transfer files, forward or upload to me. -- To transfer restricted content, right click the content, copy the message link, and send to me. -- Tap Status on replied status message to locate current job. -- Uploading through url will call Onedrive's API, which means Onedrive's server will visit the url and download the file for you. If the url is invalid to OneDrive, the bot will try using bot's uploader to transfer. -- Each log page contains 50 lines of logs. -''' - - -check_in_group_res = ''' -This bot must be used in a Group! - -Add this bot to a Group as Admin, and give it ability to Delete Messages. -''' - - -tg_not_login_res = ''' -You haven't logined to Telegram. -''' - - -od_not_login_res = ''' -You haven't logined to OneDrive. -''' - - -links_res = ''' -Command /links format wrong. - -Usage: -```/links $message_link $range``` -''' - - -url_res = ''' -Command /url format wrong. - -Usage: -```/url $file_url``` -''' - - -logs_res = ''' -Command /logs format wrong. - -Usage: -```/logs``` - -```/logs $range``` - -```/logs clear``` -''' - - -drive_res = ''' -Command /drive format wrong. - -Usage: -```/drive``` - -```/drive add``` - -```/drive $index``` - -```/drive logout``` - -```/drive logout $index``` -''' - - -dir_res = ''' -Command /dir format wrong. - -Usage: -```/dir``` - -```/dir reset``` - -```/dir $remote_path``` - -```/dir temp $remote_path``` - -```/dir temp cancel``` -''' \ No newline at end of file +] \ No newline at end of file diff --git a/modules/handlers/auth.py b/modules/handlers/auth.py index b36dcdc..9558995 100644 --- a/modules/handlers/auth.py +++ b/modules/handlers/auth.py @@ -15,7 +15,7 @@ from modules.utils import check_in_group from modules.client import tg_bot, tg_client, onedrive, init_tg_client from modules.log import logger -from modules.global_var import tg_login_max_attempts +from modules.global_var import TG_LOGIN_MAX_ATTEMPTS class Code_Callback: @@ -127,7 +127,7 @@ async def auth_handler(event, propagate=False): phone=tg_user_phone, password=tg_user_password, code_callback=tg_code_callback, - max_attempts=tg_login_max_attempts + max_attempts=TG_LOGIN_MAX_ATTEMPTS ) tg_client = _tg_client await conv.send_message(logger("Login to Telegram successful!")) diff --git a/modules/handlers/dir.py b/modules/handlers/dir.py index 709577c..e0c8f5f 100644 --- a/modules/handlers/dir.py +++ b/modules/handlers/dir.py @@ -9,7 +9,7 @@ from modules.client import tg_bot from modules.env import tg_user_name from modules.utils import check_in_group, check_tg_login, check_od_login, cmd_parser, CMDException -from modules.global_var import dir_res +from modules.res import dir_res from modules.onedrive.utils import Dir diff --git a/modules/handlers/help.py b/modules/handlers/help.py index 3a7097c..06e4227 100644 --- a/modules/handlers/help.py +++ b/modules/handlers/help.py @@ -8,7 +8,7 @@ from telethon import events from modules.client import tg_bot from modules.env import tg_user_name -from modules.global_var import help_res +from modules.res import help_res from modules.utils import check_in_group diff --git a/modules/handlers/links.py b/modules/handlers/links.py index e94b22a..77d5685 100644 --- a/modules/handlers/links.py +++ b/modules/handlers/links.py @@ -9,7 +9,7 @@ from modules.client import tg_bot, tg_client from modules.env import tg_user_name from modules.utils import check_in_group, check_tg_login, check_od_login, cmd_parser, delete_message -from modules.global_var import links_res +from modules.res import links_res @tg_bot.on(events.NewMessage(pattern="/links", incoming=True, from_users=tg_user_name)) diff --git a/modules/handlers/logs.py b/modules/handlers/logs.py index 720dba2..2fb6bb6 100644 --- a/modules/handlers/logs.py +++ b/modules/handlers/logs.py @@ -12,7 +12,8 @@ from modules.env import tg_user_name from modules.utils import check_in_group, check_tg_login, check_od_login, cmd_parser from modules.log import log_path -from modules.global_var import logs_res, logs_lines_per_page +from modules.global_var import LOGS_LINES_PER_PAGE +from modules.res import logs_res class Tail_File_Page: @@ -82,7 +83,7 @@ async def logs_handler(event): # /logs if len(cmd) == 1: - with Tail_File_Page(log_path, logs_lines_per_page) as file: + with Tail_File_Page(log_path, LOGS_LINES_PER_PAGE) as file: await event.respond('Outputting logs...') for logs in file.read_all(): await event.respond(logs) @@ -107,7 +108,7 @@ async def logs_handler(event): await event.reply('Logs page range should be integer.') raise events.StopPropagation - with Tail_File_Page(log_path, logs_lines_per_page) as file: + with Tail_File_Page(log_path, LOGS_LINES_PER_PAGE) as file: await event.respond('Outputting logs...') for logs in file.read_pages(pages): await event.respond(logs) diff --git a/modules/handlers/start.py b/modules/handlers/start.py index cd40ff3..4618b27 100644 --- a/modules/handlers/start.py +++ b/modules/handlers/start.py @@ -8,7 +8,7 @@ from telethon import events from modules.client import tg_bot from modules.env import tg_user_name -from modules.global_var import start_res +from modules.res import start_res from modules.utils import check_in_group diff --git a/modules/handlers/url.py b/modules/handlers/url.py index 8c34d01..fd7cd59 100644 --- a/modules/handlers/url.py +++ b/modules/handlers/url.py @@ -22,7 +22,7 @@ ) from modules.log import logger from modules.transfer import multi_parts_uploader_from_url -from modules.global_var import url_res +from modules.res import url_res @tg_bot.on(events.NewMessage(pattern="/url", incoming=True, from_users=tg_user_name)) diff --git a/modules/onedrive/session.py b/modules/onedrive/session.py index 2c6fcbc..3f15330 100644 --- a/modules/onedrive/session.py +++ b/modules/onedrive/session.py @@ -8,7 +8,7 @@ import sqlite3 from onedrivesdk.session import Session from modules.onedrive.database import Database, UserNotFoundException -from modules.global_var import od_session_path +from modules.global_var import OD_SESSION_PATH class NoSessionExecption(Exception): @@ -16,7 +16,7 @@ class NoSessionExecption(Exception): class SQLiteSession(Session): - db = Database(path=od_session_path) + db = Database(path=OD_SESSION_PATH) def __init__( self, diff --git a/modules/res.py b/modules/res.py new file mode 100644 index 0000000..f54a685 --- /dev/null +++ b/modules/res.py @@ -0,0 +1,132 @@ +""" +:project: telegram-onedrive +:author: L-ING +:copyright: (C) 2023 L-ING +:license: MIT, see LICENSE for more details. +""" + +start_res = ''' +Transfer files to Onedrive. + +Forward or upload files to me, or pass message link to transfer restricted content from group or channel. + +- /help: Ask for help. +''' + + +help_res = ''' +- /auth to authorize for Telegram and OneDrive. +- /clear to clear all history except status message. +- /autoDelete to toggle whether bot should auto delete message. +- /logs to show all logs. +- /drive to list all OneDrive accounts. +- /dir to show current OneDrive directory. + +```/links $message_link $range``` +To transfer sequential restricted content. +```/url $file_url``` +To upload file through url. +```/logs $range``` +To show the most recent logs for the specified page number. +```/logs clear``` +To clear logs. +```/drive add``` +To add a OneDrive account. +```/drive $index``` +To change the OneDrive account. +```/drive logout``` +To logout current OneDrive account. +```/drive logout $index``` +To logout specified OneDrive account. +```/dir $remote_path``` +To set OneDrive directory. +```/dir temp $remote_path``` +To set temporary OneDrive directory. +```/dir temp cancel``` +To restore OneDrive directory to the previous one. +```/dir reset``` +To reset OneDrive directory to default. + +- To transfer files, forward or upload to me. +- To transfer restricted content, right click the content, copy the message link, and send to me. +- Tap Status on replied status message to locate current job. +- Uploading through url will call Onedrive's API, which means Onedrive's server will visit the url and download the file for you. If the url is invalid to OneDrive, the bot will try using bot's uploader to transfer. +- Each log page contains 50 lines of logs. +''' + + +check_in_group_res = ''' +This bot must be used in a Group! + +Add this bot to a Group as Admin, and give it ability to Delete Messages. +''' + + +tg_not_login_res = ''' +You haven't logined to Telegram. +''' + + +od_not_login_res = ''' +You haven't logined to OneDrive. +''' + + +links_res = ''' +Command /links format wrong. + +Usage: +```/links $message_link $range``` +''' + + +url_res = ''' +Command /url format wrong. + +Usage: +```/url $file_url``` +''' + + +logs_res = ''' +Command /logs format wrong. + +Usage: +```/logs``` + +```/logs $range``` + +```/logs clear``` +''' + + +drive_res = ''' +Command /drive format wrong. + +Usage: +```/drive``` + +```/drive add``` + +```/drive $index``` + +```/drive logout``` + +```/drive logout $index``` +''' + + +dir_res = ''' +Command /dir format wrong. + +Usage: +```/dir``` + +```/dir reset``` + +```/dir $remote_path``` + +```/dir temp $remote_path``` + +```/dir temp cancel``` +''' \ No newline at end of file diff --git a/modules/utils.py b/modules/utils.py index 4e48067..0c575d8 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -14,7 +14,8 @@ from copy import copy from modules.client import tg_bot, tg_client, onedrive from modules.log import logger -from modules.global_var import check_in_group_res, tg_not_login_res, od_not_login_res, file_param_name_list, base_headers +from modules.global_var import FILE_PARAM_NAME_LIST, BASE_HEADERS +from modules.res import check_in_group_res, tg_not_login_res, od_not_login_res from modules.mime import mime_dict from modules.onedrive.utils import preprocess_url_file_name @@ -153,7 +154,7 @@ def get_filename_from_url(url): parsed_url = urlparse(url) captured_value_dict = parse_qs(parsed_url.query) for item_name in captured_value_dict: - if item_name.lower() in file_param_name_list: + if item_name.lower() in FILE_PARAM_NAME_LIST: name = captured_value_dict[item_name] break if not name: @@ -165,7 +166,7 @@ def get_filename_from_url(url): def get_filename(url): - headers = copy(base_headers) + headers = copy(BASE_HEADERS) # some video resource websites need Referer, or it will return 404 headers['Referer'] = url headers["Connection"] = "close"