Cara otomatisasi upload sertifikat
Dokumentasi: Sistem Auto-Post Sertifikat ke Blogger & Drive
Panduan ini dibuat untuk menginstal ulang sistem otomatisasi sertifikat jika komputer diganti atau di-reset. Sistem ini membaca nama file gambar, menguploadnya ke Google Drive, memposting ke Blogger, dan memindahkan file ke arsip secara otomatis.
I. Persiapan Software (Wajib)
1. Instal Python
- Download Python 3.11 (Versi Stabil). Jangan gunakan versi terbaru (3.14+) karena sering belum support library.
- Saat menginstal, WAJIB CENTANG kotak bertuliskan
Add Python.exe to PATHdi bagian bawah installer. - Klik Install Now.
2. Instal Library Pendukung
Buka Command Prompt (CMD) dan jalankan perintah berikut satu per satu:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip install easyocr google-api-python-client google-auth-httplib2 google-auth-oauthlib
II. Pengaturan Google Cloud Console
Ini langkah paling krusial untuk mendapatkan file kunci (client_secrets.json).
- Buka Google Cloud Console.
- Buat New Project.
- Masuk ke menu APIs & Services > Library. Aktifkan dua layanan ini:
- Google Drive API
- Blogger API v3
- Masuk ke menu OAuth Consent Screen:
- Pilih External.
- Isi nama aplikasi dan email developer.
- PENTING: Di bagian Test Users, klik Add Users dan masukkan alamat email Gmail Anda sendiri. Jika tidak, akses akan diblokir.
- Masuk ke menu Credentials:
- Klik Create Credentials > OAuth Client ID.
- Pilih tipe Desktop App.
- Download file JSON-nya, ubah namanya menjadi
client_secrets.json.
III. Struktur Folder
Buat folder baru (misal: otomatis-blogger) dan pastikan isinya seperti ini:
┣ π sertifikat_png (Tempat menaruh gambar .png)
┣ π sudah_upload (Akan dibuat otomatis oleh script)
┣ π client_secrets.json (File dari Google)
┣ π bot_final.py (Script Python Utama)
┗ π KLIK_SAYA.bat (Tombol jalan pintas)
IV. Kode Script Utama (bot_final.py)
Copy kode di bawah ini dan simpan dengan nama bot_final.py. Jangan lupa edit bagian BLOG_ID dan FOLDER_DRIVE_ID.
import os
import shutil
import io # Library memori untuk mengatasi File Lock Windows
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseUpload
# --- KONFIGURASI (EDIT BAGIAN INI) ---
BLOG_ID = 'MASUKKAN_ID_BLOG_DISINI'
FOLDER_DRIVE_ID = 'MASUKKAN_ID_FOLDER_DRIVE_DISINI'
FOLDER_GAMBAR_LOKAL = 'sertifikat_png'
FOLDER_SUDAH_UPLOAD = 'sudah_upload'
# -------------------------------------
SCOPES = [
'https://www.googleapis.com/auth/blogger',
'https://www.googleapis.com/auth/drive.file'
]
def main():
# 1. LOGIN GOOGLE
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('client_secrets.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
service_drive = build('drive', 'v3', credentials=creds)
service_blogger = build('blogger', 'v3', credentials=creds)
print("\n--- MEMULAI PROSES (METODE RAM - ANTI LOCK) ---")
if not os.path.exists(FOLDER_GAMBAR_LOKAL):
print(f"Error: Folder '{FOLDER_GAMBAR_LOKAL}' tidak ditemukan!")
return
if not os.path.exists(FOLDER_SUDAH_UPLOAD):
os.makedirs(FOLDER_SUDAH_UPLOAD)
files = os.listdir(FOLDER_GAMBAR_LOKAL)
png_files = [f for f in files if f.lower().endswith('.png')]
if not png_files:
print(f"Folder '{FOLDER_GAMBAR_LOKAL}' bersih! Tidak ada file baru.")
return
for file_name in png_files:
print(f"\n------------------------------------------------")
print(f"Sedang memproses file: {file_name}")
file_path_sumber = os.path.join(FOLDER_GAMBAR_LOKAL, file_name)
file_path_tujuan = os.path.join(FOLDER_SUDAH_UPLOAD, file_name)
try:
# A. AMBIL JUDUL (Dari nama file)
judul_post = os.path.splitext(file_name)[0]
print(f" >> Judul: {judul_post}")
# B. BACA KE MEMORI (Agar file asli bisa ditutup)
with open(file_path_sumber, 'rb') as f:
file_buffer = io.BytesIO(f.read())
# C. UPLOAD KE DRIVE
print(" >> Upload ke Google Drive...")
media = MediaIoBaseUpload(file_buffer, mimetype='image/png', resumable=True)
file_drive = service_drive.files().create(
body={'name': file_name, 'parents': [FOLDER_DRIVE_ID]},
media_body=media, fields='id'
).execute()
file_id = file_drive.get('id')
# D. SET IZIN PUBLIK
service_drive.permissions().create(
fileId=file_id, body={'type': 'anyone', 'role': 'reader'}
).execute()
# E. LINK GAMBAR (Thumbnail HD)
url_gambar = f"https://drive.google.com/thumbnail?id={file_id}&sz=w1000"
# F. POST KE BLOGGER
print(" >> Posting ke Blogger...")
konten_html = f"""
<div style="text-align: center; clear: both;">
<a href="{url_gambar}" style="margin-left: 1em; margin-right: 1em;">
<img border="0" src="{url_gambar}" width="640" />
</a>
<br /><br />
<h3>Nomor Sertifikat: {judul_post}</h3>
<p>Dokumen ini adalah sertifikat resmi yang valid.</p>
</div>
"""
body = {'title': judul_post, 'content': konten_html}
service_blogger.posts().insert(blogId=BLOG_ID, body=body, isDraft=False).execute()
print(f" >> SUKSES! Postingan terbit.")
# G. PINDAHKAN FILE
if os.path.exists(file_path_tujuan):
os.remove(file_path_tujuan)
shutil.move(file_path_sumber, file_path_tujuan)
print(f" >> BERHASIL dipindahkan ke Arsip")
except Exception as e:
print(f" >> GAGAL. Error: {e}")
if __name__ == '__main__':
main()
V. Membuat Tombol Pintas (KLIK_SAYA.bat)
Buka Notepad, copy kode ini, dan simpan dengan nama KLIK_SAYA.bat (Pilih 'All Files' saat menyimpan).
cd /d "%~dp0"
echo --- SEDANG MENJALANKAN ROBOT POSTING BLOGGER ---
echo.
python bot_final.py
echo.
echo --- PROSES SELESAI ---
pause
VI. Cara Penggunaan Sehari-hari
- Siapkan gambar sertifikat dalam format .PNG.
- Beri nama file sesuai nomor sertifikat (Contoh:
20250101-nama.png). - Masukkan gambar ke dalam folder
sertifikat_png. - Klik dua kali file KLIK_SAYA.bat.
- Tunggu proses selesai. File yang sukses akan otomatis pindah ke folder
sudah_upload.
VII. Troubleshooting & Perawatan (PENTING)
1. Jika Script Tiba-tiba Gagal Login
Token akses Google kadang bisa kadaluarsa (expired) setelah beberapa minggu atau jika Anda mengganti password Google. Jika script error saat login:
- Buka folder
otomatis-blogger. - Hapus file bernama token.json.
- Jalankan script lagi. Browser akan terbuka otomatis untuk meminta Anda login ulang.
2. Jangan Buka File Saat Proses Berjalan
Saat robot sedang bekerja (layar hitam CMD muncul), JANGAN membuka file gambar di folder sertifikat_png. Hal ini akan menyebabkan error Permission Denied karena Windows mengunci file yang sedang Anda buka.
3. Backup File Kunci
File client_secrets.json adalah kunci utama aplikasi Anda. Simpan salinannya di Google Drive atau Flashdisk. Jika file ini hilang, Anda harus membuat ulang project di Google Cloud Console dari nol.