Added Song class

This commit is contained in:
Matthieu Grieger 2014-09-13 20:59:26 -07:00
parent d1c28dc10a
commit 115d6b07eb
2 changed files with 74 additions and 1 deletions

View file

@ -12,7 +12,7 @@
# Server address
# DEFAULT VALUE: 'localhost'
SERVER_ADDRESS = 'matthieugrieger.com'
SERVER_ADDRESS = 'localhost'
# Server port
# DEFAULT VALUE: 64738
@ -97,6 +97,20 @@ MOVE_ADMIN_ONLY = True
# DEFAULT VALUE: True
KILL_ADMIN_ONLY = True
# ---------------------
# STORAGE CONFIGURATION
# ---------------------
# Delete audio files after they have been played.
# DEFAULT VALUE: True
DELETE_AUDIO = True
# Delete thumbnails after they have been used.
# DEFAULT VALUE: True
DELETE_THUMBNAILS = True
# ------------------
# CHAT CONFIGURATION
# ------------------

59
mumbledj/song.py Normal file
View file

@ -0,0 +1,59 @@
#----------------------#
# MumbleDJ #
# By Matthieu Grieger #
#----------------------#---------------------------#
# song.py #
# Contains definitions of Song class & methods #
#--------------------------------------------------#
import pafy
from config import *
class Song:
# Constructs a new Song object with all of the necessary attributes
# for the given YouTube url. No downloading of audio or thumbnails
# is done at this stage.
def __init__(self, youtube_url):
song = pafy.new(youtube_url)
self.song_title = song.title
self.song_duration = song.duration
self.song_thumbnail = song.thumb
self.song_audio = song.getbestaudio(preftype = 'ogg')
self.audio_ready = False
self.song_skips = 0
# Downloads the audio file that was found in __init__. Download progress
# callbacks are passed to check_download_status.
def download_song(self):
self.song_audio.download(quiet = True, callback = self._check_download_status)
# A callback function that checks the download status of an audio file. Simply
# sets audio_ready to True when the ratio is 1 (download is completed).
def _check_download_status(total, recvd, ratio, rate, eta):
if ratio == 1:
self.audio_ready = True
# Returns the status of a song download.
def audio_ready(self):
return self.audio_ready
# Called after a song is done playing. Audio files have no use to us after the song
# has finished, so they should just be deleted.
def delete_song(self):
if DELETE_AUDIO:
pass # Delete audio
# Downloads thumnail from location specified by song_thumbnail.
def download_thumbnail(self):
if SHOW_CHAT_NOTIFICATIONS and SHOW_YT_THUMBNAILS:
pass # Download thumbnail
# Called after a song is done playing. Much like audio files, thumbnails are of no
# use to us after the song has finished.
def delete_thumbnail(self):
if DELETE_THUMBNAILS and SHOW_CHAT_NOTIFICATIONS and SHOW_YT_THUMBNAILS:
pass # Delete thumbnail
# Increments the skip count when a user uses the skip command.
def increment_skip_count(self):
self.song_skips = self.song_skips + 1