def addSong(self, user, url): print("URL: {}".format(url)) try: song = Song.objects.get(url=url) print("Only queue!") self.queueSong(user, str(song.id)) return True except ObjectDoesNotExist: pass except Exception as e: print(e) return False try: print("Adding song!") title = self.getTitle(url) except Exception as e: Logger.instance().Log("Invalid url: {} - {}".format(url, e)) return False song = Song() song.active = True song.url = url song.title = title song.user = user song.save() self.queueSong(user, str(song.id)) return True
def nextSong(self, force=False): if force is False: self.currentSong.skipCounter = self.currentSong.skipCounter + 1 self.currentSong.save() queueItemsCount = QueueItem.objects.count() if queueItemsCount > 0: index = random.randint(0, queueItemsCount - 1) print("Random {} from {}".format(index, queueItemsCount)) item = QueueItem.objects.all()[index] song = item.song item.delete() print("Item: {} - {}".format(item.user, item.song)) self.playSong(song) return if ShuffleItem.objects.count() == 0: self.shufflePlaylist() if ShuffleItem.objects.count() == 0: Logger.instance().Log("No songs, add something guys...") return shuffleItem = ShuffleItem.objects.first() song = shuffleItem.song shuffleItem.delete() print("Playing song: {}".format(song.title)) self.playSong(song) return
def __call__(self, request): if request.path.startswith("/login") or request.path.startswith( "/register") or request.path.startswith( "/admin/") or request.path.startswith( "/token") or request.path.startswith('/api/logger'): response = self.get_response(request) elif self.validateToken(request): token_str = request.COOKIES['token'] token = AuthToken.Get(token_str) pathsToIgnore = [ "/", "/api/status", "/api/queue", "/api/playlist", "/api/playlist/all", "/api/users", ] #if request.path != "/api/status" and request.path != "/api/queue": if request.path not in pathsToIgnore: Logger.instance().Log("{} -> {}".format( token.user.displayName, request.path)) response = self.get_response(request) elif request.path.startswith("/api"): response = HttpResponse("Access denied!", status=401) response.delete_cookie('token') else: response = redirect('/login') response.delete_cookie('token') return response
def getTitle(self, url): try: video = pafy.new(url) except Exception as e: Logger.instance().Log("Pafy [ {} ]: {}".format(url, e)) return return video.title
def start(self): if self.isPlaying() is False: Logger.instance().Log("Started party!") if self.currentSong is None: self.nextSong(True) else: self.vlcPlayer.play() self.logCurrentQueue() return True return False
def shufflePlaylist(self): songs = list( Song.objects.filter(active=True).values_list('id', flat=True)) random.shuffle(songs) print(songs) for a in songs: item = ShuffleItem() item.song_id = a item.save() Logger.instance().Log("Everyday im shufflin~")
def setVolume(self, volume): volume = int(volume) if volume < 20: volume = 20 if volume > 150: volume = 150 self.vlcPlayer.audio_set_volume(volume) volume = self.getVolume() Logger.instance().Log("Current volume: {}".format(volume)) return volume
def Generate(userType, address, login, password=""): userType = int(userType) if userType != USER_TYPE_WEB and userType != USER_TYPE_SKYPE: #if userType not in USER_TYPES: print("Invalid user type!") return None if userType == USER_TYPE_SKYPE and address != "127.0.0.1": print("Wrong address? {} , {}".format(userType != USER_TYPE_SKYPE, address != "127.0.0.1")) return None try: user = User.objects.get(login=login) if user is None or user.active == False or userType != user.type: Logger.instance().Log( "Account \"{}\" is disabled, contact your administrator!". format(login)) print("Account inactive? {} ?= {}".format(userType, user.type)) print("{}, {}, {}".format(user is None, user.active == False, userType != user.type)) return None except: print("No user - " + login) return None if user.type == USER_TYPE_WEB: passwordHash = user.PasswordHash( password) #str(sha512(password + user.salt)) if passwordHash != user.password: print("Wrong password hash") return None print("Removing old tokens!") #AuthToken.delete(user = user) oldTokens = AuthToken.objects.filter(user=user) for ot in oldTokens: print("Deleting token: {}".format(ot.token)) ot.delete() print("Generating new token!") token = AuthToken() #print "Token = {} {} {}".format(str(sha512(str(time.time()))), login, str(random.random())) tokenRandomValue = str(time.time()) + login + str(random.random()) token.token = sha512(tokenRandomValue.encode('ascii')).hexdigest() token.user = user token.address = address token.save() return token
def logCurrentQueue(self): if self.currentSong is not None: msg = "DJPajton zapuszcza: {}".format(self.currentSong.title) else: msg = "DJPajton spi..." if QueueItem.objects.count() > 0: msg = msg + "\n\n" msg = msg + "W kolejce:" for item in QueueItem.objects.all(): msg = msg + "\n- {}".format(item.song.title) Logger.instance().Log(msg) return
def LoggerWait(request, index): index = int(index) logger = Logger.instance() while index == logger.Latest(): sleep(1) return HttpResponse(logger.Latest())
def playSong(self, song): try: url = pafy.new(song.url).getbestaudio().url print("Url: " + url) self.vlcPlayer.set_mrl(url) self.vlcPlayer.play() print("Ok!") if song.title == "_": song.title = self.getTitle(song.url) song.save() self.currentSong = song self.logCurrentQueue() except Exception as e: Logger.instance().Log("Player error ({} - {}): {}".format( song.id, song.title, e)) self.nextSong(True)
def LoggerSince(request, index): index = int(index) logger = Logger.instance() data = logger.GetSince(index) return HttpResponse(json.dumps(data))
def LoggerRecent(request): recent = Logger.instance().Latest() return HttpResponse(recent)
def stop(self): if self.isPlaying(): self.vlcPlayer.stop() Logger.instance().Log("Booooh...") return True return False