import json import pathlib from datetime import datetime from pathlib import Path from typing import Optional, Generator from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ARRAY, ForeignKey, Computed from sqlalchemy.orm import relationship, Session, deferred from sqlalchemy.orm.collections import InstrumentedList from wrolpi.common import Base, tsvector, ModelHelper, logger, get_media_directory from wrolpi.dates import now, TZDateTime from wrolpi.db import get_db_curs from wrolpi.downloader import Download, download_manager from wrolpi.errors import UnknownVideo, UnknownFile, UnknownDirectory, InvalidDownload from wrolpi.media_path import MediaPathType, MediaPath logger = logger.getChild(__name__) class Video(ModelHelper, Base): __tablename__ = 'video' id = Column(Integer, primary_key=True) channel_id = Column(Integer, ForeignKey('channel.id')) channel = relationship('Channel', primaryjoin='Video.channel_id==Channel.id', back_populates='videos') idempotency = Column(String) validated = Column(Boolean, default=False) # File paths caption_path = Column(MediaPathType) description_path = Column(MediaPathType) ext = Column(String) info_json_path = Column(MediaPathType) poster_path = Column(MediaPathType) video_path = Column(MediaPathType) caption = deferred(Column(String)) # slow to fetch censored = Column(Boolean, default=False) duration = Column(Integer) favorite = Column(TZDateTime) modification_datetime = Column(TZDateTime) size = Column(Integer) source_id = Column(String) title = Column(String) upload_date = Column(TZDateTime) url = Column(String) view_count = Column(Integer) viewed = Column(TZDateTime) textsearch = deferred( Column(tsvector, Computed('''to_tsvector('english'::regconfig, ((COALESCE(title, ''::text) || ' '::text) || COALESCE(caption, ''::text)))'''))) def __repr__(self): video_path = self.video_path if self.video_path and isinstance(self.video_path, MediaPath): video_path = self.video_path.path.relative_to(get_media_directory()) return f'