deemix-py/deemix/types/DownloadObjects.py

115 lines
3.5 KiB
Python
Raw Normal View History

2021-03-19 13:31:32 +00:00
class IDownloadObject:
2021-04-10 09:53:52 +00:00
"""DownloadObject interface"""
2021-03-24 16:41:03 +00:00
def __init__(self, obj):
self.type = obj['type']
self.id = obj['id']
self.bitrate = obj['bitrate']
self.title = obj['title']
self.artist = obj['artist']
self.cover = obj['cover']
self.explicit = obj.get('explicit', False)
self.size = obj['size']
self.downloaded = obj.get('downloaded', 0)
self.failed = obj.get('failed', 0)
self.progress = obj.get('progress', 0)
self.errors = obj.get('errors', [])
self.files = obj.get('files', [])
2021-03-19 14:44:21 +00:00
self.progressNext = 0
2021-03-19 13:31:32 +00:00
self.uuid = f"{self.type}_{self.id}_{self.bitrate}"
self.ack = None
self.__type__ = None
def toDict(self):
return {
'type': self.type,
'id': self.id,
'bitrate': self.bitrate,
'uuid': self.uuid,
'title': self.title,
'artist': self.artist,
'cover': self.cover,
'explicit': self.explicit,
'size': self.size,
'downloaded': self.downloaded,
'failed': self.failed,
'progress': self.progress,
'errors': self.errors,
'files': self.files,
'ack': self.ack,
'__type__': self.__type__
}
def getResettedDict(self):
item = self.toDict()
item['downloaded'] = 0
item['failed'] = 0
item['progress'] = 0
item['errors'] = []
item['files'] = []
return item
def getSlimmedDict(self):
light = self.toDict()
propertiesToDelete = ['single', 'collection', 'convertable']
2021-04-10 09:53:52 +00:00
for prop in propertiesToDelete:
if prop in light:
del light[prop]
2021-03-19 13:31:32 +00:00
return light
2021-03-19 14:44:21 +00:00
def updateProgress(self, interface=None):
if round(self.progressNext) != self.progress and round(self.progressNext) % 2 == 0:
self.progress = round(self.progressNext)
if interface: interface.send("updateQueue", {'uuid': self.uuid, 'progress': self.progress})
2021-03-19 13:31:32 +00:00
class Single(IDownloadObject):
2021-03-24 16:41:03 +00:00
def __init__(self, obj):
super().__init__(obj)
self.size = 1
self.single = obj['single']
2021-03-19 13:31:32 +00:00
self.__type__ = "Single"
def toDict(self):
item = super().toDict()
item['single'] = self.single
return item
2021-03-19 14:44:21 +00:00
def completeTrackProgress(self, interface=None):
self.progressNext = 100
self.updateProgress(interface)
def removeTrackProgress(self, interface=None):
self.progressNext = 0
self.updateProgress(interface)
2021-03-19 13:31:32 +00:00
class Collection(IDownloadObject):
2021-03-24 16:41:03 +00:00
def __init__(self, obj):
super().__init__(obj)
self.collection = obj['collection']
2021-03-19 13:31:32 +00:00
self.__type__ = "Collection"
def toDict(self):
item = super().toDict()
item['collection'] = self.collection
return item
2021-03-19 14:44:21 +00:00
def completeTrackProgress(self, interface=None):
self.progressNext += (1 / self.size) * 100
self.updateProgress(interface)
def removeTrackProgress(self, interface=None):
self.progressNext -= (1 / self.size) * 100
self.updateProgress(interface)
2021-03-19 13:31:32 +00:00
class Convertable(Collection):
2021-03-24 16:41:03 +00:00
def __init__(self, obj):
super().__init__(obj)
self.plugin = obj['plugin']
self.conversion_data = obj['conversion_data']
2021-03-19 13:31:32 +00:00
self.__type__ = "Convertable"
def toDict(self):
item = super().toDict()
item['plugin'] = self.plugin
item['conversion_data'] = self.conversion_data
return item