podcast: duration is lazy-loaded

This commit is contained in:
boyska 2021-03-03 00:02:31 +01:00
parent 4d175b9451
commit 9854445f18

View file

@ -6,11 +6,11 @@ import sys
from subprocess import CalledProcessError, check_output
import requests
from larigira.fsutils import download_http
from lxml import html
from pytimeparse.timeparse import timeparse
from larigira.fsutils import download_http
def delta_humanreadable(tdelta):
if tdelta is None:
@ -35,8 +35,9 @@ def get_duration(url):
url,
]
).split(b"\n")
except CalledProcessError as exc:
raise ValueError("error probing `%s`" % url) from exc
except CalledProcessError:
logging.exception("error probing `%s`", url)
return 0
duration = next(l for l in lineout if l.startswith(b"duration="))
value = duration.split(b"=")[1]
return int(float(value))
@ -45,9 +46,7 @@ def get_duration(url):
class Audio(object):
def __init__(self, url, duration=None, date=None):
self.url = url
if duration is None:
duration = get_duration(url.encode("utf-8"))
self.duration = duration
self._duration = duration
self.date = date
self.end_date = datetime.datetime(
9999, 12, 31, tzinfo=datetime.timezone.utc
@ -58,9 +57,16 @@ class Audio(object):
def __repr__(self):
return "<Audio {} ({} {})>".format(
self.url, self.duration, delta_humanreadable(self.age)
self.url, self._duration, delta_humanreadable(self.age)
)
@property
def duration(self):
"""lazy-calculation"""
if self._duration is None:
self._duration = get_duration(self.url.encode("utf-8"))
return self._duration
@property
def urls(self):
return [self.url]