Added wordpress blog upload and config params

This commit is contained in:
AjaySi
2023-10-07 15:47:02 +05:30
parent 82525a0dfa
commit f61bec596a
3 changed files with 131 additions and 64 deletions

View File

@@ -1,54 +0,0 @@
## wordpress_api_integration.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

View File

@@ -0,0 +1,101 @@
import requests
import json
def upload_blog_post(wordpress_site_url, wordpress_username, wordpress_password, blog_post_title, blog_post_content, blog_post_image_url=None):
"""
Uploads a blog post to a WordPress website.
Args:
wordpress_site_url: The URL of the WordPress website.
wordpress_username: The username for the WordPress website.
wordpress_password: The password for the WordPress website.
blog_post_title: The title of the blog post.
blog_post_content: The content of the blog post.
blog_post_image_url: The URL of the blog post image.
Returns:
None.
"""
# Get the WordPress authentication token.
wordpress_auth_token = get_wordpress_auth_token(wordpress_site_url, wordpress_username, wordpress_password)
# Create the request body.
request_body = {
"title": blog_post_title,
"content": blog_post_content
}
# If a blog post image URL is provided, add it to the request body.
if blog_post_image_url:
request_body["featured_media"] = blog_post_image_url
# Make the request to the WordPress API.
try:
response = requests.post(
f"{wordpress_site_url}/wp-json/wp/v2/posts",
headers={"Authorization": f"Bearer {wordpress_auth_token}"},
json=request_body
)
except Exception as e:
raise e
# Check the response status code.
if response.status_code != 201:
raise Exception(f"Failed to upload blog post: {response.status_code}")
# Print a success message.
print("Blog post uploaded successfully!")
def get_wordpress_auth_token(wordpress_site_url, wordpress_username, wordpress_password):
"""
Gets the WordPress authentication token.
Args:
wordpress_site_url: The URL of the WordPress website.
wordpress_username: The username for the WordPress website.
wordpress_password: The password for the WordPress website.
Returns:
A string containing the WordPress authentication token.
"""
# Create the request body.
request_body = {
"username": wordpress_username,
"password": wordpress_password
}
# Make the request to the WordPress API.
try:
response = requests.post(
f"{wordpress_site_url}/wp-json/jwt-auth/v1/token",
json=request_body
)
except Exception as e:
raise e
# Check the response status code.
if response.status_code != 200:
raise Exception(f"Failed to get WordPress authentication token: {response.status_code}")
# Return the WordPress authentication token.
return response.json()["token"]
# Sample usage:
# Get the WordPress site URL, username, and password.
wordpress_site_url = "https://example.com"
wordpress_username = "YOUR_WORDPRESS_USERNAME"
wordpress_password = "YOUR_WORDPRESS_PASSWORD"
# Upload the blog post.
try:
upload_blog_post(wordpress_site_url, wordpress_username, wordpress_password,
"My first blog post", "This is my first blog post.")
except Exception as e:
print(e)

View File

@@ -7,7 +7,8 @@
################################################## ##################################################
# Set the Openai API key # Set the Openai API key.
# https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key
openai_api_key="" openai_api_key=""
# bard_api="" # bard_api=""
# ms_bing_api="" # ms_bing_api=""
@@ -15,13 +16,32 @@ openai_api_key=""
# Mention which model to use, default is GPT-3.5 # Mention which model to use, default is GPT-3.5
model_name="" model_name=""
# Write the prompt for generating TEXT reply from GenAI engine
txt_prompt=""
# An effective text prompt may have several components, including: ###################################################
# Main subject: the who of the prompt, e.g. Small puppy with a fluffy white tail wearing a red collar. #
# Action: the what/how of the prompt, e.g. Joyfully carrying a long wooden stick. # Wordpress and WIX integration and details
# Surroundings: the when/where of the prompt, e.g. On a busy street corner at dusk. A small basketball court in the background. #
# Visual aesthetics: how you want the images to look, e.g. Shot from above, soft yellow light, blurred background. ###################################################
# Write the prompt for generating IMG reply from GenAI engines
img_text="" # Set webhosting as "wordpress" or "wix"
webhosting="wix"
# https://dev.wix.com/docs/rest/articles/getting-started/api-keys
wix_site_id = "1234567890"
wix_api_key = "YOUR_WIX_API_KEY"
# Set the WordPress site URL, username, and password.
# https://wordpress.stackexchange.com/questions/301035/how-to-check-wordpress-website-username-and-password-is-correct
wordpress_site_url = "https://example.com"
wordpress_username = "YOUR_WORDPRESS_USERNAME"
wordpress_password = "YOUR_WORDPRESS_PASSWORD"
####################################################
#
# Details for generating blog images.
#
####################################################
#dall-e2
# https://imagen.research.google/
#imagen
#bing images