Blogen-V0.1 Added features. WIP

This commit is contained in:
AjaySi
2023-11-24 15:15:27 +05:30
parent d91688055b
commit 3ec74d22b5
12 changed files with 681 additions and 331 deletions

View File

@@ -1,78 +0,0 @@
import requests
import json
import os
class WixAPI:
def __init__(self, api_key, site_id):
self.api_key = api_key
self.site_id = site_id
self.headers = {
"Authorization": f"Bearer {self.api_key}"
}
def upload_blog(self, blog_title, blog_content, blog_image=None):
"""Uploads a blog to a Wix website.
Args:
blog_title: The title of the blog.
blog_content: The content of the blog.
blog_image: The image for the blog (optional).
Returns:
The ID of the uploaded blog.
"""
response = requests.post(
f"https://www.wix.com/api/v1/sites/{self.site_id}/blogs",
headers=self.headers,
json={
"title": blog_title,
"content": blog_content,
"image": blog_image
}
)
if response.status_code == 201:
return json.loads(response.content)["id"]
else:
raise Exception(f"Failed to upload blog: {response.status_code}")
def upload_blogs(wix_api, local_directory):
"""Uploads all blogs from a local directory to a Wix website.
Args:
wix_api: A WixAPI object.
local_directory: The local directory containing the blogs.
"""
for blog_file in os.listdir(local_directory):
blog_path = os.path.join(local_directory, blog_file)
# Read the blog content from the file.
with open(blog_path, "r") as f:
blog_content = f.read()
# Get the blog title from the file name.
blog_title = blog_file.split(".")[0]
# Upload the blog to the Wix website.
blog_id = wix_api.upload_blog(blog_title, blog_content)
print(f"Uploaded blog {blog_title} with ID {blog_id}")
if __name__ == "__main__":
# Get the Wix API key.
wix_api_key = "IST.eyJraWQiOiJQb3pIX2FDMiIsImFsZyI6IlJTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcIjk3MDFlNTlhLTJlNmEtNDVhMy1hYmU2LWQ0ZWMxMWI4YWFhY1wiLFwiaWRlbnRpdHlcIjp7XCJ0eXBlXCI6XCJhcHBsaWNhdGlvblwiLFwiaWRcIjpcImNjYmI5OWQxLTk1ZmYtNGRmZC1iNGIxLTYwOWRmNWExNmUwN1wifSxcInRlbmFudFwiOntcInR5cGVcIjpcImFjY291bnRcIixcImlkXCI6XCJhNTZiYTM1Zi02NDUzLTQxMDAtYWM1ZC1lM2M4OGU4YTdjN2RcIn19IiwiaWF0IjoxNjk2NjY4MDE1fQ.XhR3cBfxXhjRIeRL28Y7x0lG7o3pN6Cibpe50rN2saJRxFGyVcQGpWt6R_RnyMaBXQrxyKQcLjpTTSxmdnC6Myby1oCFAHuOpmUoGnYz634J_Epfc2BdwnA2SbnvAEktbOoFhIlMf7is2Xt89bE-h7LUPIejGHdCUucv_F1n6gBY6Bl0KxQhA_9k7M92bKr_mvoncDwTPVoeI_CL6fsQZ19tWzSDfe-DvornEIPId-Pp8Gh-lx9LmyhWepQDxpDDXEtlCEEeWvTB8_6ohOC_Jc2gSp8pw7uEawmoAaaqRKsLPBHFjrdgddKJ9jesWWMXxUGWcvJtBtoB3bZypgJSkQ"
# Get the Wix site ID.
wix_site_id = "a56ba35f-6453-4100-ac5d-e3c88e8a7c7d"
# Create a WixAPI object.
wix_api = WixAPI(wix_api_key, wix_site_id)
# Get the local directory containing the blogs.
local_directory = "/home/ajsingh/pseo_experiments/lib/webhosting_integrations"
# Upload all blogs from the local directory to the Wix website.
upload_blogs(wix_api, local_directory)

View File

@@ -1,101 +0,0 @@
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)