# 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
                # Create album if not exists
                album = self.create_album(tags['album'], tags['date'], None)

                songpath = smart_text(song.replace(settings.MEDIA_ROOT, ''))

                if (songpath[0] == "/"):
                    songpath = songpath[1:]

                self.create_song(tags['title'], artist, style, album, songpath)

        self.stdout.write("Song scan finished")

        # Import artists
        self.stdout.write("Import artists")
        ImportArtists.import_artists()

        # Import covers
        self.stdout.write("Import covers")
        ImportCovers.import_covers()

        self.stdout.write("Scan finished")

    def create_artist(self, artist, picture):
        artist, created = Artist.objects.get_or_create(name=artist,
                                                       picture=picture)
        if created:
            artist.save()
        return artist

    def create_style(self, name):

# 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

                # Create album if not exists
                album = self.create_album(tags['album'], tags['date'], None)

                songpath = smart_text(song.replace(settings.MEDIA_ROOT, ''))

                if (songpath[0] == "/"):
                    songpath = songpath[1:]

                self.create_song(tags['title'], artist, style, album, songpath)

        self.stdout.write("Song scan finished")

        # Import artists
        self.stdout.write("Import artists")
        ImportArtists.import_artists()

        # Import covers
        self.stdout.write("Import covers")
        ImportCovers.import_covers()

        self.stdout.write("Scan finished")

    def create_artist(self, artist, picture):
        artist, created = Artist.objects.get_or_create(name=artist,
                                                       picture=picture)
        if created:
            artist.save()
        return artist

    def create_style(self, name):

        style, created = Style.objects.get_or_create(name=name)