-
Notifications
You must be signed in to change notification settings - Fork 1
/
blogger_importer.py
107 lines (93 loc) · 3.66 KB
/
blogger_importer.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import markdown
import optfunc
from basic.blog.models import Post
from dateutil.parser import parse
from django.conf import settings
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify, striptags
from gdata import service
"""
Required Dependencies:
- dateutils
- gdata
- django
- django basic apps (http://github.com/nathanborror/django-basic-apps)
- optfunc (http://github.com/simonw/optfunc)
"""
def import_entries(blogger_service, blog_id):
query = service.Query()
query.feed = '/feeds/' + blog_id + '/posts/default'
query.max_results = 999
feed = blogger_service.Get(query.ToUri())
for entry in feed.entry:
site = Site.objects.get(id__exact=settings.SITE_ID)
entry_id = entry.id.text.split('.post-')[-1]
title = entry.title.text
slug = slugify(title[0:50])
body = entry.content.text
publish = parse(entry.published.text)
query = service.Query()
query.feed = '/feeds/' + blog_id + '/' + entry_id + '/comments/default'
query.max_results = 999
comment_feed = blogger_service.Get(query.ToUri())
try:
post = Post.objects.get(title = title, slug = slug)
except Post.DoesNotExist:
post = Post(
title = title,
slug = slug,
body = body,
publish = publish,
)
post.save()
post.sites.add(site)
print "Imported post: %s" % (post)
for comment in comment_feed.entry:
if comment.author:
for author in comment.author:
user_name = author.name.text
user_email = author.email.text
if author.uri:
user_uri = author.uri.text
else:
user_uri = ''
if comment.updated:
submit_date = parse(comment.updated.text)
else:
submit_date = parse(comment.published.text)
comment = comment.content.text
content_type = ContentType.objects.get(app_label="blog", model="post")
try:
comment = Comment.objects.get(comment = comment, submit_date = submit_date)
except Comment.DoesNotExist:
comment = Comment(
site = site,
content_type = content_type,
object_pk = post.id,
user_name = user_name,
user_email = user_email,
user_url = user_uri,
comment = striptags(comment),
submit_date = submit_date,
)
comment.save()
print "Imported comment: %s" % (comment)
# Hack to get around some bug that was breaking get_next_by_foo() for Post objects
p = Post.objects.all()
for post in p:
post.save()
@optfunc.arghelp('blog_id', 'your Blogger id')
@optfunc.arghelp('email', 'your email address')
@optfunc.arghelp('password', 'your password')
def blogger_importer(blog_id, email, password):
"""Usage: %prog <blog_id> <email> <password>- Import Blogger entries into Django Basic Blog"""
blogger_service = service.GDataService(email, password)
blogger_service.service = 'blogger'
blogger_service.account_type = 'GOOOGLE'
blogger_service.server = 'www.blogger.com'
blogger_service.ProgrammaticLogin()
import_entries(blogger_service, blog_id)
if __name__ == '__main__':
optfunc.main(blogger_importer)