-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampache.py
2969 lines (2617 loc) · 111 KB
/
ampache.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Copyright (C)2020 Ampache.org
-------------------------------------------
Ampache XML and JSON Api 420000 for python3
-------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import hashlib
import json
import os
import requests
import time
import urllib.parse
import urllib.request
from xml.etree import ElementTree
class API(object):
def __init__(self):
self.AMPACHE_API = 'xml'
self.AMPACHE_DEBUG = False
self.AMPACHE_URL = ''
self.AMPACHE_SESSION = ''
self.AMPACHE_USER = ''
self.AMPACHE_KEY = ''
# Test colors for printing
self.OKGREEN = '\033[92m'
self.WARNING = '\033[93m'
self.FAIL = '\033[91m'
self.ENDC = '\033[0m'
"""
----------------
HELPER FUNCTIONS
----------------
"""
def set_format(self, myformat: str):
""" set_format
Allow forcing a default format
INPUTS
* myformat = (string) 'xml'|'json'
"""
if myformat == 'xml' or myformat == 'json':
print('AMPACHE_API set to ' + myformat)
self.AMPACHE_API = myformat
def set_debug(self, mybool: bool):
""" set_debug
This function can be used to enable/disable debugging messages
INPUTS
* bool = (boolean) Enable/disable debug messages
"""
if mybool:
print('AMPACHE_DEBUG' + f": {self.OKGREEN}enabled{self.ENDC}")
else:
print('AMPACHE_DEBUG' + f": {self.WARNING}disabled{self.ENDC}")
self.AMPACHE_DEBUG = mybool
def set_user(self, myuser: str):
""" set_user
set user for connection
INPUTS
* myuser = (string) 'xml'|'json'
"""
self.AMPACHE_USER = myuser
def set_key(self, mykey: str):
""" set_key
set api key
INPUTS
* mykey = (string) 'xml'|'json'
"""
self.AMPACHE_API = mykey
def set_url(self, myurl: str):
""" set_url
set the ampache url
INPUTS
* myurl = (string) 'xml'|'json'
"""
self.AMPACHE_URL = myurl
def test_result(self, result, title):
""" set_debug
This function can be used to enable/disable debugging messages
INPUTS
* bool = (boolean) Enable/disable debug messages
"""
if not result:
print("ampache." + title + f": {self.FAIL}FAIL{self.ENDC}")
return False
if 'Require: ' in result:
print(f"ampache." + title + f": {self.WARNING}WARNING{self.ENDC} " + result)
return True
if result:
print("ampache." + title + f": {self.OKGREEN}PASS{self.ENDC}")
return True
print("ampache." + title + f": {self.FAIL}FAIL{self.ENDC}")
return False
def return_data(self, data):
# json format
if self.AMPACHE_API == 'json':
json_data = json.loads(data.decode('utf-8'))
return json_data
# xml format
else:
try:
tree = ElementTree.fromstring(data.decode('utf-8'))
except ElementTree.ParseError:
return False
return tree
def get_id_list(self, data, attribute: str):
""" get_id_list
return a list of id's from the data you've got from the api
INPUTS
* data = (mixed) XML or JSON from the API
* attribute = (string) attribute you are searching for
"""
id_list = list()
if not data:
return id_list
if self.AMPACHE_API == 'xml':
try:
for child in data:
if child.tag == attribute:
id_list.append(child.attrib['id'])
except KeyError:
id_list.append(data['id'])
else:
try:
for data_object in data[attribute]:
id_list.append(data_object['id'])
except TypeError:
for data_object in data:
id_list.append(data_object[0])
except KeyError:
id_list.append(data['id'])
return id_list
@staticmethod
def get_object_list(data, field: str, data_format: str = 'xml'):
""" get_id_list
return a list of objects from the data matching your field stirng
INPUTS
* data = (mixed) XML or JSON from the API
* field = (string) field you are searching for
* data_format = (string) 'xml','json'
"""
id_list = list()
if data_format == 'xml':
return data.findall(field)
else:
try:
for data_object in data[field]:
id_list.append(data_object['id'])
except TypeError:
for data_object in data:
id_list.append(data_object[0])
return id_list
@staticmethod
def write_xml(xmlstr, filename: str):
""" write_xml
This function can be used to write your xml responses to a file.
INPUTS
* xmlstr = (xml) xml to write to file
* filename = (string) path and filename (e.g. './ampache.xml')
"""
if xmlstr:
text_file = open(filename, "w")
text_file.write(ElementTree.tostring(xmlstr).decode())
text_file.close()
@staticmethod
def get_message(data):
""" get_id_list
return a list of objects from the data matching your field stirng
INPUTS
* data = (mixed) XML or JSON from the API
"""
message = data
print(data)
if 'error' in data:
try:
message = data['error']['message']
except TypeError:
message = data['error']
except KeyError:
message = data['error']['errorMessage']
if 'success' in data:
try:
message = data['success']['message']
except TypeError:
message = data['success']
return message
@staticmethod
def write_json(json_data: str, filename: str):
""" write_json
This function can be used to write your json responses to a file.
INPUTS
* json_data = (json) json to write to file
* filename = (string) path and filename (e.g. './ampache.json')
"""
if json_data:
text_file = open(filename, "w")
text_file.write(json.dumps(json_data))
text_file.close()
@staticmethod
def encrypt_password(password: str, current_time: int):
""" encrypt_password
This function can be used to encrypt your password into the accepted format.
INPUTS
* password = (string) unencrypted password string
* time = (integer) linux time
"""
key = hashlib.sha256(password.encode()).hexdigest()
passphrase = str(current_time) + key
sha_signature = hashlib.sha256(passphrase.encode()).hexdigest()
return sha_signature
@staticmethod
def encrypt_string(api_key: str, username: str):
""" encrypt_string
This function can be used to encrypt your apikey into the accepted format.
INPUTS
* api_key = (string) unencrypted apikey
* user = (string) username
"""
key = hashlib.sha256(api_key.encode()).hexdigest()
passphrase = username + key
sha_signature = hashlib.sha256(passphrase.encode()).hexdigest()
return sha_signature
def fetch_url(self, full_url: str, api_format: str, method: str):
""" fetch_url
This function is used to fetch the string results using urllib
INPUTS
* full_url = (string) url to fetch
* api_format = (string) 'xml'|'json'
* method = (string)
"""
try:
result = urllib.request.urlopen(full_url)
except urllib.error.URLError:
return False
except urllib.error.HTTPError:
return False
except ValueError:
return False
ampache_response = result.read()
result.close()
if self.AMPACHE_DEBUG:
url_response = ampache_response.decode('utf-8')
print(url_response)
print(full_url)
try:
text_file = open("docs/" + api_format + "-responses/" + method + "." + api_format, "w", encoding="utf-8")
text_file.write(url_response)
text_file.close()
except FileNotFoundError:
pass
return ampache_response
"""
-------------
API FUNCTIONS
-------------
"""
def handshake(self, ampache_url: str, ampache_api: str, ampache_user: str = False,
timestamp: int = 0, version: str = '5.0.0'):
""" handshake
MINIMUM_API_VERSION=380001
This is the function that handles verifying a new handshake
Takes a timestamp, auth key, and username.
INPUTS
* ampache_url = (string) Full Ampache URL e.g. 'https://music.com.au'
* ampache_api = (string) encrypted apikey OR password if using password auth
* user = (string) username //optional
* timestamp = (integer) UNIXTIME() //optional
* version = (string) //optional
"""
self.AMPACHE_URL = ampache_url
if timestamp == 0:
timestamp = int(time.time())
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'handshake',
'auth': ampache_api,
'user': ampache_user,
'timestamp': str(timestamp),
'version': version}
if not ampache_user:
data.pop('user')
if not timestamp or not ampache_user:
data.pop('timestamp')
if not version:
data.pop('version')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'handshake')
if not ampache_response:
return False
# json format
if self.AMPACHE_API == 'json':
json_data = json.loads(ampache_response.decode('utf-8'))
if 'auth' in json_data:
self.AMPACHE_SESSION = json_data['auth']
return json_data['auth']
else:
return False
# xml format
else:
try:
tree = ElementTree.fromstring(ampache_response.decode('utf-8'))
except ElementTree.ParseError:
return False
try:
token = tree.find('auth').text
except AttributeError:
token = False
self.AMPACHE_SESSION = token
return token
def ping(self, ampache_url: str, ampache_api: str = False, version: str = '5.0.0'):
""" ping
MINIMUM_API_VERSION=380001
This can be called without being authenticated, it is useful for determining if what the status
of the server is, and what version it is running/compatible with
INPUTS
* ampache_url = (string) Full Ampache URL e.g. 'https://music.com.au'
* ampache_api = (string) encrypted apikey //optional
"""
ampache_url = ampache_url + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'ping',
'version': version,
'auth': ampache_api}
if not ampache_api:
data.pop('auth')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'ping')
if not ampache_response:
return False
# json format
if self.AMPACHE_API == 'json':
json_data = json.loads(ampache_response.decode('utf-8'))
if 'session_expire' in json_data:
if not self.AMPACHE_URL:
self.AMPACHE_URL = ampache_url
self.AMPACHE_SESSION = ampache_api
return ampache_api
else:
return False
# xml format
else:
try:
tree = ElementTree.fromstring(ampache_response.decode('utf-8'))
except ElementTree.ParseError:
return False
try:
tree.find('session_expire').text
if not self.AMPACHE_URL:
self.AMPACHE_URL = ampache_url
self.AMPACHE_SESSION = ampache_api
except AttributeError:
return False
return ampache_api
def goodbye(self):
""" goodbye
MINIMUM_API_VERSION=400001
Destroy session for ampache_api auth key.
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'goodbye',
'auth': self.AMPACHE_SESSION}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'goodbye')
if not ampache_response:
return False
return self.return_data(ampache_response)
def url_to_song(self, url):
""" url_to_song
MINIMUM_API_VERSION=380001
This takes a url and returns the song object in question
INPUTS
* url = (string) Full Ampache URL from server, translates back into a song XML
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'url_to_song',
'auth': self.AMPACHE_SESSION,
'url': url}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'url_to_song')
if not ampache_response:
return False
return self.return_data(ampache_response)
def get_similar(self, object_type, filter_id: int,
offset=0, limit=0):
""" get_similar
MINIMUM_API_VERSION=420000
Return similar artist id's or similar song ids compared to the input filter
INPUTS
* object_type = (string) 'song'|'album'|'artist'|'playlist'
* filter_id = (integer) $artist_id or song_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'get_similar',
'auth': self.AMPACHE_SESSION,
'type': object_type,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'get_similar')
if not ampache_response:
return False
return self.return_data(ampache_response)
def get_indexes(self, object_type,
filter_str: str = False, exact: int = False, add: int = False, update: int = False,
include=False, offset=0, limit=0):
""" get_indexes
MINIMUM_API_VERSION=400001
This takes a collection of inputs and returns ID + name for the object type
INPUTS
* object_type = (string) 'song'|'album'|'artist'|'album_artist'|'playlist'
* filter_str = (string) search the name of the object_type //optional
* exact = (integer) 0,1, if true filter is exact rather then fuzzy //optional
* add = (integer) UNIXTIME() //optional
* update = (integer) UNIXTIME() //optional
* include = (integer) 0,1 include songs if available for that object //optional
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
if bool(include):
include = 1
else:
include = 0
data = {'action': 'get_indexes',
'auth': self.AMPACHE_SESSION,
'type': object_type,
'filter': filter_str,
'exact': exact,
'add': add,
'update': update,
'include': include,
'offset': str(offset),
'limit': str(limit)}
if not filter_str:
data.pop('filter')
if not add:
data.pop('add')
if not update:
data.pop('update')
if not include:
data.pop('include')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'get_indexes')
if not ampache_response:
return False
return self.return_data(ampache_response)
def artists(self, filter_str: str = False,
add: int = False, update: int = False, offset=0, limit=0, include=False):
""" artists
MINIMUM_API_VERSION=380001
This takes a collection of inputs and returns artist objects.
INPUTS
* filter_str = (string) search the name of an artist //optional
* add = (integer) UNIXTIME() //optional
* update = (integer) UNIXTIME() //optional
* offset = (integer) //optional
* limit = (integer) //optional
* include = (string) 'albums', 'songs' //optional
* album_artist = (boolean) 0,1 if true filter for album artists only //optional
* self.AMPACHE_API = (string) 'xml'|'json' //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
if bool(include) and not isinstance(include, str):
include = 'albums,songs'
data = {'action': 'artists',
'auth': self.AMPACHE_SESSION,
'filter': filter_str,
'add': add,
'update': update,
'offset': str(offset),
'limit': str(limit),
'include': include}
if not filter_str:
data.pop('filter')
if not add:
data.pop('add')
if not update:
data.pop('update')
if not include:
data.pop('include')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'artists')
if not ampache_response:
return False
return self.return_data(ampache_response)
def artist(self, filter_id: int, include=False):
""" artist
MINIMUM_API_VERSION=380001
This returns a single artist based on the UID of said artist
INPUTS
* filter_id = (integer) $artist_id
* include = (string) 'albums', 'songs' //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
if bool(include) and not isinstance(include, str):
include = 'albums,songs'
data = {'action': 'artist',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'include': include}
if not include:
data.pop('include')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'artist')
if not ampache_response:
return False
return self.return_data(ampache_response)
def artist_albums(self, filter_id: int, offset=0, limit=0):
""" artist_albums
MINIMUM_API_VERSION=380001
This returns the albums of an artist
INPUTS
* filter_id = (integer) $artist_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'artist_albums',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'artist_albums')
if not ampache_response:
return False
return self.return_data(ampache_response)
def artist_songs(self, filter_id: int, offset=0, limit=0):
""" artist_songs
MINIMUM_API_VERSION=380001
This returns the songs of the specified artist
INPUTS
* filter_id = (integer) $artist_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'artist_songs',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'artist_songs')
if not ampache_response:
return False
return self.return_data(ampache_response)
def albums(self, filter_str: str = False,
exact=False, add: int = False, update: int = False, offset=0, limit=0,
include=False):
""" albums
MINIMUM_API_VERSION=380001
This returns albums based on the provided search filters
INPUTS
* filter_str = (string) search the name of an album //optional
* exact = (integer) 0,1, if true filter is exact rather then fuzzy //optional
* add = (integer) UNIXTIME() //optional
* update = (integer) UNIXTIME() //optional
* offset = (integer) //optional
* limit = (integer) //optional
* include = (string) 'songs' //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
if bool(include) and not isinstance(include, str):
include = 'songs'
data = {'action': 'albums',
'auth': self.AMPACHE_SESSION,
'filter': filter_str,
'exact': exact,
'add': add,
'update': update,
'offset': str(offset),
'limit': str(limit),
'include': include}
if not filter_str:
data.pop('filter_str')
if not add:
data.pop('add')
if not update:
data.pop('update')
if not include:
data.pop('include')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'albums')
if not ampache_response:
return False
return self.return_data(ampache_response)
def album(self, filter_id: int, include=False):
""" album
MINIMUM_API_VERSION=380001
This returns a single album based on the UID provided
INPUTS
* filter_id = (integer) $album_id
* include = (string) 'songs' //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
if bool(include) and not isinstance(include, str):
include = 'songs'
data = {'action': 'album',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'include': include}
if not include:
data.pop('include')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'album')
if not ampache_response:
return False
return self.return_data(ampache_response)
def album_songs(self, filter_id: int, offset=0, limit=0):
""" album_songs
MINIMUM_API_VERSION=380001
This returns the songs of a specified album
INPUTS
* filter_id = (integer) $album_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'album_songs',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'album_songs')
if not ampache_response:
return False
return self.return_data(ampache_response)
def genres(self, filter_str: str = False,
exact: int = False, offset=0, limit=0):
""" genres
MINIMUM_API_VERSION=380001
This returns the genres (Tags) based on the specified filter
INPUTS
* filter_str = (string) search the name of a genre //optional
* exact = (integer) 0,1, if true filter is exact rather then fuzzy //optional
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'genres',
'auth': self.AMPACHE_SESSION,
'exact': exact,
'filter': filter_str,
'offset': str(offset),
'limit': str(limit)}
if not filter_str:
data.pop('filter')
if not exact:
data.pop('exact')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'genres')
if not ampache_response:
return False
return self.return_data(ampache_response)
def genre(self, filter_id: int):
""" genre
MINIMUM_API_VERSION=380001
This returns a single genre based on UID
INPUTS
* filter_id = (integer) $genre_id
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'genre',
'auth': self.AMPACHE_SESSION,
'filter': filter_id}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'genre')
if not ampache_response:
return False
return self.return_data(ampache_response)
def genre_artists(self, filter_id: int, offset=0, limit=0):
""" genre_artists
MINIMUM_API_VERSION=380001
This returns the artists associated with the genre in question as defined by the UID
INPUTS
* filter_id = (integer) $genre_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'genre_artists',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'genre_artists')
if not ampache_response:
return False
return self.return_data(ampache_response)
def genre_albums(self, filter_id: int, offset=0, limit=0):
""" genre_albums
MINIMUM_API_VERSION=380001
This returns the albums associated with the genre in question
INPUTS
* filter_id = (integer) $genre_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'genre_albums',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'genre_albums')
if not ampache_response:
return False
return self.return_data(ampache_response)
def genre_songs(self, filter_id: int, offset=0, limit=0):
""" genre_songs
MINIMUM_API_VERSION=380001
returns the songs for this genre
INPUTS
* filter_id = (integer) $genre_id
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'genre_songs',
'auth': self.AMPACHE_SESSION,
'filter': filter_id,
'offset': str(offset),
'limit': str(limit)}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'genre_songs')
if not ampache_response:
return False
return self.return_data(ampache_response)
def songs(self, filter_str: str = False, exact: int = False,
add: int = False, update: int = False, offset=0, limit=0):
""" songs
MINIMUM_API_VERSION=380001
Returns songs based on the specified filter_str
INPUTS
* filter_str = (string) search the name of a song //optional
* exact = (integer) 0,1, if true filter is exact rather then fuzzy //optional
* add = (integer) UNIXTIME() //optional
* update = (integer) UNIXTIME() //optional
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'songs',
'auth': self.AMPACHE_SESSION,
'exact': exact,
'add': add,
'update': update,
'filter': filter_str,
'offset': str(offset),
'limit': str(limit)}
if not filter_str:
data.pop('filter')
if not exact:
data.pop('exact')
if not add:
data.pop('add')
if not update:
data.pop('update')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'songs')
if not ampache_response:
return False
return self.return_data(ampache_response)
def song(self, filter_id: int):
""" song
MINIMUM_API_VERSION=380001
returns a single song
INPUTS
* filter_id = (integer) $song_id
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'song',
'auth': self.AMPACHE_SESSION,
'filter': filter_id}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'song')
if not ampache_response:
return False
return self.return_data(ampache_response)
def song_delete(self, filter_id: int):
""" song_delete
MINIMUM_API_VERSION=5.0.0
Delete an existing song.
INPUTS
* filter_id = (string) UID of song to delete
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'song_delete',
'auth': self.AMPACHE_SESSION,
'filter': filter_id}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'song')
if not ampache_response:
return False
return self.return_data(ampache_response)
def playlists(self, filter_str: str = False, exact: int = False, offset=0, limit=0):
""" playlists
MINIMUM_API_VERSION=380001
This returns playlists based on the specified filter
INPUTS
* filter_str = (string) search the name of a playlist //optional
* exact = (integer) 0,1, if true filter is exact rather then fuzzy //optional
* offset = (integer) //optional
* limit = (integer) //optional
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'playlists',
'auth': self.AMPACHE_SESSION,
'exact': exact,
'filter': filter_str,
'offset': str(offset),
'limit': str(limit)}
if not filter_str:
data.pop('filter')
if not exact:
data.pop('exact')
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'playlists')
if not ampache_response:
return False
return self.return_data(ampache_response)
def playlist(self, filter_id: int):
""" playlist
MINIMUM_API_VERSION=380001
This returns a single playlist
INPUTS
* filter_id = (integer) $playlist_id
"""
ampache_url = self.AMPACHE_URL + '/server/' + self.AMPACHE_API + '.server.php'
data = {'action': 'playlist',
'auth': self.AMPACHE_SESSION,
'filter': filter_id}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
ampache_response = self.fetch_url(full_url, self.AMPACHE_API, 'playlist')
if not ampache_response:
return False
return self.return_data(ampache_response)
def playlist_songs(self, filter_id: int, offset=0, limit=0):
""" playlist_songs
MINIMUM_API_VERSION=380001