-
Notifications
You must be signed in to change notification settings - Fork 1
/
pychee.py
424 lines (340 loc) · 15 KB
/
pychee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python
# coding=utf-8
"""
# pychee: Client for Lychee, written in Python.
For additional information, visit: https://github.com/LycheeOrg/Lychee.
"""
from posixpath import join
from typing import List
from urllib.parse import unquote
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 resource was not found."""
class LycheeError(Exception):
"""Raised for general Lychee errors."""
#FIXME add error code handling
#FIXME adjust to API sending JSON because we changed Accept
#FIXME fix type hints...
class LycheeAPISession(Session):
"""
Lychee API Session Handler.
Wrapper around Session to set base API URL and throw exception if request
needs auth and user is not logged in.
"""
FORBID_MESSAGES = [
'"Warning: Album private!"',
'"Error: validation failed"'
]
NOT_FOUND_MESSAGES = [
'"Error: no pictures found!"'
]
# CSRF-related field names
_CSRF_HEADER = "X-XSRF-TOKEN"
_CSRF_COOKIE = "XSRF-TOKEN"
BASE_API_FRAGMENT = "api"
def __init__(self, prefix_url: str, *args, **kwargs):
"""Initialize the `requests.session`."""
super().__init__(*args, **kwargs)
self._prefix_url = prefix_url
# Initial CSRF
super().request('GET', self._prefix_url)
self._set_csrf_header()
# Lychee now explicitly requires client to accept JSON,
# else throws exception
self.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
def request(self, method, url, *args, **kwargs):
"""Make an HTTP request with the configured session."""
url = join(self._prefix_url, self.BASE_API_FRAGMENT, url)
response = super().request(method, url, *args, **kwargs)
self._set_csrf_header()
# Update CSRF header if changed
if response.text in self.FORBID_MESSAGES:
raise LycheeForbidden(response.text)
if response.text in self.NOT_FOUND_MESSAGES:
raise LycheeNotFound(response.text)
if response.text == 'false' or response.text is None:
raise LycheeError('Could be unauthorized, wrong args, who knows?')
return response
def _set_csrf_header(self) -> None:
"""
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
requests to contain a specific value which has been set
as a cookie in a previous GET request.
Thus, a previous GET request is needed so this method works.
"""
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('=', '')
class LycheeClient:
"""
Lychee API Client.
The primary [Lychee API](https://lycheeorg.github.io/docs/api.html) client
to interact with the specified Lychee server.
"""
def __init__(self, url: str):
"""Initialize a new Lychee session for given URL."""
self._session = LycheeAPISession(url)
self._session.post('Session::init', json={})
def login(self, username: str, password: str) -> None:
"""Log in to Lychee server."""
auth = {'username': username, 'password': password}
# Session takes care of setting cookies
login_response = self._session.post('Session::login', json=auth)
def logout(self):
"""Log out from Lychee server."""
self._session.post('Session::logout', json={})
self._session.cookies.clear()
def get_albums(self) -> dict:
"""
Get List of Available Albums in Lychee.
Returns an array of albums or false on failure.
"""
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.
Returns the album with only map related data.
"""
return self._session.post('Albums::getPositionData', json={}).json()
def get_album(self, album_id: str) -> dict:
"""
Get a Specific Album's Information.
Provided an albumID, returns the album.
"""
data = {'albumID': album_id}
return self._session.post('Album::get', json=data).json()
def get_public_album(self, album_id: str, password: str = 'rand'):
"""
Get Public Album Information.
Provided the albumID and passwords, return whether the album can be
accessed or not. The API won't work if password if empty, even if no
password.
"""
data = {'albumID': album_id, 'password': password}
self._session.post('Album::getPublic', json=data)
def add_album(self, title: str, parent_id: str = None) -> str:
"""
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 self._session.post('Album::add', json=data).json()
def set_albums_title(self, album_ids: List[str], title: str):
"""Change the title of the albums."""
data = {'albumIDs': ','.join(album_ids), 'title': title}
self._session.post('Album::setTitle', json=data)
def set_album_description(self, album_id: str, description: str):
"""Change the description of the album."""
data = {'albumID': album_id, 'description': description}
self._session.post('Album::setDescription', json=data)
def set_album_public(
self,
album_id: str,
public: int,
visible: int,
nsfw: int,
downloadable: int,
share_button_visible: int,
full_photo: int,
password: str = ""
):
"""
Change the sharing properties of the album.
Contrary to getPublic API call, password can be empty (obv).
"""
data = {
'albumID': album_id,
'public': public,
'visible': visible,
'nsfw': nsfw,
'downloadable': downloadable,
'share_button_visible': share_button_visible,
'full_photo': full_photo,
'password': password
}
self._session.post('Album::setPublic', json=data)
def delete_album(self, album_id: List[str]):
"""Delete the albums and all pictures in the album."""
data = {'albumIDs': album_id}
self._session.post('Album::delete', json=data)
def merge_albums(self, dest_id: str, source_ids: List[str]):
"""
Merge albums into one.
Seems that if destination album is one of the sources,
it will be deleted. Don't do this.
"""
data = {'albumIDs': dest_id + ',' + ','.join(source_ids)}
self._session.post('Album::merge', json=data)
def move_albums(self, dest_id: str, source_ids: List[str]):
"""Move albums into another one, which becomes their parent."""
data = {'albumIDs': dest_id + ',' + ','.join(source_ids)}
self._session.post('Album::move', json=data)
def set_album_license(self, album_id: str, license: str):
"""
Set the license of an album.
See https://github.com/LycheeOrg/Lychee/blob/master/app/Assets/Helpers.php
for authorized licenses (search get_all_licenses())
Returns false if license name is unrecognized.
"""
data = {'albumID': album_id, 'license': license}
self._session.post('Album::setLicense', json=data)
def get_albums_archive(self, album_ids: List[str]) -> bytes:
"""
Get a ZIP file of the pictures of the albums and their subalbums.
Archive is returned as bytes, you can open a file
with wb mode and write a file.
"""
data = {'albumIDs': ','.join(album_ids)}
# For large archives, maybe we would use
# stream=True and iterate over chunks of answer.
return self._session.get('Album::getArchive', params=data).content
def get_frame_settings(self) -> dict:
"""
Get frame mode settings.
For now, the only setting returns the refresh time, in milliseconds.
"""
return self._session.post('Frame::getSettings', json={}).json()
def get_photo(self, photo_id) -> dict:
"""Get information about a photo."""
data = {'photoID': photo_id}
return self._session.post('Photo::get', json=data).json()
def get_random_photo(self) -> bytes:
"""Get a random photo with current auth."""
return self._session.post('Photo::getRandom', json={}).content
def set_photos_title(self, photo_ids: List[str], title: str):
"""Set the title of one or multiple photos."""
data = {'photoIDs': ','.join(photo_ids), 'title': title}
self._session.post('Photo::setTitle', json=data)
def set_photo_description(self, photo_id: str, description: str):
"""Set the description of one or multiple photos."""
data = {'photoID': photo_id, 'description': description}
self._session.post('Photo::setDescription', json=data)
def set_photos_star(self, photo_ids: List[str]):
"""
Toggle the favorite status of one or multiple photos.
A starred photo will be unstarred, and vice versa.
"""
data = {'photoIDs': ','.join(photo_ids)}
self._session.post('Photo::setStar', json=data)
def set_photo_public(self, photo_id: str):
"""
Toggle the public status of a photo.
A public photo will be unstarred, and vice versa.
"""
data = {'photoID': photo_id}
self._session.post('Photo::setPublic', json=data)
def set_photos_album(self, photo_ids: List[str], album_id: str):
"""Put one or multiple photos into an album."""
data = {'photoIDs': ','.join(photo_ids), 'albumID': album_id}
self._session.post('Photo::setAlbum', json=data)
def set_photos_tags(self, photo_ids: List[str], tags: List[str]):
"""Set tags for one or multiple photos."""
data = {'photoIDs': ','.join(photo_ids), 'tags': ','.join(tags)}
self._session.post('Photo::setTags', json=data)
def add_photo(self, photo: bytes, photo_name: str, album_id: str) -> str:
"""
Upload a photo into an album.
photo should be open('/your/photo', 'rb').read().
Return the ID of the uploaded image.
"""
data = {'albumID': album_id}
# Lychee expects a multipart/form-data with a field called name and being `file`,
# which contradicts with API doc for now
# See syntax there : https://stackoverflow.com/a/12385661
files = {'file': (photo_name, photo)}
return self._session.post('Photo::add', data=data, files=files).json()
def delete_photo(self, photo_ids: List[str]):
"""Delete one or multiple photos."""
data = {'photoIDs': ','.join(photo_ids)}
self._session.post('Photo::delete', json=data)
def duplicate_photos(self, photo_ids: List[str], album_id: str):
"""Duplicate one or multiple photos into an album."""
data = {'photoIDs': ','.join(photo_ids), 'albumID': album_id}
self._session.post('Photo::duplicate', json=data)
def set_photo_license(self, photo_id: str, license: str):
"""
Set the license of a photo.
See https://github.com/LycheeOrg/Lychee/blob/master/app/Assets/Helpers.php
for authorized licenses (search get_all_licenses())
Returns false if license name is unrecognized.
"""
data = {'photoID': photo_id, 'license': license}
self._session.post('Photo::setLicense', json=data)
def get_photos_archive(self, photo_ids: List[str], kind: str) -> bytes:
"""
Get a photo or an archive of photos.
Kind is one of the following : FULL, LIVEPHOTOVIDEO, MEDIUM2X, MEDIUM, SMALL2X, SMALL, THUMB2X, THUMB.
If len(photo_ids) == 1, returns an image. Otherwise returns a ZIP archive.
Content is returned as bytes, you can open a file
with wb mode and write a file.
"""
data = {'photoIDs': ','.join(photo_ids), 'kind': kind}
return self._session.get('Photo::getArchive', params=data).content
def clear_photos_symlink(self):
"""
Remove all photo's symlinks, if activated.
Symlinks are disabled by default and allow to create expirable links to photos, preventing to guess the URL of the full sized photo.
See [the documentation](https://lycheeorg.github.io/docs/settings.html#symbolic-link).
"""
self._session.get('Photo::clearSymLink')
def shared_albums(self) -> dict:
"""Get list of shared album."""
return self._session.post('Sharing::List', json={}).json()
def shared_users(self, album_ids: List[str]) -> dict:
"""Get users with whom one or several albums are shared."""
data = {'albumIDs': ','.join(album_ids)}
return self._session.post('Sharing::ListUser', json=data).json()
def share_with_users(self, user_ids: List[str], album_ids: List[str]):
"""Share given albums with given users."""
data = {
'UserIDs': ','.join(user_ids),
'albumIDs': ','.join(album_ids)}
self._session.post('Sharing::Add', json=data)
def delete_shares(self, share_ids: List[str]):
"""
Delete given shares.
Share IDs can be found in the `id` field of
the `shared` array when calling shared_albums().
"""
data = {'ShareIDs': ','.join(share_ids)}
return self._session.post('Sharing::Delete', json=data).json()
def change_login(self,
old_username: str,
old_password: str,
new_username: str = '',
new_password: str = ''):
"""
Change username or password.
If new_username of new_password is blank, it will stay the same.
"""
if new_username == '':
new_username = old_username
if new_password == '':
new_password = old_password
data = {
'username': new_username,
'password': new_password,
'oldUsername': old_username,
'oldPassword': old_password
}
self._session.post('Settings::setLogin', json=data)
def import_photo_from_url(self, url: str, album_id: str):
"""Import a photo from URL into an album."""
data = {'url': url, 'albumID': album_id}
self._session.post('Import::url', json=data)