from bs4 import BeautifulSoup
import requests
import telebot
bot = telebot.TeleBot('qwerty123')
tracks_title = []
tracks_url = []
page_number = 0
tracks_on_page = 3
keyboard = telebot.types.ReplyKeyboardMarkup()
keyboard.row('Топ за месяц', 'Топ за неделю', 'Топ за день')
keyboard.row('Как пользоваться?', 'Канал Автора')
@bot.message_handler(commands = ['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Привет, я бот для поиска музыки. Для того чтобы '
'найти музыку, просто напиши мне название трека, либо имя исполнителя, а я постараю'
'сь найти нужную тебе песню.', reply_markup = keyboard)
@bot.message_handler(content_types = ['text'])
def send_text(message):
if message.text == 'Как пользоваться?':
bot.send_message(message.chat.id, 'Для того чтобы найти музыку, просто напи'
'ши мне либо название трека, либо имя исполнителя, а я постараюсь найти нуж'
'ную тебе песню.')
elif message.text == 'Канал Автора':
chanal = telebot.types.InlineKeyboardMarkup()
chanal_button = telebot.types.InlineKeyboardButton(text='qwerty123', \
url='https://t.me/qwerty123')
chanal.row(chanal_button)
bot.send_message(message.chat.id, 'Подписывайся', reply_markup = chanal)
else:
search(message)
@bot.callback_query_handler(func=lambda c:True)
def inlin(c):
bot.delete_message(chat_id=c.message.chat.id, message_id=c.message.message_id)
if c.data == 'next_page': search_results_page('next_page', c.message)
elif c.data == 'back_page': search_results_page('back_page', c.message)
else:
download_track(c)
search_results_page('', c.message)
def search(message):
global tracks_title
webpage = site_search(message.text)
parsing(webpage)
if len(tracks_title) == 0:
bot.send_message(message.chat.id, 'По данному запросу ничего не найдено')
else: search_results_page('new_page', message)
def site_search(search_query):
if search_query == 'Топ за месяц': webpage = requests.get('https://z1.fm/?sort=month')
elif search_query == 'Топ за неделю': webpage = requests.get('https://z1.fm/?sort=week')
elif search_query == 'Топ за день': webpage = requests.get('https://z1.fm/?sort=views')
else:
data = requests.Session()
webpage = data.post('https://z1.fm/')
param = {'keywords': search_query}
webpage = requests.get('https://z1.fm/mp3/search', params=param, cookies=data.cookies)
return webpage.text
def parsing(webpage):
global tracks_url
global tracks_title
tracks_url.clear()
tracks_title.clear()
soup = BeautifulSoup(webpage, 'html.parser')
tracks = soup.find_all('span', class_='song-plus btn4 plus')
for span in tracks:
track_title = span.get('data-title')
track_id = span.get('data-sid')
track_url = 'https://z1.fm/download/' + track_id
tracks_title.append(track_title)
tracks_url.append(track_url)
if len(tracks_title) == 30: break
def search_results_page(request, message):
global tracks_title
global tracks_url
global page_number
global tracks_on_page
total_pages = int((len(tracks_title) + tracks_on_page - 1) / tracks_on_page)
search_results = telebot.types.InlineKeyboardMarkup()
if request == 'new_page': page_number = 1
elif request == 'next_page': page_number += 1
elif request == 'back_page': page_number -= 1
i = (page_number - 1) * tracks_on_page
b = 0
while i < tracks_on_page * page_number:
button = telebot.types.InlineKeyboardButton(text=tracks_title[i], callback_data=str(b))
search_results.add(button)
i += 1
b += 1
if i == len(tracks_title): break
back_page = telebot.types.InlineKeyboardButton(text = '<<Назад', callback_data='back_page')
next_page = telebot.types.InlineKeyboardButton(text = 'Далее>>', callback_data='next_page')
if page_number == 1 and total_pages > 1: search_results.row(next_page)
elif page_number == total_pages: search_results.row(back_page)
elif total_pages == 1: search_results.row()
else: search_results.row(back_page, next_page)
text = 'Страница ' + str(page_number) + ' из ' + str(total_pages) + '.\nВыберите нужную песню:'
bot.send_message(message.chat.id, text, reply_markup = search_results)
def download_track(c):
global tracks_url
global page_number
global tracks_on_page
track_id = int(c.data) + (page_number - 1) * tracks_on_page
bot.send_message(c.message.chat.id, tracks_url[track_id])
bot.polling()