Wrote a program yesterday

It’s a pretty short program, and it doesn’t do as much as I
wish it did, but it will make a tedious and error-prone job a
little bit less tedious.

I’ve mentioned this before, but turning a website from html
into a content-manged site is a pain in the neck, and one of the
most painful things is getting the images and pdf files and such
into the media library, because that’s a terrible web-based
program, that only lets you do one file at a time.

So my program uses the python wordpresslib. It takes
one argument, which is the name of the file to upload. (The url,
username, and password
for the wordpress blog are hard-coded for my purposes.)

The program uploads the file and returns the URL for accessing
the raw file in the media library.

This isn’t really as good as you would hope for — what you’d
want is to supply a title and caption for the item and get the
link that shows the image in your post as a link to the page
page in the media library. You have to do all of that through
the web interface. But at least that web interface is a
reasonable program. The one for uploading gives you two
choices, one of which never works for me and the other one makes
you pick the filename via the browser even if you know it.

I’m glad I got programming energy for this even if it isn’t
much of a program. I think it’s important to use skills like
that pretty often. Of course I’ve been writing some PHP in the
course of the website redesign, but that’s closer to writing
html than it is to real programming.

Here’s the program if you want to use it:

#!/usr/bin/env python

# 09-Aug-20 lconrad; created
# usage: addmedia.py filename
# adds filename as a media object to the serpentpublications.org wordpress blog

# import library
import wordpresslib, sys

if sys.argv < 2:
print "usage: addmedia.py filename"

filename = sys.argv[1]
# note that it's the xmlrpc.php interface you need to specify
wordpress = "http://serpentpublications.org/wordpress/xmlrpc.php"
user = "*redacted*"
password = "*redacted*"

# prepare client object
wp = wordpresslib.WordPressClient(wordpress, user, password)

# select blog id

# upload image for post
imageSrc = wp.newMediaObject(filename)

print "Image uploaded to: %s" % imageSrc


1 thought on “Wrote a program yesterday”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s