python-wordpress-xmlrpc
python-wordpress-xmlrpc copied to clipboard
'overwrite' item not overwriting uploaded media
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'