-
Notifications
You must be signed in to change notification settings - Fork 7
/
tagging_hook.py
97 lines (80 loc) · 2.87 KB
/
tagging_hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
# -*- coding: utf-8 -*-
####
# 01/2011 Bernd Schlapsi <[email protected]>
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Dependencies:
# * python-mutagen (Mutagen is a Python module to handle audio metadata)
#
# This hook script adds episode title and podcast title to the audio file
# The episode title is written into the title tag
# The podcast title is written into the album tag
import datetime
import os
import gpodder
from gpodder.liblogger import log
try:
from mutagen import File
mutagen_installed = True
except:
log( '(tagging hook) Could not find mutagen')
mutagen_installed = False
## settings
strip_album_from_title = True
genre_tag = u'Podcast'
class gPodderHooks(object):
def __init__(self):
log('tagging extension is initializing.')
def on_episode_downloaded(self, episode):
# exit if mutagen is not installed
if not mutagen_installed:
return
# read filename (incl. file path) from gPodder database
filename = episode.local_filename(create=False, check_only=True)
if filename is None:
return
# open file with mutagen
audio = File(filename, easy=True)
if audio is None:
return
# read title+album from gPodder database
album = episode.channel.title
title = episode.title
if (strip_album_from_title and title and album and title.startswith(album)):
title = title[len(album):].lstrip()
# convert pubDate to string
try:
pubDate = datetime.datetime.fromtimestamp(episode.pubDate).strftime('%Y-%m-%d %H:%M')
except:
pubDate = None
# write title+album information into audio files
if audio.tags is None:
audio.add_tags()
# write album+title
if album is not None:
audio.tags['album'] = album
if title is not None:
audio.tags['title'] = title
# write genre tag
if genre_tag is not None:
audio.tags['genre'] = genre_tag
else:
audio.tags['genre'] = ''
# write pubDate
if pubDate is not None:
audio.tags['date'] = pubDate
audio.save()
log(u'tagging.on_episode_downloaded(%s/%s)' % (episode.channel.title, episode.title))