πŸ‘ˆπŸΌ πŸ•ΈπŸ’ πŸ‘‰πŸΌ
Skip to main content

August 14, 2015

 

Publishing Markdown to Micropub Endpoints with Python

2 min read

You’re probably sick of my posts about Micropub by this point, so I’ll make this my last for a while. Maybe.

I’m up in Seattle this week on business, and the airplane ride from Los Angeles gave me a chance to play around a bit more with Micropub. I whipped up a quick script that allows for quickly publishing Markdown-formatted content to a Micropub endpoint.

This very post is published with the script! Micropub inception! Take a look:

#!/usr/bin/env python

'''
A script for quickly publishing blog posts to a Micropub endpoint. To use
this script, first ensure that you have installed:

    PyYAML
    requests
    markdown

You will also need to set the following environment variables:

    INDIEAUTH_TOKEN
    MICROPUB_ENDPOINT

Run the script, and pass it the path to a Markdown-formatted file. YAML
formatted front matter can be provided with Micropub arguments such as
`name`, `published`, and `slug`.
'''

import sys, os, yaml
import requests, markdown

# configuration
data = {}
token = os.environ.get('INDIEAUTH_TOKEN')
endpoint = os.environ.get('MICROPUB_ENDPOINT')

# make sure the user has provided adequate information
if not token or not endpoint:
    print 'INDIEAUTH_TOKEN & MICROPUB_ENDPOINT environment variables not set.'
    sys.exit(0)

if len(sys.argv) < 2:
    print 'Usage:'
    print '    blogit <path to markdown file>'
    sys.exit(0)

# read in the content
filename = sys.argv[1]
raw_content = open(filename, 'rb').read()

# check for front matter
if raw_content.startswith('---'):

    # parse out the front matter from the raw content
    _, front_matter, raw_content = raw_content.split('---', 2)

    # parse the frontmatter
    data = yaml.load(front_matter)

# render markdown
data['content'] = markdown.markdown(raw_content, extensions=[
    'markdown.extensions.codehilite',
    'markdown.extensions.extra',
    'markdown.extensions.smarty'
])

# populate remaining details
data['access_token'] = token
if 'h' not in data:
    data['h'] = 'entry'

# create the post
result = requests.post(endpoint, data=data)

# check the result
if result.status_code not in (200, 201):
    print 'Failed to publish post with status code: %d' % result.status_code
    sys.exit(1)

print 'Published successfully.'

You’ll note that it supports some Markdown extras, including code highlighting with Pygments, and several other extras.

Feedback is, of course, welcome. On my flight home, I plan on polishing the script up a bit, making it easy to install, and adding a few options.

 

Currently, in Seattle...

Currently, in Seattle...

Manhattan and Fried Chicken Livers at Radiator Whiskey.

 
 

Yes: I'm headed to OpenStack Trove Day in San Jose in a few weeks. Catch up with me there!

 

Documents Confirm Apple is Building a Self-Driving Car

Will they go the Tesla Motors route, and use age-old technology and materials, but complement them with innovative software? I sort of hope not. I'd prefer to see them take the BMW route, and use innovative materials and processes, along with great software. Only, you know, not make their cars as hideous as the BMW i3, which is by all accounts, one of the ugliest vehicles ever created.

Exciting times in motoring.

Update (August 21, 2:15 PM PT): The plot thickens!

 

Recipe: Fresh Whisky Sours

Ingredients

  • 1 cup bourbon whiskey
  • 1/2 cup freshly squeezed lemon juice
  • 1/2 cup freshly squeezed lime juice
  • 1/2 cup freshly squeezed orange juice
  • 2/3 cup sugar syrup
  • Ice cubes

Takes . Serves four.

Combine the whiskey, lemon juice, lime juice, orange juice and syrup. Fill a cocktail shaker halfway with ice and pour in the drink mix 2/3 full. Shake for 15 seconds and pour into glasses.

Note: To make the sugar syrup, bring 1 cup of water and 1 cup of sugar to a boil, and simmer until the sugar is completely dissolved. Chill before using.

 

I created a plugin for @withknown for posting recipes. https://github.com/cleverdevil/Known-Recipes