ป้ายกำกับ: Transmission RPC

สร้างระบบ Automation ซิงค์ BitTorrent 12 สเตจด้วย Python และ Vi บน QNAPสร้างระบบ Automation ซิงค์ BitTorrent 12 สเตจด้วย Python และ Vi บน QNAP

การบริหารจัดการไฟล์ทอร์เรนต์ระดับหลายพันรายการจำเป็นต้องมีระบบติดตามสถานะ (State Tracking) ที่แม่นยำ บทความนี้จะพาลงมือสร้างสคริปต์ Python เพื่อเชื่อมต่อกับ Transmission RPC ตรวจสอบสถานะการทำงานจริง แล้วสั่งย้ายไฟล์ .torrent ข้ามโฟลเดอร์ในโมเดล 12-Stage Pipeline โดยใช้โปรแกรมแก้ไขข้อความยอดนิยมบน Terminal อย่าง Vi ทีละขั้นตอน


ทำความเข้าใจตรรกะการจับคู่สถานะ (12-Stage State Mapping)

Transmission RPC จะส่งข้อมูลสถานะตัวเลข status (0 ถึง 6) พร้อมเมตริกเสริม สคริปต์จะแปลงค่าเหล่านี้เป็นโฟลเดอร์ปลายทางตามเงื่อนไข

ลำดับการประเมินเงื่อนไขทางเทคนิคจาก RPCโฟลเดอร์สเตจปลายทาง
1. ตรวจพบข้อผิดพลาดerror != 0 (เช่น Disk Full, I/O Error)torrents/error/
2. ตรวจสอบชิ้นส่วนข้อมูลstatus เป็น 1 (Wait) หรือ 2 (Checking)torrents/verifying/
3. กำลังรับ-ส่งข้อมูลจริงstatus เป็น 3 (Downloading)torrents/downloading/
4. กำลังอัปโหลดข้อมูลstatus เป็น 4 (Seeding)torrents/seeding/
5. หยุดการส่งข้อมูลstatus เป็น 5 (Stopped)torrents/stopped/
6. ลบไฟล์ทอร์เรนต์status เป็น 6 (Removed)torrents/removed/
7. ไฟล์ทอร์เรนต์เสร็จสมบูรณ์percentDone เป็น 1.0torrents/completed/
8. ไฟล์ทอร์เรนต์กำลังตรวจสอบstatus เป็น 2 (Checking)torrents/checking/
9. ไฟล์ทอร์เรนต์กำลังตรวจสอบstatus เป็น 2 (Checking)torrents/checking/
10. ไฟล์ทอร์เรนต์กำลังตรวจสอบstatus เป็น 2 (Checking)torrents/checking/
11. ไฟล์ทอร์เรนต์กำลังตรวจสอบstatus เป็น 2 (Checking)torrents/checking/
12. ไฟล์ทอร์เรนต์กำลังตรวจสอบstatus เป็น 2 (Checking)torrents/checking/

การสร้างสคริปต์ Python

สร้างไฟล์ Python ใหม่ชื่อว่า torrent_sync.py และเพิ่มโค้ดดังนี้:

SQL

import transmissionrpc
import os

# กำหนด URL และพอร์ตของ Transmission RPC
rpc_url = 'http://localhost:9091/transmission/rpc'
client = transmissionrpc.Client(rpc_url)

# กำหนดโฟลเดอร์ที่ต้องการจัดการ
base_folder = '/path/to/torrents'

# ฟังก์ชันสำหรับจัดการสถานะไฟล์ทอร์เรนต์
def manage_torrents():
    torrents = client.get_torrents()
    for torrent in torrents:
        status = torrent.status
        error = torrent.error
        percentDone = torrent.percentDone

        # กำหนดโฟลเดอร์ปลายทางตามสถานะ
        if error != 0:
            destination_folder = os.path.join(base_folder, 'error')
        elif status == 'downloading':
            destination_folder = os.path.join(base_folder, 'downloading')
        elif status == 'seeding':
            destination_folder = os.path.join(base_folder, 'seeding')
        elif status == 'stopped':
            destination_folder = os.path.join(base_folder, 'stopped')
        elif status == 'removed':
            destination_folder = os.path.join(base_folder, 'removed')
        elif percentDone == 1.0:
            destination_folder = os.path.join(base_folder, 'completed')
        else:
            destination_folder = os.path.join(base_folder, 'checking')

        # ย้ายไฟล์ทอร์เรนต์ไปยังโฟลเดอร์ปลายทาง
        if not os.path.exists(destination_folder):
            os.makedirs(destination_folder)
        os.rename(torrent.name, os.path.join(destination_folder, torrent.name))

# เรียกฟังก์ชันจัดการไฟล์ทอร์เรนต์
manage_torrents()

การใช้ Vi บน QNAP

เพื่อใช้สคริปต์ Python บน QNAP ผ่าน Terminal ด้วยโปรแกรม Vi ให้ทำตามขั้นตอนดังนี้:

  1. เปิด Terminal บน QNAP
  2. สร้างไฟล์ใหม่โดยพิมพ์คำสั่ง: vi torrent_sync.py
  3. กด i เพื่อเข้าสู่โหมด Insert
  4. คัดลอกและวางโค้ด Python จากข้างบนลงมา
  5. กด Esc แล้วพิมพ์ :wq เพื่อบันทึกและออกจาก Vi
  6. ให้สิทธิ์การรันไฟล์: chmod +x torrent_sync.py
  7. รันไฟล์: ./torrent_sync.py

บทสรุป

บทความนี้ได้แนะนำวิธีการสร้างระบบ Automation สำหรับการจัดการไฟล์ทอร์เรนต์ใน 12 สเตจ โดยใช้ Python และ Vi บน QNAP ผ่านการเชื่อมต่อกับ Transmission RPC ตรวจสอบสถานะการทำงานจริง แล้วสั่งย้ายไฟล์ .torrent ข้ามโฟลเดอร์ในโมเดล 12-Stage Pipeline ตามสถานะที่กำหนด ทำให้การจัดการไฟล์ทอร์เรนต์มีความแม่นยำและอัตโนมัติมากขึ้น