نص برمجي بلغة Python لتصدير الصور من Zendesk Community

هل تحتاج إلى ترحيل الصور من مجتمع Zendesk إلى Discourse؟ إليك برنامج Python نصي!

مرحباً بالجميع،

لقد كنت أعمل على ترحيل مجتمع Zendesk إلى Discourse وواجهت مشكلة محبطة: تصدير الصور من Zendesk. المشكلة؟ يبدأ شبكة توصيل المحتوى (CDN) الخاصة بـ Zendesk في حظر الوصول بعد استرداد عدد قليل من الصور مباشرة، مما يجعل التنزيل المجمع تحديًا حقيقيًا.

بعد تجربة عدة طرق، انتهى بي الأمر بإنشاء برنامج Python نصي (بمساعدة الذكاء الاصطناعي) يتجاوز هذا القيد. يستخدم البرنامج النصي Selenium لفتح كل عنوان URL للصورة في المتصفح، والتقاط لقطة شاشة للصورة، وحفظها محليًا. إنها ليست بنفس نظافة تنزيل الصور مباشرة، لكنها تعمل بشكل موثوق، وتأتي الصور بجودة عالية.

إذا كنت تتعامل مع ترحيل مماثل، آمل أن يساعدك هذا!


ما ستحتاجه

  1. Python: مثبت وجاهز للاستخدام.
  2. ChromeDriver:
    قم بتنزيله من Chrome for Testing، وفك ضغطه، وقم بتحديث البرنامج النصي بالمسار إلى برنامج التشغيل الخاص بك.
  3. ملف CSV:
    • قم بإنشاء ملف CSV بعمود واحد يسمى URL.
    • املأه بعناوين URL المباشرة للصور التي تريد تصديرها من Zendesk.
    • قم بتحديث البرنامج النصي بالمسار إلى هذا الملف.
  4. موقع حفظ:
    قم بتحديث البرنامج النصي بمسار المجلد حيث تريد حفظ الصور.

أخيرًا، ستحتاج إلى تثبيت بعض مكتبات Python:

pip install selenium pillow

البرنامج النصي

إليك برنامج Python النصي. لا تتردد في تعديله ليناسب إعداداتك:

import csv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from PIL import Image
import io
import re
import os

# Function to extract the image ID from the URL
def extract_image_id(url):
    match = re.search(r'/([^/]+)\.(png|jpg|jpeg|gif)$', url, re.IGNORECASE)
    if match:
        return match.group(1)
    return 'image'

# Function to download and save an image from a URL
def download_image(driver, image_url, download_folder):
    driver.get(image_url)
    
    # Wait for the image to load
    wait = WebDriverWait(driver, 20)
    img_element = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'img')))

    # Get the image element's location and size
    location = img_element.location
    size = img_element.size

    # Take a screenshot of the entire page
    screenshot = driver.get_screenshot_as_png()
    
    # Convert screenshot to PIL Image
    screenshot_image = Image.open(io.BytesIO(screenshot))

    # Define the bounding box for the image (left, top, right, bottom)
    left = location['x']
    top = location['y']
    right = left + size['width']
    bottom = top + size['height']
    bbox = (left, top, right, bottom)

    # Crop the image to the bounding box
    cropped_image = screenshot_image.crop(bbox)

    # Extract the image ID from the URL
    image_id = extract_image_id(image_url)

    # Save the cropped image with the image ID as the filename in the download folder
    cropped_image.save(os.path.join(download_folder, f'{image_id}.png'))

# Function to load URLs from a CSV file
def load_urls_from_csv(csv_file):
    urls = []
    with open(csv_file, mode='r', newline='', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        for row in reader:
            urls.append(row['URL'])  # Assuming the CSV has 'id' and 'url' columns
    return urls

# Set up ChromeDriver service
service = Service("C:\\\\Users\\\\tslam\\\\Downloads\\\\chromedriver-win64\\\\chromedriver-win64\\\\chromedriver.exe")
driver = webdriver.Chrome(service=service)

try:

    # Maximize browser window to full screen
    driver.maximize_window()
    
    # Load URLs from the CSV file
    csv_file = 'C:\\\\Users\\\\tslam\\\\Zendesk Migration\\\\image_urls.csv'
    image_urls = load_urls_from_csv(csv_file)
    
    # Define the download folder path
    download_folder = 'C:\\\\Users\\\\tslam\\\\Zendesk Migration\\\\downloads'
    
    # Ensure the download folder exists
    if not os.path.exists(download_folder):
        os.makedirs(download_folder)

    # Process each image URL
    for url in image_urls:
        download_image(driver, url, download_folder)

finally:
    driver.quit()

6 إعجابات

رائع! يمكن استخدام هذا حتى لعمليات ترحيل أخرى تواجه نفس المشكلة، وليس فقط Zendesk. شكراً جزيلاً على المشاركة.

3 إعجابات