Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This script facilitates posting to Micro.blog on iOS with Drafts (using Pythonista).
import requests
import sys
import webbrowser
# Replace the 'API_KEY' value below with your API key;
# get one at https://micro.blog/account
API_KEY = "XXXXXXXXXXXXXXXXXXX"
HEADER_VALUE = "Bearer " + API_KEY
# Grab input from Drafts
content = str(sys.argv[1])
def send_request(content):
# Request
# POST https://micro.blog/micropub
try:
response = requests.post(
url="https://micro.blog/micropub",
headers={
"Authorization": HEADER_VALUE,
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
data={
"h": "entry",
"content": content,
},
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
# Set Drafts URL, then open it when post successful.
drafts_url = 'drafts4://x-callback-url/create?text=' + ''
webbrowser.open(drafts_url)
except requests.exceptions.RequestException:
print('HTTP Request failed')
if __name__ == "__main__":
send_request(content)
Owner

jbwhaley commented Nov 23, 2017

How to Use

This script enables easy posting to micro.blog on iOS directly from Drafts by way of Pythonista. Setting it up is easy.

  • Head over to your account page at Micro.blog and create a new App Token: click on "Edit Apps", enter the name of this script ("MicroBlogger.py") in the "App Name" field, and generate a new token.
  • Copy the Python script and paste it into a new file in Pythonista 3; be sure to call the file "MicroBlogger.py".
  • Replace the "XXXXXX..." on line 8 with the API token you generated earlier.
  • In Drafts, create a new URL action that sends text to this URL: pythonista://MicroBlogger?action=run&argv=%5B%5Bdraft%5D%5D
  • Now just type your post into Drafts, fire the action, and there you go.

Notes

  • Note that if you are using the current beta of Pythonista, you may need to add a "root" parameter to the Drafts URL action, if you are using an alternative root folder in Pythonista.
  • You can find me on Micro.blog at @jbwhaley if you have any questions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment