Skip to content

Latest commit

ย 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ WordPress JSON API CLI Tool

Wpjson Python CLI โ€” Fetch posts, categories, and media from WordPress REST API with ease.
Python CLI yang lebih modern, ringan, dan mudah digunakan.

Python License Status


๐Ÿ“‹ Daftar Isi


โœจ Fitur

โœ… 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() dan cleanText() 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

๐Ÿ“ฆ Persyaratan

Komponen Versi Minimum
Python 3.8+
requests 2.28.0+
pip 20.0+

Install Dependencies

# 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

๐Ÿš€ Instalasi

Cara 1: Clone Repository

git clone https://github.com/ahmadsopyan9/Wpjson-Python-CLI.git
cd Wpjson-Python-CLI
pip install -r requirements.txt
chmod +x wpjson.py

Cara 2: Download Langsung

curl -O https://raw.githubusercontent.com/ahmadsopyan9/Wpjson-Python-CLI/main/wpjson.py
pip install requests
chmod +x wpjson.py

Cara 3: Install via pip

pip install wpjson-cli

Verifikasi Instalasi

python wpjson.py --help
# atau
./wpjson.py --help

โšก Penggunaan Cepat

๐Ÿ”น Fetch Semua Kategori

python wpjson.py -t https://example.com -c

๐Ÿ”น Fetch Posts dari Kategori Tertentu

# 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 20

๐Ÿ”น Save Single Post by URL

python wpjson.py -t https://example.com \
  -s "https://example.com/wp-json/wp/v2/posts/123" \
  -C 5

๐Ÿ”น Download Gambar + Ganti Link

python wpjson.py -t https://example.com \
  -p 3 \
  -d \
  -r https://mysite.com \
  -o my_output

๐Ÿ”น Output ke Stdout Saja (Tanpa Save File)

python wpjson.py -t https://example.com -c --no-save | jq '.[0].name'

๐Ÿ”น Mode Quiet (Minimal Output)

python wpjson.py -t https://example.com -p 1 -q

๐Ÿ“– Referensi CLI

๐Ÿ”ง Arguments Utama

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

๐Ÿ”น Contoh Kombinasi Advanced

# 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.py

โš™๏ธ Konfigurasi

Via CLI Arguments (Recommended)

Semua konfigurasi bisa diatur via command-line arguments seperti dijelaskan di atas.

Via Code (Jika digunakan sebagai module)

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()

Environment Variables (Opsional)

export WPJSON_TARGET="https://example.com"
export WPJSON_OUTPUT_DIR="production_data"
export WPJSON_DOWNLOAD_IMAGES="true"

python wpjson.py -c

๐Ÿ“ Struktur Output

Buat Folder sesuai contoh dibawah

output/                          # 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

๐Ÿ“„ Format JSON Output

๐Ÿ“Œ Kategori Response

[
  {
    "id": 5,
    "count": 42,
    "description": "Technology news and tutorials",
    "link": "https://example.com/category/tech",
    "name": "Technology",
    "slug": "tech",
    "taxonomy": "category",
    "parent": 0
  }
]

๐Ÿ“Œ Post Response (dari build_data_post_category / save_post)

{
  "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"
  ]
}

๐Ÿงฉ Penggunaan sebagai Module

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)

๐ŸŒ Localization & Unicode

๐Ÿ‡ฎ๐Ÿ‡ฉ Indonesian Content Support

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"

๐Ÿ”ค Transliterasi Unicode

Fungsi seofy() dan clean_text() menangani:

  • Karakter khusus: รฉ โ†’ e, รฑ โ†’ n, รผ โ†’ u
  • Emoji & simbol: dihapus otomatis
  • Spasi berlebih: dinormalisasi

๐Ÿ’ก Tips: Install unidecode untuk 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)

๐Ÿ› Troubleshooting

โŒ "targetUrl Config is Required!"

# Pastikan flag -t / --target diisi
python wpjson.py -t https://example.com -c  # โœ… Benar
python wpjson.py -c                          # โŒ Salah

โŒ "SSL Certificate Verify Failed"

# 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()

โŒ "404 Not Found" pada WP API

# 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

โŒ Gambar Tidak Terdownload

# 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()

โŒ Output JSON Berantakan / Encoding Error

# 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.json

๐Ÿค Kontribusi

Kami sangat terbuka untuk kontribusi! ๐ŸŽ‰

๐Ÿ”„ Cara Berkontribusi

  1. Fork repository ini
  2. Buat feature branch: git checkout -b fitur-keren
  3. Commit perubahan: git commit -m 'โœจ Tambah fitur keren'
  4. Push ke branch: git push origin fitur-keren
  5. Buka Pull Request

๐Ÿงช Testing

# 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

๐Ÿ“ Guidelines

  • Gunakan type hints untuk fungsi publik
  • Tambahkan docstring untuk fungsi baru
  • Maintain kompatibilitas Python 3.8+
  • Update dokumentasi jika mengubah behavior

๐Ÿ“„ Lisensi

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

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! โญ

About

Wpjson - Python CLI Tools yang lebih modern, ringan, dan mudah digunakan.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages