Files
ALwrity/lib/wordpress_api_integration/V2/main.py
2023-10-07 10:51:22 +05:30

55 lines
1.5 KiB
Python

## main.py
import os
import requests
import json
class WordPressAPIIntegration:
def __init__(self, credentials: dict):
self.credentials = credentials
def upload_file(self, file_path: str) -> bool:
if not self._check_file(file_path):
return False
if not self._authenticate():
return False
if not self._upload_file_to_api(file_path):
return False
return True
def _check_file(self, file_path: str) -> bool:
max_file_size = 10 * 1024 * 1024 # 10MB
file_size = os.path.getsize(file_path)
if file_size > max_file_size:
return False
valid_file_types = ['.jpg', '.jpeg', '.png', '.gif']
file_extension = os.path.splitext(file_path)[1]
if file_extension not in valid_file_types:
return False
return True
def _authenticate(self) -> bool:
url = "https://wordpress-api.com/authenticate"
headers = {'Content-Type': 'application/json'}
data = json.dumps(self.credentials)
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return True
return False
def _upload_file_to_api(self, file_path: str) -> bool:
url = "https://wordpress-api.com/upload"
files = {'file': open(file_path, 'rb')}
response = requests.post(url, files=files)
if response.status_code == 200:
return True
return False