2 # -*- coding: utf-8 -*-
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/>.
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
35 from gpodder
import util
38 logger
= logging
.getLogger(__name__
)
42 __title__
= _('Tin Foil Hat Shownotes')
43 __description__
= _('extract the shownotes from the "Tin Foil Hat" podcast')
44 __author__
= 'Bernd Schlapsi <brot@gmx.info>'
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
)
64 logger
.info('Extension "%s" is being loaded.' % __title__
)
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
:
77 if TFH_TITLE
not in [e
.channel
.title
for e
in episodes
if e
.file_exists()]:
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
)
88 imagefile
= self
.extract_image(filename
)
92 shownotes
= self
.extract_shownotes(imagefile
)
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
)
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"""
107 if eyeD3
.isMp3File(filename
):
108 tag
= eyeD3
.Mp3AudioFile(filename
).getTag()
109 images
= tag
.getImages()
111 tempdir
= tempfile
.gettempdir()
113 imagefile
= img
.getDefaultFileName()
114 img
.writeFile(path
=tempdir
, name
=imagefile
)
115 imagefile
= "%s/%s" % (tempdir
, imagefile
)
117 logger
.info(u
'No image found in %s' % filename
)
124 def extract_shownotes(self
, imagefile
, remove_image
=True):
125 """extract shownotes from the FRONT_COVER.jpeg"""
127 password
= 'tinfoilhat'
128 shownotes_file
= '/tmp/shownotes.txt'
130 if not os
.path
.exists(imagefile
):
133 cmd
= STEGHIDE_CMD
% {
136 'file': shownotes_file
138 myprocess
= subprocess
.Popen(shlex
.split(cmd
),
139 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
140 (stdout
, stderr
) = myprocess
.communicate()
145 if myprocess
.returncode
== 0:
147 f
= open(shownotes_file
, 'r')
148 shownotes
= unicode(f
.read(), "utf-8")
151 logger
.error(u
'Error extracting shownotes from the image file %s' % imagefile
)