Wordpress Auto Post via XMLRPC

2024-11-04 13:25:15 - Coderja

create posterwp.py

import xmlrpc.client
import time
import base64

# Fungsi untuk mengunggah gambar dan mendapatkan attachment ID
def upload_image(server, login, password, image_path):
    with open(image_path, "rb") as img_file:
        image_data = xmlrpc.client.Binary(img_file.read())
    
    image_name = image_path.split('/')[-1]
    
    data = {
        'name': image_name,
        'type': 'image/jpeg',  # Ganti sesuai tipe gambar
        'bits': image_data,
        'overwrite': False
    }
    
    response = server.wp.uploadFile(1, login, password, data)
    return response['id']

# Load sites from sites.txt
sites = []
with open('sites.txt', 'r') as f:
    for line in f:
        site, login, password = line.strip().split('|')
        sites.append((site, login, password))

# Load konten from konten.txt
konten = []
with open('konten.txt', 'r') as f:
    for line in f:
        title, content, image_path = line.strip().split('|')
        konten.append((title, content, image_path))

# Bulk post to all sites
with open('posted.txt', 'a') as f_posted:
    for site, login, password in sites:
        for title, content, image_path in konten:
            while True:
                try:
                    server = xmlrpc.client.ServerProxy(site + '/xmlrpc.php')
                    
                    # Upload featured image and get attachment ID
                    image_id = upload_image(server, login, password, image_path)
                    
                    # Publish the post with the featured image
                    post_id = server.wp.newPost(1, login, password, {
                        'post_title': title,
                        'post_content': content,
                        'post_status': 'publish',
                        'post_thumbnail': image_id  # Set featured image
                    })
                    post_url = f"{site}?p={post_id}"
                    print(post_url)
                    f_posted.write(f"{post_url}|{login}|{password}\n")
                    break
                except (xmlrpc.client.Fault, xmlrpc.client.ProtocolError, Exception) as e:
                    print(f"Error posting to {site}: {e}. Skipping...")
                    break

Create kontent.txt

judul|isi kontent|gambar.jpg

Create sitelist sites.txt

https://target.com|login|password


More Posts