Skip to content

Instantly share code, notes, and snippets.

@jboynyc
Created October 24, 2013 19:24
Show Gist options
  • Save jboynyc/7143420 to your computer and use it in GitHub Desktop.
Save jboynyc/7143420 to your computer and use it in GitHub Desktop.
Get a list of all images attached to posts within a category. Uses WordPress' XML-RPC API, enabled by default in 3.5. Documentation for python-wordpress-xmlrpc: http://python-wordpress-xmlrpc.readthedocs.org/en/latest/index.html
config = {
'site' : 'doe13',
'user' : 'notarealname',
'password' : 'notarealpassword'
}
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts, media
def flatten(l):
'''flatten a list'''
r = []
for i in l:
if isinstance(i,list):
r.extend(flatten(i))
else:
r.append(i)
return r
eport = Client('http://macaulay.cuny.edu/eportfolios/%s/xmlrpc.php' % config['site'], config['user'], config['password'])
ids = []
offset = 0
increment = 10
while True:
posten = eport.call(posts.GetPosts({'number': increment, 'offset': offset}))
if len(posten) == 0:
break
for p in posten:
if "My City" in str(p.terms):
ids.append(p.id)
offset = offset + increment
pics = []
for ID in ids:
pics.append(eport.call(media.GetMediaLibrary({'parent_id':ID})))
img = flatten(pics)
for i in img:
print '<p><a href="%s"><img src="%s"/></a>%s</p>' % (i.link, i.thumbnail, i.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment