Editorial Workflows

Upload to WordPress

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: This is a modified version of @jackenhack's workflow. He did all of the hard work, all I did was remove some stuff to streamline the workflow for WorkflowApp.

Please visit jackenhack.com for more info about the originator of this script.

Thanks,

Jeff Benjamin

Shared by: @JeffBenjam

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Set SiteURL ?
Variable Name
SiteURL
Value
http://yourblog.com/xmlrpc.php
Set Wordpress Username ?
Variable Name
username
Value
your-name-here
Set Wordpress Password ?
Variable Name
password
Value
your-password-here
Check modules Installed ?
Source Code
#coding: utf-8
import workflow
import os, sys

#sys.path += [os.path.join(os.getcwd(), 'pypi-modules')]
sys.path += [os.path.join(os.getcwd(), '../../../Documents/pypi-modules')]
import imp
try:
    imp.find_module('wordpress_xmlrpc')
    workflow.set_output('True')
except ImportError:
    workflow.set_output('False')
if missing module... ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
True
Missing module alert ?
Title
Missing Module
Message
You need to install the wordpress_xmlrpc module first.
Button 1
Instructions
Output Value
instruction
Button 2
(don't show)
Output Value
Button 3
(don't show)
Output Value
Show Cancel Button
ON
…End If
If instruction button pressed, go to instructions ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
instruction
Open URL ?
Open in
  • In-App Browser
  • Default App / Safari
URL
http://www.jackenhack.com/editorial-app-import-modules-with-pipista/
Tab
  • Last-used Tab
  • New Tab
  • Tab with ID:
Unique identifier
Wait until Loaded
OFF
Reveal Browser Automatically
ON
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
OFF
Message
Stopped
…End If
Python scale and upload to WP ?
Source Code
#coding: utf-8
import workflow
#import ui

import sys, os, photos, console, clipboard, Image
#sys.path += [os.path.join(os.getcwd(), '../Commands/pypi-modules')]
sys.path += [os.path.join(os.getcwd(), '../../../Documents/pypi-modules')]
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
from cStringIO import StringIO
from PIL import Image
import pickle

prefsfile = 'prefs.pkl'
DEFAULTWIDTH = 600
imagequality = 85

def readprefs(filename):
	if os.path.isfile(filename):
		input = open(filename, 'r')
		width = pickle.load(input)
		input.close()
	else:
		width = DEFAULTWIDTH
	return width
	
def writeprefs(filename, width):
	output = open(filename, 'wb')
	# Pickle dictionary using protocol 0.
	pickle.dump(width, output)
	output.close()

def scaleimage(img, newwidth):
	if img.size[0] < newwidth:
		# dont upscale small image
		console.alert('Image size', 'Image is smaller than ' + str(newwidth) + ' px wide.', 'OK')
		return img
	wpercent = (newwidth/float(img.size[0]))
	hsize = int((float(img.size[1])*float(wpercent)))
	img = img.resize((newwidth,hsize), Image.ANTIALIAS)
	return img

username = workflow.get_variable('username')
site = workflow.get_variable('SiteURL')
password = workflow.get_variable('password')
if not username:
	console.alert('Missing Username', 'Set the username in the script variable of this workflow.')
	workflow.stop()
if not site:
	console.alert('Missing SiteURL', 'Set the site URL to xmlpc.php in the script variable of this Workflow')
	workflow.stop()

if not password:
	console.alert('Missing password', 'Set the password in the script variable of this Workflow')
	workflow.stop()

prefs = {}
#console.clear()
# Try to log in to Wordpress site

try:
	client = Client(site, username, password)
except Exception as e:
	console.hud_alert('Problem logging in', 'error',4)
	workflow.stop()
selphoto = photos.get_image(image_index=-1)
if not selphoto:
	console.hud_alert('No photo selected!', 'error', 2)
	workflow.stop()
width = readprefs(prefsfile)

# Ask for file name
filename = console.input_alert('Image description?', '', '', 'Save')
if not filename:
	print 'No filename given.'
	workflow.stop()
# check for file extension, add if missing
if filename[-4:] != '.png':
	filename = filename + '.png'
# remove all spaces in filename
filename = filename.replace(' ', '-')

imgfile = StringIO()
selphoto.save(imgfile, format='PNG', quality = imagequality)
imagestring = imgfile.getvalue()

# prepare metadata
data = {
        'name': filename,
        'type': 'image/png',  # mimetype
}

console.show_activity()

# read the binary file and let the XMLRPC library encode it into base64
data['bits'] = xmlrpc_client.Binary(imagestring)

try:
	response = client.call(media.UploadFile(data))
except Exception as e:
	console.hud_alert('Problem uploading', 'error', 3)
	workflow.stop()

# response == {
#       'id': 6,
#       'file': 'picture.jpg'
#       'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
#       'type': 'image/jpg',
# }
attachment_id = response['id']
# url, x, y, imageid, imgsize
s = []
imginfo = response['url'] + str(attachment_id) + '|' + 'full'
s.append('full' + ' ' + '	' + imginfo)
result = client.call(media.GetMediaItem(attachment_id))
link = result.link
head, tail = os.path.split(link)
for x in result.metadata['sizes']:
 	if x:
 		img = result.metadata['sizes'][x]['file']
 		w = str(result.metadata['sizes'][x]['width'])
		h = str(result.metadata['sizes'][x]['height'])
	
 		url = head + '/' + img
		imagesize = url + '|' + w + '|' + h + '|' + str(attachment_id) + '|' + x
		s.append(x + ' ' + w + 'x' + h + '	' + imagesize)
		output = '\n'.join(s)
workflow.set_output(output)
console.hide_activity()