summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-05-19 17:33:42 -0500
committerHombreLaser <sebastian-440@live.com>2022-05-19 17:33:42 -0500
commitb9254f5b9baf35107cae0c91cf9d979fbbfa14aa (patch)
treecce1d69c27295ddf63603ff92902b30fe12aa75a
parentc2172c94524cff1958de412eedd9efa21f60466f (diff)
Añadida descarga, búsqueda de álbum
-rw-r--r--src/ampache/ampache.py64
-rw-r--r--src/ampache/models.py11
-rw-r--r--src/test.py3
3 files changed, 72 insertions, 6 deletions
diff --git a/src/ampache/ampache.py b/src/ampache/ampache.py
index 12ccbd7..64939d9 100644
--- a/src/ampache/ampache.py
+++ b/src/ampache/ampache.py
@@ -1,4 +1,9 @@
+"""
+The main file. The AmpacheClient class allows us to make requests to the
+API.
+"""
from datetime import datetime
+from sys import version
from ampache import models, exceptions
import requests
@@ -63,13 +68,14 @@ class AmpacheClient:
if not response.ok:
response.raise_for_status()
- data = response.json()
+ if headers['Content-type'] == 'application/json':
+ response = response.json()
- if 'error' in data:
- self.raise_by_status(models.Error(int(data['error']['errorCode']),
- data['error']['errorMessage']))
+ if 'error' in response:
+ self.raise_by_status(models.Error(int(response['error']['errorCode']),
+ response['error']['errorMessage']))
- return data
+ return response
def authenticate(self):
"""
@@ -109,6 +115,54 @@ class AmpacheClient:
return song
+ def get_album(self, album_id):
+ params = {
+ 'action': 'album_songs',
+ 'auth': self._auth.auth,
+ 'filter': album_id,
+ 'version': self._version
+ }
+ headers = {
+ 'Content-type': 'application/json'
+ }
+ data = self.request(params, headers)
+ album = self.process_album(data)
+
+ return album
+
+ def process_album(self, album_data):
+ album_id = album_data['song'][0]['album']['id']
+ album_title = album_data['song'][0]['album']['name']
+ album_artist = album_data['song'][0]['artist']['name']
+ songs = []
+
+ for song in album_data['song']:
+ new_song = models.Song(song['id'], song['title'], album_title,
+ album_artist)
+ songs.append(new_song)
+
+ return models.Album(album_id, album_title, album_artist, songs)
+
+ def download(self, song_id, destination):
+ """
+ Download a song to destination.
+ """
+ params = {
+ 'action': 'download',
+ 'auth': self._auth.auth,
+ 'type': 'song',
+ 'id': song_id,
+ 'version': self._version,
+ 'format': 'mp3'
+ }
+ headers = {
+ 'Content-type': 'audio/mpeg'
+ }
+ data = self.request(params, headers)
+
+ with open(destination, 'wb') as song:
+ song.write(data.content)
+
def renew_token(self):
if datetime.now(self._auth.expires.tzinfo) > self._auth.expires:
self.authenticate()
diff --git a/src/ampache/models.py b/src/ampache/models.py
index 9508b2e..452b1c5 100644
--- a/src/ampache/models.py
+++ b/src/ampache/models.py
@@ -26,6 +26,17 @@ class Song:
@dataclass
+class Album:
+ album_id: str
+ title: str
+ artist: str
+ songs: list[Song]
+
+ def __str__(self):
+ return f"{self.title} by {self.artist}"
+
+
+@dataclass
class Error:
code: int
message: str
diff --git a/src/test.py b/src/test.py
index fdf8c5b..e75baa3 100644
--- a/src/test.py
+++ b/src/test.py
@@ -1,4 +1,5 @@
from ampache.ampache import AmpacheClient
my_ampache = AmpacheClient('https://music.silosneeded.com', '4dee7fe5554cf581a3f69ea023ea378a', '532000')
-print(my_ampache.get_song(4713))
+album = my_ampache.get_album(4724)
+print(album)