grpc
This commit is contained in:
33
daemons/api.py
Normal file
33
daemons/api.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from utils.mongo import mongo
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/stats/json', methods=['GET'])
|
||||
def stats_json():
|
||||
replies = 0
|
||||
for doc in mongo.counter_collection.find({}):
|
||||
replies += doc['count']
|
||||
return {
|
||||
"Всего чатов": mongo.chats_collection.count_documents({"chat_id": {"$lt": 0}}),
|
||||
"Отвечено": replies
|
||||
}
|
||||
|
||||
|
||||
@app.route('/rating', methods=['GET'])
|
||||
def main():
|
||||
rating = defaultdict(int)
|
||||
for doc in mongo.counter_collection.find({"username": {"$ne": None}}):
|
||||
rating[doc["username"]] += doc['count']
|
||||
rating_list = []
|
||||
for user, count in rating.items():
|
||||
rating_list.append({'username': user, 'count': count})
|
||||
page = '<html><body><h1>Рейтинг затроленных</h1><br><ul>'
|
||||
for item in sorted(rating_list, key=lambda x: x['count'], reverse=True):
|
||||
page += f'<li><a target="_blank" href="https://t.me/{item["username"]}">{item["username"]}</a> - {item["count"]}</li>'
|
||||
page += '</ul></body></html>'
|
||||
return page
|
||||
@@ -1,3 +1,18 @@
|
||||
class BaseDaemon:
|
||||
import os
|
||||
import grpc
|
||||
from queues import tasks_pb2_grpc
|
||||
|
||||
|
||||
stage = os.getenv("STAGE", 'local')
|
||||
if stage == 'local':
|
||||
QUEUES_URL = 'localhost:50051'
|
||||
else:
|
||||
QUEUES_URL = 'queues-grpc:50051'
|
||||
|
||||
|
||||
class Daemon:
|
||||
def __init__(self):
|
||||
self.channel = grpc.insecure_channel(QUEUES_URL)
|
||||
self.stub = tasks_pb2_grpc.TasksStub(channel=self.channel)
|
||||
def execute(self):
|
||||
raise NotImplementedError
|
||||
raise NotImplemented
|
||||
|
||||
25
daemons/updater.py
Normal file
25
daemons/updater.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import datetime
|
||||
import random
|
||||
from time import sleep
|
||||
|
||||
from utils.mongo import mongo
|
||||
|
||||
|
||||
def update():
|
||||
from main import bot, client
|
||||
while True:
|
||||
if client.get_config('updater')['enabled']:
|
||||
fltr = {"last_time_updated": {"$lte": datetime.datetime.now() - datetime.timedelta(seconds=client.get_config('updater')['seconds_delay'])}}
|
||||
if not client.get_config('updater')['send_to_single']:
|
||||
fltr["chat_id"] = {"$lt": 0}
|
||||
for chat in mongo.chats_collection.find(fltr):
|
||||
users = list(mongo.counter_collection.find({"chat_id": chat['chat_id'], 'username': {"$ne": None}}))
|
||||
if not users:
|
||||
continue
|
||||
user = random.choice(users)
|
||||
try:
|
||||
bot.send_message(chat['chat_id'], f"В этом чате давно не было активности, а как говорится, кто молчит - тот трансвестит, а трансвестит сегодня - @{user['username']}")
|
||||
except:
|
||||
pass
|
||||
mongo.chats_collection.update_one({"chat_id": chat['chat_id']}, {"$set": {"last_time_updated": datetime.datetime.now()}})
|
||||
sleep(client.get_config('updater')['seconds_delay'])
|
||||
@@ -6,9 +6,9 @@ from utils import queues
|
||||
from telebot.types import Message
|
||||
import json
|
||||
|
||||
from mongo import mongo
|
||||
from sprint_platform import PlatformClient
|
||||
from storage import set_values, get_chat_info
|
||||
from utils.mongo import mongo
|
||||
from utils.sprint_platform import PlatformClient
|
||||
from utils.storage import set_values, get_chat_info
|
||||
|
||||
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
||||
stage = os.getenv("STAGE", 'local')
|
||||
@@ -49,6 +49,7 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
||||
|
||||
def reply(self, text: str, chat_id: int, message_id: int):
|
||||
queues.set_task(
|
||||
self.stub,
|
||||
'botalka_mailbox',
|
||||
{
|
||||
'project': 'pizda-bot',
|
||||
@@ -64,6 +65,7 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
||||
|
||||
def send(self, text: str, chat_id: int):
|
||||
queues.set_task(
|
||||
self.stub,
|
||||
'botalka_mailbox',
|
||||
{
|
||||
'project': 'pizda-bot',
|
||||
|
||||
Reference in New Issue
Block a user