-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongsQueue.py
117 lines (93 loc) · 3.31 KB
/
SongsQueue.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
import logging
import os
from json import JSONDecodeError
import jsonpickle
import pafy
from pytube import Search
from pytube.exceptions import RegexMatchError
from Song import Song
class SongsQueue:
def __init__(self):
self.songs = []
try:
f = open(os.getcwd() + '/queue.json')
self.songs = jsonpickle.decode(f.read()).songs
logging.debug("successfully replicated queue from json")
except JSONDecodeError:
logging.warning("queue.json JSONDecodeError possibly empty file")
except FileNotFoundError:
logging.warning("No such file or directory: 'queue.json'")
except PermissionError:
logging.warning("Permission denied: 'queue.json'")
def __str__(self):
return str(self.songs)
def is_empty(self):
return self.songs == []
def name_add(self, name):
s = Search(name)
self.add(s.results[0].video_id)
def add(self, videoId):
try:
video = pafy.new("https://www.youtube.com/watch?v=" + videoId)
# try:
song = Song(video.videoid, video.title, video.author, video.getbestthumb(), video.length)
self.songs.append(song)
# except TypeError as e:
# logging.critical(str(e) + "\nRETRYING...")
# self.add(videoId)
#
# except PytubeError as e:
# logging.critical(str(e) + "\nRETRYING...")
# self.add(videoId)
except RegexMatchError:
pass
def getFirstId(self):
return self.peek(0).id
def remove_by_index(self, index):
if index > 0 or index <= len(self.songs):
self.songs.pop(index)
def remove_by_id(self, videoId):
for song in self.songs:
if song.id == videoId:
self.songs.remove(song)
break
def name_remove(self, name):
for song in self.songs:
if song.title == name:
self.songs.remove(song)
break
def get_by_id(self, videoId):
for song in self.songs:
if song.id == videoId:
return song
def get_by_name(self, name):
for song in self.songs:
if song.title == name:
return song
def size(self):
return len(self.songs)
def peek(self, index):
try:
return self.songs[index]
except IndexError:
return None
def move(self, start, end):
if start < 0 or start >= len(self.songs) or end < 0 or end >= len(self.songs):
raise ValueError
self.songs.insert(end, self.songs.pop(start))
def move_by_id(self, videoId, position):
if position > len(self.songs) or position < 0:
raise ValueError
song = self.get_by_id(videoId)
curPos = self.songs.index(song)
if curPos == position:
raise KeyError
if song:
self.songs.remove(song)
self.songs.insert(position, song)
def empty(self):
self.songs = []
def restore(self, videoId, position):
video = pafy.new("https://www.youtube.com/watch?v=" + videoId)
song = Song(video.videoid, video.title, video.author, video.getbestthumb(), video.length)
self.songs.insert(position, song)