reorder import, change code style, avoid /music and other commands in groups

This commit is contained in:
BennyThink 2021-05-05 12:07:03 +08:00
parent a02e304e3c
commit be5d94dc49
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
4 changed files with 36 additions and 6 deletions

View File

@ -6,11 +6,12 @@ RUN pip3 install --user -r /tmp/requirements.txt && rm /tmp/requirements.txt
FROM python:alpine
RUN apk update && apk add --no-cache ffmpeg
RUN apk update && apk add --no-cache ffmpeg git
COPY --from=builder /root/.local /usr/local
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY . /ytdl-bot
RUN cd /ytdl-bot && git submodule update --init --recursive && apk del git
WORKDIR /ytdl-bot
ENV TZ=Asia/Shanghai

View File

@ -9,15 +9,35 @@ Download videos from YouTube and other platforms through a Telegram Bot
Send link from YouTube directly to the bot.
Any platform [supported by youtube-dl](https://ytdl-org.github.io/youtube-dl/supportedsites.html) will also work.
# Feature
![](assets/1.jpeg)
1. fast download and upload. Many thanks to [FastTelethon](https://gist.github.com/painor/7e74de80ae0c819d3e9abcf9989a8dd6) and
[JasonKhew96](https://github.com/JasonKhew96)'s contribution on this!
2. ads free - I'll never send ads to you, also I don't even print logs that will identify you.
So feel free to download any type of video from any website.
3. support progress bar
# How to deploy?
## Normal
1. clone code and update submodule `git submodule update --init --recursive`
1. install Python 3.6+
2. pip3 install -r requirements.txt
3. set environment variables `TOKEN`, `APP_ID` and `APP_HASH`
4. `python3 bot.py`
5. supervisor on your own preference.
## docker
see [here](https://github.com/tgbot-collection/BotsRunner)
# commdands
```
start - let's start
about - wanna contribute?
ping - bot running status
help - it's not working?
ytdl - download video in group
```
# License
Apache License 2.0

BIN
assets/1.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

19
ytdl.py
View File

@ -18,15 +18,19 @@ import functools
import fakeredis
import youtube_dl
from FastTelethon.FastTelethon import upload_file
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from hachoir.metadata.video import MkvMetadata
from telethon import TelegramClient, events
from telethon.tl.types import DocumentAttributeFilename, DocumentAttributeVideo
from telethon.utils import get_input_media
from hachoir.metadata.video import MkvMetadata
from tgbot_ping import get_runtime
from FastTelethon.FastTelethon import upload_file
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
token = os.getenv("TOKEN") or "17Zg"
@ -141,9 +145,14 @@ async def echo_all(event):
chat_id = event.message.chat_id
url = re.sub(r'/ytdl\s*', '', event.message.text)
logging.info("start %s", url)
# if this is in a group/channel
if not event.message.is_private and not event.message.text.lower().startswith("/ytdl"):
logging.info("%s, it's annoying me...🙄️ ", event.message.text)
return
if not re.findall(r"^https?://", url.lower()):
await event.reply("I think you should send me a link. Don't you agree with me?")
return
message = await event.reply("Processing...")
temp_dir = tempfile.TemporaryDirectory()
@ -172,7 +181,7 @@ async def echo_all(event):
),
DocumentAttributeFilename(
os.path.basename(video_path)),
]
]
input_media.mime_type = mime_type
await bot.send_file(chat_id, input_media)
await bot.edit_message(chat_id, message, 'Download success!✅')
@ -193,13 +202,13 @@ def get_metadata(video_path):
duration=metadata.get('duration').seconds,
w=metadata['video[1]'].get('width'),
h=metadata['video[1]'].get('height')
), metadata.get('mime_type')
), metadata.get('mime_type')
else:
return dict(
duration=metadata.get('duration').seconds,
w=metadata.get('width'),
h=metadata.get('height')
), metadata.get('mime_type')
), metadata.get('mime_type')
except Exception as e:
logging.error(e)
return dict(duration=0, w=0, h=0), 'application/octet-stream'