|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | +from asposepdfcloud import ApiClient, PdfApi |
| 7 | + |
| 8 | +# Configure logging |
| 9 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 10 | + |
| 11 | +class CompareMain: |
| 12 | + def __init__(self, credentials_file: Path): |
| 13 | + self.pdf_api = None |
| 14 | + self._init_api(credentials_file) |
| 15 | + |
| 16 | + def _init_api(self, credentials_file: Path): |
| 17 | + """Initialize the API client.""" |
| 18 | + try: |
| 19 | + with credentials_file.open("r", encoding="utf-8") as file: |
| 20 | + credentials = json.load(file) |
| 21 | + api_key, app_id = credentials.get("key"), credentials.get("id") |
| 22 | + if not api_key or not app_id: |
| 23 | + raise ValueError("Error: Missing API keys in the credentials file.") |
| 24 | + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) |
| 25 | + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: |
| 26 | + logging.error(f"Failed to load credentials: {e}") |
| 27 | + |
| 28 | + def upload_file(self, fileName: str, localFolder: Path): |
| 29 | + """ Upload a local fileName to the Aspose Cloud server. """ |
| 30 | + if self.pdf_api: |
| 31 | + file_path = localFolder / fileName |
| 32 | + try: |
| 33 | + self.pdf_api.upload_file(fileName, str(file_path)) |
| 34 | + logging.info(f"upload_file(): File '{fileName}' uploaded successfully.") |
| 35 | + except Exception as e: |
| 36 | + logging.error(f"upload_document(): Failed to upload file: {e}") |
| 37 | + |
| 38 | + def download_result(self, document: str, outputDocument: str, localFolder: Path): |
| 39 | + """Download the processed PDF document from the Aspose Cloud server.""" |
| 40 | + if self.pdf_api: |
| 41 | + try: |
| 42 | + temp_file = self.pdf_api.download_file(document) |
| 43 | + local_path = localFolder / outputDocument |
| 44 | + shutil.move(temp_file, str(local_path)) |
| 45 | + logging.info(f"download_result(): File successfully downloaded: {local_path}") |
| 46 | + except Exception as e: |
| 47 | + logging.error(f"download_result(): Failed to download file: {e}") |
0 commit comments