subsonic.py (1810B)
1 import libsonic 2 3 4 class Artist: 5 def __init__(self, id: str, name: str, albumCount: int, **kwargs): 6 self.id = id 7 self.name = name 8 self.album_count = albumCount 9 10 def __str__(self): 11 return f'{self.name} ({self.album_count})' 12 13 14 class Album: 15 def __init__(self, id: str, title: str, year: int, **kwargs): 16 self.id = id 17 self.title = title 18 self.year = year 19 20 def __str__(self): 21 return f'{self.title} ({self.year})' 22 23 24 class Song: 25 def __init__(self, id: str, title: str, track: int, duration: int, 26 **kwargs): 27 self.id = id 28 self.title = title 29 self.track = track 30 self.duration = duration 31 32 def __str__(self): 33 return f'{self.title}' 34 35 36 class Subsonic: 37 def __init__(self, config): 38 self.s = libsonic.Connection(config['url'], 39 config['username'], 40 config['password'], 41 port=443, 42 useGET=True) 43 44 def get_artists(self): 45 artists = [] 46 artists_idx = self.s.getIndexes()['indexes']['index'] 47 for i in artists_idx: 48 for a in i['artist']: 49 artists.append(Artist(**a)) 50 return artists 51 52 def get_albums_from_artist(self, artist): 53 albums = self.s.getArtist(artist.id)['artist']['album'] 54 return [Album(**a) for a in albums] 55 56 def get_songs_from_album(self, album): 57 songs = self.s.getAlbum(album.id)['album']['song'] 58 return [Song(**s) for s in songs] 59 60 def get_song_stream_url(self, song): 61 q = self.s._getQueryDict({'id': song.id, 'format': 'raw'}) 62 req = self.s._getRequest('stream.view', q) 63 return req.get_full_url()