Created
March 19, 2010 21:43
-
-
Save micktwomey/338227 to your computer and use it in GitHub Desktop.
Simple script I used to import entries from my old blog to tumblr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""My old blog -> tumblr importer | |
""" | |
import csv | |
from getpass import getpass | |
import json | |
import logging | |
import sys | |
import urllib | |
import httplib2 | |
LOG = logging.getLogger("import") | |
HTTP = httplib2.Http() | |
class PostException(Exception): | |
"""Raised when there are problems posting an entry""" | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.INFO) | |
email = None # Set this to something | |
password = getpass() | |
blogs = json.load(open("../../Backups/translucentcode.json")) | |
# Read in any existing posts so we don't duplicate | |
# Simple csv mapping original id -> tumblr id | |
try: | |
LOG.info("Read in posted entries.") | |
posted = set(int(original) for original, tumblr in csv.reader(open("posted.csv"))) | |
except IOError: | |
LOG.info("Creating fresh set of posted entries.") | |
posted = set() | |
entries = (entry for entry in blogs["entries"] | |
if (entry["blog"] == "mick") and (entry["pub_state"] == "published")) | |
# Track what we've posted | |
posted_fp = open("posted.csv", "ab") | |
posted_writer = csv.writer(posted_fp) | |
for entry in entries: | |
original_id = entry["id"] | |
if int(original_id) in posted: | |
LOG.info("Already posted id %r", original_id) | |
continue | |
content = entry["content"] | |
content_type = entry["content_type"] | |
slug = entry["name"] | |
published_date = entry["pub_date"] | |
rendered = entry["rendered"] | |
tags = entry["tags"] | |
tags.append("imported from translucentcode.og") # stick on a tag to find stuff later | |
title = entry["title"] | |
LOG.info("Posting %r - %r", original_id, title) | |
params = { | |
"email": email, | |
"password": password, | |
"type": "regular", | |
"title": title.encode("utf-8"), | |
"body": rendered.encode("utf-8"), | |
"generator": "mick's importer", | |
"date": published_date, | |
"private": "0", | |
"tags": ", ".join('"%s"' % tag for tag in tags), # "tag1", "tag2", "tag3" | |
"format": "html", | |
"slug": slug.encode("utf-8"), | |
"state": "published", | |
"send-to-twitter": "no" | |
} | |
LOG.debug("Params: %r", params) | |
data = urllib.urlencode(params) | |
LOG.debug("Content: %r", data) | |
response, content = HTTP.request("http://www.tumblr.com/api/write", | |
method="POST", body=data, headers={"content-type": "application/x-www-form-urlencoded"}) | |
LOG.debug("response: %r content: %r", response, content) | |
if response.status != 201: | |
LOG.error("Problem posting: response: %r content: %r", response, content) | |
sys.exit(1) | |
LOG.info("Posted to %r", content) | |
posted_writer.writerow((original_id, content)) | |
posted_fp.flush() # just in case I hit ctrl+c :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment