Create appy.py
#19
by JaMMiK - opened
appy.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import yt_dlp
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from telebot import TeleBot
|
| 5 |
+
|
| 6 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 7 |
+
bot = TeleBot(8212986545:AAE0vvo0PU30Ak6vMxgkQ1SQtZVMg7olO_g)
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
@bot.message_handler(commands=['start'])
|
| 12 |
+
def start(msg):
|
| 13 |
+
bot.reply_to(msg, "🎵 Salom! Menga qo'shiq nomini yuboring, men MP3 qilib beraman.")
|
| 14 |
+
|
| 15 |
+
@bot.message_handler(func=lambda message: True)
|
| 16 |
+
def get_song(message):
|
| 17 |
+
query = message.text
|
| 18 |
+
bot.reply_to(message, "🔍 Qidirilmoqda...")
|
| 19 |
+
|
| 20 |
+
ydl_opts = {
|
| 21 |
+
'format': 'bestaudio/best',
|
| 22 |
+
'noplaylist': True,
|
| 23 |
+
'outtmpl': '%(title)s.%(ext)s',
|
| 24 |
+
'postprocessors': [{
|
| 25 |
+
'key': 'FFmpegExtractAudio',
|
| 26 |
+
'preferredcodec': 'mp3',
|
| 27 |
+
'preferredquality': '192',
|
| 28 |
+
}],
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 32 |
+
info = ydl.extract_info(f"ytsearch:{query}", download=True)
|
| 33 |
+
filename = ydl.prepare_filename(info['entries'][0]).replace(".webm", ".mp3")
|
| 34 |
+
|
| 35 |
+
with open(filename, "rb") as audio:
|
| 36 |
+
bot.send_audio(message.chat.id, audio)
|
| 37 |
+
os.remove(filename)
|
| 38 |
+
|
| 39 |
+
bot.infinity_polling()
|