#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 the 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.pick_image(show_albums=True)
if not selphoto:
console.hud_alert('No photo selected!', 'error', 2)
workflow.stop()
width = readprefs(prefsfile)
shouldresize = console.alert('Image Size' + str(selphoto.size[0]) + 'x' + str(selphoto.size[1]) + '.', 'Do you want to resize?', 'Original', 'Resize')
if shouldresize == 2:
newwidth = console.input_alert('Current image width ' + str(selphoto.size[0]) + ', resize to', '', str(width), 'Scale')
newwidth = int(newwidth)
if newwidth != width:
# User changed the width, save for next time in preference file
width = newwidth
writeprefs(prefsfile, width)
# Ask for file name
filename = console.input_alert('filename', '', '', 'Save')
if not filename:
print 'No filename given.'
workflow.stop()
# check for file extension, add if missing
if filename[-4:] != '.jpg':
filename = filename + '.jpg'
if shouldresize == 'Resize':
selphoto = scaleimage(selphoto, int(width))
imgfile = StringIO()
selphoto.save(imgfile, format='JPEG', quality = imagequality)
imagestring = imgfile.getvalue()
# prepare metadata
data = {
'name': filename,
'type': 'image/jpg', # 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']
s = []
result = client.call(media.GetMediaItem(attachment_id))
link = result.link
head, tail = os.path.split(link)
for x in result.metadata['sizes']:
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(tail + ' - ' + x + ' ' + w + 'x' + h + ' ' + imagesize)
output = '\n'.join(s)
workflow.set_output(output)
console.hide_activity()
There are no comments yet.