-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuildPlayer.py
More file actions
173 lines (110 loc) · 5.66 KB
/
GuildPlayer.py
File metadata and controls
173 lines (110 loc) · 5.66 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# this file doesnt use typing because i couldnt add the typing library
# and make it run on my linux machine so if you want to do it i would
# love to see it in the file
from Constants import VideoStatus
from YTMGraper import Graper
import threading
import random
# Structure for refrence Only
class Audio:
def __init__(self):
self.ID = None
self.Name = None
self.Buffer = None
self.Format = None
self.Status = VideoStatus.NOT_INSTALLED
self.m3u8Playlist = None
class Player():
def __init__(self):
self.GuildID = None
self.Channal = None # this is used to keep track of the data of each guild
self.AudioData = {
# "<YoutubeID>" : Aduio()
}
self.AudioGraper = Graper()
self.AudioGraper.AudioData = self.AudioData
self.AudioPlaying = None
self.Queue = []
self.History = []
self.IsPlaying = False
self.QueueLoopBack = False # Read only (if you want to change it use the function "SetQueueLoopBack")
self.AudioLoopBack = False # read only (if you want to change it use the function "SetAudioLoopBack")
# API Functions
self.PlayBufferFunction = None
self.PlayMEU8Function = None
self.StopFunction = None # not implemented and for the discord api it wont be
self.SeekFunction = None # still need to be implemented
def ClearQueue(self):
self.Queue = []
def ShuffleQueue(self):
random.shuffle(self.Queue)
def AddToQueue(self, ID, Title=None, Index=None):
_id = self.AudioGraper.extract_youtube_video_id(ID)
try:
self.AudioData[_id]
except:
self.AudioGraper.Preparing(_id, Title=Title)
if Index == None:
self.Queue.append(self.AudioData[_id])
else:
self.Queue.insert(Index, self.AudioData[_id])
def AddPlayList(self, YoutubePLID):
_ids_titles = self.AudioGraper.PlaylistHerfsRequest(YoutubePLID)
for _id, _title in _ids_titles:
self.AddToQueue(_id, Title=_title)
def SetQueueLoopBack(self, state):
if state == True:
self.QueueLoopBack = state
elif not state == False:
self.QueueLoopBack = state
else:
self.QueueLoopBack = not self.QueueLoopBack
if self.QueueLoopBack and self.AudioPlaying and (not self.Queue or (self.Queue and self.Queue[0] != self.AudioPlaying)):
self.Queue.append(self.AudioPlaying)
def SetAudioLoopBack(self, state):
if state == True:
self.AudioLoopBack = state
elif not state == False:
self.AudioLoopBack = state
else:
self.AudioLoopBack = not self.AudioLoopBack
if self.AudioLoopBack and self.AudioPlaying and (not self.Queue or (self.Queue and self.Queue[0] != self.AudioPlaying)):
self.Queue.insert(0, self.AudioPlaying)
if not self.AudioLoopBack and self.Queue and self.Queue[0] == self.AudioPlaying:
self.Queue.pop(0)
# this update function should be ran in a loop
async def Update(self):
if self.IsPlaying == False and self.Channal:
if not self.AudioPlaying and self.Queue:
self.AudioPlaying = self.Queue[0]
self.Queue.pop(0)
if not self.History or self.History[-1] != self.AudioPlaying: # make sure that the last element is not already there this helps prevent looped audio showing on history more than once
self.History.append(self.AudioPlaying) # What ever got played add to History
if self.QueueLoopBack:
self.Queue.append(self.History[-1]) # simply just add it at the end
if self.AudioLoopBack:
self.Queue.insert(0, self.History[-1]) # simply just insert it back at the start
if self.AudioPlaying:
try:
if self.AudioPlaying.Status == VideoStatus.PREPARING:
return
# this if statment will be used if you have the video in buffer
if self.AudioPlaying.Status == VideoStatus.READY:
if self.Channal and not self.IsPlaying:
self.IsPlaying = True
await self.PlayBufferFunction(self.AudioPlaying.Buffer, self.Channal, self.GuildID)
if self.AudioPlaying.Status == VideoStatus.HAS_MEU8_URL:
if self.Channal and not self.IsPlaying:
self.IsPlaying = True
await self.PlayMEU8Function(self.AudioPlaying.m3u8Playlist, self.Channal, self.GuildID)
if self.AudioPlaying.Status == VideoStatus.NOT_INSTALLED:
#self.AudioPlaying.Status = "Preparing" # change the varable just in case so we dont run through errors
threading.Thread(target=self.AudioGraper.Grap_m3u8_And_Title, args=(self.AudioPlaying.ID,)).start()
if self.AudioPlaying.Status == VideoStatus.ERROR:
self.AudioPlaying.Buffer = None
self.AudioPlaying.Status = VideoStatus.NOT_INSTALLED
self.AudioData = []
self.IsPlaying = False
self.AudioPlaying = None
except:
pass