- Content hosting. We have hosted our own server in the basement for long enough to invite Google to have a go at it for a while. We have already moved e-mail out to Google.
- Ease of use. We don't need all the power offered by WordPress (we could edit the PHP, add a module, or other things), and those features that we use are now supported by Blogger.
Blogger also has a limit on the number of posts that you can add on any given day, so if you have too many you will have to split it over a couple of days, or, perhaps, use a different mechanism.
#!/usr/bin/python
# Sample script to import data from wordpress to blogger.
# Assumes a modicum of scripting ability & access to the wp database.
# Don't blame me when it doesn't work!
#
from gdata import service
import gdata
import gdata
import atom
import MySQLdb
# Replace the following global variables with the
# correct values.
USER='blogger@gmail.com'
PASS='blogger_password'
BLOGID=12345
DB_NAME='wp_database'
DB_USER='db_user'
DB_PASSWORD='db_password'
WP_USER='me'
DRAFT_MODE=False
# Semi-permanent. You can find this via comment feed.
TAG='tag:blogger.com,1999:blog-%s.post-" % BLOGID
# And now for the actual script (which is pretty simple).
blogger = service.GDataService(USER, PASS)
blogger.source = 'my-dataimporter-1.0'
blogger.service = 'blogger'
blogger.account_type = 'GOOGLE'
blogger.server = 'www.blogger.com'
blogger.ProgrammaticLogin()
# Creates a post with title, content, and post date.
def CreatePost(title, content, dt):
entry = gdata.GDataEntry()
entry.title = atom.Title('xhtml', title)
entry.content = atom.Content(
content_type='html', text=content)
if dt is not None:
entry.published = atom.Published(
text=dt.strftime("%Y-%m-%dT%H:%M:%SZ"))
if DRAFT_MODE:
control = atom.Control()
control.draft = atom.Draft(text='yes')
entry.control = control
return blogger.Post(entry,
'/feeds/%s/posts/default' % blogid)
# Creates a comment with title, content, and post date.
# (Actually, the title becomes part of the post).
def CreateComment(postid, title, text, dt):
entry = gdata.GDataEntry()
entry.content = atom.Content(content_type='html',
text=re.sub(r'<[^>]*?>', '',
"%s\n%s" % (title, text)))
if dt is not None:
entry.published = atom.Published(
text=dt.strftime("%Y-%m-%dT%H:%M:%SZ"))
blogger.Post(entry,
'/feeds/%s/%s/comments/default" %(blogid, postid))
conn = MySQLdb.connect(host='localhost',
user=DB_USER,
passwd=DB_PASSWD,
db=DB_NAME)
# Create all posts.
cursor = conn.cursor()
cursor.execute( "SELECT u.user_login, p.post_title, " +
"p.post_content, p.post_date_gmt "+
"FROM wp_posts p, wp_users u " +
"WHERE p.post_author = u.id ")
result_set = cursor.fetchall()
for row in result_set:
print "Adding %s" % str(row)
CreatePost(row[1], row[2],
"Posted by %s\n%s" %(row[0],row[3]))
# Create all comments
post_feed = blogger.GetFeed(
"/feeds/%s/posts/default" % blogid)
for entry in post_feed:
postid = entry.id.text[len(TAG):]
if entry.title.text is not None:
cursor.execute(
"SELECT c.comment_author, c.comment_author_url "+
"c.comment_content, c.comment_date " +
"FROM wp_comments c, wp_posts p " +
"WHERE c.comment_post_id = p.id " +
"AND p.post_title = '%s'" %
MySQLdb.escape_string(entry.title.text))
comment_set = cursor.fetchall()
for row in comment_set:
t = "Posted by %s" % row[0]
if len(row[1]) > len('http://com'):
t = t + " at %s" % row[1]
CreateComment(postid, t, row[2], row[3])
See the blogger python api (gdata api).
0 comments:
Post a Comment