# -*- coding: utf-8 -*-
#!/usr/bin/env python
import json
from datetime import datetime

from tumblr import Api

from django.conf import settings
from django.core.management.base import BaseCommand

from blog.models import Post
from people.models import Staff

class Command(BaseCommand):
    sites = []
    
    def handle(self, *args, **options):
        author = Staff.objects.get(username='goulwen')
        try:
            self.sites = getattr(settings, 'TUMBLR')
        except:
            print 'Unable to find the TUMBLR sites to import in the settings'
        
        for site in self.sites:
            api = Api(site)
            post = api.read()
            
            for post in api.read():
                type = post['type']
                print 'Add %(slug)s with type %(type)s' % {'slug': post['slug'],
                                                           'type': type }
                if type == 'link':
                    p = Post(title = post['link-text'],
                             slug = post['slug'],
                             author = author,
                             body = post['link-description'],
                             tease = post['link-url'],
                             allow_comments = False,
                             created_at = datetime.fromtimestamp(post['unix-timestamp']),
                             updated_at = datetime.fromtimestamp(post['unix-timestamp']),
                             published_at = datetime.fromtimestamp(post['unix-timestamp'])
                             )
                    p.save()
                elif type == 'quote':
                    p = Post(title = post['quote-text'],
                             slug = post['slug'],
                             author = author,
                             body = post['quote-source'],
                             allow_comments = False,
                             created_at = datetime.fromtimestamp(post['unix-timestamp']),
                             updated_at = datetime.fromtimestamp(post['unix-timestamp']),
                             published_at = datetime.fromtimestamp(post['unix-timestamp'])
                             )
                    p.save()
                elif type == 'photo':
                    p = Post(title = 'Photo',
                             slug = post['slug'],
                             author = author,
                             body = post['photo-caption'],
                             allow_comments = False,
                             created_at = datetime.fromtimestamp(post['unix-timestamp']),
                             updated_at = datetime.fromtimestamp(post['unix-timestamp']),
                             published_at = datetime.fromtimestamp(post['unix-timestamp'])
                             )
                    p.save()
                elif type == 'regular':
                    p = Post(title = post['regular-title'],
                             slug = post['slug'],
                             author = author,
                             body = post['regular-body'],
                             allow_comments = False,
                             created_at = datetime.fromtimestamp(post['unix-timestamp']),
                             updated_at = datetime.fromtimestamp(post['unix-timestamp']),
                             published_at = datetime.fromtimestamp(post['unix-timestamp'])
                             )
                    p.save()
                elif type == 'video':
                    p = Post(title = 'Vidéo',
                             slug = post['slug'],
                             author = author,
                             body = post['video-player'],
                             tease = post['video-source'],
                             allow_comments = False,
                             created_at = datetime.fromtimestamp(post['unix-timestamp']),
                             updated_at = datetime.fromtimestamp(post['unix-timestamp']),
                             published_at = datetime.fromtimestamp(post['unix-timestamp'])
                             )
                    p.save()