-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicClasses.py
130 lines (99 loc) · 3.64 KB
/
MusicClasses.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import discord
import youtube_dl
class Song(dict):
"""
This is a class for creating a song object from an url or a song name.
Attributes:
url (str): Mp3 file url of the song.
title (str): Title of the song.
thumbnail (str): Thumbnail url of the song.
channel (str): Channel name of the song uploader.
duration_raw (int): Duration of the song by seconds.
duration_formatted (str): Duration of the song as '<minutes>m <seconds>s'.
upload_date_raw (int): Upload date of the song as 'yyyymmdd'.
upload_date_formatted (str): Upload date of the song as 'dd/mm/yyyy'.
views (int): Number of views of the song.
requested_by (discord.Member): User who requested the song.
"""
YDL_OPTIONS = {
'format': 'bestaudio/best',
'noplaylist': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
def __init__(self, url:str, author:discord.Member):
super().__init__()
self.download_info(url, author)
def download_info(self, url, author:discord.Member):
with youtube_dl.YoutubeDL(self.YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
if url.startswith("ytsearch:"):
info = ydl.extract_info(info["entries"][0]["webpage_url"], download=False)
info.pop("formats")
info.pop("thumbnails")
self.update(info)
self["requested_by"] = author
print(self)
@property
def url(self):
return self.get("url")
@property
def title(self):
return self.get("title")
@property
def thumbnail(self):
return self.get("thumbnail")
@property
def channel(self):
return self.get("channel")
@property
def duration_raw(self):
return self.get("duration")
@property
def duration_formatted(self):
minutes, seconds = self.duration_raw // 60, self.duration_raw % 60
return f"{minutes}m {seconds}s"
@property
def upload_date_raw(self):
return self.get("upload_date")
@property
def upload_date_formatted(self):
date = self.upload_date_raw
d, m, y = date[6:8], date[4:6], date[0:4]
return f"{d}/{m}/{y}"
@property
def views(self):
return self.get("view_count")
@property
def requested_by(self):
return self.get("requested_by")
class Queue(list):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._currentSong = None
def next_song(self):
next_one = self.pop(0)
self._currentSong = next_one
return self._currentSong
def clear(self):
super().clear()
self._currentSong = None
def get_embed(self, song_index):
if song_index == 0: #default value from songinfo command in musicCog
song = self._currentSong
else:
song = self[song_index - 1]
embed = discord.Embed()
embed.color = discord.Color.teal()
embed.title = "Song Info"
embed.set_thumbnail(url=song.thumbnail)
embed.add_field(name="Title", value = song.title)
embed.add_field(name="Channel", value = song.channel)
embed.add_field(name="Duration", value = song.duration_formatted)
embed.add_field(name="Upload Date", value = song.upload_date_formatted)
embed.add_field(name="Views", value = song.views)
embed.add_field(name="Requested By", value = song.requested_by.mention)
return embed