Wpjson Python CLI โ Fetch posts, categories, and media from WordPress REST API with ease.
Python CLI yang lebih modern, ringan, dan mudah digunakan.
- โจ Fitur
- ๐ฆ Persyaratan
- ๐ Instalasi
- โก Penggunaan Cepat
- ๐ Referensi CLI
- โ๏ธ Konfigurasi
- ๐ Struktur Output
- ๐งฉ Contoh JSON Output
- ๐ง Penggunaan sebagai Module
- ๐ Localization & Unicode
- ๐ Troubleshooting
- ๐ค Kontribusi
- ๐ Lisensi
โ Multi-Action CLI
- Fetch categories, posts by category, atau single post by URL
โ Image Downloader
- Download featured media + semua gambar dalam konten secara otomatis
โ Link Replacement
- Ganti semua link dari target WordPress ke website Anda
โ JSON Output
- Hasil dalam format JSON yang rapi, siap untuk import ke sistem lain
โ SEO-Friendly Slug Generator
- Fungsi
seofy()dancleanText()untuk sanitasi teks
โ Error Handling
- Handle timeout, SSL, dan network errors dengan graceful
โ Progress Output
- Feedback real-time saat proses fetch/download (bisa dimatikan dengan
--quiet)
โ Cross-Platform
- Berjalan di Windows, macOS, dan Linux
| Komponen | Versi Minimum |
|---|---|
| Python | 3.8+ |
| requests | 2.28.0+ |
| pip | 20.0+ |
# Clone atau download script
git clone https://github.com/ahmadsopyan9/Wpjson-Python-CLI.git
cd Wpjson-Python-CLI
# Install dependencies
pip install -r requirements.txt๐ก Opsional: Untuk transliterasi Unicode yang lebih akurat (misal: "Cafรฉ" โ "cafe"):
pip install unidecode
git clone https://github.com/ahmadsopyan9/Wpjson-Python-CLI.git
cd Wpjson-Python-CLI
pip install -r requirements.txt
chmod +x wpjson.pycurl -O https://raw.githubusercontent.com/ahmadsopyan9/Wpjson-Python-CLI/main/wpjson.py
pip install requests
chmod +x wpjson.pypip install wpjson-clipython wpjson.py --help
# atau
./wpjson.py --helppython wpjson.py -t https://example.com -c# Kategori ID 5, halaman 1, 10 items
python wpjson.py -t https://example.com -p 5
# Dengan pagination
python wpjson.py -t https://example.com -p 5 --page 2 --per-page 20python wpjson.py -t https://example.com \
-s "https://example.com/wp-json/wp/v2/posts/123" \
-C 5python wpjson.py -t https://example.com \
-p 3 \
-d \
-r https://mysite.com \
-o my_outputpython wpjson.py -t https://example.com -c --no-save | jq '.[0].name'python wpjson.py -t https://example.com -p 1 -q| Argument | Short | Type | Default | Deskripsi |
|---|---|---|---|---|
--target |
-t |
str |
required | URL WordPress target (tanpa /wp-json) |
--categories |
-c |
flag |
- | Fetch semua kategori |
--category-posts |
-p |
int |
- | Fetch posts dari kategori ID tertentu |
--save-post |
-s |
str |
- | Save single post dari WP JSON API URL |
--category-id |
-C |
int |
1 |
Category ID untuk action --save-post |
--page |
- | int |
1 |
Nomor halaman untuk pagination |
--per-page |
- | int |
10 |
Jumlah items per halaman (max 100 untuk WP API) |
--download-images |
-d |
flag |
false |
Download featured media + gambar dalam konten |
--replace-link |
-r |
str |
- | Ganti URL target dengan URL website Anda |
--output-dir |
-o |
str |
"output" |
Nama direktori output |
--no-save |
- | flag |
false |
Jangan simpan ke file JSON (stdout only) |
--quiet |
-q |
flag |
false |
Suppress progress messages |
--help |
-h |
- | - | Tampilkan bantuan |
# Batch fetch multiple categories
for id in 1 2 3 4 5; do
python wpjson.py -t https://example.com -p $id --page 1 --per-page 50 -d
done
# Fetch dengan custom output + link replacement
python wpjson.py -t https://oldsite.com \
-p 10 \
--page 1 \
--per-page 100 \
-d \
-r https://newsite.com \
-o migration_data \
--no-save | jq '.[] | {title, slug, category_id}'
# Pipeline ke tool lain
python wpjson.py -t https://example.com -c --no-save | \
python process_categories.pySemua konfigurasi bisa diatur via command-line arguments seperti dijelaskan di atas.
from wpjson import Wpjson
config = {
"targetUrl": "https://example.com",
"downloadImage": True,
"myWebsiteLink": "https://mysite.com",
"dirOutputName": "my_output",
"saveFileJson": True
}
wp = Wpjson(config)
wp.build_data_post_category(ctg_id=5, page=1, per_page=20)
result = wp.response_object()export WPJSON_TARGET="https://example.com"
export WPJSON_OUTPUT_DIR="production_data"
export WPJSON_DOWNLOAD_IMAGES="true"
python wpjson.py -coutput/ # Default: --output-dir
โโโ images/ # Semua gambar yang didownload
โ โโโ featured-image-123.jpg
โ โโโ content-photo.png
โ โโโ screenshot_2024.webp
โ
โโโ posts/ # File JSON hasil fetch
โโโ post_ctg_5_page_1_limit_10.json
โโโ post_ctg_5_page_2_limit_10.json
โโโ my-awesome-post-slug.json # Hasil dari --save-post
[
{
"id": 5,
"count": 42,
"description": "Technology news and tutorials",
"link": "https://example.com/category/tech",
"name": "Technology",
"slug": "tech",
"taxonomy": "category",
"parent": 0
}
]{
"author_id": 1,
"title": "Judul Artikel Lengkap Di Sini",
"slug": "judul-artikel-lengkap-di-sini",
"featured_media": "featured-image-123.jpg",
"summary": "<p>Ringkasan artikel dalam HTML...</p>",
"content": "<p>Konten lengkap dengan gambar dan formatting...</p>",
"category_id": 5,
"wp_featured_media_url": "https://example.com/wp-content/uploads/2024/01/image.jpg",
"wp_categories": [5, 12, 23],
"wp_tags": [45, 67, 89],
"status": "publish",
"all_image_content": [
"https://example.com/wp-content/uploads/2024/01/img1.jpg",
"https://example.com/wp-content/uploads/2024/01/img2.png"
]
}wpjson.py bisa di-import ke project Python lain:
# example_usage.py
from wpjson import Wpjson
import json
# Inisialisasi
wp = Wpjson({
"targetUrl": "https://example.com",
"downloadImage": True,
"myWebsiteLink": "https://mysite.com"
})
# Fetch categories
categories = wp.get_all_category(page=1, per_page=100).response_object()
print(f"Found {len(categories)} categories")
# Fetch posts from category
posts = wp.build_data_post_category(ctg_id=5, page=1, per_page=20).response_object()
print(f"Fetched {len(posts)} posts")
# Process data
for post in posts:
print(f"๐ {post['title']}")
print(f"๐ผ๏ธ Featured: {post['featured_media']}")
print(f"๐ Images: {len(post['all_image_content'])} found\n")
# Output to custom file
with open('custom_output.json', 'w', encoding='utf-8') as f:
json.dump(posts, f, indent=2, ensure_ascii=False)Tool ini mendukung penuh konten berbahasa Indonesia:
# Fetch posts dengan judul & konten bahasa Indonesia
python wpjson.py -t https://blogindonesia.com -p 3 -d
# Output akan mempertahankan karakter Unicode
# Contoh: "Mengenal Python untuk Pemula" โ slug: "mengenal-python-untuk-pemula"Fungsi seofy() dan clean_text() menangani:
- Karakter khusus:
รฉ โ e,รฑ โ n,รผ โ u - Emoji & simbol: dihapus otomatis
- Spasi berlebih: dinormalisasi
๐ก Tips: Install
unidecodeuntuk hasil transliterasi lebih akurat:# Di wpjson.py, ganti: # from unicodedata import normalize from unidecode import unidecode # Lalu di fungsi _seofy dan _clean_text: s_string = unidecode(s_string)
# Pastikan flag -t / --target diisi
python wpjson.py -t https://example.com -c # โ
Benar
python wpjson.py -c # โ Salah# Opsi 1: Update certificates
pip install --upgrade certifi
# Opsi 2: (Tidak direkomendasikan untuk production) Skip SSL verify
# Edit wpjson.py, tambahkan verify=False di requests.get()# Pastikan:
# 1. URL target benar (tanpa /wp-json di akhir)
# 2. WordPress REST API aktif (biasanya aktif default di WP 4.7+)
# 3. Tidak ada plugin security yang memblokir API
# Test API manual:
curl https://example.com/wp-json/wp/v2/posts?per_page=1# Cek:
# 1. URL gambar publicly accessible (tidak butuh login)
# 2. Server target tidak memblokir user-agent / hotlinking
# 3. Cukup ruang disk di folder output/images/
# Debug: Tambahkan print di fungsi _download_single_image()# Pastikan terminal mendukung UTF-8
# Windows: chcp 65001
# Linux/macOS: export LANG=en_US.UTF-8
# Atau redirect ke file:
python wpjson.py -t https://example.com -c --no-save > output.jsonKami sangat terbuka untuk kontribusi! ๐
- Fork repository ini
- Buat feature branch:
git checkout -b fitur-keren - Commit perubahan:
git commit -m 'โจ Tambah fitur keren' - Push ke branch:
git push origin fitur-keren - Buka Pull Request
# Install dev dependencies (jika ada)
pip install pytest requests-mock
# Jalankan test (jika tersedia)
pytest tests/
# Lint code
flake8 wpjson.py
black wpjson.py --check- Gunakan type hints untuk fungsi publik
- Tambahkan docstring untuk fungsi baru
- Maintain kompatibilitas Python 3.8+
- Update dokumentasi jika mengubah behavior
Distributed under MIT License. Lihat LICENSE untuk informasi lengkap.
MIT License
Copyright (c) 2024 Ahmad Sofyan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
...
Terima kasih untuk:
- ๐ Komunitas WordPress untuk REST API yangawesome
- ๐ Komunitas Python untuk ekosistem yang luar biasa
- ๐ป Semua contributor yang telah membantu pengembangan tool ini
pjson-Python-CLI Dibuat dengan โค๏ธ untuk developer Indonesia
Semoga tool ini membantu migrasi, backup, atau integrasi WordPress Anda menjadi lebih mudah!
๐ Links Penting
๐ฌ Contact
Punya pertanyaan, bug report, atau feature request?
๐ Buka Issue atau DM saya!
โญ Jangan lupa star repository ini jika membantu! โญ