# Path: djangozik/models.py
# class Song(models.Model):
#     title = models.CharField(max_length=250)
#     artist = models.ForeignKey(Artist, related_name="song")
#     style = models.ForeignKey(Style, related_name="song")
#     album = models.ForeignKey(Album, related_name="song")
#     filepath = models.CharField(max_length=256)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.title)
#         super(Song, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# class Artist(models.Model):
#     name = models.CharField(max_length=250)
#     picture = models.CharField(max_length=256, null=True)
#     text = models.TextField(null=True)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Artist, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# class Album(models.Model):
#     name = models.CharField(max_length=250)
#     date = models.DateField('date released', null=True)
#     picture = models.CharField(max_length=256, null=True)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Album, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# class Style(models.Model):
#     name = models.CharField(max_length=250)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Style, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# Path: infos_grabber/metadataGrabber.py
# class MetadataGrabber:
#     def __init__(self):
#         self.cover_grabber = CoverGrabber()
#         self.artist_infos = ArtistInfos()
# 
#     def get_and_save_artist(self, keyword, destination, filename):
#         infos = self.artist_infos.get(keyword)
#         if infos['success'] is True:
#             image_downloader = ImageDownloader(destination)
#             if infos['infos']['image'] is not None:
#                 file_path = image_downloader.download(infos['infos']['image'],
#                                                       filename)
#                 if file_path is not None:
#                     infos['infos']['image'] = file_path
#                     return infos
#                 else:
#                     return None
#             else:
#                 return None
#         else:
#             return None
# 
#     def get_and_save_cover(self, keyword, destination, filename):
#         image_url = self.cover_grabber.grab(keyword)
#         if image_url is not None:
#             image_downloader = ImageDownloader(destination)
#             file_path = image_downloader.download(image_url, filename)
#             if file_path is not None:
#                 return file_path
#             else:
#                 return None
#         else:
#             return None


from django.core.management.base import BaseCommand
from django.utils.encoding import smart_text
from django.conf import settings
from djangozik.models import Song, Artist, Album, Style
from infos_grabber.metadataGrabber import MetadataGrabber
from optparse import make_option
import re
import os
import mutagen
        self.stdout.write("Cleaning old songs")
        if options['verbose']:
            self.stdout.write("Scanning : %s" % settings.MEDIA_ROOT)
        else:
            self.stdout.write("Scanning music folder")
        songs = []
        for root, dirs, files in os.walk(settings.MEDIA_ROOT):
            for filename in files:
                if not filename.endswith(
                    ('.mp3', '.ogg', '.m4a')) or filename.startswith('.'):
                    continue

                songs.append(os.path.join(root, filename))

                song = os.path.join(root, filename)

                # If song exists, skip to the next
                song_path = song.replace(settings.MEDIA_ROOT, '')
                nb_song = Song.objects.filter(filepath=song_path).count()
                if (nb_song > 0):
                    if options['verbose']:
                        self.stdout.write("skip %s " %
                                          song_path.decode('utf-8', 'ignore'))
                    continue

                tags = self.get_tags(song)

                # Check if a similar song exists
                try:
                    artist = Artist.objects.filter(name=tags['artist'])

# Path: djangozik/models.py
# class Song(models.Model):
#     title = models.CharField(max_length=250)
#     artist = models.ForeignKey(Artist, related_name="song")
#     style = models.ForeignKey(Style, related_name="song")
#     album = models.ForeignKey(Album, related_name="song")
#     filepath = models.CharField(max_length=256)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.title)
#         super(Song, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# class Artist(models.Model):
#     name = models.CharField(max_length=250)
#     picture = models.CharField(max_length=256, null=True)
#     text = models.TextField(null=True)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Artist, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# class Album(models.Model):
#     name = models.CharField(max_length=250)
#     date = models.DateField('date released', null=True)
#     picture = models.CharField(max_length=256, null=True)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Album, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# class Style(models.Model):
#     name = models.CharField(max_length=250)
#     slug = models.SlugField(db_index=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Style, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ['name']
# 
# Path: infos_grabber/metadataGrabber.py
# class MetadataGrabber:
#     def __init__(self):
#         self.cover_grabber = CoverGrabber()
#         self.artist_infos = ArtistInfos()
# 
#     def get_and_save_artist(self, keyword, destination, filename):
#         infos = self.artist_infos.get(keyword)
#         if infos['success'] is True:
#             image_downloader = ImageDownloader(destination)
#             if infos['infos']['image'] is not None:
#                 file_path = image_downloader.download(infos['infos']['image'],
#                                                       filename)
#                 if file_path is not None:
#                     infos['infos']['image'] = file_path
#                     return infos
#                 else:
#                     return None
#             else:
#                 return None
#         else:
#             return None
# 
#     def get_and_save_cover(self, keyword, destination, filename):
#         image_url = self.cover_grabber.grab(keyword)
#         if image_url is not None:
#             image_downloader = ImageDownloader(destination)
#             file_path = image_downloader.download(image_url, filename)
#             if file_path is not None:
#                 return file_path
#             else:
#                 return None
#         else:
#             return None


# Path: djangozik/management/commands/importmusic.py
from django.core.management.base import BaseCommand
from django.utils.encoding import smart_text
from django.conf import settings
from djangozik.models import Song, Artist, Album, Style
from infos_grabber.metadataGrabber import MetadataGrabber
from optparse import make_option
import re
import os
import mutagen

        self.stdout.write("Cleaning old songs")
        if options['verbose']:
            self.stdout.write("Scanning : %s" % settings.MEDIA_ROOT)
        else:
            self.stdout.write("Scanning music folder")
        songs = []
        for root, dirs, files in os.walk(settings.MEDIA_ROOT):
            for filename in files:
                if not filename.endswith(
                    ('.mp3', '.ogg', '.m4a')) or filename.startswith('.'):
                    continue

                songs.append(os.path.join(root, filename))

                song = os.path.join(root, filename)

                # If song exists, skip to the next
                song_path = song.replace(settings.MEDIA_ROOT, '')
                nb_song = Song.objects.filter(filepath=song_path).count()
                if (nb_song > 0):
                    if options['verbose']:
                        self.stdout.write("skip %s " %
                                          song_path.decode('utf-8', 'ignore'))
                    continue

                tags = self.get_tags(song)

                # Check if a similar song exists
                try:
                    artist = Artist.objects.filter(name=tags['artist'])

                    album = Album.objects.filter(name=tags['album'])