Created
November 19, 2021 04:12
-
-
Save khai-nguyen-dinh/d1e852ef35ed8b978f0a56a3087a47d4 to your computer and use it in GitHub Desktop.
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
import requests | |
import base64 | |
import random | |
import urllib.request | |
def download_image(url): | |
name = random.randrange(10000000, 100000000) | |
fullname = str(name) + ".jpg" | |
urllib.request.urlretrieve(url, fullname) | |
return fullname | |
def header(user, password): | |
credentials = user + ':' + password | |
token = base64.b64encode(credentials.encode()) | |
header_json = {'Authorization': 'Basic ' + token.decode('utf-8')} | |
return header_json | |
def upload_image_to_wordpress(image, url, header_json): | |
fileName = download_image(image) | |
print(fileName) | |
media = {'file': open('./' + fileName, "rb"), 'caption': 'My great demo picture'} | |
responce = requests.post(url + "wp-json/wp/v2/media", headers=header_json, files=media) | |
newDict = responce.json() | |
print(newDict['guid']['raw']) | |
hed = header("username", "Application Password") | |
upload_image_to_wordpress('http://image/url.jpg', | |
'https://wordpress-domain.com/', hed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script auto download image from image url and upload to wordpress using python and wordpress api. To use this script please follow steps bellow: