Extensions: Various fixes and clean-ups from review
[gpodder.git] / share / gpodder / extensions / tfh_shownotes.py
blobe9b9cd650a966d0122ac4ca06d4d4c93aa868f81
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ####
4 # 10/2010 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 3 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-eyed3 (eyeD3 python library - http://eyed3.nicfit.net/)
21 # * steghide (steganography program - http://steghide.sourceforge.net/)
23 # The script extract the shownotes from the "Tin Foil Hat" podcast
24 # You can find the instructions how to extract shownotes for the
25 # "Tin Foil Hat" podcast here:
26 # http://cafeninja.blogspot.com/2010/10/tin-foil-hat-show-episode-001.html
28 import eyeD3
29 import os
30 import shlex
31 import subprocess
32 import tempfile
34 import gpodder
35 from gpodder import util
37 import logging
38 logger = logging.getLogger(__name__)
40 _ = gpodder.gettext
42 __title__ = _('Tin Foil Hat Shownotes')
43 __description__ = _('extract the shownotes from the "Tin Foil Hat" podcast')
44 __author__ = 'Bernd Schlapsi <brot@gmx.info>'
47 DefaultConfig = {
48 'context_menu': True,
51 TFH_TITLE = 'Tin Foil Hat'
52 STEGHIDE_CMD = 'steghide extract -f -p %(pwd)s -sf %(img)s -xf %(file)s'
55 class gPodderExtension():
56 def __init__(self, container):
57 self.container = container
59 program = shlex.split(STEGHIDE_CMD)[0]
60 if not util.find_command(program):
61 raise ImportError("Couldn't find program '%s'" % program)
63 def on_load(self):
64 logger.info('Extension "%s" is being loaded.' % __title__)
66 def on_unload(self):
67 logger.info('Extension "%s" is being unloaded.' % __title__)
69 def _download_shownotes(self, episodes):
70 for episode in episodes:
71 self.on_episode_downloaded(episode)
73 def on_episodes_context_menu(self, episodes):
74 if not self.container.config.context_menu:
75 return None
77 if TFH_TITLE not in [e.channel.title for e in episodes if e.file_exists()]:
78 return None
80 return [(self.container.metadata.title, self._download_shownotes)]
82 def on_episode_downloaded(self, episode):
83 if episode.channel.title.startswith(TFH_TITLE):
84 filename = self.get_filename(episode)
85 if filename is None:
86 return
88 imagefile = self.extract_image(filename)
89 if imagefile is None:
90 return
92 shownotes = self.extract_shownotes(imagefile)
93 if shownotes is None:
94 return
96 # save shownotes in the database
97 if episode.description.find(shownotes) == -1:
98 episode.description = "%s\n\n<pre>%s</pre>" % (episode.description, shownotes)
99 episode.save()
100 episode.db.commit()
101 logger.info(u'updated shownotes for podcast: (%s/%s)' % (episode.channel.title, episode.title))
103 def extract_image(self, filename):
104 """extract image from the podcast file"""
105 imagefile = None
106 try:
107 if eyeD3.isMp3File(filename):
108 tag = eyeD3.Mp3AudioFile(filename).getTag()
109 images = tag.getImages()
110 if images:
111 tempdir = tempfile.gettempdir()
112 img = images[0]
113 imagefile = img.getDefaultFileName()
114 img.writeFile(path=tempdir, name=imagefile)
115 imagefile = "%s/%s" % (tempdir, imagefile)
116 else:
117 logger.info(u'No image found in %s' % filename)
118 except:
119 pass
121 return imagefile
124 def extract_shownotes(self, imagefile, remove_image=True):
125 """extract shownotes from the FRONT_COVER.jpeg"""
126 shownotes = None
127 password = 'tinfoilhat'
128 shownotes_file = '/tmp/shownotes.txt'
130 if not os.path.exists(imagefile):
131 return shownotes
133 cmd = STEGHIDE_CMD % {
134 'pwd': password,
135 'img': imagefile,
136 'file': shownotes_file
138 myprocess = subprocess.Popen(shlex.split(cmd),
139 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
140 (stdout, stderr) = myprocess.communicate()
142 if remove_image:
143 os.remove(imagefile)
145 if myprocess.returncode == 0:
146 #read shownote file
147 f = open(shownotes_file, 'r')
148 shownotes = unicode(f.read(), "utf-8")
149 f.close()
150 else:
151 logger.error(u'Error extracting shownotes from the image file %s' % imagefile)
153 return shownotes