From b6b14342a71d5293cab506195b89ba41abf088cf Mon Sep 17 00:00:00 2001 From: Quentin Duchemin Date: Sat, 5 Mar 2022 00:14:30 +0100 Subject: [PATCH] Sample usage, return album id at creation --- .github/workflows/pydocstyle.yml | 2 +- README.md | 39 ++++++++++++++++++++++++++++++++ docsource/conf.py | 28 +++++++++++------------ pychee/__init__.py | 2 +- pychee/pychee.py | 15 ++++++------ setup.py | 1 + 6 files changed, 64 insertions(+), 23 deletions(-) diff --git a/.github/workflows/pydocstyle.yml b/.github/workflows/pydocstyle.yml index 65fd36d..b0f2958 100644 --- a/.github/workflows/pydocstyle.yml +++ b/.github/workflows/pydocstyle.yml @@ -26,4 +26,4 @@ jobs: - name: Test NumpyDoc Style run: | cd pychee - pydocstyle --convention=numpy \ No newline at end of file + pydocstyle --convention=numpy diff --git a/README.md b/README.md index 7dc60f9..7bf3a3b 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,45 @@ The API is partially implemented and focused on photo management, _i.e._ only `A Disclaimer : I usually suck at coding, so use with caution and at your own risks. Tested with Lychee v4.3.4. The code probably won't be retrocompatible and should just work with the latest version. +## Example usage + +A sample of common API calls : + +```python +#!/usr/bin/env python +# coding=utf-8 +from pychee import pychee + +# Initialize instance +client = pychee.LycheeClient('https://pic.chosto.me') + +# Login +client.login('login', 'password') + +# Create a new album +album_name = 'test_name' +album_id = client.add_album(album_name) + +# Add a photo in the created album +path_to_your_photo = '/your/path/photo.jpg' +with open(path_to_your_photo, 'rb') as f: + photo_id = client.add_photo(f, 'photo.jpg', album_id) + +# Set uploaded photo public +client.set_photo_public(photo_id) + +# Set licence of uploaded photo +client.set_photo_license(photo_id, 'CC0') + +# Download an archive of the created album +output_path = '/tmp/photos.zip' +with(open(output_path, 'wb')) as f: + f.write(client.get_albums_archive([id])) + +# Logout +client.logout() +``` + ## Documentation Documentation is automatically published there : https://chostakovitch.github.io/pychee/index.html diff --git a/docsource/conf.py b/docsource/conf.py index 2221fe9..97b9a7c 100644 --- a/docsource/conf.py +++ b/docsource/conf.py @@ -1,19 +1,19 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html +""" +Configuration file for the Sphinx documentation builder. -# -- Path setup -------------------------------------------------------------- +This file only contains a selection of the most common options. For a full +list see the documentation: +https://www.sphinx-doc.org/en/master/usage/configuration.html -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -# import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) +-- Path setup -------------------------------------------------------------- +If extensions (or modules to document with autodoc) are in another directory, +add these directories to sys.path here. If the directory is relative to the +documentation root, use os.path.abspath to make it absolute, like shown here. +import os +import sys +sys.path.insert(0, os.path.abspath('.')) +""" # -- Project information ----------------------------------------------------- @@ -54,4 +54,4 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] \ No newline at end of file +html_static_path = ['_static'] diff --git a/pychee/__init__.py b/pychee/__init__.py index 44e05f1..4a365fa 100644 --- a/pychee/__init__.py +++ b/pychee/__init__.py @@ -4,4 +4,4 @@ # pychee: Client for Lychee, written in Python. For additonal information, visit: [Lychee](https://github.com/LycheeOrg/Lychee). -""" \ No newline at end of file +""" diff --git a/pychee/pychee.py b/pychee/pychee.py index 11454d0..3b89556 100644 --- a/pychee/pychee.py +++ b/pychee/pychee.py @@ -10,7 +10,7 @@ from typing import List from requests import Session -__version__ = '0.1.1' +__version__ = '0.1.2' class LycheeForbidden(Exception): """Raised when the Lychee request is unauthorized.""" @@ -117,14 +117,16 @@ def get_public_album(self, album_id: str, password: str = 'rand') -> bool: data = {'albumID': album_id, 'password': password} return 'true' in self._session.post('Album::getPublic', data=data).text - def add_album(self, title: str, parent_id: str = "0") -> bool: + def add_album(self, title: str, parent_id: str = "0") -> str: """ - Add a new Album with option parent. + Add a new Album with optional parent. API won't work with empty parent_id, use 0 as in webapp for no parent. + + Return the ID of the new image. """ data = {'title': title, 'parent_id': parent_id} - return 'true' in self._session.post('Album::add', data=data).text + return self._session.post('Album::add', data=data).text def set_albums_title(self, album_ids: List[str], title: str) -> bool: """Change the title of the albums.""" @@ -263,7 +265,7 @@ def set_photos_tags(self, photo_ids: List[str], tags: List[str]) -> bool: data = {'photoIDs': ','.join(photo_ids), 'tags': ','.join(tags)} return 'true' in self._session.post('Photo::setTags', data=data).text - def add_photo(self, photo: bytes, photo_name: str, album_id: str) -> int: + def add_photo(self, photo: bytes, photo_name: str, album_id: str) -> str: """ Upload a photo into an album. @@ -273,7 +275,7 @@ def add_photo(self, photo: bytes, photo_name: str, album_id: str) -> int: """ data = {'albumID': album_id} files = {'0': (photo_name, photo)} - return int(self._session.post('Photo::add', data=data, files=files).text) + return self._session.post('Photo::add', data=data, files=files).text def delete_photo(self, photo_ids: List[str]) -> bool: """Delete one or multiple photos.""" @@ -335,7 +337,6 @@ def share_with_users(self, user_ids: List[str], album_ids: List[str]) -> bool: data = { 'UserIDs': ','.join(user_ids), 'albumIDs': ','.join(album_ids)} - print(data) return 'true' in self._session.post('Sharing::Add', data=data).text def delete_shares(self, share_ids: List[str]) -> bool: diff --git a/setup.py b/setup.py index 8bf1ba9..77c5f1e 100644 --- a/setup.py +++ b/setup.py @@ -1,2 +1,3 @@ +"""Install pychee.""" from setuptools import setup setup()