forked from Mr-Dark-Prince/MissRose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissRoseSong_Bot.py
113 lines (101 loc) · 3.76 KB
/
MissRoseSong_Bot.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# © Mr_D_P
from pyrogram import Client, filters
from pyrogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup
)
import youtube_dl
from youtube_search import YoutubeSearch
import requests
import os
from config import Config
bot = Client(
'MissRose',
bot_token = Config.BOT_TOKEN,
api_id = Config.API_ID,
api_hash = Config.API_HASH
)
## Extra Fns -------------------------------
# Convert hh:mm:ss to seconds
def time_to_seconds(time):
stringt = str(time)
return sum(int(x) * 60 ** i for i, x in enumerate(reversed(stringt.split(':'))))
## Commands --------------------------------
@bot.on_message(filters.command(['start']))
def start(client, message):
darkprince = f'👋 Hello @{message.from_user.username}\n\n [😌🍀🤚](https://telegra.ph/file/86cc2e654b1157f12b94f.jpg)\n I\'m Rose, I can upload songs from YouTube. Type /a song name:'
message.reply_text(
text=darkprince,
quote=False,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('Owner🎸', url='https://t.me/mr_dark_prince'),
InlineKeyboardButton('Source💿', url='https://github.com/Mr-Dark-Prince/MissRose')
]
]
)
)
@bot.on_message(filters.command(['a']))
def a(client, message):
query = ''
for i in message.command[1:]:
query += ' ' + str(i)
print(query)
m = message.reply('🔎 Searching the song...')
ydl_opts = {"format": "bestaudio[ext=m4a]"}
try:
results = []
count = 0
while len(results) == 0 and count < 6:
if count>0:
time.sleep(1)
results = YoutubeSearch(query, max_results=1).to_dict()
count += 1
# results = YoutubeSearch(query, max_results=1).to_dict()
try:
link = f"https://youtube.com{results[0]['url_suffix']}"
# print(results)
title = results[0]["title"]
thumbnail = results[0]["thumbnails"][0]
duration = results[0]["duration"]
## UNCOMMENT THIS IF YOU WANT A LIMIT ON DURATION. CHANGE 1800 TO YOUR OWN PREFFERED DURATION AND EDIT THE MESSAGE (30 minutes cap) LIMIT IN SECONDS
# if time_to_seconds(duration) >= 1800: # duration limit
# m.edit("Exceeded 30mins cap")
# return
views = results[0]["views"]
thumb_name = f'thumb{message.message_id}.jpg'
thumb = requests.get(thumbnail, allow_redirects=True)
open(thumb_name, 'wb').write(thumb.content)
except Exception as e:
print(e)
m.edit('Found nothing. Try changing the spelling a little.')
return
except Exception as e:
m.edit(
"✖️ Found Nothing. Sorry.\n\nTry another keywork or maybe spell it properly."
)
print(str(e))
return
m.edit("⏬ Downloading.")
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
audio_file = ydl.prepare_filename(info_dict)
ydl.process_info(info_dict)
rep = f'🎧 **Title**: [{title[:35]}]({link})\n⏳ **Duration**: `{duration}`\n👁🗨 **Views**: `{views}`'
secmul, dur, dur_arr = 1, 0, duration.split(':')
for i in range(len(dur_arr)-1, -1, -1):
dur += (int(dur_arr[i]) * secmul)
secmul *= 60
message.reply_audio(audio_file, caption=rep, parse_mode='md',quote=False, title=title, duration=dur, thumb=thumb_name)
m.delete()
except Exception as e:
m.edit('❌ Error')
print(e)
try:
os.remove(audio_file)
os.remove(thumb_name)
except Exception as e:
print(e)
bot.run()