From 1a967c5f41df7c1ad87b59c046170412a561c1d6 Mon Sep 17 00:00:00 2001 From: "Baruch Odem (Rothkoff)" Date: Sun, 26 Apr 2020 18:47:58 +0300 Subject: [PATCH 01/12] Create Docker-Publish.yml --- .github/workflows/Docker-Publish.yml | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 .github/workflows/Docker-Publish.yml diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml new file mode 100644 index 0000000..190c585 --- /dev/null +++ b/.github/workflows/Docker-Publish.yml @@ -0,0 +1,76 @@ +name: Docker Publish + +on: + push: + # Publish `master` as Docker `latest` image. + branches: + - production + + # Publish `v1.2.3` tags as releases. + tags: + - v* + + # Run tests for any PRs. + pull_request: + +env: + # TODO: Change variable to your image's name. + IMAGE_NAME: telegram-download-deamon + +jobs: + # Run tests. + # See also https://docs.docker.com/docker-hub/builds/automated-testing/ + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Run tests + run: | + if [ -f docker-compose.test.yml ]; then + docker-compose --file docker-compose.test.yml build + docker-compose --file docker-compose.test.yml run sut + else + docker build . --file Dockerfile + fi + + # Push image to GitHub Packages. + # See also https://docs.docker.com/docker-hub/builds/ + push: + # Ensure test job passes before pushing image. + needs: test + + runs-on: ubuntu-latest + if: github.event_name == 'push' + + steps: + - uses: actions/checkout@v2 + + - name: Build image + run: docker build . --file Dockerfile --tag image + + - name: Log into registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin + + - name: Push image + run: | + IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME + + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + + # Use Docker `latest` tag convention + [ "$VERSION" == "master" ] && VERSION=latest + + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + + docker tag image $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION From 4e7ddc5f76589c57396b84182c8d675dd3ddbc76 Mon Sep 17 00:00:00 2001 From: "Baruch Odem (Rothkoff)" Date: Sun, 26 Apr 2020 18:53:48 +0300 Subject: [PATCH 02/12] Fix latest tag --- .github/workflows/Docker-Publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index 190c585..39948b2 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -67,7 +67,7 @@ jobs: [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') # Use Docker `latest` tag convention - [ "$VERSION" == "master" ] && VERSION=latest + [ "$VERSION" == "production" ] && VERSION=latest echo IMAGE_ID=$IMAGE_ID echo VERSION=$VERSION From 9abb56ed5b460d04ef5931816d94ad5db4b2b634 Mon Sep 17 00:00:00 2001 From: "Baruch Odem (Rothkoff)" Date: Tue, 19 May 2020 18:33:19 +0300 Subject: [PATCH 03/12] Publish to DockerHub --- .github/workflows/Docker-Publish.yml | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index 39948b2..d439029 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -35,11 +35,29 @@ jobs: docker build . --file Dockerfile fi + pushDockerHub: + runs-on: ubuntu-latest + name: Push to Docker Hub + needs: [test] + if: github.event_name == 'push' + + steps: + - uses: actions/checkout@v2 + + - uses: docker/build-push-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + repository: ${{ github.actor }}/telegram-download-daemon + tags: latest + + # Push image to GitHub Packages. # See also https://docs.docker.com/docker-hub/builds/ - push: + pushGitHub: + name: Push to Github Registry # Ensure test job passes before pushing image. - needs: test + needs: [test] runs-on: ubuntu-latest if: github.event_name == 'push' From 0f5daacf7081b8b895967b94ce904230232476ff Mon Sep 17 00:00:00 2001 From: Baruch Rothkoff Date: Tue, 19 May 2020 18:38:27 +0300 Subject: [PATCH 04/12] Adjust for upstream --- .github/workflows/Docker-Publish.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index d439029..a6288d9 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -4,7 +4,7 @@ on: push: # Publish `master` as Docker `latest` image. branches: - - production + - master # Publish `v1.2.3` tags as releases. tags: @@ -14,7 +14,6 @@ on: pull_request: env: - # TODO: Change variable to your image's name. IMAGE_NAME: telegram-download-deamon jobs: @@ -48,7 +47,7 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - repository: ${{ github.actor }}/telegram-download-daemon + repository: ${{ github.actor }}/${{ IMAGE_NAME }} tags: latest @@ -66,7 +65,7 @@ jobs: - uses: actions/checkout@v2 - name: Build image - run: docker build . --file Dockerfile --tag image + run: docker build . --file Dockerfile --tag latest - name: Log into registry run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin From a171eda904c18f0ffcdb96e9a3c955ff8e9f8865 Mon Sep 17 00:00:00 2001 From: Baruch Rothkoff Date: Tue, 19 May 2020 18:45:11 +0300 Subject: [PATCH 05/12] Use docker username --- .github/workflows/Docker-Publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index a6288d9..0dae50b 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -47,7 +47,7 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - repository: ${{ github.actor }}/${{ IMAGE_NAME }} + repository: ${{ secrets.DOCKER_USERNAME }}/${{ IMAGE_NAME }} tags: latest From 241318a21ac563b4189e91af0e00ae26c3624245 Mon Sep 17 00:00:00 2001 From: Baruch Rothkoff Date: Tue, 19 May 2020 18:53:56 +0300 Subject: [PATCH 06/12] fix IMAGE_NAME --- .github/workflows/Docker-Publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index 0dae50b..aebe5ab 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -47,7 +47,7 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - repository: ${{ secrets.DOCKER_USERNAME }}/${{ IMAGE_NAME }} + repository: ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME tags: latest From 90b65cd02de9c7a49cbaa3fe105cf72c91b1fafe Mon Sep 17 00:00:00 2001 From: Baruch Rothkoff Date: Tue, 19 May 2020 19:00:00 +0300 Subject: [PATCH 07/12] Can't pass env var as input --- .github/workflows/Docker-Publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index aebe5ab..c087892 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -47,7 +47,7 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - repository: ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME + repository: ${{ secrets.DOCKER_USERNAME }}/telegram-download-deamon tags: latest From 9b87086b9bbf8a2b0d994307a79e9c7e2f766dca Mon Sep 17 00:00:00 2001 From: Baruch Rothkoff Date: Tue, 19 May 2020 19:04:00 +0300 Subject: [PATCH 08/12] fix image --- .github/workflows/Docker-Publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index c087892..013594e 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -65,7 +65,7 @@ jobs: - uses: actions/checkout@v2 - name: Build image - run: docker build . --file Dockerfile --tag latest + run: docker build . --file Dockerfile --tag image - name: Log into registry run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin From 7392526441ec188e20b3559bd22d5e83bc83f86c Mon Sep 17 00:00:00 2001 From: Baruch Odem Date: Sun, 24 May 2020 18:21:24 +0300 Subject: [PATCH 09/12] reply instead of respond --- telegram-download-daemon.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/telegram-download-daemon.py b/telegram-download-daemon.py index 82b35fa..67dcee4 100644 --- a/telegram-download-daemon.py +++ b/telegram-download-daemon.py @@ -40,9 +40,9 @@ client = TelegramClient(session, api_id, api_hash, proxy=proxy).start() @client.on(events.NewMessage()) async def handler(event): - async def log_respond(respond): - print(respond) - await event.respond(respond) + async def log_reply(reply): + print(reply) + await event.reply(reply) if event.to_id != PeerChannel(channel_id): return @@ -51,10 +51,10 @@ async def handler(event): if event.media: filename=event.media.document.attributes[0].file_name - log_respond(f"Downloading file {filename} ({event.media.document.size} bytes)") + log_reply(f"Downloading file {filename} ({event.media.document.size} bytes)") await client.download_media(event.message, downloadFolder) - log_respond(f"{filename} ready") + log_reply(f"{filename} ready") with client: From 92cba7798e8befc7082b3e699c76ea4d2bad6a9e Mon Sep 17 00:00:00 2001 From: Baruch Odem Date: Sun, 24 May 2020 18:32:29 +0300 Subject: [PATCH 10/12] Yapf default formatting --- sessionManager.py | 11 +++++-- telegram-download-daemon.py | 62 ++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/sessionManager.py b/sessionManager.py index 98a531d..5d8a53e 100644 --- a/sessionManager.py +++ b/sessionManager.py @@ -5,8 +5,10 @@ TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH") sessionName = "DownloadDaemon" stringSessionFilename = f"{sessionName}.session" + def _getStringSessionIfExists(): - sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH, stringSessionFilename) + sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH, + stringSessionFilename) if path.isfile(sessionPath): with open(sessionPath, 'r') as file: session = file.read() @@ -14,15 +16,18 @@ def _getStringSessionIfExists(): return session return None + def getSession(): if TELEGRAM_DAEMON_SESSION_PATH == None: return sessionName - + return StringSession(_getStringSessionIfExists()) + def saveSession(session): if TELEGRAM_DAEMON_SESSION_PATH != None: - sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH, stringSessionFilename) + sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH, + stringSessionFilename) with open(sessionPath, 'w') as file: file.write(StringSession.save(session)) print(f"Session saved in {sessionPath}") diff --git a/telegram-download-daemon.py b/telegram-download-daemon.py index 61e8c0b..23039cd 100644 --- a/telegram-download-daemon.py +++ b/telegram-download-daemon.py @@ -11,7 +11,8 @@ from telethon import TelegramClient, events from telethon.tl.types import PeerChannel import logging -logging.basicConfig(format='[%(levelname) 5s/%(asctime)s]%(name)s:%(message)s',level=logging.WARNING) +logging.basicConfig(format='[%(levelname) 5s/%(asctime)s]%(name)s:%(message)s', + level=logging.WARNING) import argparse import asyncio @@ -22,11 +23,38 @@ TELEGRAM_DAEMON_CHANNEL = getenv("TELEGRAM_DAEMON_CHANNEL") TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH") -parser = argparse.ArgumentParser(description="Script to download files from Telegram Channel.") -parser.add_argument("--api-id", required=TELEGRAM_DAEMON_API_ID == None, type=int, default=TELEGRAM_DAEMON_API_ID, help='api_id from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_ID env var)') -parser.add_argument("--api-hash", required=TELEGRAM_DAEMON_API_HASH == None, type=str, default=TELEGRAM_DAEMON_API_HASH, help='api_hash from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_HASH env var)') -parser.add_argument("--dest", type=str, default=getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads"), help='Destenation path for downloading files (default is /telegram-downloads).') -parser.add_argument("--channel", required=TELEGRAM_DAEMON_CHANNEL == None, type=int, default=TELEGRAM_DAEMON_CHANNEL, help='Channel id to download from it (default is TELEGRAM_DAEMON_CHANNEL env var') +parser = argparse.ArgumentParser( + description="Script to download files from Telegram Channel.") +parser.add_argument( + "--api-id", + required=TELEGRAM_DAEMON_API_ID == None, + type=int, + default=TELEGRAM_DAEMON_API_ID, + help= + 'api_id from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_ID env var)' +) +parser.add_argument( + "--api-hash", + required=TELEGRAM_DAEMON_API_HASH == None, + type=str, + default=TELEGRAM_DAEMON_API_HASH, + help= + 'api_hash from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_HASH env var)' +) +parser.add_argument( + "--dest", + type=str, + default=getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads"), + help= + 'Destenation path for downloading files (default is /telegram-downloads).') +parser.add_argument( + "--channel", + required=TELEGRAM_DAEMON_CHANNEL == None, + type=int, + default=TELEGRAM_DAEMON_CHANNEL, + help= + 'Channel id to download from it (default is TELEGRAM_DAEMON_CHANNEL env var' +) args = parser.parse_args() api_id = args.api_id @@ -36,18 +64,21 @@ downloadFolder = args.dest # Edit these lines: proxy = None -# End of interesting parameters + +# End of interesting parameters async def sendHelloMessage(client, peerChannel): entity = await client.get_entity(peerChannel) await client.send_message(entity, "Hi! Ready for you files!") + async def log_respond(event, respond): print(respond) await event.respond(respond) -with TelegramClient(getSession(), api_id, api_hash, proxy=proxy).start() as client: +with TelegramClient(getSession(), api_id, api_hash, + proxy=proxy).start() as client: saveSession(client.session) @@ -58,18 +89,21 @@ with TelegramClient(getSession(), api_id, api_hash, proxy=proxy).start() as clie if event.to_id != peerChannel: return - + print(event) - + if event.media: - filename=event.media.document.attributes[0].file_name - await log_respond(event, f"Downloading file {filename} ({event.media.document.size} bytes)") + filename = event.media.document.attributes[0].file_name + await log_respond( + event, + f"Downloading file {filename} ({event.media.document.size} bytes)" + ) await client.download_media(event.message, downloadFolder) await log_respond(event, f"{filename} ready") - + async def start(): await sendHelloMessage(client, peerChannel) await client.run_until_disconnected() - + client.loop.run_until_complete(start()) From adab6ccf9a72c8d8c6d04458e0a7f146fbfefc8f Mon Sep 17 00:00:00 2001 From: "Alfonso E.M" Date: Sun, 24 May 2020 18:25:39 +0200 Subject: [PATCH 11/12] Fix small typo --- telegram-download-daemon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram-download-daemon.py b/telegram-download-daemon.py index a20f857..a45a95f 100644 --- a/telegram-download-daemon.py +++ b/telegram-download-daemon.py @@ -69,7 +69,7 @@ proxy = None # End of interesting parameters async def sendHelloMessage(client, peerChannel): entity = await client.get_entity(peerChannel) - await client.send_message(entity, "Hi! Ready for you files!") + await client.send_message(entity, "Hi! Ready for your files!") async def log_reply(event : events.ChatAction.Event, reply): From c65c893f5c311fe7e32377d2a957a2271d8909c4 Mon Sep 17 00:00:00 2001 From: "Alfonso E.M" Date: Sun, 24 May 2020 18:34:44 +0200 Subject: [PATCH 12/12] fix 'deamon' typo in docker publish action --- .github/workflows/Docker-Publish.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Docker-Publish.yml b/.github/workflows/Docker-Publish.yml index 013594e..7ae71d9 100644 --- a/.github/workflows/Docker-Publish.yml +++ b/.github/workflows/Docker-Publish.yml @@ -14,7 +14,7 @@ on: pull_request: env: - IMAGE_NAME: telegram-download-deamon + IMAGE_NAME: telegram-download-daemon jobs: # Run tests. @@ -47,7 +47,7 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - repository: ${{ secrets.DOCKER_USERNAME }}/telegram-download-deamon + repository: ${{ secrets.DOCKER_USERNAME }}/telegram-download-daemon tags: latest