2 # -*- coding: utf-8 -*-
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/>.
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
32 logger
= logging
.getLogger(__name__
)
35 from mutagen
import File
36 mutagen_installed
= True
38 logger
.error( '(tagging extension) Could not find mutagen')
39 mutagen_installed
= False
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>'
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
:
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
):
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:
81 # read title+album from gPodder database
82 info
['album'] = episode
.channel
.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()
89 # convert pubDate to string
91 pubDate
= datetime
.datetime
.fromtimestamp(episode
.pubDate
)
92 info
['pubDate'] = pubDate
.strftime('%Y-%m-%d %H:%M')
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')
99 info
['pubDate'] = None
103 def write_info2file(self
, info
):
104 # open file with mutagen
105 audio
= File(info
['filename'], easy
=True)
109 # write title+album information into audio files
110 if audio
.tags
is None:
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']
120 if self
.container
.config
.genre_tag
is not None:
121 audio
.tags
['genre'] = self
.container
.config
.genre_tag
123 audio
.tags
['genre'] = ''
126 if info
['pubDate'] is not None:
127 audio
.tags
['date'] = info
['pubDate']