Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload image from URL to pymeural #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions custom_components/meural/pymeural.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import asyncio
import logging
import json
import aiohttp
import async_timeout
import io
from PIL import Image
import os
from urllib.parse import urlparse
import base64

from typing import Dict
import aiohttp
import async_timeout

from aiohttp.client_exceptions import ClientResponseError

from homeassistant.exceptions import HomeAssistantError

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

BASE_URL = "https://api.meural.com/v0/"

Expand Down Expand Up @@ -128,6 +135,49 @@ async def sync_device(self, device_id):
async def get_item(self, item_id):
return await self.request("get", f"items/{item_id}")

async def upload_image_from_url(self, image_url):
"""
Upload an image from a URL to Meural.

:param image_url: The URL of the image to upload
:return: The ID of the uploaded item
"""
try:
# Download the image
async with self.session.get(image_url) as response:
if response.status != 200:
raise Exception(f"Failed to download image: HTTP {response.status}")
image_data = await response.read()

# Get the filename from the URL
parsed_url = urlparse(image_url)
filename = os.path.basename(parsed_url.path)

# Prepare the form data
data = aiohttp.FormData()
data.add_field('image',
image_data,
filename=filename,
content_type=response.headers.get('Content-Type', 'image/jpeg'))

# Prepare the URL and headers
url = f"{BASE_URL}items"
headers = {
"Authorization": f"Token {self.token}",
"x-meural-api-version": "3"
}

# Send the request using self.session
async with self.session.post(url, data=data, headers=headers) as resp:
resp.raise_for_status()
response_data = await resp.json()

item_id = response_data['data']['id']
return item_id

except Exception as e:
raise Exception(f"Failed to upload image: {str(e)}")

class LocalMeural:
def __init__(self, device, session: aiohttp.ClientSession):
self.ip = device["localIp"]
Expand Down Expand Up @@ -248,12 +298,12 @@ async def send_postcard(self, url, content_type):

return response

class CannotConnect(HomeAssistantError):
class CannotConnect():
"""Error to indicate we cannot connect."""


class InvalidAuth(HomeAssistantError):
class InvalidAuth():
"""Error to indicate there is invalid auth."""

class DeviceTurnedOff(HomeAssistantError):
"""Error to indicate device turned off or not connected to the network."""
class DeviceTurnedOff():
"""Error to indicate device turned off or not connected to the network."""