This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (53 loc) · 2.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
import json
from typing import Tuple
import telebot
from dbhelper import DBHelper
from utils import validate_playist_url
data = json.loads(open('bot_data.json', 'r').read())
bot_token = data['token']
bot = telebot.TeleBot(bot_token)
db = DBHelper()
db.setup()
def handle_playlist_url_from_user(url: str = "", user_id: int = 1) -> str:
success, error_message = validate_playist_url(url)
if success:
if db.check_if_user_could_add_playlist_url(user_id):
db.add_playlist_url_from_user(url, user_id)
return "Готово! Уверен, кому-то понравится твоя музыка :)"
else:
return "Нельзя добавлять больше одного плейлиста в день!"
else:
return error_message
def handle_request_for_playlist_url_from_user(user_id: int) -> Tuple[bool, str]:
if db.check_if_user_could_get_playlist_url(user_id):
playlist_url = db.get_random_playlist_url_for_user(user_id)
if playlist_url:
return True, playlist_url
else:
return False, "У меня нет для тебя плейлиста, но попробуй вернуться позже!"
return False, "Ты можешь запросить только один плейлист в день!"
@bot.message_handler(commands=['start', 'help'])
def handle_start_command(message):
bot.reply_to(message, """
Привет, я помогаю находить новую музыку!
Пожалуйста, сделай публичный плейлист в Spotify песен на 5 и дай мне на него ссылку
(она выглядит вот так: https://open.spotify.com/playlist/14yxhrdNVZiJj6xHuik4gq?si=c2029f818b95425a),
а я буду делиться с ней другими людьми. Если же ты сам хочешь плейлист от кого-то, то введи команду “/get” 🙂
""")
@bot.message_handler(commands=['get'])
def handle_get_command(message):
user_id = message.from_user.id
success, result = handle_request_for_playlist_url_from_user(user_id)
if success:
message = bot.reply_to(message, "Вот тебе небольшой плейлист на сегодня: %s" % result)
db.add_info_about_requested_url(result, user_id, message.id)
else:
bot.reply_to(message, result)
@bot.message_handler(func=lambda m: True)
def handle_usual_messages(message):
user_id = message.from_user.id
text = message.text
answer_text = handle_playlist_url_from_user(text, user_id)
bot.reply_to(message, answer_text)
bot.infinity_polling()