a | b | |
---|
| 0 | + | # -*- coding: utf-8 -*- |
---|
| 0 | + | #!/usr/bin/env python |
---|
| 0 | + | import json |
---|
| 0 | + | from datetime import datetime |
---|
| 0 | + | |
---|
| 0 | + | from tumblr import Api |
---|
| 0 | + | |
---|
| 0 | + | from django.conf import settings |
---|
| 0 | + | from django.core.management.base import BaseCommand |
---|
| 0 | + | |
---|
| 0 | + | from blog.models import Post |
---|
| 0 | + | from people.models import Staff |
---|
| 0 | + | |
---|
| 0 | + | class Command(BaseCommand): |
---|
| 0 | + | sites = [] |
---|
| 0 | + | |
---|
| 0 | + | def handle(self, *args, **options): |
---|
| 0 | + | author = Staff.objects.get(username='goulwen') |
---|
| 0 | + | try: |
---|
| 0 | + | self.sites = getattr(settings, 'TUMBLR') |
---|
| 0 | + | except: |
---|
| 0 | + | print 'Unable to find the TUMBLR sites to import in the settings' |
---|
| 0 | + | |
---|
| 0 | + | for site in self.sites: |
---|
| 0 | + | api = Api(site) |
---|
| 0 | + | post = api.read() |
---|
| 0 | + | |
---|
| 0 | + | for post in api.read(): |
---|
| 0 | + | type = post['type'] |
---|
| 0 | + | print 'Add %(slug)s with type %(type)s' % {'slug': post['slug'], |
---|
| 0 | + | 'type': type } |
---|
| 0 | + | if type == 'link': |
---|
| 0 | + | p = Post(title = post['link-text'], |
---|
| 0 | + | slug = post['slug'], |
---|
| 0 | + | author = author, |
---|
| 0 | + | body = post['link-description'], |
---|
| 0 | + | tease = post['link-url'], |
---|
| 0 | + | allow_comments = False, |
---|
| 0 | + | created_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | updated_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | published_at = datetime.fromtimestamp(post['unix-timestamp']) |
---|
| 0 | + | ) |
---|
| 0 | + | p.save() |
---|
| 0 | + | elif type == 'quote': |
---|
| 0 | + | p = Post(title = post['quote-text'], |
---|
| 0 | + | slug = post['slug'], |
---|
| 0 | + | author = author, |
---|
| 0 | + | body = post['quote-source'], |
---|
| 0 | + | allow_comments = False, |
---|
| 0 | + | created_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | updated_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | published_at = datetime.fromtimestamp(post['unix-timestamp']) |
---|
| 0 | + | ) |
---|
| 0 | + | p.save() |
---|
| 0 | + | elif type == 'photo': |
---|
| 0 | + | p = Post(title = 'Photo', |
---|
| 0 | + | slug = post['slug'], |
---|
| 0 | + | author = author, |
---|
| 0 | + | body = post['photo-caption'], |
---|
| 0 | + | allow_comments = False, |
---|
| 0 | + | created_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | updated_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | published_at = datetime.fromtimestamp(post['unix-timestamp']) |
---|
| 0 | + | ) |
---|
| 0 | + | p.save() |
---|
| 0 | + | elif type == 'regular': |
---|
| 0 | + | p = Post(title = post['regular-title'], |
---|
| 0 | + | slug = post['slug'], |
---|
| 0 | + | author = author, |
---|
| 0 | + | body = post['regular-body'], |
---|
| 0 | + | allow_comments = False, |
---|
| 0 | + | created_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | updated_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | published_at = datetime.fromtimestamp(post['unix-timestamp']) |
---|
| 0 | + | ) |
---|
| 0 | + | p.save() |
---|
| 0 | + | elif type == 'video': |
---|
| 0 | + | p = Post(title = 'Vidéo', |
---|
| 0 | + | slug = post['slug'], |
---|
| 0 | + | author = author, |
---|
| 0 | + | body = post['video-player'], |
---|
| 0 | + | tease = post['video-source'], |
---|
| 0 | + | allow_comments = False, |
---|
| 0 | + | created_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | updated_at = datetime.fromtimestamp(post['unix-timestamp']), |
---|
| 0 | + | published_at = datetime.fromtimestamp(post['unix-timestamp']) |
---|
| 0 | + | ) |
---|
| 0 | + | p.save() |
---|
... | |
---|