podcastrr/app/models/podcast.py
2025-06-14 22:53:38 -07:00

89 lines
No EOL
3.1 KiB
Python

"""
Podcast and Episode models for Podcastrr.
"""
from datetime import datetime
from app.models.database import db
class Podcast(db.Model):
"""
Model representing a podcast.
"""
__tablename__ = 'podcasts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
author = db.Column(db.String(255))
description = db.Column(db.Text)
image_url = db.Column(db.String(512))
feed_url = db.Column(db.String(512), nullable=False, unique=True)
external_id = db.Column(db.String(255), unique=True)
last_updated = db.Column(db.DateTime, default=datetime.utcnow)
last_checked = db.Column(db.DateTime, default=datetime.utcnow)
auto_download = db.Column(db.Boolean, default=False)
# Relationships
episodes = db.relationship('Episode', backref='podcast', lazy='dynamic', cascade='all, delete-orphan')
def __repr__(self):
return f'<Podcast {self.title}>'
def to_dict(self):
"""
Convert podcast to dictionary for API responses.
"""
return {
'id': self.id,
'title': self.title,
'author': self.author,
'description': self.description,
'image_url': self.image_url,
'feed_url': self.feed_url,
'external_id': self.external_id,
'last_updated': self.last_updated.isoformat() if self.last_updated else None,
'last_checked': self.last_checked.isoformat() if self.last_checked else None,
'auto_download': self.auto_download,
'episode_count': self.episodes.count()
}
class Episode(db.Model):
"""
Model representing a podcast episode.
"""
__tablename__ = 'episodes'
id = db.Column(db.Integer, primary_key=True)
podcast_id = db.Column(db.Integer, db.ForeignKey('podcasts.id'), nullable=False)
title = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text)
audio_url = db.Column(db.String(512), nullable=False)
image_url = db.Column(db.String(512))
published_date = db.Column(db.DateTime)
duration = db.Column(db.Integer) # Duration in seconds
file_size = db.Column(db.Integer) # Size in bytes
episode_number = db.Column(db.String(50))
guid = db.Column(db.String(512), unique=True)
downloaded = db.Column(db.Boolean, default=False)
file_path = db.Column(db.String(512))
def __repr__(self):
return f'<Episode {self.title}>'
def to_dict(self):
"""
Convert episode to dictionary for API responses.
"""
return {
'id': self.id,
'podcast_id': self.podcast_id,
'title': self.title,
'description': self.description,
'audio_url': self.audio_url,
'image_url': self.image_url,
'published_date': self.published_date.isoformat() if self.published_date else None,
'duration': self.duration,
'file_size': self.file_size,
'episode_number': self.episode_number,
'guid': self.guid,
'downloaded': self.downloaded,
'file_path': self.file_path
}