Hseapp
This commit is contained in:
@@ -13,7 +13,7 @@ def api():
|
||||
all_users = mongo.users_collection.count_documents({})
|
||||
teachers = mongo.users_collection.count_documents({"is_teacher": True})
|
||||
text = f"Всего пользователей: {all_users}<br>" \
|
||||
f"Пользователей прошедших регистрацию: {mongo.users_collection.count_documents({'hse_id': {'$ne': None}})}<br>" \
|
||||
f"Пользователей прошедших регистрацию: {mongo.users_collection.count_documents({'email': {'$ne': None}})}<br>" \
|
||||
f"Студентов: {all_users - teachers}<br>" \
|
||||
f"Преподавателей: {teachers}<br>" \
|
||||
f"Отписались от уведомлений: {mongo.users_collection.count_documents({'notify_minutes': None})}<br>" \
|
||||
@@ -22,13 +22,13 @@ def api():
|
||||
f"<br>" \
|
||||
f"<br>" \
|
||||
f"Пользователей из Москвы: {mongo.users_collection.count_documents({'campus': 'Москва'}) + mongo.users_collection.count_documents({'campus': {'$exists': False}})}<br>" \
|
||||
f"Пользователей из Москвы (регистрация): {mongo.users_collection.count_documents({'campus': 'Москва', 'hse_id': {'$ne': None}}) + mongo.users_collection.count_documents({'campus': {'$exists': False}, 'hse_id': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Москвы (регистрация): {mongo.users_collection.count_documents({'campus': 'Москва', 'email': {'$ne': None}}) + mongo.users_collection.count_documents({'campus': {'$exists': False}, 'email': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Перми: {mongo.users_collection.count_documents({'campus': 'Пермь'})}<br>" \
|
||||
f"Пользователей из Перми (регистрация): {mongo.users_collection.count_documents({'campus': 'Пермь', 'hse_id': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Перми (регистрация): {mongo.users_collection.count_documents({'campus': 'Пермь', 'email': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Нижнего Новгорода: {mongo.users_collection.count_documents({'campus': 'Нижний Новгород'})}<br>" \
|
||||
f"Пользователей из Нижнего Новгорода (регистрация): {mongo.users_collection.count_documents({'campus': 'Нижний Новгород', 'hse_id': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Нижнего Новгорода (регистрация): {mongo.users_collection.count_documents({'campus': 'Нижний Новгород', 'email': {'$ne': None}})}<br>" \
|
||||
f"Пользователей из Санкт-Петербурга: {mongo.users_collection.count_documents({'campus': 'Санкт-Петербург'})}<br>" \
|
||||
f"Пользователей из Санкт-Петербурга (регистрация): {mongo.users_collection.count_documents({'campus': 'Санкт-Петербург', 'hse_id': {'$ne': None}})}<br>"
|
||||
f"Пользователей из Санкт-Петербурга (регистрация): {mongo.users_collection.count_documents({'campus': 'Санкт-Петербург', 'email': {'$ne': None}})}<br>"
|
||||
return text
|
||||
|
||||
@app.route('/alice', methods=['POST'])
|
||||
|
||||
@@ -16,5 +16,5 @@ def on_start(message: Message):
|
||||
|
||||
@bot.message_handler()
|
||||
def do_action(message: Message):
|
||||
from helpers.answer import answer
|
||||
answer.process(message)
|
||||
from helpers.answer import Answer
|
||||
Answer(message).process()
|
||||
|
||||
@@ -2,16 +2,14 @@ import datetime
|
||||
import logging
|
||||
from time import sleep
|
||||
|
||||
from helpers import now, User
|
||||
from helpers.models import UserSchema
|
||||
from helpers import now, campus_timdelta
|
||||
from helpers.mongo import mongo
|
||||
from helpers.ruz import ruz
|
||||
|
||||
|
||||
def fetch_schedule_for_user(user: User):
|
||||
def fetch_schedule_for_user(user: dict):
|
||||
today = now(user)
|
||||
next_day = today + datetime.timedelta(days=7)
|
||||
schedule = ruz.get_schedule(user, today, next_day)
|
||||
schedule = ruz.get_schedule(user, today)
|
||||
if schedule is None:
|
||||
return False
|
||||
saved_ids = []
|
||||
@@ -23,35 +21,35 @@ def fetch_schedule_for_user(user: User):
|
||||
"discipline": element['discipline'],
|
||||
"auditorium": element['auditorium'],
|
||||
"link": element['url1'],
|
||||
"hse_user_id": user.hse_id,
|
||||
"user_email": user['email'],
|
||||
"begin": datetime.datetime(
|
||||
year=int(year),
|
||||
month=int(month),
|
||||
day=int(day),
|
||||
hour=int(begin_hour),
|
||||
minute=int(begin_minute),
|
||||
)
|
||||
) + datetime.timedelta(hours=campus_timdelta[user.get('campus', 'Москва')])
|
||||
})
|
||||
if lesson is None:
|
||||
result = mongo.lessons_collection.insert_one({
|
||||
"discipline": element['discipline'],
|
||||
"auditorium": element['auditorium'],
|
||||
"link": element['url1'],
|
||||
"hse_user_id": user.hse_id,
|
||||
"user_email": user['email'],
|
||||
"begin": datetime.datetime(
|
||||
year=int(year),
|
||||
month=int(month),
|
||||
day=int(day),
|
||||
hour=int(begin_hour),
|
||||
minute=int(begin_minute),
|
||||
),
|
||||
) + datetime.timedelta(hours=campus_timdelta[user.get('campus', 'Москва')]),
|
||||
"end": datetime.datetime(
|
||||
year=int(year),
|
||||
month=int(month),
|
||||
day=int(day),
|
||||
hour=int(end_hour),
|
||||
minute=int(end_minute),
|
||||
),
|
||||
) + datetime.timedelta(hours=campus_timdelta[user.get('campus', 'Москва')]),
|
||||
"building": element['building'],
|
||||
"lecturer": element['lecturer'],
|
||||
"notified": False,
|
||||
@@ -60,13 +58,17 @@ def fetch_schedule_for_user(user: User):
|
||||
saved_ids.append(result.inserted_id)
|
||||
else:
|
||||
saved_ids.append(lesson['_id'])
|
||||
mongo.lessons_collection.delete_many({"hse_user_id": user.hse_id, "_id": {"$nin": saved_ids}})
|
||||
mongo.lessons_collection.delete_many({"user_email": user['email'], "_id": {"$nin": saved_ids}})
|
||||
mongo.users_collection.update_one({"_id": user['_id']}, {"$set": {'last_schedule_fetch': datetime.datetime.now()}})
|
||||
return True
|
||||
|
||||
|
||||
def process():
|
||||
for user in mongo.users_collection.find({"hse_id": {"$ne": None}}):
|
||||
fetch_schedule_for_user(UserSchema().load(user))
|
||||
for user in mongo.users_collection.find({"email": {"$exists": True, "$ne": None}}).sort([
|
||||
("last_schedule_fetch", 1)
|
||||
]):
|
||||
fetch_schedule_for_user(user)
|
||||
sleep(5)
|
||||
|
||||
|
||||
def delete_old():
|
||||
@@ -77,9 +79,12 @@ def fetch():
|
||||
while True:
|
||||
logging.info("fetch start")
|
||||
begin = datetime.datetime.now()
|
||||
if begin.hour > 22 or begin.hour < 7:
|
||||
logging.info("Too late, sleeping")
|
||||
sleep(30 * 60)
|
||||
continue
|
||||
process()
|
||||
end = datetime.datetime.now()
|
||||
logging.info('fetch finished')
|
||||
logging.info("time elapsed %s", (end - begin).total_seconds())
|
||||
delete_old()
|
||||
sleep(60 * 5)
|
||||
|
||||
@@ -6,16 +6,15 @@ from telebot.apihelper import ApiTelegramException
|
||||
|
||||
from daemons.bot import bot
|
||||
from helpers import now
|
||||
from helpers.models import UserSchema
|
||||
from helpers.mongo import mongo
|
||||
from helpers.ruz import ruz
|
||||
|
||||
|
||||
def process():
|
||||
for user in mongo.users_collection.find({"notify_minutes": {"$ne": None}, "hse_id": {"$ne": None}}):
|
||||
time_now = now(UserSchema().load(user))
|
||||
for user in mongo.users_collection.find({"notify_minutes": {"$ne": None}, "email": {"$ne": None}}):
|
||||
time_now = now(user)
|
||||
for lesson in mongo.lessons_collection.find({
|
||||
"hse_user_id": user["hse_id"],
|
||||
"user_email": user["email"],
|
||||
"begin": {"$lte": time_now + datetime.timedelta(minutes=user["notify_minutes"])},
|
||||
"notified": False
|
||||
}):
|
||||
@@ -36,34 +35,33 @@ def process():
|
||||
mongo.lessons_collection.update_one({"_id": lesson['_id']}, {"$set": {"notified": True}})
|
||||
time_now = datetime.datetime.now()
|
||||
for user in mongo.users_collection.find({"next_daily_notify_time": {"$lte": time_now}}):
|
||||
user_model = UserSchema().load(user)
|
||||
deny_weekday = 6 if user_model.daily_notify_today else 5
|
||||
deny_weekday = 6 if user.get('daily_notify_today', True) else 5
|
||||
if time_now.weekday() != deny_weekday:
|
||||
if user_model.daily_notify_today:
|
||||
lessons = mongo.get_today_lessons(user_model)
|
||||
if user.get('daily_notify_today', True):
|
||||
lessons = mongo.get_today_lessons(user)
|
||||
else:
|
||||
lessons = mongo.get_tomorrow_lessons(user_model)
|
||||
lessons = mongo.get_tomorrow_lessons(user)
|
||||
if len(lessons) == 0:
|
||||
text = f"{'Сегодня' if user_model.daily_notify_today else 'Завтра'} у тебя нет пар, отдыхай."
|
||||
text = f"{'Сегодня' if user.get('daily_notify_today', True) else 'Завтра'} у тебя нет пар, отдыхай."
|
||||
else:
|
||||
text = ruz.schedule_builder(lessons)
|
||||
try:
|
||||
bot.send_message(
|
||||
user["chat_id"],
|
||||
f"Уведомляю о занятиях! Твое расписание на {'сегодня' if user_model.daily_notify_today else 'завтра'}:\n" + text,
|
||||
f"Уведомляю о занятиях! Твое расписание на {'сегодня' if user.get('daily_notify_today', True) else 'завтра'}:\n" + text,
|
||||
parse_mode='Markdown'
|
||||
)
|
||||
except:
|
||||
pass
|
||||
mongo.users_collection.update_one(
|
||||
{"chat_id": user["chat_id"]},
|
||||
{"$set": {"next_daily_notify_time": user_model.next_daily_notify_time + datetime.timedelta(days=1)}}
|
||||
{"$set": {"next_daily_notify_time": user['next_daily_notify_time'] + datetime.timedelta(days=1)}}
|
||||
)
|
||||
|
||||
for user in mongo.users_collection.find({"first_lesson_notify": {"$exists": True}, "first_lesson_notify": {"$ne": None}, "hse_id": {"$ne": None}}):
|
||||
time_now = now(UserSchema().load(user))
|
||||
for user in mongo.users_collection.find({"first_lesson_notify": {"$exists": True, "$ne": None}, "email": {"$ne": None}}):
|
||||
time_now = now(user)
|
||||
for lesson in mongo.lessons_collection.find({
|
||||
"hse_user_id": user["hse_id"],
|
||||
"user_email": user["email"],
|
||||
"begin": {"$lte": time_now + datetime.timedelta(minutes=user["first_lesson_notify"])},
|
||||
"notified_today": {"$ne": True}
|
||||
}).sort("begin"):
|
||||
@@ -92,7 +90,7 @@ def process():
|
||||
except ApiTelegramException:
|
||||
pass
|
||||
start_of_day = datetime.datetime(year=time_now.year, month=time_now.month, day=time_now.day)
|
||||
mongo.lessons_collection.update_many({"begin": {"$gte": start_of_day, "$lt": (start_of_day + datetime.timedelta(days=1))}, "hse_user_id": user["hse_id"]}, {"$set": {"notified_today": True}})
|
||||
mongo.lessons_collection.update_many({"begin": {"$gte": start_of_day, "$lt": (start_of_day + datetime.timedelta(days=1))}, "user_email": user["email"]}, {"$set": {"notified_today": True}})
|
||||
break
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user