140 lines
4.3 KiB
Python
140 lines
4.3 KiB
Python
"""
|
|
Podcast routes for the Podcastrr application.
|
|
"""
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app
|
|
from app.models.podcast import Podcast, Episode
|
|
from app.models.database import db
|
|
from app.services.podcast_search import search_podcasts
|
|
from app.services.podcast_downloader import download_episode
|
|
|
|
podcasts_bp = Blueprint('podcasts', __name__)
|
|
|
|
@podcasts_bp.route('/')
|
|
def index():
|
|
"""
|
|
Display all tracked podcasts.
|
|
"""
|
|
podcasts = Podcast.query.all()
|
|
return render_template('podcasts/index.html',
|
|
title='Podcasts',
|
|
podcasts=podcasts)
|
|
|
|
@podcasts_bp.route('/search', methods=['GET', 'POST'])
|
|
def search():
|
|
"""
|
|
Search for podcasts.
|
|
"""
|
|
results = []
|
|
|
|
if request.method == 'POST':
|
|
query = request.form.get('query', '')
|
|
if query:
|
|
results = search_podcasts(query)
|
|
|
|
return render_template('podcasts/search.html',
|
|
title='Search Podcasts',
|
|
results=results)
|
|
|
|
@podcasts_bp.route('/add/<string:podcast_id>', methods=['POST'])
|
|
def add(podcast_id):
|
|
"""
|
|
Add a podcast to track.
|
|
"""
|
|
# Check if podcast already exists
|
|
existing = Podcast.query.filter_by(external_id=podcast_id).first()
|
|
|
|
if existing:
|
|
flash('Podcast is already being tracked.', 'info')
|
|
else:
|
|
# Get podcast details from service
|
|
podcast_data = search_podcasts(podcast_id=podcast_id)
|
|
|
|
if podcast_data:
|
|
podcast = Podcast(
|
|
title=podcast_data['title'],
|
|
author=podcast_data['author'],
|
|
description=podcast_data['description'],
|
|
image_url=podcast_data['image_url'],
|
|
feed_url=podcast_data['feed_url'],
|
|
external_id=podcast_id
|
|
)
|
|
|
|
db.session.add(podcast)
|
|
db.session.commit()
|
|
|
|
flash('Podcast added successfully!', 'success')
|
|
else:
|
|
flash('Failed to add podcast.', 'error')
|
|
|
|
return redirect(url_for('podcasts.index'))
|
|
|
|
@podcasts_bp.route('/<int:podcast_id>')
|
|
def view(podcast_id):
|
|
"""
|
|
View a specific podcast and its episodes.
|
|
"""
|
|
podcast = Podcast.query.get_or_404(podcast_id)
|
|
episodes = Episode.query.filter_by(podcast_id=podcast_id).order_by(Episode.published_date.desc()).all()
|
|
|
|
return render_template('podcasts/view.html',
|
|
title=podcast.title,
|
|
podcast=podcast,
|
|
episodes=episodes)
|
|
|
|
@podcasts_bp.route('/delete/<int:podcast_id>', methods=['POST'])
|
|
def delete(podcast_id):
|
|
"""
|
|
Delete a podcast from tracking.
|
|
"""
|
|
podcast = Podcast.query.get_or_404(podcast_id)
|
|
|
|
# Delete associated episodes
|
|
Episode.query.filter_by(podcast_id=podcast_id).delete()
|
|
|
|
db.session.delete(podcast)
|
|
db.session.commit()
|
|
|
|
flash(f'Podcast "{podcast.title}" has been deleted.', 'success')
|
|
return redirect(url_for('podcasts.index'))
|
|
|
|
@podcasts_bp.route('/download/<int:episode_id>')
|
|
def download(episode_id):
|
|
"""
|
|
Download an episode.
|
|
"""
|
|
episode = Episode.query.get_or_404(episode_id)
|
|
|
|
try:
|
|
download_path = download_episode(episode)
|
|
flash(f'Episode downloaded to {download_path}', 'success')
|
|
except Exception as e:
|
|
flash(f'Download failed: {str(e)}', 'error')
|
|
|
|
return redirect(url_for('podcasts.view', podcast_id=episode.podcast_id))
|
|
|
|
@podcasts_bp.route('/update/<int:podcast_id>', methods=['POST'])
|
|
def update(podcast_id):
|
|
"""
|
|
Manually update a podcast to fetch new episodes.
|
|
"""
|
|
podcast = Podcast.query.get_or_404(podcast_id)
|
|
|
|
try:
|
|
from app.services.podcast_updater import update_podcast
|
|
|
|
logger.info(f"Manually updating podcast: {podcast.title} (ID: {podcast.id})")
|
|
stats = update_podcast(podcast_id)
|
|
|
|
if stats['new_episodes'] > 0:
|
|
flash(f"Found {stats['new_episodes']} new episodes!", 'success')
|
|
else:
|
|
flash("No new episodes found.", 'info')
|
|
|
|
logger.info(f"Manual update completed: {stats}")
|
|
except Exception as e:
|
|
logger.error(f"Error updating podcast: {str(e)}")
|
|
flash(f"Error updating podcast: {str(e)}", 'error')
|
|
|
|
return redirect(url_for('podcasts.view', podcast_id=podcast_id))
|