Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would you save message content nearby downloaded file? #51

Open
kluger opened this issue Apr 24, 2021 · 1 comment
Open

Would you save message content nearby downloaded file? #51

kluger opened this issue Apr 24, 2021 · 1 comment

Comments

@kluger
Copy link

kluger commented Apr 24, 2021

Hello, @alfem !

I did some changes to save messages with downloading files and some debug info send back to telegram message. I did it because messages could contains some sensitive info like password for archive :) See diff bellow. Also i did somewhere safe concatenation dir and filename with os module.

diff --git a/telegram-download-daemon.py b/telegram-download-daemon.py
index 36ece73..2da9ffc 100644
--- a/telegram-download-daemon.py
+++ b/telegram-download-daemon.py
@@ -3,8 +3,8 @@
 # Author: Alfonso E.M. <[email protected]>
 # You need to install telethon (and cryptg to speed up downloads)
 
-from os import getenv
-from shutil import move
+import os
+import shutil
 import subprocess
 import math
 import time
@@ -25,14 +25,14 @@ import asyncio
 
 TDD_VERSION="1.4"
 
-TELEGRAM_DAEMON_API_ID = getenv("TELEGRAM_DAEMON_API_ID")
-TELEGRAM_DAEMON_API_HASH = getenv("TELEGRAM_DAEMON_API_HASH")
-TELEGRAM_DAEMON_CHANNEL = getenv("TELEGRAM_DAEMON_CHANNEL")
+TELEGRAM_DAEMON_API_ID = os.getenv("TELEGRAM_DAEMON_API_ID")
+TELEGRAM_DAEMON_API_HASH = os.getenv("TELEGRAM_DAEMON_API_HASH")
+TELEGRAM_DAEMON_CHANNEL = os.getenv("TELEGRAM_DAEMON_CHANNEL")
 
-TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH")
+TELEGRAM_DAEMON_SESSION_PATH = os.getenv("TELEGRAM_DAEMON_SESSION_PATH")
 
-TELEGRAM_DAEMON_DEST=getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads")
-TELEGRAM_DAEMON_TEMP=getenv("TELEGRAM_DAEMON_TEMP", "")
+TELEGRAM_DAEMON_DEST=os.getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads")
+TELEGRAM_DAEMON_TEMP=os.getenv("TELEGRAM_DAEMON_TEMP", "")
 
 TELEGRAM_DAEMON_TEMP_SUFFIX="tdd"
 
@@ -184,6 +184,7 @@ with TelegramClient(getSession(), api_id, api_hash,
                 print('Events handler error: ', e)
 
     async def worker():
+        global downloadFolder
         while True:
             try:
                 element = await queue.get()
@@ -201,12 +202,34 @@ with TelegramClient(getSession(), api_id, api_hash,
 
                 await client.download_media(event.message, "{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), progress_callback = download_callback)
                 set_progress(filename, message, 100, 100)
-                move("{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename))
+                if event.message.message:
+                    try:
+                        _dir = os.path.splitext(filename)[0]
+                    except:
+                        _dir = filename
+                    downloadFolder = os.path.join(downloadFolder, _dir)
+                    if not os.path.exists(downloadFolder):
+                        os.makedirs(downloadFolder)
+                    with open(os.path.join(downloadFolder, "message.txt"), "w") as f:
+                        f.write(f'{event.message.message}\n')
+                try:
+                    move_from = os.path.join(tempFolder, "{}.{}".format(filename,TELEGRAM_DAEMON_TEMP_SUFFIX))
+                    move_to = os.path.join(downloadFolder, filename)
+                    if os.path.exists(move_from):
+                        shutil.move(move_from, move_to)
+                    else:
+                        set_progress(filename, f"error: tmpfile for '{filename}' doesn't exist!", 0, 0)
+                except Exception as exc:
+                    set_progress(filename, f"shutil error: {str(exc)}", 0, 0)
+                    print('shutil error: ', e)
+
                 await log_reply(message, "{0} ready".format(filename))
 
                 queue.task_done()
             except Exception as e:
+                set_progress(filename, f"Queue worker error: {str(e)}", 0, 0)
                 print('Queue worker error: ', e)
+
  
     async def start():
@kluger kluger changed the title Would you save message content nearby downloading file? Would you save message content nearby downloaded file? Apr 24, 2021
@alfem
Copy link
Owner

alfem commented Apr 24, 2021

Thank for this contribution. It looks nice.

Could you create a pull request using development branch so I can merge and test your changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants