Migrate to writefreely
Short script used:
import requests
import yaml
import json
import glob
import os
import datetime
class TokenAuth( requests.auth.AuthBase ):
def __init__( self, token ):
self.token = token
def __call__( self, r ):
r.headers[ 'Authorization' ] = "Token %s" % self.token
r.headers[ 'Content-Type' ] = "application/json"
r.headers[ 'Accept-Encoding' ] = "application/json"
return r
authdata = { "alias": "XXXXXXXX", "pass": "XXXXXXXXXXXXXXXX" }
authresp = requests.post( "https://blog.maz.nu/api/auth/login", json = authdata ).json()
auth = TokenAuth( authresp[ 'data' ][ 'access_token' ] )
for post in glob.glob( "content/blog/*/*/*.md" ):
if os.path.basename( post ) == "index.md":
continue
bits = open( post, 'r' ).read().split( "\n---\n" )
body = bits[ -1 ].strip()
if not body:
continue
try:
meta = yaml.load( bits[ -2 ] )
except:
meta = {}
created = meta.get( '$dates', {} ).get( 'published', datetime.datetime.now() )
data = { "body": body,
"title": meta.get( '$title', meta.get( '$path', '' ).replace( "/", " " ) ),
# "created": created.strftime( "%Y-%m-%dT%H:%M:%SZ" ),
"font": "serif",
"lang": "en",
}
resp = requests.post( "https://blog.maz.nu/api/posts", json = data, auth = auth ).json()
print( "UPDATE posts SET collection_id=1, slug='%s', created='%s', updated='%s' WHERE id='%s';" % ( meta[ '$path' ][ 6: ], created.strftime( "%Y-%m-%d %H:%M:%S" ), created.strftime( "%Y-%m-%d %H:%M:%S" ), resp[ 'data' ][ 'id' ] ) )