7dijfZ9vHQmV1UbzV49jf9 changeset

Changeset303139326565 (b)
ParentNone (a)
ab
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()
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--- Revision None
+++ Revision 303139326565
@@ -0,0 +1,88 @@
+# -*- 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()