Skip to content

Commit

Permalink
refactor: capitalize global variable, seperate res
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Dec 29, 2023
1 parent 21098fa commit f3b9c95
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 153 deletions.
6 changes: 3 additions & 3 deletions modules/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
143 changes: 8 additions & 135 deletions modules/global_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down Expand Up @@ -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```
'''
]
4 changes: 2 additions & 2 deletions modules/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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!"))
Expand Down
2 changes: 1 addition & 1 deletion modules/handlers/dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion modules/handlers/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion modules/handlers/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions modules/handlers/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modules/handlers/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion modules/handlers/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions modules/onedrive/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
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):
pass


class SQLiteSession(Session):
db = Database(path=od_session_path)
db = Database(path=OD_SESSION_PATH)

def __init__(
self,
Expand Down
Loading

0 comments on commit f3b9c95

Please sign in to comment.