zdgtl 10 months ago

Bulk post Wordpress Via xmlrpc Menggunakan Python

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

Penjelasan Modifikasi 📝

  1. Fungsi upload_image: Fungsi ini mengunggah gambar ke WordPress dan mengembalikan ID attachment dari gambar tersebut.
  2. Modifikasi konten.txt: Setiap baris di file konten.txt sekarang memiliki struktur title|content|image_path untuk menyertakan jalur gambar.
  3. Upload dan Set Featured Image: Pada bagian pengunggahan gambar, ID dari gambar tersebut diambil dan digunakan sebagai post_thumbnail.

Sekarang skrip ini akan mengunggah gambar terlebih dahulu, kemudian menggunakannya sebagai featured image untuk setiap postingan yang dibuat. 📸✨

0
423
Menghilangkan Spasi Di depan Baris

Menghilangkan Spasi Di depan Baris

1743468932.jpg
zdgtl
9 months ago
Time Ago to Date Format Converter

Time Ago to Date Format Converter

1743468932.jpg
zdgtl
10 months ago
TMDB Movie Scraper

TMDB Movie Scraper

1743468932.jpg
zdgtl
10 months ago
Bulk Remove line containing

Bulk Remove line containing

1743468932.jpg
zdgtl
9 months ago
How to move the post title below content wordpress

How to move the post title below content wordpress

1743468932.jpg
zdgtl
9 months ago