-
Notifications
You must be signed in to change notification settings - Fork 7
/
tfh_shownotes_hook.py
113 lines (96 loc) · 3.74 KB
/
tfh_shownotes_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# -*- coding: utf-8 -*-
####
# 10/2010 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 3 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-eyed3 (eyeD3 python library - http://eyed3.nicfit.net/)
# * steghide (steganography program - http://steghide.sourceforge.net/)
#
# The script extract the shownotes from the "Tin Foil Hat" podcast
# You can find the instructions how to extract shownotes for the
# "Tin Foil Hat" podcast here:
# http://cafeninja.blogspot.com/2010/10/tin-foil-hat-show-episode-001.html
import gpodder
import os
import subprocess
import tempfile
from gpodder.liblogger import log
try:
import eyeD3
except:
log( '(tfh shownotes hook) Could not find eyeD3')
TFH_TITLE='Tin Foil Hat'
class gPodderHooks(object):
def __init__(self):
log('"Tin Foil Hat" shownote extractor extension is initializing.')
def __extract_image(self, filename):
"""
extract image from the podcast file
"""
imagefile = None
try:
if eyeD3.isMp3File(filename):
tag = eyeD3.Mp3AudioFile(filename).getTag()
images = tag.getImages()
if images:
tempdir = tempfile.gettempdir()
img = images[0]
imagefile = img.getDefaultFileName()
img.writeFile(path=tempdir, name=imagefile)
imagefile = "%s/%s" % (tempdir, imagefile)
else:
log(u'No image found in %s' % filename)
except:
pass
return imagefile
def __extract_shownotes(self, imagefile):
"""
extract shownotes from the FRONT_COVER.jpeg
"""
shownotes = None
password = 'tinfoilhat'
shownotes_file = '/tmp/shownotes.txt'
myprocess = subprocess.Popen(['steghide', 'extract', '-f', '-p', password,
'-sf', imagefile, '-xf', shownotes_file],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = myprocess.communicate()
os.remove(imagefile)
if stderr.startswith('wrote extracted data to'):
#read shownote file
f = open(shownotes_file)
shownotes = f.read()
f.close()
else:
log(u'Error extracting shownotes from the image file %s' % imagefile)
return shownotes
def on_episode_downloaded(self, episode):
if episode.channel.title == TFH_TITLE:
filename = episode.local_filename(create=False, check_only=True)
if filename is None:
return
imagefile = self.__extract_image(filename)
if imagefile is None:
return
shownotes = self.__extract_shownotes(imagefile)
if shownotes is None:
return
# save shownotes in the database
if episode.description.find(shownotes) == -1:
episode.description = "%s\n\n<pre>%s</pre>" % (episode.description, shownotes)
episode.save()
episode.db.commit()
log(u'updated shownotes for podcast: (%s/%s)' % (episode.channel.title, episode.title))