refactor(memobird): improve portability with dependency checks and error handling
This commit is contained in:
@@ -1,14 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
import base64
|
||||
from io import BytesIO
|
||||
from random import randint
|
||||
from PIL import Image as PILImg, ImageOps
|
||||
import argparse
|
||||
|
||||
# Dependency Check
|
||||
MISSING_DEPS = []
|
||||
try:
|
||||
import requests
|
||||
except ImportError:
|
||||
MISSING_DEPS.append("requests")
|
||||
try:
|
||||
from PIL import Image as PILImg, ImageOps
|
||||
except ImportError:
|
||||
MISSING_DEPS.append("Pillow (PIL)")
|
||||
|
||||
if MISSING_DEPS:
|
||||
print(f"Error: Missing dependencies: {', '.join(MISSING_DEPS)}")
|
||||
print("Please install them using: pip install " + " ".join(["requests", "Pillow"]))
|
||||
sys.exit(1)
|
||||
|
||||
# Default IP for Moritz's Memobird
|
||||
DEFAULT_HOST = "192.168.10.165"
|
||||
|
||||
@@ -48,6 +62,14 @@ class MemobirdSender:
|
||||
resp = requests.post(self.uri, json=payload, timeout=10)
|
||||
resp.raise_for_status()
|
||||
return resp.status_code
|
||||
except requests.exceptions.ConnectTimeout:
|
||||
print(f"Error: Connection to Memobird at {self.host} timed out.")
|
||||
print("Check if the printer is powered on and on the same network.")
|
||||
sys.exit(1)
|
||||
except requests.exceptions.ConnectionError:
|
||||
print(f"Error: Could not connect to Memobird at {self.host}.")
|
||||
print("Verify the IP address and network connectivity.")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error sending to Memobird: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user