Skip to content

Commit

Permalink
Merge pull request #5 from engineerjoe440/add-albums-tree-method
Browse files Browse the repository at this point in the history
Add albums tree method
  • Loading branch information
Chostakovitch authored Jun 15, 2023
2 parents 5db862a + 8eddb4f commit 2e7276f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.linting.pylintEnabled": true,
"python.linting.pydocstyleEnabled": true,
"python.linting.enabled": true
}
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

Client for [Lychee](https://github.com/LycheeOrg/Lychee), written in Python.

Lychee is a photo management system I've been using for years. I had the idea to make a « Lychee filesystem » with [FUSE](https://fr.wikipedia.org/wiki/Filesystem_in_Userspace), so I needed an API client.
Lychee is a photo management system I've been using for years. I had the idea to
make a « Lychee filesystem » with
[FUSE](https://fr.wikipedia.org/wiki/Filesystem_in_Userspace), so I needed an
API client.

## Installation

Expand All @@ -13,18 +16,24 @@ python3 -m pip install pychee
## Notes

My reference is [this documentation](https://lycheeorg.github.io/docs/api.html).
The API is partially implemented and focused on photo management, _i.e._ only `Albums`, `Photo`, `Frame`, `Sharing` and `Settings::setLogin`. Users can do whatever they want with their albums and photos and change their password.
The API is partially implemented and focused on photo management, _i.e._ only
`Albums`, `Photo`, `Frame`, `Sharing` and `Settings::setLogin`. Users can do
whatever they want with their albums and photos and change their password.

Disclaimer : I usually suck at coding, so use with caution and at your own risks.
Tested with Lychee v4.7.0. The code probably won't be retrocompatible and should just work with the latest version.
Tested with Lychee v4.7.0. The code probably won't be retrocompatible and should
just work with the latest version.

## TODO

Add tests

## Example usage

> ⚠️ `pychee` returns exactly what the API sends back, just parsing it to `dict` if necessary. As sample answers are not documented in the API and because answers tend to change over time, you should inspect requests in browser if your client code does not works anymore.
> ⚠️ `pychee` returns exactly what the API sends back, just parsing it to `dict`
if necessary. As sample answers are not documented in the API and because answers
tend to change over time, you should inspect requests in browser if your client
code does not works anymore.

A sample of common API calls :

Expand Down Expand Up @@ -65,4 +74,5 @@ client.logout()

## Documentation

Documentation is automatically published there : https://chostakovitch.github.io/pychee/index.html
Documentation is automatically published on GitHub:
https://chostakovitch.github.io/pychee/index.html
26 changes: 19 additions & 7 deletions pychee/pychee.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
"""
# pychee: Client for Lychee, written in Python.
For additonal information, visit: https://github.com/LycheeOrg/Lychee.
For additional information, visit: https://github.com/LycheeOrg/Lychee.
"""
from posixpath import join
from typing import List
from requests import Session
from urllib.parse import unquote

__version__ = '0.2.1'
from requests import Session

__version__ = '0.2.2'

class LycheeForbidden(Exception):
"""Raised when the Lychee request is unauthorized."""

class LycheeNotFound(Exception):
"""Raised when the requested ressource was not found."""
"""Raised when the requested resource was not found."""

class LycheeError(Exception):
"""Raised for general Lychee errors."""
Expand Down Expand Up @@ -54,7 +55,7 @@ def __init__(self, prefix_url: str, *args, **kwargs):
# Initial CSRF
super().request('GET', self._prefix_url)
self._set_csrf_header()
# Lychee now explicitely requires client to accept JSON,
# Lychee now explicitly requires client to accept JSON,
# else throws exception
self.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'

Expand All @@ -74,7 +75,7 @@ def request(self, method, url, *args, **kwargs):

def _set_csrf_header(self) -> None:
"""
Sets CSRF header from cookie for the whole session.
Set CSRF header from cookie for the whole session.
CSRF generally prevents an attacker from forging a request
sent from another website, e.g. in a JS script, by forcing
Expand All @@ -86,7 +87,9 @@ def _set_csrf_header(self) -> None:
csrf_token = self.cookies.get(self._CSRF_COOKIE)
if csrf_token is not None:
if csrf_token != self.headers.get(self._CSRF_HEADER):
self.headers[self._CSRF_HEADER] = unquote(csrf_token).replace('=', '')
self.headers[self._CSRF_HEADER] = unquote(
csrf_token
).replace('=', '')

class LycheeClient:
"""
Expand Down Expand Up @@ -120,6 +123,15 @@ def get_albums(self) -> dict:
"""
return self._session.post('Albums::get', json={}).json()

def get_albums_tree(self):
"""
Get List of Album Trees in Lychee.
Returns a list of albums dictionaries or an informative message on
failure.
"""
return self._session.post('Albums::tree', json={}).json()

def get_albums_position_data(self) -> dict:
"""
Get List of Available Album Data.
Expand Down
36 changes: 36 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,39 @@ requires = [
"wheel"
]
build-backend = "setuptools.build_meta"

[project]
name = "pychee"
description = "Client for Lychee, written in Python."
readme = "README.md"
dynamic = ["version"]
license = {file = "LICENSE"}
maintainers = [
{name = "Quentin Duchemin", email = "[email protected]"}
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Operating System :: OS Independent",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries"
]
requires-python = ">=3.7"

[project.optional-dependencies]
dev = [
"pydocstyle",
"pylint",
]
test = [
"pytest",
]

dependencies = [
"requests"
]

[project.urls]
Home = "https://github.com/Chostakovitch/pychee"
Repository = "https://github.com/Chostakovitch/pychee"
Issues = "https://github.com/Chostakovitch/pychee/issues"
Documentation = "https://chostakovitch.github.io/pychee/index.html"
16 changes: 0 additions & 16 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
[metadata]
name = pychee
version = attr: pychee.pychee.__version__
author = Quentin Duchemin
author_email = [email protected]
description = Client for Lychee, written in Python.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/Chostakovitch/pychee
keywords = lychee
license = AGPL-3.0
classifiers =
License :: OSI Approved :: GNU Affero General Public License v3
Programming Language :: Python :: 3
Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries

[options]
zip_safe = False
include_package_data = True
Expand Down

0 comments on commit 2e7276f

Please sign in to comment.