aboutsummaryrefslogtreecommitdiff
path: root/pyfunkwhale/funkwhale.py
blob: 1f8257b88e71d42e513cff47b41278d56413a1e2 (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python

from requests.models import Response

from pyfunkwhale.client import Client


class Funkwhale(object):

    def __init__(self, client_name, redirect_uri, client_id,
                 client_secret, scopes, username, password, domain,
                 authorization_endpoint, token_endpoint, token_filename):
        self.client = Client(
                client_name, redirect_uri, client_id, client_secret,
                scopes, username, password, domain, authorization_endpoint,
                token_endpoint, token_filename)

    def _build_params(self, arguments: dict) -> dict:
        """
        Build params dict for python-requests. Not that all key who start
        with an underscore are treated as par of the endpoint uri and are not
        as uri parameters.

        Parameters
        ----------
        arguments: dict
            Arguments of a function
        """
        params = {}
        for k, v in arguments.items():
            if k != 'self' and v is not None and not k.startswith("_"):
                params[k] = v

        return params

    def create_app(self, name: str, redirect_uris: str = None,
                   scopes: str = None) -> dict:
        """
        Register an OAuth application

        Parameters
        ----------
        name : str
            Name of the application
        redirect_uris : str, optional
            Uris where the instance will redirect
        scopes : str, optional
            Rights of the application on the instance
            Default value: read
        """

        arguments = locals()

        datas = self._build_params(arguments)

        return self.client.call('/oauth/apps/', 'post', data=datas).json()

    def user_me(self):
        """
        Retrieve profile informations of the current user
        """

        return self.client.call('/users/users/me', 'get').json()

    def artists(self, q: str = None, ordering: str = None,
                playable: bool = None, page: int = None,
                page_size: int = None) -> dict:
        """
        List artists

        Parameters
        ----------
        q : str, optional
            Search query used to filter artists
        ordering : str, optional
            Ordering for the results, prefix with - for DESC ordering
            Available values: creation_date, id, name
        playable : bool, optional
            Filter/exclude resources with playable artits
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25

        Raises
        ------
        ValueError
            If `ordering` are set with wrong values
        """

        arguments = locals()

        ordering_field = ['creation_date', 'id', 'name']
        if ordering is not None and ordering not in ordering_field:
            raise ValueError("The ordering field {} is not in the ordering"
                             "fields accepted".format(ordering))

        params = self._build_params(arguments)

        return self.client.call('/artists/', 'get', params).json()

    def artist(self, _id: int, refresh: bool = False) -> dict:
        """
        Retrieve a single artist

        Parameters
        ----------
        _id : int
            Object ID
        refresh : bool, optional
            Trigger an ActivityPub fetch to refresh local data
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/artists/{_id}', 'get', params).json()

    def artist_libraries(self, _id: int, page: int = None,
                         page_size: int = None) -> dict:
        """
        List available user libraries containing work from this artist

        Parameters
        ----------
        _id : int
            Object ID
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(
                f'/artists/{_id}/libraries/', 'get', params).json()

    def albums(self, q: str = None, artist: int = None, ordering: str = None,
               playable: bool = None, page: int = None,
               page_size: int = None) -> dict:
        """
        List albums


        Parameters
        ----------
        q : str, optional
            Search query used to filter albums
        artist : int, optional
            Only include albums by the requested artist
        ordering : str, optional
            Ordering for the results, prefix with - for DESC ordering
            Available values: creation_date, release_date, title
        playable : bool, optional
            Filter/exclude resources with playable albums
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25

        Raises
        ------
        ValueError
            If `ordering` are set with wrong values
        """

        arguments = locals()

        ordering_field = ['creation_date', 'release_date', 'title']
        if ordering is not None and ordering not in ordering_field:
            raise ValueError("The ordering field {} is not in the ordering"
                             "fields accepted".format(ordering))

        params = self._build_params(arguments)

        return self.client.call('/albums/', 'get', params).json()

    def album(self, _id: int, refresh: bool = False) -> dict:
        """
        Retrieve a single album

        Parameters
        ----------
        _id : int
            Object ID
        refresh : bool, optional
            Trigger an ActivityPub fetch to refresh local data
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/albums/{_id}', 'get', params).json()

    def album_libraries(self, _id: int, page: int = None,
                        page_size: int = None) -> dict:
        """
        List available user libraries containing work from this album

        Parameters
        ----------
        _id : int
            Object ID
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(
                f'/albums/{_id}/libraries/', 'get', params).json()

    def tracks(self, q: str = None, artist: int = None, ordering: str = None,
               playable: bool = None, page: int = None,
               page_size: int = None) -> dict:
        """
        List tracks

        Parameters
        ----------
        q : str, optional
            Search query used to filter tracks
        artist : int, optional
            Only include tracks by the requested artist
        favorites : bool, optional
            filter/exclude tracks favorited by the current user
        album : int, optional
            Only include tracks from the requested album
        license : str, optional
            Only include tracks with the given license
        ordering : str, optional
            Ordering for the results, prefix with - for DESC ordering
            Available values: creation_date, release_date, title
        playable : bool, optional
            Filter/exclude resources with playable tracks
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25

        Raises
        ------
        ValueError
            If `ordering` are set with wrong values
        """

        arguments = locals()

        ordering_field = ['creation_date', 'release_date', 'title']
        if ordering is not None and ordering not in ordering_field:
            raise ValueError("The ordering field {} is not in the ordering"
                             "fields accepted".format(ordering))

        params = self._build_params(arguments)

        return self.client.call('/tracks/', 'get', params).json()

    def track(self, _id: int, refresh: bool = False) -> dict:
        """
        Retrieve a single track

        Parameters
        ----------
        _id : int
            Object ID
        refresh : bool, optional
            Trigger an ActivityPub fetch to refresh local data
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/tracks/{_id}', 'get', params).json()

    def track_libraries(self, _id: int, page: int = None,
                        page_size: int = None) -> dict:
        """
        List available user libraries containing work from this track

        Parameters
        ----------
        _id : int
            Object ID
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(
                f'/tracks/{_id}/libraries/', 'get', params).json()

    def listen(self, _uuid, to: str = None, upload: str = None) -> Response:
        """
        Download the audio file matching the given track uuid

        Given a track uuid (and not ID), return the first found audio file
        accessible by the user making the request.

        In case of a remote upload, this endpoint will fetch the audio file
        from the remote and cache it before sending the response.

        Parameters
        ----------
        _uuid : str
            Track uuid
        to : str, optional
            If specified, the endpoint will return a transcoded version of the
            original audio file.
            Since transcoding happens on the fly, it can significantly
            increase response time, and it's recommended to request transcoding
            only for files that are not playable by the client.
            This endpoint support bytess-range requests.
            Available values : ogg, mp3
        upload: str, optional
            If specified, will return the audio for the given upload uuid.
            This is useful for tracks that have multiple uploads available.

        Raises
        ------
        ValueError
            If `to` are set with wrong values
        """

        arguments = locals()

        to_fields = ['ogg', 'mp3']
        if to is not None and to not in to_fields:
            raise ValueError("The to field {} is not in the to"
                             "fields accepted".format(to))

        params = self._build_params(arguments)

        return self.client.call(f'/listen/{_uuid}', 'get', params)

    def licenses(self, page: str = None, page_size: str = None) -> dict:
        """
        List license

        Parameters
        ----------
        page : int, optional
            Default value: 1
        page_size : int, optional
            Default value: 25
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/licenses/', 'get', params).json()

    def license(self, _code) -> dict:
        """
        Retrieve a single license

        Parameters
        ----------
        _code : str
            License code
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/licenses/{_code}', 'get', params).json()

    def favorites_tracks(self, q: str = None, user: str = None,
                         page: int = None, page_size: int = None) -> dict:
        """
        List favorites tracks

        Parameters
        ----------
        q : str, optional
            Search query used to filter favorites tracks
        user : str, optional
            Limit results to favorites tracks belonging to the given user
        page : int, optional
            Default value : 1
        page_size : int, optional
            Default value : 25
        """

        arguments = locals()

        params = self._build_params(arguments)

        return self.client.call(f'/favorites/tracks/', 'get', params).json()

    def add_favorite_track(self, track: str) -> dict:
        """
        Add a track to favorite

        Parameters
        ----------
        track : str
            The track id to add to favorites
        """

        arguments = locals()

        data = self._build_params(arguments)

        return self.client.call(
                f'/favorites/tracks', 'post', data=data).json()

    def delete_favorite_track(self, track: str) -> Response:
        """
        Remove a track from favorites.


        Parameters
        ----------
        track : str
            The track id to remove from favorites
        """

        arguments = locals()

        data = self._build_params(arguments)

        return self.client.call(
                f'/favorites/tracks/remove/', 'post', data=data)