Extensions: Various fixes and clean-ups from review
[gpodder.git] / share / gpodder / extensions / tagging.py
blob86faf339b458c7523778694c5d9c442ca29c177b
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ####
4 # 01/2011 Bernd Schlapsi <brot@gmx.info>
6 # This script is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Dependencies:
20 # * python-mutagen (Mutagen is a Python module to handle audio metadata)
22 # This extension script adds episode title and podcast title to the audio file
23 # The episode title is written into the title tag
24 # The podcast title is written into the album tag
26 import datetime
27 import os
29 import gpodder
31 import logging
32 logger = logging.getLogger(__name__)
34 try:
35 from mutagen import File
36 mutagen_installed = True
37 except:
38 logger.error( '(tagging extension) Could not find mutagen')
39 mutagen_installed = False
41 _ = gpodder.gettext
43 __title__ = _('Tag downloaded files using Mutagen')
44 __description__ = _('Add episode and podcast titles to MP3/OGG tags')
45 __author__ = 'Bernd Schlapsi <brot@gmx.info>'
48 DefaultConfig = {
49 'strip_album_from_title': True,
50 'genre_tag': 'Podcast',
54 class gPodderExtension:
55 def __init__(self, container):
56 self.container = container
58 def on_episode_downloaded(self, episode):
59 # exit if mutagen is not installed
60 if not mutagen_installed:
61 return
63 info = self.read_episode_info(episode)
64 self.write_info2file(info)
66 logger.info(u'tagging.on_episode_downloaded(%s/%s)' % (episode.channel.title, episode.title))
68 def read_episode_info(self, episode):
69 info = {
70 'filename': None,
71 'album': None,
72 'title': None,
73 'pubDate': None
76 # read filename (incl. file path) from gPodder database
77 info['filename'] = episode.local_filename(create=False, check_only=True)
78 if info['filename'] is None:
79 return
81 # read title+album from gPodder database
82 info['album'] = episode.channel.title
83 title = episode.title
84 if (self.container.config.strip_album_from_title and title and info['album'] and title.startswith(info['album'])):
85 info['title'] = title[len(info['album']):].lstrip()
86 else:
87 info['title'] = title
89 # convert pubDate to string
90 try:
91 pubDate = datetime.datetime.fromtimestamp(episode.pubDate)
92 info['pubDate'] = pubDate.strftime('%Y-%m-%d %H:%M')
93 except:
94 try:
95 # since version 3 the published date has a new/other name
96 pubDate = datetime.datetime.fromtimestamp(episode.published)
97 info['pubDate'] = pubDate.strftime('%Y-%m-%d %H:%M')
98 except:
99 info['pubDate'] = None
101 return info
103 def write_info2file(self, info):
104 # open file with mutagen
105 audio = File(info['filename'], easy=True)
106 if audio is None:
107 return
109 # write title+album information into audio files
110 if audio.tags is None:
111 audio.add_tags()
113 # write album+title
114 if info['album'] is not None:
115 audio.tags['album'] = info['album']
116 if info['title'] is not None:
117 audio.tags['title'] = info['title']
119 # write genre tag
120 if self.container.config.genre_tag is not None:
121 audio.tags['genre'] = self.container.config.genre_tag
122 else:
123 audio.tags['genre'] = ''
125 # write pubDate
126 if info['pubDate'] is not None:
127 audio.tags['date'] = info['pubDate']
129 audio.save()