-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_api.py
More file actions
53 lines (47 loc) · 1.46 KB
/
wp_api.py
File metadata and controls
53 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import requests
import os
import Config
from pprint import pprint
import logging
def upload_image(image_path, login_data):
api_endpoint = Config.WPAPI.media_api_endpoint
data = open(image_path, 'rb').read()
filename = os.path.basename(image_path)
res = requests.post(
url=api_endpoint,
data=data,
headers={
'Content-Type': 'image/jpg',
'Content-Disposition': 'attachment; filename=%s' % filename},
auth=(login_data['username'], login_data['password'])
)
response = res.json()
media_id = response.get('id')
image_url = response.get('guid').get("rendered")
return {
'id': media_id,
'url': image_url
}
def delete_image(image_id, login_data):
api_endpoint =Config.WPAPI.media_api_endpoint + str(image_id)
logging.info('Deletting %s...' % api_endpoint)
res = requests.delete(
url=api_endpoint,
data={'force': True},
auth=(login_data['username'], login_data['password'])
)
response = res.json()
return response.get('deleted')
if __name__ == '__main__':
logging.basicConfig(
level=logging.DEBUG,
format=Config.Logging.format
)
login_data = {
'username': Config.WPAPI.username,
'password': Config.WPAPI.password,
}
print(upload_image('1.jpeg', login_data))
image_id = input('Enter Image ID to delete: ')
if image_id.strip() != '':
print(delete_image(image_id, login_data))