Skip to content

Commit

Permalink
perf: offer more information of error
Browse files Browse the repository at this point in the history
- offer more information of error with details and analysis
- use pre-built cryptg docker image to accelerate building progress
  • Loading branch information
hlf20010508 committed Sep 14, 2023
1 parent 45bf165 commit 153d9a0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
9 changes: 2 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
FROM python:3.8.16-alpine3.17 AS cryptg_builder
RUN apk add --update --no-cache rustup build-base &&\
rustup-init -y &&\
source $HOME/.cargo/env &&\
pip install cryptg==0.4.0

FROM hlf01/cryptg:cryptg0.4.0-python3.8.16-alpine3.17 AS cryptg_builder
FROM python:3.8.16-alpine3.17
WORKDIR /telegram-onedrive
COPY ./ ./
RUN apk add --update --no-cache libgcc &&\
pip install --no-cache-dir telethon requests flask onedrivesdk==1.1.8
COPY --from=cryptg_builder /usr/local/lib/python3.8/site-packages/cryptg /usr/local/lib/python3.8/site-packages
COPY --from=cryptg_builder /cryptg /usr/local/lib/python3.8/site-packages
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Example:
- In the group or channel, forward or upload files(or videos, photos).
- If you want to transfer restricted content from a group or channel, right click the content, copy the message link, and send the link.
- Wait until the transfer completes. You can check status on pinned status message.
- Use `/help` for more information about other commands.
- Use `/help` for more information about other command.

## Preparation
1. Open `docker-compose.yml` and edit the environment config.
Expand Down
53 changes: 35 additions & 18 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,17 @@ async def url(event):
_url = cmd[0]
name = _url.split('/')[-1]
except:
await delete_message(event)
await event.respond('''
await event.reply('''
Command `/url` format wrong.
Usage: `/url` file_url
''')
raise events.StopPropagation

if not get_link(_url):
await event.reply(logger("Please offer an HTTP url."))
raise events.StopPropagation

try:
progress_url = onedrive.upload_from_url(_url)
print(progress_url)
Expand All @@ -343,16 +346,30 @@ async def url(event):

status = "Uploaded: %.2f%%" % float(progress['percentageComplete'])
logger(status)
logger("File uploaded to %s"%os.path.join(onedrive.remote_root_path, name))
msg_link = 'https://t.me/c/%d/%d'%(event.message.peer_id.channel_id, event.message.id)
await tg_bot.edit_message(status_bar, 'Status:\n\n%s\n\n%s'%(msg_link, status))
if not delete_flag:
await event.reply('Done.')
await delete_message(event)
await tg_bot.edit_message(status_bar, 'Status:\n\nNo job yet.')
if 'fail' not in str(progress) and 'error' not in str(progress):
logger("File uploaded to %s"%os.path.join(onedrive.remote_root_path, name))
msg_link = 'https://t.me/c/%d/%d'%(event.message.peer_id.channel_id, event.message.id)
await tg_bot.edit_message(status_bar, 'Status:\n\n%s\n\n%s'%(msg_link, status))
if not delete_flag:
await event.reply('Done.')
await delete_message(event)
await tg_bot.edit_message(status_bar, 'Status:\n\nNo job yet.')
else:
await event.reply(logger('Error: something is wrong\n\nResponse: %s' % progress))
await event.reply(logger("Analysis: try again later, or offer a proper url"))

except Exception as e:
logger(e)
await event.reply('Error: %s'%e)
await event.reply('Error: %s\nResponse: %s' % (logger(e), logger(progress)))
try:
if progress['errorCode'] == 'ParameterIsTooLong':
await event.reply(logger("Analysis: url too long.OneDrive API doesn't support long url."))
elif progress['errorCode'] == 'Forbidden':
await event.reply(logger("Analysis: url protocol is not HTTP, or the url has been forbidden."))
elif progress['errorCode'] == 'NotFound':
await event.reply(logger("Analysis: content not found."))
except Exception as e:
logger(e)

raise events.StopPropagation


Expand All @@ -370,8 +387,7 @@ async def links(event):
for offset in range(offsets):
await tg_client.send_message(event.chat_id, message='%s/%d'%(link_body, head_message_id + offset))
except:
await delete_message(event)
await event.respond('''
await event.reply('''
Command `/links` format wrong.
Usage: `/links` message_link range
Expand Down Expand Up @@ -418,8 +434,7 @@ async def callback(current, total):
await delete_message(message)
await tg_bot.edit_message(status_bar, "Status:\n\nNo job yet.")
except Exception as e:
logger(e)
await event.reply('Error: %s'%e)
await event.reply('Error: %s' % logger(e))

else:
msg_link = get_link(event.text)
Expand Down Expand Up @@ -454,10 +469,12 @@ async def callback(current, total):
await tg_bot.edit_message(status_bar, "Status:\n\nNo job yet.")

except Exception as e:
logger(e)
await event.reply('Error: %s'%e)
await event.reply('Error: %s' % logger(e))
else:
await event.reply("Message not found.")
await event.reply(logger("Message not found."))
else:
if event.text != '/auth':
await event.reply("Use /help for available command.")
raise events.StopPropagation


Expand Down
11 changes: 9 additions & 2 deletions log.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from datetime import datetime
from traceback import print_exc
from io import StringIO

log_path = 'log'

def logger(message):
with open(log_path, 'a') as log_file:
time = datetime.now()
message = '%s\n%s\n'%(time, message)
if isinstance(message, Exception):
message = StringIO()
print_exc(file=message)
message = message.getvalue()
message = '%s\n%s\n' % (time, message)
print(message, end='')
log_file.write(message)
log_file.write(message)
return message

0 comments on commit 153d9a0

Please sign in to comment.