podcast: duration is lazy-loaded
This commit is contained in:
parent
4d175b9451
commit
9854445f18
1 changed files with 14 additions and 8 deletions
|
@ -6,11 +6,11 @@ import sys
|
||||||
from subprocess import CalledProcessError, check_output
|
from subprocess import CalledProcessError, check_output
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from larigira.fsutils import download_http
|
|
||||||
from lxml import html
|
from lxml import html
|
||||||
from pytimeparse.timeparse import timeparse
|
from pytimeparse.timeparse import timeparse
|
||||||
|
|
||||||
|
from larigira.fsutils import download_http
|
||||||
|
|
||||||
|
|
||||||
def delta_humanreadable(tdelta):
|
def delta_humanreadable(tdelta):
|
||||||
if tdelta is None:
|
if tdelta is None:
|
||||||
|
@ -35,8 +35,9 @@ def get_duration(url):
|
||||||
url,
|
url,
|
||||||
]
|
]
|
||||||
).split(b"\n")
|
).split(b"\n")
|
||||||
except CalledProcessError as exc:
|
except CalledProcessError:
|
||||||
raise ValueError("error probing `%s`" % url) from exc
|
logging.exception("error probing `%s`", url)
|
||||||
|
return 0
|
||||||
duration = next(l for l in lineout if l.startswith(b"duration="))
|
duration = next(l for l in lineout if l.startswith(b"duration="))
|
||||||
value = duration.split(b"=")[1]
|
value = duration.split(b"=")[1]
|
||||||
return int(float(value))
|
return int(float(value))
|
||||||
|
@ -45,9 +46,7 @@ def get_duration(url):
|
||||||
class Audio(object):
|
class Audio(object):
|
||||||
def __init__(self, url, duration=None, date=None):
|
def __init__(self, url, duration=None, date=None):
|
||||||
self.url = url
|
self.url = url
|
||||||
if duration is None:
|
self._duration = duration
|
||||||
duration = get_duration(url.encode("utf-8"))
|
|
||||||
self.duration = duration
|
|
||||||
self.date = date
|
self.date = date
|
||||||
self.end_date = datetime.datetime(
|
self.end_date = datetime.datetime(
|
||||||
9999, 12, 31, tzinfo=datetime.timezone.utc
|
9999, 12, 31, tzinfo=datetime.timezone.utc
|
||||||
|
@ -58,9 +57,16 @@ class Audio(object):
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Audio {} ({} {})>".format(
|
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
|
@property
|
||||||
def urls(self):
|
def urls(self):
|
||||||
return [self.url]
|
return [self.url]
|
||||||
|
|
Loading…
Reference in a new issue