mirror of
https://github.com/alfem/telegram-download-daemon.git
synced 2024-11-10 17:05:26 +08:00
Merge branch 'master' into filename-bug
This commit is contained in:
commit
674d1d0296
3 changed files with 151 additions and 19 deletions
93
.github/workflows/Docker-Publish.yml
vendored
Normal file
93
.github/workflows/Docker-Publish.yml
vendored
Normal file
|
@ -0,0 +1,93 @@
|
|||
name: Docker Publish
|
||||
|
||||
on:
|
||||
push:
|
||||
# Publish `master` as Docker `latest` image.
|
||||
branches:
|
||||
- master
|
||||
|
||||
# Publish `v1.2.3` tags as releases.
|
||||
tags:
|
||||
- v*
|
||||
|
||||
# Run tests for any PRs.
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
IMAGE_NAME: telegram-download-daemon
|
||||
|
||||
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
|
||||
|
||||
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: ${{ secrets.DOCKER_USERNAME }}/telegram-download-daemon
|
||||
tags: latest
|
||||
|
||||
|
||||
# Push image to GitHub Packages.
|
||||
# See also https://docs.docker.com/docker-hub/builds/
|
||||
pushGitHub:
|
||||
name: Push to Github Registry
|
||||
# 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" == "production" ] && VERSION=latest
|
||||
|
||||
echo IMAGE_ID=$IMAGE_ID
|
||||
echo VERSION=$VERSION
|
||||
|
||||
docker tag image $IMAGE_ID:$VERSION
|
||||
docker push $IMAGE_ID:$VERSION
|
|
@ -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}")
|
||||
|
|
|
@ -11,7 +11,8 @@ from telethon import TelegramClient, events
|
|||
from telethon.tl.types import PeerChannel, DocumentAttributeFilename
|
||||
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,22 +64,25 @@ 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)
|
||||
print("Hi! Ready for you files!")
|
||||
await client.send_message(entity, "Hi! Ready for you files!")
|
||||
|
||||
|
||||
async def log_respond(event, respond):
|
||||
print(respond)
|
||||
await event.respond(respond)
|
||||
async def log_reply(event : events.ChatAction.Event, reply):
|
||||
print(reply)
|
||||
await event.reply(reply)
|
||||
|
||||
def getFilename(event: events.NewMessage.Event):
|
||||
return next(x for x in event.media.document.attributes if isinstance(x, DocumentAttributeFilename)).file_name
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
@ -62,18 +93,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=getFilename(event)
|
||||
await log_respond(event, f"Downloading file {filename} ({event.media.document.size} bytes)")
|
||||
await log_reply(
|
||||
event,
|
||||
f"Downloading file {filename} ({event.media.document.size} bytes)"
|
||||
)
|
||||
|
||||
await client.download_media(event.message, downloadFolder)
|
||||
await log_respond(event, f"{filename} ready")
|
||||
await log_reply(event, f"{filename} ready")
|
||||
|
||||
async def start():
|
||||
await sendHelloMessage(client, peerChannel)
|
||||
await client.run_until_disconnected()
|
||||
|
||||
|
||||
client.loop.run_until_complete(start())
|
||||
|
|
Loading…
Reference in a new issue