Files
kontor/python/kontor-cli/kontor/controllers/media.py
T
2025-01-21 16:46:12 +01:00

106 lines
3.4 KiB
Python

from pathlib import Path
from cement import Controller, ex
from kontor_schema import KontorDB
from kontor_video import VideoLink
class Media(Controller):
class Meta:
label = 'media'
stacked_type = 'nested'
stacked_on = 'clibase'
@ex(
label='update',
help='update title for mediafiles',
)
def update_title(self):
db = self.app.kontor_db
updates = db.get_update_list()
self.app.log.info(f"found {len(updates)} links for update")
for file_id, url in updates.items():
link = VideoLink(url)
title = link.get_title()
if title is not None:
db.update_entry('media_file', file_id, {'title': title, 'review': 0,})
@ex(
label='download',
help='download all marked videos',
arguments=[
(['-d', '--dir'],
{'help': 'directory to store videos',
'action': 'store',
'dest': 'media_dir'})
],
)
def download(self):
data = {
'media_dir': '/data/media',
}
if self.app.pargs.media_dir is not None:
data['media_dir'] = self.app.pargs.media_dir
db = self.app.kontor_db
downloads = db.get_download_list()
self.app.log.info(f"found {len(downloads)} links for download")
for file_id, url in downloads.items():
link = VideoLink(url)
file_name = link.download(download_dir=data['media_dir'])
if file_name is None:
db.update_entry('media_file', file_id, {'file_name': None, 'should_download': 1})
else:
download_file = Path(file_name)
download_file.with_name(f"{file_id}{download_file.suffix}")
link.file_name = download_file.name
link.should_download = 0
link.cloud_link = download_file.absolute()
db.update_entry('media_file', file_id,
{
'file_name': download_file.name,
'should_download': 0,
'cloud_link': download_file.absolute()}
)
@ex(
help='add url to database',
arguments=[
(['-u', '--url'],
{'help': 'link to downloadable video',
'action': 'store',
'dest': 'link'})
],
)
def add(self):
data = {
'link_url': None
}
if self.app.pargs.link is not None:
data['link_url'] = self.app.pargs.link
if self.app.pargs.dry_run:
print(f"add url {data['link_url']} to database")
else:
db = self.app.kontor_db
result = db.add_link(self.app.pargs.link)
self.log.info(result)
else:
print("no url was given.")
@ex(
help='check files if existing',
arguments=[
(['-d', '--dir'],
{'help': 'directory to store videos',
'action': 'store',
'dest': 'media_dir'})
],
)
def check(self):
data = {
'media_dir': '/data/media',
}
if self.app.pargs.media_dir is not None:
data['media_dir'] = self.app.pargs.media_dir
db = self.app.kontor_db
db.check_files()