Playlist import code, various cleanups

master
Bradlee Speice 2013-01-15 13:32:34 -05:00
parent 43bd55d1f5
commit 8125942901
4 changed files with 62 additions and 5 deletions

View File

@ -2,9 +2,11 @@ from django.db import models
import re, itertools
class IntegerListField(models.TextField):
"""Store a list of integers in a database string.
"""
Store a list of integers in a database string.
Format is:
[<int_1>, <int_2>, <int_3>, ... , <int_n>]"""
[<int_1>, <int_2>, <int_3>, ... , <int_n>]
"""
description = "Field type for storing lists of integers."

View File

@ -1,8 +1,12 @@
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from song import Song
from listfield import IntegerListField
import re
from warnings import warn
"""
Playlist model
Each playlist is a high-level ordering of songs. There really isn't much to a playlist - just its name, and the songs inside it.
@ -152,7 +156,7 @@ class Playlist (models.Model):
song = self.songs.get(id = song_id)
playlist_string += "#EXTINF:" + str(song.duration) + "," + song.artist + " - " + song.title + "\n"
playlist_string += song.url + "\n"
playlist_string += song.url + "\n\n"
#Playlist footer
@ -161,9 +165,60 @@ class Playlist (models.Model):
def _import(self, playlist_string = None):
"""
Import and convert a playlist into native DB format.
This function will return true if the playlist format was recognized, false otherwise.
It will return true even if there are errors processing individual songs in the playlist.
As a side note - the _import() name is used since python doesn't let
you name a function import().
"""
#TODO: Code playlist importing
self.song_list = []
if not playlist_string:
#Make sure we have a string to operate on.
return False
#Figure out what format we're in
if playlist_string[0:7] == "#EXTM3U":
#Import m3u format playlist
#Expected format is "#EXTINF:" followed by the song url on the next line.
line_iterator = playlist_string.split("\n").__iter__()
#In case we end iteration early
try:
for line in line_iterator:
if line[0:8] != "#EXTINF:":
song_url = line.next() #Consume the next line
try:
song = Song.objects.get(url = song_url)
self.song_list.append(song.id)
except ObjectDoesNotExist:
#The URL of our song could not be found
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist."
#Silently end processing
except StopIteration:
pass
return True
if playlist_string[0:10] == "[playlist]":
#Import pls format playlist
#This one is a bit simpler - we're just looking for lines that start with "File="
pls_regex = re.compile("^File=", re.IGNORECASE)
for file_line in pls_regex.match(pls_regex, playlist_string):
song_url = file_line[5:]
try:
song = Song.objects.get(url = song_url)
self.song_list.append(song.id)
except ObjectDoesNotExist:
#The URL of our song could not be found
warn("The playlist entry: " + song_url + " could not be found, and has not been added to your playlist."
return True
#If we got here, the playlist format wasn't recognized.
return False

View File

@ -12,7 +12,7 @@ and helps in doing sorting etc.
_default_title = "_UNAVAILABLE_"
_default_artist = "_UNAVAILABLE_"
_default_album = "_UNAVAILABLE_"
_default_release_date = datetime.datetime.now #Function will be called per new song, rather than once at loading the file
_default_release_date = datetime.datetime.now #Function will be called per new song, rather than only being called right now.
_default_genre = "_UNAVAILABLE_"
_default_bpm = -1
_default_disc_number = -1

View File

@ -1 +1 @@
# Create your views here.
#The archiver application is view-less - this is purely a backend manager.