2022-02-22 12:57:48 +09:00
|
|
|
import time
|
|
|
|
|
2022-02-22 19:43:09 +09:00
|
|
|
import aiofiles
|
|
|
|
from quart import current_app
|
|
|
|
|
2022-02-22 12:57:48 +09:00
|
|
|
from anonstream.segments import get_playlist, Offline
|
2022-02-22 19:43:09 +09:00
|
|
|
from anonstream.wrappers import ttl_cache_async
|
|
|
|
|
|
|
|
CONFIG = current_app.config
|
2022-02-22 12:57:48 +09:00
|
|
|
|
2022-02-22 19:43:09 +09:00
|
|
|
@ttl_cache_async(CONFIG['STREAM_TITLE_CACHE_LIFETIME'])
|
|
|
|
async def get_stream_title():
|
|
|
|
try:
|
|
|
|
async with aiofiles.open(CONFIG['STREAM_TITLE']) as fp:
|
|
|
|
title = await fp.read(8192)
|
|
|
|
except FileNotFoundError:
|
|
|
|
title = ''
|
|
|
|
return title
|
2022-02-14 19:16:09 +09:00
|
|
|
|
2022-02-22 12:57:48 +09:00
|
|
|
def get_stream_uptime(rounded=True):
|
|
|
|
try:
|
|
|
|
playlist, mtime = get_playlist()
|
|
|
|
except Offline:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
last_modified_ago = time.time() - mtime
|
|
|
|
|
|
|
|
n_segments = playlist.media_sequence + len(playlist.segments)
|
|
|
|
duration = playlist.target_duration * n_segments
|
|
|
|
|
|
|
|
uptime = duration + last_modified_ago
|
|
|
|
uptime = round(uptime, 2) if rounded else uptime
|
|
|
|
return uptime
|
|
|
|
|
|
|
|
def is_online():
|
|
|
|
try:
|
|
|
|
get_playlist()
|
|
|
|
except Offline:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|