Created
April 26, 2020 19:55
-
-
Save 0xc0d/20a561ca5cd01868aaca7a6044f58df1 to your computer and use it in GitHub Desktop.
upload files using wordpress api and pplication-passwords plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import json | |
import base64 | |
user = 'USERNAME' | |
token = 'WWWW WWWW WWWW WWWW WWWW WWWW' # auth. token from application-passwords plugin | |
apiVer = 'v2' | |
url = f'http://www.example.com/wp-json/wp/{apiVer}' | |
token = base64.standard_b64encode((user + ':' + token).encode('utf-8')) | |
headers = {'Authorization': 'Basic ' + token.decode('utf-8')} | |
filename = "/home/user/Images/cat.jpg" | |
with open(filename,'rb')} as mediaFile: | |
media = {'file': mediaFile} | |
# upload media | |
image = requests.post(url + '/media', headers=headers, files=media) | |
link = json.loads(image.content.decode('utf-8'))['link'] | |
postid = json.loads(image.content.decode('utf-8'))['id'] | |
# add metadata | |
post = { | |
'caption': 'CAPTION', | |
'description': 'DESCRIPTION' | |
} | |
meta = requests.post(url + '/media/'+str(postid), headers=headers, json=post) | |
print('Your image is updated on {} with ID {}'.format(link, postid)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment