Skip to content

Commit

Permalink
feat: send whole log file when using /logs, remove /logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Jun 24, 2024
1 parent 124d087 commit e7ebdbc
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.venv
.DS_Store
__pycache__
log
log.txt
session
temp
dev.*
Expand Down
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ A Telegram Bot to transfer files to OneDrive.
<summary>/clear</summary>
<img src="https://github.com/hlf20010508/telegram-onedrive/assets/76218469/86485b4f-57b5-4a03-b74b-3bffd2800582" alt="/clear">
</details>
<details>
<summary>/logs</summary>
<img src="https://github.com/hlf20010508/telegram-onedrive/assets/76218469/db07faa8-e8a9-4c4a-ae4f-bf1c88423280" alt="/logs">
</details>
<details>
<summary>/logs $range</summary>
<img src="https://github.com/hlf20010508/telegram-onedrive/assets/76218469/b373456e-2525-45ba-9859-9580a8f93d72" alt="/logs range">
</details>
<details>
<summary>/autoDelete</summary>
<img src="https://github.com/hlf20010508/telegram-onedrive/assets/76218469/ff564f9f-66b0-4296-afe4-e8e3cdf70428" alt="/autoDelete">
Expand Down Expand Up @@ -176,8 +168,7 @@ If you don't follow these steps, the bot may not works.
- `/drive logout $index` to logout specified OneDrive account.
- `/links $message_link $range` to transfer sequential restricted content.
- `/url $file_url` to upload the file through url.
- `/logs` to show all logs.
- `/logs $range` to show the most recent logs for the specified page number.
- `/logs` to send log file.
- `/logs clear` to clear logs.
- `/dir` to show current OneDrive directory.
- `/dir $remote_path` to set OneDrive directory.
Expand All @@ -191,7 +182,6 @@ The bot support files with extension `.t2o` as scripts. You can use them to auto
### Example
- `/links https://t.me/c/xxxxxxx/100 2` will transfer `https://t.me/c/xxxxxxx/100` and `https://t.me/c/xxxxxxx/101`.
- `/url https://example.com/file.txt` will upload `file.txt`. The headers of the file response must includes `Content-Length`.
- `/logs 2` will show 2 pages of the most recent logs. Each page contains 50 lines of logs.
- In a file named `example.t2o`, write these lines for example:
```
[link]
Expand Down
76 changes: 1 addition & 75 deletions modules/handlers/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,6 @@
from modules.res import logs_res


class Tail_File_Page:
def __init__(self, path, lines_per_page):
self.pos = 0
self.lines_per_page = lines_per_page
self.file = open(path, "rb")

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.file.close()

def read_all(self):
while True:
string = ""
for _ in range(self.lines_per_page):
line = self.file.readline().decode()
if line == "":
break
string += line
if string == "":
break
yield string

def read_pages(self, pages):
self._seek_lines(pages * self.lines_per_page)
while pages:
pages -= 1
string = self._read_lines(self.lines_per_page)
if string == "":
break
yield string

def _read_lines(self, lines_num):
string = ""
while lines_num:
lines_num -= 1
line = self.file.readline().decode()
if line == "":
break
string += line
return string

def _seek_lines(self, lines_num):
while lines_num:
try:
self.pos -= 1
self.file.seek(self.pos, os.SEEK_END)
if self.file.read(1) == b"\n":
lines_num -= 1
except:
self.file.seek(0, os.SEEK_SET)
break


@tg_bot.on(events.NewMessage(pattern="/logs", incoming=True, from_users=tg_user_name))
@check_in_group
@check_tg_login
Expand All @@ -83,12 +28,7 @@ async def logs_handler(event):

# /logs
if len(cmd) == 1:
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)
await asyncio.sleep(1)
await event.respond("Finished.")
await tg_bot.send_file(event.chat_id, log_path)

elif len(cmd) == 2:
sub_cmd = cmd[1]
Expand All @@ -100,20 +40,6 @@ async def logs_handler(event):
else:
await event.respond("Logs not found.")

# /logs $range
else:
try:
pages = int(sub_cmd)
except ValueError:
await event.reply("Logs page range should be integer.")
raise events.StopPropagation

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)
await asyncio.sleep(1)
await event.respond("Finished.")
else:
await event.respond(logs_res)
raise events.StopPropagation
2 changes: 1 addition & 1 deletion modules/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from traceback import print_exc
from io import StringIO

log_path = "log"
log_path = "log.txt"


def logger(message):
Expand Down
6 changes: 1 addition & 5 deletions modules/res.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
- /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.
- /logs to send log file.
- /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```
Expand Down Expand Up @@ -97,8 +95,6 @@
Usage:
```/logs```
```/logs $range```
```/logs clear```
"""

Expand Down

0 comments on commit e7ebdbc

Please sign in to comment.