Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 9c3d82087413ea0b9078ae7b8fee9412f61b2f86 0 parents
@jonthornton authored
Showing with 107 additions and 0 deletions.
  1. +25 −0 README
  2. +82 −0 wp2tumblr.py
25 README
@@ -0,0 +1,25 @@
+wp2tumblr is a simple Python script to import a Wordpress blog XML export into Tumblr with publishing dates intact. The program is run from the command line with the following syntax:
+
+Useage: python wp2tumblr.py -u [tumblr email address] -p [tumblr passwordd] wordpress-xml-export-path
+
+This software is made available under the open source MIT License:
+
+Copyright (c) 2010 Jon Thornton
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
82 wp2tumblr.py
@@ -0,0 +1,82 @@
+'''
+wp2tumblr by Jon Thornton
+thornton.jon@gmail.com
+Updated 2010-03-29
+
+This script will take an exported WordPress blog and import it into
+a Tumblr account. Everything will be posted as text.
+
+'''
+
+from xml.dom import minidom
+import urllib, urllib2, time, sys, os, getopt, types
+
+tumblr_credentials = {}
+tumblr_api = 'http://www.tumblr.com/api/write'
+USAGE = 'Useage: python wp2tumblr.py -u [tumblr email address] -p [tumblr passwordd] wordpress-xml-export-path'
+
+
+# check the command arguments
+try:
+ login_opts, file_path = getopt.getopt(sys.argv[1:], 'u:p:')
+ wp_xml = file_path[0]
+
+ for opt, value in login_opts:
+ if opt == '-u':
+ tumblr_credentials['email'] = value;
+ elif opt == '-p':
+ tumblr_credentials['password'] = value
+except:
+ print USAGE
+ sys.exit(2)
+
+if len(tumblr_credentials) != 2:
+ print USAGE
+ sys.exit(2)
+
+if not os.path.exists(wp_xml):
+ print 'WordPress xml file ' + wp_xml + ' not found!'
+ sys.exit(2)
+
+try:
+ dom = minidom.parse(wp_xml)
+except Exception, detail:
+ print 'XML file must be well-formed. You\'ll need to edit the file to fix the problem.'
+ print detail
+ sys.exit(2)
+
+items = dom.getElementsByTagName('item')
+for item in items:
+ if item.getElementsByTagName('wp:status')[0].firstChild.nodeValue != 'publish':
+ continue;
+
+ post = tumblr_credentials
+ post['type'] = 'text';
+ post['title'] = item.getElementsByTagName('title')[0].firstChild.nodeValue.strip()
+ post['date'] = item.getElementsByTagName('pubDate')[0].firstChild.nodeValue
+
+ content = item.getElementsByTagName('content:encoded')[0].firstChild
+
+ if content.__class__.__name__ != 'CDATASection':
+ continue
+
+ post['body'] = item.getElementsByTagName('content:encoded')[0].firstChild.nodeValue
+ print post["body"]
+
+ # deal with WordPress's stupid embedded Unicode characters
+ post = dict([(k,v.encode('utf-8') if type(v) is types.UnicodeType else v) for (k,v) in post.items()])
+
+ data = urllib.urlencode(post) # Use urllib to encode the parameters
+
+ try:
+ request = urllib2.Request(tumblr_api, data)
+ response = urllib2.urlopen(request) # This request is sent in HTTP POST
+
+ page = response.read(200000)
+ print page
+ except Exception, detail:
+ print detail
+
+ time.sleep(1) # don't overload the Tumblr API
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.