Skip to content

Commit

Permalink
housekeeping
Browse files Browse the repository at this point in the history
- ruff formatter
- readme
- license
- pyproject updates
  • Loading branch information
mkornat committed Sep 4, 2024
1 parent e06a600 commit eb1977b
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 148 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Marcin Kornat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
https://docs.photoprism.dev/#/
# Photoprism Python API Client

## Installation

```bash
pip install photoprism
```

## Usage

```python
import asyncio
from pathlib import Path

from photoprism import PhotoprismSession, PhotoprismClient
from photoprism.models.query import Size


async def main():
session = PhotoprismSession(
username="username",
password="password",
host="localhost",
protocol="http",
)
client = PhotoprismClient(session)

albums = await client.albums.filter(count=3, q="cats")
for album in albums:
image_path = await client.albums.download_cover_image(
album_uid=album.uid,
size=Size.Tile50,
file_dir=Path("data/album_covers"),
)
print(image_path)


if __name__ == '__main__':
asyncio.run(main())

```
2 changes: 2 additions & 0 deletions photoprism/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from photoprism.session import PhotoprismSession
from photoprism.client import PhotoprismClient
12 changes: 12 additions & 0 deletions photoprism/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@
from photoprism.api.errors import PhotoprismErrorsApi
from photoprism.api.faces import PhotoprismFacesApi
from photoprism.api.library import PhotoprismLibraryApi


__all__ = [
"PhotoprismConfigApi",
"PhotoprismAlbumsApi",
"PhotoprismLabelsApi",
"PhotoprismPhotosApi",
"PhotoprismFilesApi",
"PhotoprismErrorsApi",
"PhotoprismFacesApi",
"PhotoprismLibraryApi",
]
31 changes: 11 additions & 20 deletions photoprism/api/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

class PhotoprismAlbumsApi(PhotoprismApiBase):
async def filter(
self,
count: int,
offset: int | None = None,
order: Literal["favorites", "name", "title", "added", "edited"] | None = None,
q: str | None = None,
self,
count: int,
offset: int | None = None,
order: Literal["favorites", "name", "title", "added", "edited"] | None = None,
q: str | None = None,
) -> list[search.Album]:
data = await self._session.req(
method="GET",
Expand All @@ -21,7 +21,7 @@ async def filter(
"offset": offset,
"order": order,
"q": q,
}
},
)
return [search.Album(**item) for item in data]

Expand All @@ -39,15 +39,11 @@ async def create(self, title: str, favorite: bool = False) -> entity.Album:
data={
"Title": title,
"Favorite": favorite,
}
},
)
return entity.Album(**data)

async def update(
self,
album_uid: str,
album_data: form.Album
) -> entity.Album:
async def update(self, album_uid: str, album_data: form.Album) -> entity.Album:
data = await self._session.req(
method="PUT",
path="albums/{}".format(album_uid),
Expand All @@ -72,13 +68,9 @@ async def batch_delete(self, album_uids: list[str]) -> None:
)

async def clone(
self,
source_album_uids: list[str],
destination_album_uid: str
self, source_album_uids: list[str], destination_album_uid: str
) -> entity.Album:
selection_data = form.Selection(
albums=source_album_uids
)
selection_data = form.Selection(albums=source_album_uids)
data = await self._session.req(
method="POST",
path="albums/{}/clone".format(destination_album_uid),
Expand Down Expand Up @@ -170,7 +162,7 @@ async def add_photos(
albums=albums,
labels=labels,
places=places,
subjects=subjects
subjects=subjects,
)
data = await self._session.req(
method="POST",
Expand Down Expand Up @@ -224,4 +216,3 @@ async def download_cover_image(
filename=filename,
mode="DOWNLOAD",
)

10 changes: 2 additions & 8 deletions photoprism/api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ async def get_by_uid(self, file_hash: str) -> entity.File:
return entity.File(**data)

async def download(
self,
file_hash: str,
file_dir: Path,
filename: str | None = None
self, file_hash: str, file_dir: Path, filename: str | None = None
) -> Path:
return await self._session.req(
method="GET",
Expand Down Expand Up @@ -68,10 +65,7 @@ async def delete(self, photo_uid: str, file_uid: str) -> entity.Photo:
return entity.Photo(**data)

async def update_orientation(
self,
photo_uid: str,
file_uid: str,
orientation: int
self, photo_uid: str, file_uid: str, orientation: int
) -> entity.Photo:
orientation_data = form.File(orientation=orientation)
data = await self._session.req(
Expand Down
5 changes: 1 addition & 4 deletions photoprism/api/labels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from pprint import pprint

from photoprism.api.base import PhotoprismApiBase
from photoprism.models import form, search, entity, query
Expand All @@ -8,9 +7,7 @@
class PhotoprismLabelsApi(PhotoprismApiBase):
async def search(self, search_labels: form.SearchLabels) -> list[search.Label]:
data = await self._session.req(
method='GET',
path='labels',
params=search_labels.model_dump()
method="GET", path="labels", params=search_labels.model_dump()
)
return [search.Label(**item) for item in data]

Expand Down
7 changes: 1 addition & 6 deletions photoprism/api/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ async def update(self, photo_uid: str, photo_data: form.Photo) -> entity.Photo:
return entity.Photo(**data)

async def download(
self,
photo_uid: str,
file_dir: Path,
filename: str | None = None
self, photo_uid: str, file_dir: Path, filename: str | None = None
) -> Path:
return await self._session.req(
method="GET",
Expand Down Expand Up @@ -180,5 +177,3 @@ async def batch_archive(self, selection: form.Selection) -> None:
path="batch/photos/archive",
data=selection.model_dump(exclude_none=True),
)


2 changes: 1 addition & 1 deletion photoprism/models/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Memory(BaseModel):

cores: int
memory: Memory
routines: int
routines: int
4 changes: 3 additions & 1 deletion photoprism/models/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ class Photo(BaseModel):
title: str | None = Field(serialization_alias="Title", default=None)
title_src: str | None = Field(serialization_alias="TitleSrc", default=None)
description: str | None = Field(serialization_alias="Description", default=None)
description_src: str | None = Field(serialization_alias="DescriptionSrc", default=None)
description_src: str | None = Field(
serialization_alias="DescriptionSrc", default=None
)
details: Details | None = Field(serialization_alias="Details", default=None)
stack: int | None = Field(serialization_alias="Stack", default=None)
favorite: bool | None = Field(serialization_alias="Favorite", default=None)
Expand Down
19 changes: 6 additions & 13 deletions photoprism/session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import asyncio
from enum import StrEnum
from pathlib import Path
from typing import Any, Literal, overload, reveal_type, Mapping
from typing import Any, Literal, overload, Mapping

import aiohttp
from aiohttp import ClientTimeout, ClientResponse
from aiohttp import ClientTimeout
from yarl import URL

from photoprism.exceptions import (
Expand Down Expand Up @@ -59,8 +58,7 @@ async def req(
filename: None = None,
timeout: float | None = None,
include_auth_token: bool = True,
) -> dict[str, Any] | list[dict[str, Any]]:
...
) -> dict[str, Any] | list[dict[str, Any]]: ...

@overload
async def req(
Expand All @@ -75,8 +73,7 @@ async def req(
method: Literal["GET", "POST", "PUT", "DELETE"] = "GET",
timeout: float | None = None,
include_auth_token: bool = True,
) -> Path:
...
) -> Path: ...

async def req(
self,
Expand Down Expand Up @@ -106,9 +103,7 @@ async def req(
params["t"] = await self.get_download_token()
elif mode == "PREVIEW":
params["t"] = await self.get_preview_token()
cleaned_params = {
p_k: p_v for p_k, p_v in params.items() if p_v is not None
}
cleaned_params = {p_k: p_v for p_k, p_v in params.items() if p_v is not None}
url = URL(self._url).join(URL(path)).update_query(cleaned_params)
try:
async with self._session.request(
Expand Down Expand Up @@ -149,9 +144,7 @@ async def req(
raise PhotoprismError(exc) from exc

def determine_filename(self, headers: Mapping[str, str]) -> str:
header_filename = (
headers["Content-Disposition"].split("; ")[1].split("=")[1]
)
header_filename = headers["Content-Disposition"].split("; ")[1].split("=")[1]
# Sometimes the filename in the header is enclosed, sometimes it isn't.
# This is to account for that.
if header_filename[0] == '"' and header_filename[-1:] == '"':
Expand Down
Loading

0 comments on commit eb1977b

Please sign in to comment.