1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3####
4# 01/2011 Bernd Schlapsi <brot@gmx.info>
5#
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.
10#
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.
15#
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/>.
18#
19# Dependencies:
20# * python-mutagen (Mutagen is a Python module to handle audio metadata)
21#
22# This extension scripts removes coverart from all downloaded ogg files.
23# The reason for this script is that my media player (MEIZU SL6)
24# couldn't handle ogg files with included coverart
25
26import logging
27import os
28
29from mutagen.oggvorbis import OggVorbis
30
31import gpodder
32
33logger = logging.getLogger(__name__)
34
35
36_ = gpodder.gettext
37
38__title__ = _('Remove cover art from OGG files')
39__description__ = _('removes coverart from all downloaded ogg files')
40__authors__ = 'Bernd Schlapsi <brot@gmx.info>'
41__doc__ = 'https://gpodder.github.io/docs/extensions/removeoggcover.html'
42__payment__ = 'https://flattr.com/submit/auto?user_id=BerndSch&url=http://wiki.gpodder.org/wiki/Extensions/RemoveOGGCover'
43__category__ = 'post-download'
44
45
46DefaultConfig = {
47    'context_menu': True,  # Show item in context menu
48}
49
50
51class gPodderExtension:
52    def __init__(self, container):
53        self.container = container
54        self.config = self.container.config
55
56    def on_episode_downloaded(self, episode):
57        self.rm_ogg_cover(episode)
58
59    def on_episodes_context_menu(self, episodes):
60        if not self.config.context_menu:
61            return None
62
63        episode_types = [e.mime_type for e in episodes
64                         if e.mime_type is not None and e.file_exists()]
65        if 'audio/ogg' not in episode_types:
66            return None
67
68        return [(_('Remove cover art'), self._rm_ogg_covers)]
69
70    def _rm_ogg_covers(self, episodes):
71        for episode in episodes:
72            self.rm_ogg_cover(episode)
73
74    def rm_ogg_cover(self, episode):
75        filename = episode.local_filename(create=False)
76        if filename is None:
77            return
78
79        basename, extension = os.path.splitext(filename)
80
81        if episode.file_type() != 'audio':
82            return
83
84        if extension.lower() != '.ogg':
85            return
86
87        try:
88            ogg = OggVorbis(filename)
89
90            found = False
91            for key in ogg.keys():
92                if key.startswith('cover'):
93                    found = True
94                    ogg.pop(key)
95
96            if found:
97                logger.info('Removed cover art from OGG file: %s', filename)
98                ogg.save()
99        except Exception as e:
100            logger.warn('Failed to remove OGG cover: %s', e, exc_info=True)
101