group/channel join require

This commit is contained in:
BennyThink 2021-10-30 14:27:43 +08:00
parent 055b18514e
commit dfda37b6ea
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
4 changed files with 48 additions and 19 deletions

View File

@ -17,7 +17,7 @@ platform [supported by youtube-dl](https://ytdl-org.github.io/youtube-dl/support
Because I have limited resources, hundreds of Gigabytes doesn't sound like a sustainable solution.
**In that case, I added one limitation: 5GiB per 24 hours for each user. Might change in future**
**In that case, I added one limitation: 5 GiB per 24 hours for each user. Might change in future**
You can choose to become 'VIP' if you really need large traffic. And also, you could always deploy your own bot.
@ -80,11 +80,12 @@ vim ~/ytdl/env/ytdl.env
```
you can configure all the following environment variables:
* WORKERS: default 500
* APP_ID: **REQUIRED**
* APP_HASH: **REQUIRED**
* TOKEN: **REQUIRED**
* REDIS: **REQUIRED**
* OWNER: owner username
* QUOTA: quota in bytes
* EX: quota expire time
@ -96,7 +97,10 @@ you can configure all the following environment variables:
* COFFEE_TOKEN
* AFD_TOKEN
* AFD_USER_ID
* WORKERS: default 100
* AUTHORIZED_USER: users that could use this bot, user_id, separated with `,`
* REQUIRED_MEMBERSHIP: group or channel username, user must join this group to use the bot. Could be use with above `AUTHORIZED_USER`
### 4. run

View File

@ -9,6 +9,14 @@ __author__ = "Benny <benny.think@gmail.com>"
import os
# general settings
WORKERS: "int" = int(os.getenv("WORKERS", 500))
APP_ID: "int" = int(os.getenv("APP_ID", 111))
APP_HASH = os.getenv("APP_HASH", "111")
TOKEN = os.getenv("TOKEN", "3703WLI")
REDIS = os.getenv("REDIS")
# quota settings
QUOTA = os.getenv("QUOTA", 5 * 1024 * 1024 * 1024) # 5G
if os.uname().sysname == "Darwin":
QUOTA = 10 * 1024 * 1024 # 10M
@ -23,13 +31,9 @@ COFFEE_LINK = os.getenv("COFFEE_LINK", "https://www.buymeacoffee.com/bennythink"
COFFEE_TOKEN = os.getenv("COFFEE_TOKEN")
AFD_TOKEN = os.getenv("AFD_TOKEN")
AFD_USER_ID = os.getenv("AFD_USER_ID")
OWNER = os.getenv("OWNER", "BennyThink")
APP_ID: "int" = int(os.getenv("APP_ID", 111))
APP_HASH = os.getenv("APP_HASH", "111")
TOKEN = os.getenv("TOKEN", "3703WLI")
REDIS = os.getenv("REDIS")
# limitation settings
AUTHORIZED_USER: "str" = os.getenv("AUTHORIZED", "")
WORKERS: "int" = int(os.getenv("WORKERS", 300))
# membership requires: the format could be username/chat_id of channel or group
REQUIRED_MEMBERSHIP: "str" = os.getenv("REQUIRED_MEMBERSHIP", "")

View File

@ -9,7 +9,8 @@ __author__ = "Benny <benny.think@gmail.com>"
import time
from config import AFD_LINK, COFFEE_LINK, ENABLE_VIP, EX, MULTIPLY, USD2CNY
from config import (AFD_LINK, COFFEE_LINK, ENABLE_VIP, EX, MULTIPLY,
REQUIRED_MEMBERSHIP, USD2CNY)
from downloader import sizeof_fmt
from limit import QUOTA, VIP
@ -72,6 +73,7 @@ __I live in a place where I don't have access to Telegram Payments. So...__
vip_pay = "Processing your payments...If it's not responding after one minute, please contact @BennyThink."
private = "This bot is for private use"
membership_require = f"You need to join this group or channel to use this bot\n\nhttps://t.me/{REQUIRED_MEMBERSHIP}"
def remaining_quota_caption(self, chat_id):
if not ENABLE_VIP:

35
ytdl.py
View File

@ -7,6 +7,7 @@
__author__ = "Benny <benny.think@gmail.com>"
import contextlib
import logging
import os
import pathlib
@ -17,11 +18,12 @@ import typing
import ffmpeg
from apscheduler.schedulers.background import BackgroundScheduler
from pyrogram import Client, filters, types
from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from tgbot_ping import get_runtime
from config import (APP_HASH, APP_ID, AUTHORIZED_USER, ENABLE_VIP, OWNER,
TOKEN, WORKERS)
REQUIRED_MEMBERSHIP, TOKEN, WORKERS)
from constant import BotText
from downloader import convert_flac, sizeof_fmt, upload_hook, ytdl_download
from limit import Redis, verify_payment
@ -57,15 +59,33 @@ def get_metadata(video_path):
def private_use(func):
def wrapper(client: "Client", message: "types.Message"):
chat_id = message.chat.id
chat_id = getattr(message.from_user, "id", None)
# message type check
if message.chat.type != "private" and not message.text.lower().startswith("/ytdl"):
logging.warning("%s, it's annoying me...🙄️ ", message.text)
return
# authorized users check
if AUTHORIZED_USER:
users = [int(i) for i in AUTHORIZED_USER.split(",")]
else:
users = []
if users and chat_id not in users:
client.send_message(message.chat.id, bot_text.private)
if users and chat_id and chat_id not in users:
message.reply_text(bot_text.private, quote=True)
return
# membership check
if REQUIRED_MEMBERSHIP:
try:
app.get_chat_member(REQUIRED_MEMBERSHIP, chat_id)
logging.info("user %s check passed for group/channel %s.", chat_id, REQUIRED_MEMBERSHIP)
except UserNotParticipant:
logging.warning("user %s is not a member of group/channel %s", chat_id, REQUIRED_MEMBERSHIP)
message.reply_text(bot_text.membership_require, quote=True)
return
return func(client, message)
return wrapper
@ -85,6 +105,9 @@ def help_handler(client: "Client", message: "types.Message"):
chat_id = message.chat.id
client.send_chat_action(chat_id, "typing")
client.send_message(chat_id, bot_text.help, disable_web_page_preview=True)
channel_identifier = "https://t.me/joinchat/SGgzYMi59G4-aVCk"
chat = app.get_chat()
app.get_chat_member()
@app.on_message(filters.command(["ping"]))
@ -136,10 +159,6 @@ def download_handler(client: "Client", message: "types.Message"):
chat_id = message.chat.id
Redis().user_count(chat_id)
if message.chat.type != "private" and not message.text.lower().startswith("/ytdl"):
logging.warning("%s, it's annoying me...🙄️ ", message.text)
return
url = re.sub(r'/ytdl\s*', '', message.text)
logging.info("start %s", url)