Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created March 26, 2013 07:30
Show Gist options
  • Save mdornseif/5243697 to your computer and use it in GitHub Desktop.
Save mdornseif/5243697 to your computer and use it in GitHub Desktop.
Move Posts from Github to Dayone.
# transfer entries from a Wordpress blog to the DayOne app
# deletes the postings in Wordpress
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost, DeletePost
from wordpress_xmlrpc.methods.users import GetUserInfo
import os
import subprocess
import time
import re
from HTMLParser import HTMLParser
# Enter the data of your Blog here
wp = Client('https://XXX.wordpress.com/xmlrpc.php', 'USER', 'PASS')
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def speichern(post):
print post.date
print post.title
print post.id
#print post.content
#print dir(post)
fd = open('posts/%s.txt' % post.id, 'w')
fd.write('%s\n\n' % post.title.encode('utf-8'))
fd.write('%s\n\n' % post.content.encode('utf-8'))
# fd.write('%s\n' % post.date)
fd.close()
ts = time.mktime(post.date.timetuple())
os.utime('posts/%s.txt' % post.id, (ts, ts))
if 'img' in post.content:
fd = open('tmp.txt', 'w')
fd.write('%s\n\n' % post.title.encode('utf-8'))
fd.write('%s\n\n' % strip_tags(post.content.encode('utf-8')))
fd.close()
cmd = "dayone --date=%s new < tmp.txt" % (post.date.date())
#subprocess.call(cmd, shell=True)
for match in re.finditer(r'''src=("|')(.*?)("|')''', post.content):
print match.group(2)
cmd = "curl '%s' > image.jpeg" % (match.group(2))
subprocess.call(cmd, shell=True)
cmd = "dayone --date=%s --photo-file=image.jpeg new < tmp.txt" % (post.date.date())
subprocess.call(cmd, shell=True)
print wp.call(DeletePost(post.id))
else:
cmd = "dayone --date=%s new < posts/%s.txt" % (post.date.date(), post.id)
subprocess.call(cmd, shell=True)
print wp.call(DeletePost(post.id))
start = 0
while True:
posts = wp.call(GetPosts({'number': 2000}))
for post in posts:
speichern(post)
#for term in post.terms:
#if term.slug == 'bohnensuppe':
# speichern(post)
# break
#print wp.call(GetUserInfo())
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import taxonomies
taxes = wp.call(taxonomies.GetTaxonomies())
#print taxes
category_tax = wp.call(taxonomies.GetTaxonomy('category'))
#print category_tax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment