[gpodder] Re: Adding prefixes to "Save to -> Local folder" output

  • From: "Nemo" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "bokkenka" for DMARC)
  • To: gpodder@xxxxxxxxxxxxx
  • Date: Tue, 12 May 2020 10:56:52 +0000 (UTC)

 Attached is the extension script I use. It saves the files with names like...
 20200508---RealPython---Docker + Python for Data Science and Machine 
Learning.mp3

That's date---podcast---title.ext.

Save it in your gpodder/extensions directory. Run gpodder and go to Preferences 
/ Extensions. Scroll down to the Post Download section and check the box next 
to it.

Nemo  # -*- coding: utf-8 -*-
# Rename files after download based on the episode title
# Copyright (c) 2011-04-04 Thomas Perl <thp.io>
# Licensed under the same terms as gPodder itself

import logging
import os

import gpodder
from gpodder import util
from gpodder.model import PodcastEpisode

logger = logging.getLogger(__name__)

_ = gpodder.gettext

__title__ = _('NEMO Rename episodes after download')
__description__ = _('NEMO Rename episodes to "<Episode Title>.<ext>" on 
download')
__authors__ = 'NEMO, Bernd Schlapsi <brot@xxxxxxxx>, Thomas Perl 
<thp@xxxxxxxxxxx>'
__doc__ = 'none'
__payment__ = 'none'
__category__ = 'post-download'

DefaultConfig = {
    'add_sortdate': True,  # Add the sortdate as prefix
    'add_podcast_title': True,  # Add the podcast title as prefix
}


class gPodderExtension:
    def __init__(self, container):
        self.container = container
        self.config = self.container.config

    def on_episode_downloaded(self, episode):
        current_filename = episode.local_filename(create=False)

        new_filename = self.make_filename(current_filename, episode.title,
                                          episode.sortdate, 
episode.channel.title)

        if new_filename != current_filename:
            logger.info('Renaming: %s -> %s', current_filename, new_filename)
            os.rename(current_filename, new_filename)
            util.rename_episode_file(episode, new_filename)

    def make_filename(self, current_filename, title, sortdate, podcast_title):
        dirname = os.path.dirname(current_filename)
        filename = os.path.basename(current_filename)
        basename, ext = os.path.splitext(filename)
        ext = '.' + util.sanitize_filename(ext, 
PodcastEpisode.MAX_FILENAME_LENGTH)

        new_basename = []
        new_basename.append(sortdate.replace('-', ''))
        new_basename.append(podcast_title)
        new_basename.append(title)
        """
        if self.config.add_podcast_title:
            new_basename.insert(0, podcast_title)
        if self.config.add_sortdate:
            new_basename.insert(0, sortdate)
            """
        new_basename = '---'.join(new_basename)

        # Remove unwanted characters and shorten filename (#494)
        new_basename = util.sanitize_filename(new_basename, 
PodcastEpisode.MAX_FILENAME_LENGTH)
        # add extension after sanitization, to keep it even if filename is 
longer than limit
        # (it's unlikely that new_basename + ext is longer than is allowed on 
platform).
        ###new_filename = os.path.join(dirname, new_basename + ext)
        new_filename = os.path.join('/home/nemo/gPodder/downloaded', 
new_basename + ext)

        if new_filename == current_filename:
            return current_filename

        for filename in util.generate_names(new_filename):
            # Avoid filename collisions
            if not os.path.exists(filename):
                return filename

Other related posts: