class RemarkablePDFUploader_v1
A standalone PDF uploader class that manages uploading PDF documents to reMarkable cloud storage using authenticated sessions and temporary database storage.
/tf/active/vicechatdev/e-ink-llm/cloudtest/upload_pdf.py
19 - 117
moderate
Purpose
This class provides a high-level interface for uploading PDF files to reMarkable devices/cloud. It handles authentication, temporary database creation for upload sessions, folder listing, and PDF document uploads. It wraps the RemarkableUploadManager with a simplified API and manages the lifecycle of temporary resources needed for uploads.
Source Code
class RemarkablePDFUploader:
"""Standalone PDF uploader using proven upload_manager logic"""
def __init__(self):
# Create a temporary database for this upload session
self.temp_dir = tempfile.mkdtemp(prefix="remarkable_upload_")
temp_db = os.path.join(self.temp_dir, "temp_upload.db")
print(f"๐ง Initializing upload manager with temp DB: {temp_db}")
# Import and create session (following upload_manager pattern)
from cloudtest.auth import RemarkableAuth
# Create authenticated session
auth = RemarkableAuth()
session = auth.get_authenticated_session()
if not session:
raise Exception("Authentication failed")
# Initialize the proven upload manager
self.upload_manager = RemarkableUploadManager(session, temp_db)
def list_folders(self):
"""List available folders for upload target selection"""
try:
# Get the folder structure from the upload manager
folders = {}
replica = self.upload_manager.get_replica()
for uuid, doc in replica.items():
if doc.get('Type') == 'CollectionType':
name = doc.get('VissibleName', 'Unnamed Folder')
folders[uuid] = name
return folders
except Exception as e:
print(f"โ ๏ธ Error listing folders: {e}")
return {}
def upload_pdf(self, pdf_path, document_name, parent_uuid=None):
"""
Upload a PDF using the proven upload_manager logic
Args:
pdf_path: Path to the PDF file
document_name: Name for the document on reMarkable
parent_uuid: UUID of parent folder (None for root)
Returns:
bool: True if upload successful, False otherwise
"""
try:
pdf_file = Path(pdf_path)
if not pdf_file.exists():
print(f"โ PDF file not found: {pdf_path}")
return False
if not pdf_file.suffix.lower() == '.pdf':
print(f"โ File is not a PDF: {pdf_path}")
return False
print(f"๐ Uploading: {pdf_file.name}")
print(f"๐ Document name: {document_name}")
if parent_uuid:
print(f"๐ Target folder: {parent_uuid}")
else:
print("๐ Target: Root folder")
# Use the proven upload manager to handle the upload
print(f"\n๐ง Using proven upload_manager.py for upload...")
# Call the working upload method
success = self.upload_manager.upload_pdf_document(
pdf_path=str(pdf_file),
name=document_name,
parent_uuid=parent_uuid
)
if success:
print("โ
Upload successful!")
return True
else:
print("โ Upload failed!")
return False
except Exception as e:
print(f"โ Upload error: {e}")
return False
def cleanup(self):
"""Clean up temporary resources"""
try:
import shutil
if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
shutil.rmtree(self.temp_dir)
print(f"๐งน Cleaned up temp directory: {self.temp_dir}")
except Exception as e:
print(f"โ ๏ธ Cleanup warning: {e}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
__init__: No parameters required. The constructor automatically creates a temporary directory and database, authenticates with reMarkable services, and initializes the upload manager.
Return Value
Instantiation returns a RemarkablePDFUploader object. The upload_pdf method returns a boolean (True for successful upload, False otherwise). The list_folders method returns a dictionary mapping folder UUIDs to folder names. The cleanup method has no return value.
Class Interface
Methods
__init__(self)
Purpose: Initialize the uploader by creating temporary storage, authenticating with reMarkable services, and setting up the upload manager
Returns: None (constructor)
list_folders(self) -> dict
Purpose: Retrieve a list of available folders from the reMarkable cloud for selecting upload targets
Returns: Dictionary mapping folder UUIDs (str) to folder names (str). Returns empty dict on error.
upload_pdf(self, pdf_path: str, document_name: str, parent_uuid: str = None) -> bool
Purpose: Upload a PDF file to reMarkable cloud storage with specified name and optional parent folder
Parameters:
pdf_path: Path to the PDF file to upload (string or Path object)document_name: Name to assign to the document on reMarkable deviceparent_uuid: UUID of the parent folder (None for root folder, or valid folder UUID string)
Returns: Boolean: True if upload was successful, False if upload failed or file validation failed
cleanup(self)
Purpose: Remove temporary directories and databases created during the upload session
Returns: None
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
temp_dir |
str | Path to the temporary directory created for this upload session, used to store temporary database | instance |
upload_manager |
RemarkableUploadManager | Instance of RemarkableUploadManager that handles the actual upload operations and cloud synchronization | instance |
Dependencies
tempfileospathlibshutilcloudtest.authcloudtest.upload_manager
Required Imports
import sys
import os
import tempfile
from pathlib import Path
from cloudtest.upload_manager import RemarkableUploadManager
Conditional/Optional Imports
These imports are only needed under specific conditions:
from cloudtest.auth import RemarkableAuth
Condition: imported in __init__ method during instantiation
Required (conditional)import shutil
Condition: imported in cleanup method when cleaning temporary resources
Required (conditional)Usage Example
# Create uploader instance
uploader = RemarkablePDFUploader()
# List available folders
folders = uploader.list_folders()
for uuid, name in folders.items():
print(f'{name}: {uuid}')
# Upload PDF to root folder
success = uploader.upload_pdf(
pdf_path='/path/to/document.pdf',
document_name='My Document',
parent_uuid=None
)
# Upload PDF to specific folder
success = uploader.upload_pdf(
pdf_path='/path/to/document.pdf',
document_name='My Document',
parent_uuid='folder-uuid-here'
)
# Clean up temporary resources
uploader.cleanup()
Best Practices
- Always call cleanup() method after finishing uploads to remove temporary directories and databases
- Check that PDF files exist and have .pdf extension before calling upload_pdf
- Handle authentication failures during instantiation with try-except blocks
- Use list_folders() to get valid parent_uuid values before uploading to specific folders
- The class creates temporary resources in __init__, so instantiate only when needed
- Each instance creates its own temporary database, so reuse the same instance for multiple uploads in a session
- The upload_pdf method returns boolean for success/failure - always check the return value
- Parent_uuid=None uploads to root folder, use a valid UUID to upload to a specific folder
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class RemarkablePDFUploader 94.1% similar
-
class RemarkableUploadManager_v1 78.1% similar
-
class SimplePDFUploadTest 77.6% similar
-
class RemarkableUploadManager 77.5% similar
-
function main_v15 70.5% similar