maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration

Home Page:http://python-wordpress-xmlrpc.rtfd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'overwrite' item not overwriting uploaded media

hposborn opened this issue · comments

I am attempting to upload "Current_image.png" with the media method from wordpress_xmlrpc.
In the media directory of my site, I already have a "Current_image.png" file, which I would like to overwrite each time I run my script (so that the image on my website is constantly up to date). Except, when I give the data dictionary the 'overwrite':True label... nothing happens. A new filename appended by -1 is create as if the "overwrite" key was not there at all. Maybe I am misinterpretting what "overwrite" actually does here?

The code:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import getpass
​
pw=getpass.getpass("password:")
client = Client('http://website.com/xmlrpc.php','user',pw)
​
# set to the path to your file
filename = 'Current_image.png'
​
# prepare metadata
data = {
        'name': 'Current_image.png',
        'type': 'image/png'  # mimetype
}
​
# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
        data['bits'] = xmlrpc_client.Binary(img.read())
​
#Adding overwrite
data['overwrite']=True
response = client.call(media.UploadFile(data))
​
attachment_id = response['id']
print(response['link'])
#>>> 'http://website.com/wp-content/uploads/2017/08/Current_image-1.png'

Looks like a Wordpress issue: https://core.trac.wordpress.org/ticket/17604
Last comment gives you the clue that a solution is to delete media first and upload after so names don't collision and need to be renamed with -1.

Cheers