Last active
November 5, 2022 10:37
-
-
Save jbwhaley/126ddcd807bf5ff95909a78d863e1e6d to your computer and use it in GitHub Desktop.
This script facilitates posting to Micro.blog on iOS with Drafts (using Pythonista).
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
pythonista://MicroBlogger?action=run&argv=%5B%5Bdraft%5D%5D
Notes