Skip to content

Instantly share code, notes, and snippets.

@cleverdevil
Last active March 6, 2018 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleverdevil/c857695bb2de1e46686d720cad9d124c to your computer and use it in GitHub Desktop.
Save cleverdevil/c857695bb2de1e46686d720cad9d124c to your computer and use it in GitHub Desktop.
Import MF2 JSON selectively
#!/usr/bin/env python
'''
A script for quickly publishing MF2 content to a Micropub endpoint. To use this
script, first ensure that you have installed:
requests
You will also need to set the following environment variables:
INDIEAUTH_TOKEN
MICROPUB_ENDPOINT
Run the script, and pass in a path to a directory containing MF2 JSON files.
'''
import sys, os, os.path, codecs, json
import requests
# configuration
token = os.environ.get('INDIEAUTH_TOKEN')
endpoint = os.environ.get('MICROPUB_ENDPOINT')
def publish(mf2_path, path, mf2_filename):
# build out the data to send
content = codecs.open(mf2_path, mode='r', encoding='utf-8').read()
data = json.loads(content)
h = {'Authorization': 'Bearer ' + token}
# create the post
result = requests.post(endpoint, json=data, headers=h)
# check the result
if result.status_code not in (200, 201):
print 'Failed to publish post with status code: %d' % result.status_code
print 'Moving file to "failed" directory.'
os.system('mkdir -p %s/failed' % path)
os.system('mv %s %s/failed' % (mf2_path, path))
return
print 'Published successfully.'
target = result.headers.get('Location')
print ' ->', target
os.system('mkdir -p %s/published' % path)
os.system('mv %s %s/published' % (mf2_path, path))
open('%s/published/%s.target' % (path, mf2_filename), 'w').write(target)
def skip(mf2_path, path):
print 'Skipping file and moving to "skipped" subdirectory.'
os.system('mkdir -p %s/skipped' % path)
os.system('mv %s %s/skipped' % (mf2_path, path))
def save(mf2_path, path):
print 'Skipping file and moving to "saved" subdirectory.'
os.system('mkdir -p %s/saved' % path)
os.system('mv %s %s/saved' % (mf2_path, path))
if __name__ == '__main__':
# 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)
path = sys.argv[1]
for mf2_file in sorted(os.listdir(path)):
mf2_path = os.path.join(path, mf2_file)
if not os.path.isfile(mf2_path): continue
data = json.loads(open(mf2_path).read())
if data.get('properties', {}).get('photo') != None:
print '=' * 80
print 'Skipping PHOTO post:', mf2_path
skip(mf2_path, path)
continue
print '=' * 80
print os.path.join(path, mf2_file)
print '-' * 80
print open(mf2_path).read()
print '-' * 80
action = raw_input('Publish file [(Y)es, (n)o, (s)ave]?: ')
if action in ('Y', 'y', ''):
publish(mf2_path, path, mf2_file)
elif action == 'n':
skip(mf2_path, path)
elif action == 's':
save(mf2_path, path)
else:
print 'Unknown action', action
now_what = raw_input('Continue [(Y)es, (n)o]?: ')
if now_what not in ('Y', 'y', ''):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment