Management command for importing posts from Tumblr Revision 303139326565 (Sat Apr 03 2010 at 16:29) - Diff Link to this snippet: https://friendpaste.com/7dijfZ9vHQmV1UbzV49jf9 Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788# -*- coding: utf-8 -*-#!/usr/bin/env pythonimport jsonfrom datetime import datetimefrom tumblr import Apifrom django.conf import settingsfrom django.core.management.base import BaseCommandfrom blog.models import Postfrom people.models import Staffclass 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()