mirror of
https://github.com/tgbot-collection/ytdlbot.git
synced 2025-02-24 23:34:44 +08:00
add broadcast
This commit is contained in:
parent
839bbfd975
commit
647d555f53
2 changed files with 58 additions and 1 deletions
56
broadcast.py
Normal file
56
broadcast.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
#!/usr/local/bin/python3
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
# ytdlbot - broadcast.py
|
||||||
|
# 8/25/21 16:11
|
||||||
|
#
|
||||||
|
|
||||||
|
__author__ = "Benny <benny.think@gmail.com>"
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import contextlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from limit import Redis
|
||||||
|
from ytdl import create_app
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='Broadcast to users')
|
||||||
|
parser.add_argument('-m', help='message', required=True)
|
||||||
|
parser.add_argument('-p', help='picture', default=None)
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
r = Redis().r
|
||||||
|
keys = r.keys("*")
|
||||||
|
user_ids = set()
|
||||||
|
for key in keys:
|
||||||
|
if key.isdigit():
|
||||||
|
user_ids.add(key)
|
||||||
|
|
||||||
|
metrics = r.hgetall("metrics")
|
||||||
|
|
||||||
|
for key in metrics:
|
||||||
|
if key.isdigit():
|
||||||
|
user_ids.add(key)
|
||||||
|
|
||||||
|
if "YES" != input("Are you sure you want to send broadcast message to %s users?\n>" % len(user_ids)):
|
||||||
|
logging.info("Abort")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile() as tmp:
|
||||||
|
with create_app(tmp.name, 1) as app:
|
||||||
|
for user_id in tqdm(user_ids):
|
||||||
|
time.sleep(random.random())
|
||||||
|
if args.p:
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
app.send_photo(user_id, args.p, caption=args.m)
|
||||||
|
else:
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
app.send_message(user_id, args.m)
|
|
@ -10,4 +10,5 @@ tgbot-ping
|
||||||
fakeredis
|
fakeredis
|
||||||
filetype
|
filetype
|
||||||
redis
|
redis
|
||||||
requests
|
requests
|
||||||
|
tqdm
|
Loading…
Reference in a new issue