From 1f396ca95a775103adb8c5e7bee3dd2cfce14a1a Mon Sep 17 00:00:00 2001 From: blacktwin Date: Thu, 4 Jun 2020 23:21:03 -0400 Subject: [PATCH 1/5] remove field attribute Addressing #504 remove field attribute as it is no longer available. adding `__repr__` to Marker class resulting in `` --- plexapi/media.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plexapi/media.py b/plexapi/media.py index 65895c3e3..d10956547 100644 --- a/plexapi/media.py +++ b/plexapi/media.py @@ -704,13 +704,15 @@ class Marker(PlexObject): """ TAG = 'Marker' + def __repr__(self): + name = self._clean(self.firstAttr('type')) + start = self._clean(self.firstAttr('start')) + end = self._clean(self.firstAttr('end')) + return '<%s>' % ':'.join([p for p in [self.__class__.__name__, name, start, end] if p]) + def _loadData(self, data): self._data = data - self.filter = data.attrib.get('filter') self.type = data.attrib.get('type') - _tag, _id = self.filter.split('=') - self.tag = self.type + _tag.capitalize() - self.id = _id self.start = cast(int, data.attrib.get('startTimeOffset')) self.end = cast(int, data.attrib.get('endTimeOffset')) From faeee7d667b65ae605d239f03b68e6f4ee4e70e2 Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 13 Jun 2020 22:11:39 -0400 Subject: [PATCH 2/5] import timedelta to utils --- plexapi/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexapi/utils.py b/plexapi/utils.py index bf26d044f..665474c61 100644 --- a/plexapi/utils.py +++ b/plexapi/utils.py @@ -4,7 +4,7 @@ import re import time import zipfile -from datetime import datetime +from datetime import datetime, timedelta from getpass import getpass from threading import Event, Thread from urllib.parse import quote From 3852ed52a71adddff01ace8f9cf372ba9bd5327c Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 13 Jun 2020 22:12:01 -0400 Subject: [PATCH 3/5] create millisecondToHuman method in utils --- plexapi/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plexapi/utils.py b/plexapi/utils.py index 665474c61..5d68f9cef 100644 --- a/plexapi/utils.py +++ b/plexapi/utils.py @@ -204,6 +204,18 @@ def toDatetime(value, format=None): return value +def millisecondToHuman(milliseconds): + """ Returns human readable time duration from milliseconds. + HH:MM:SS + + Parameters: + milliseconds (str,int): time duration in milliseconds. + """ + _milliseconds = cast(int, milliseconds) + hhmmsssm = timedelta(milliseconds=_milliseconds) + return str(hhmmsssm).split(".")[0] + + def toList(value, itemcast=None, delim=','): """ Returns a list of strings from the specified value. From 881a4fc6598a08c11e486e9b398bf4bbc1866beb Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 13 Jun 2020 22:12:35 -0400 Subject: [PATCH 4/5] use utils.millisecondToHuman for media.Marker.__repr__ --- plexapi/media.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plexapi/media.py b/plexapi/media.py index d10956547..aded0063f 100644 --- a/plexapi/media.py +++ b/plexapi/media.py @@ -706,8 +706,8 @@ class Marker(PlexObject): def __repr__(self): name = self._clean(self.firstAttr('type')) - start = self._clean(self.firstAttr('start')) - end = self._clean(self.firstAttr('end')) + start = utils.millisecondToHuman(self._clean(self.firstAttr('start'))) + end = utils.millisecondToHuman(self._clean(self.firstAttr('end'))) return '<%s>' % ':'.join([p for p in [self.__class__.__name__, name, start, end] if p]) def _loadData(self, data): From d7c215b119aacdfb100808f9b41f5858f40aea43 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Sun, 14 Jun 2020 20:21:46 +0200 Subject: [PATCH 5/5] Small change to the repr and add test. --- plexapi/media.py | 6 +++--- plexapi/utils.py | 11 ++++++----- tests/test_utils.py | 6 ++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plexapi/media.py b/plexapi/media.py index aded0063f..7a106232e 100644 --- a/plexapi/media.py +++ b/plexapi/media.py @@ -706,9 +706,9 @@ class Marker(PlexObject): def __repr__(self): name = self._clean(self.firstAttr('type')) - start = utils.millisecondToHuman(self._clean(self.firstAttr('start'))) - end = utils.millisecondToHuman(self._clean(self.firstAttr('end'))) - return '<%s>' % ':'.join([p for p in [self.__class__.__name__, name, start, end] if p]) + start = utils.millisecondToHumanstr(self._clean(self.firstAttr('start'))) + end = utils.millisecondToHumanstr(self._clean(self.firstAttr('end'))) + return '<%s:%s %s - %s>' % (self.__class__.__name__, name, start, end) def _loadData(self, data): self._data = data diff --git a/plexapi/utils.py b/plexapi/utils.py index 5d68f9cef..01e91830c 100644 --- a/plexapi/utils.py +++ b/plexapi/utils.py @@ -204,16 +204,17 @@ def toDatetime(value, format=None): return value -def millisecondToHuman(milliseconds): +def millisecondToHumanstr(milliseconds): """ Returns human readable time duration from milliseconds. - HH:MM:SS + HH:MM:SS:MMMM Parameters: milliseconds (str,int): time duration in milliseconds. """ - _milliseconds = cast(int, milliseconds) - hhmmsssm = timedelta(milliseconds=_milliseconds) - return str(hhmmsssm).split(".")[0] + milliseconds = int(milliseconds) + r = datetime.datetime.utcfromtimestamp(milliseconds / 1000) + f = r.strftime("%H:%M:%S.%f") + return f[:-2] def toList(value, itemcast=None, delim=','): diff --git a/tests/test_utils.py b/tests/test_utils.py index 1b172bc18..8587eca80 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -76,3 +76,9 @@ def test_utils_download(plex, episode): assert utils.download( episode.thumbUrl, plex._token, filename=episode.title, mocked=True ) + + + +def test_millisecondToHumanstr(): + res = utils.millisecondToHumanstr(1000) + assert res == "00:00:01:0000"