lots of progress

This commit is contained in:
JISAUAY committed 2025-01-23 16:30:50 -06:00
1 parent 1d56b5efea
commit f8815964b5
8 files changed
+16549 -16

No files matched your search

+111 -6
View File
@@ -1,3 +1,4 @@
import time
import random
import string
import hashlib
@@ -12,6 +13,21 @@ import requests
logger = logging.getLogger(__name__)
class Playlist:
id: str
name: str
songCount: int
duration: str
owner: str
coverArtId: str
def __init__(self, info: dict):
self.id = info['id']
self.name = info['name']
self.songCount = int(info['songCount'])
self.duration = time.strftime('%H:%M:%S', time.gmtime(int(info['duration'])))
self.owner = info['owner']
self.coverArtId = info['coverArt']
class Album:
id: str
@@ -22,6 +38,9 @@ class Album:
duration: int
artistId: str
songCount: int
playCount: int
year: int | None
duration: str
def __init__(self, album_info: dict):
self.id = album_info['id']
@@ -32,6 +51,12 @@ class Album:
self.duration = int(album_info['duration'])
self.artistId = album_info['artistId']
self.songCount = int(album_info['songCount'])
self.playCount = int(album_info['playCount']) if 'playCount' in album_info else 0
self.year = int(album_info['year']) if 'year' in album_info else None
self.duration = self._secondToMinutes(int(album_info['duration']))
def _secondToMinutes(self, s: int) -> str:
return f'{s // 60}:{s % 60 if s % 60 > 9 else "0" + str(s % 60)}'
class Song:
id: str
@@ -50,13 +75,14 @@ class Song:
comment: str
channelCount: int
samplingRate: int
playCount: int | None
def __init__(self, song_info: dict):
self.id = song_info['id']
self.title = song_info['title']
self.album = song_info['album']
self.artist = song_info['artist']
self.trackNumber = int(song_info['track'])
self.trackNumber = int(song_info['track']) if 'track' in song_info else None
self.contentType = song_info['contentType']
self.suffix = song_info['suffix']
self.duration = int(song_info['duration'])
@@ -64,10 +90,11 @@ class Song:
self.path = song_info['path']
self.discNumber = int(song_info['discNumber']) if 'discNumber' in song_info else None
self.albumId = song_info['albumId']
self.artistId = song_info['artistId']
self.artistId = song_info['artistId'] if 'artistId' in song_info else None
self.comment = song_info['comment']
self.channelCount = int(song_info['channelCount'])
self.samplingRate = int(song_info['samplingRate'])
self.playCount = int(song_info['playCount']) if 'playCount' in song_info else None
class Artist:
id: str
@@ -230,17 +257,95 @@ class SubpyConn:
return artists
def getAlbums(self) -> list[Album]:
def getAlbums(self, size: int | None = 25, offset: int | None = 0) -> list[Album]:
url = self.base_url + '/rest/getAlbumList?'
payload = self._getBasePayload()
payload['type'] = 'alphabeticalByName'
payload['size'] = 20
payload['size'] = size
payload['offset'] = offset
payload['f'] = 'json'
r = requests.get(url + self._encodePayload(payload))
json_r = r.json()
albums: list[Album] = []
if 'album' not in json_r['subsonic-response']['albumList']:
return []
for album in json_r['subsonic-response']['albumList']['album']:
albums.append(Album(album))
return albums
def getAllAlbums(self) -> list[Album]:
all_albums: list[Album] = []
offset = 0
while albums := self.getAlbums(50, offset):
all_albums.extend(albums)
offset += 50
return all_albums
def getPlaylists(self) -> list[Playlist]:
url = self.base_url + "/rest/getPlaylists?"
payload = self._getBasePayload()
payload['f'] = 'json'
r = requests.get(url + self._encodePayload(payload))
with open("albumList.xml" , 'wb') as file:
file.write(r.content)
json_r = r.json()
playlists: list[Playlist] = []
for playlist in json_r['subsonic-response']['playlists']['playlist']:
playlists.append(Playlist(playlist))
return playlists
def getStarred(self) -> list[Album | Song | Artist]:
url = self.base_url + "/rest/getStarred?"
payload = self._getBasePayload()
payload['f'] = 'json'
r = requests.get(url + self._encodePayload(payload))
json_r = r.json()
starred: list[Album | Song | Artist] = []
for album in json_r['subsonic-response']['starred']['artist']:
starred.append(Album(album))
for artist in json_r['subsonic-response']['starred']['artist']:
starred.append(Artist(
artist['id'],
artist['name'],
artist['albumCount'],
artist['coverArt'],
artist['artistImageUrl']
))
for song in json_r['subsonic-response']['starred']['song']:
starred.append(Song(song))
return starred
def getStarredSongs(self) -> list[Song]:
url = self.base_url + "/rest/getStarred?"
payload = self._getBasePayload()
payload['f'] = 'json'
r = requests.get(url + self._encodePayload(payload))
json_r = r.json()
starred: list[Song] = []
for song in json_r['subsonic-response']['starred']['song']:
starred.append(Song(song))
return starred
# Returns a requests Response object, the data stream can be found in Response.content
def stream(self, song_id: str) -> requests.Response: