BasicNewsRecipe re-useable cover and masthead code

Allow the code to generate covers and mastheads be more easily re-used
in sub classes.
This commit is contained in:
Kovid Goyal 2013-06-09 10:53:56 +05:30
parent 31c2b8b3d1
commit 8eca0cd853

View File

@ -1160,26 +1160,7 @@ class BasicNewsRecipe(Recipe):
self.report_progress(0, _('Trying to download cover...')) self.report_progress(0, _('Trying to download cover...'))
self.download_cover() self.download_cover()
self.report_progress(0, _('Generating masthead...')) self.report_progress(0, _('Generating masthead...'))
self.masthead_path = None self.resolve_masthead()
try:
murl = self.get_masthead_url()
except:
self.log.exception('Failed to get masthead url')
murl = None
if murl is not None:
# Try downloading the user-supplied masthead_url
# Failure sets self.masthead_path to None
self.download_masthead(murl)
if self.masthead_path is None:
self.log.info("Synthesizing mastheadImage")
self.masthead_path = os.path.join(self.output_dir, 'mastheadImage.jpg')
try:
self.default_masthead_image(self.masthead_path)
except:
self.log.exception('Failed to generate default masthead image')
self.masthead_path = None
if self.test: if self.test:
feeds = feeds[:2] feeds = feeds[:2]
@ -1268,7 +1249,10 @@ class BasicNewsRecipe(Recipe):
if not cu: if not cu:
return return
cdata = None cdata = None
if os.access(cu, os.R_OK): if hasattr(cu, 'read'):
cdata = cu.read()
cu = getattr(cu, 'name', 'cover.jpg')
elif os.access(cu, os.R_OK):
cdata = open(cu, 'rb').read() cdata = open(cu, 'rb').read()
else: else:
self.report_progress(1, _('Downloading cover from %s')%cu) self.report_progress(1, _('Downloading cover from %s')%cu)
@ -1305,13 +1289,19 @@ class BasicNewsRecipe(Recipe):
self.cover_path = None self.cover_path = None
def _download_masthead(self, mu): def _download_masthead(self, mu):
ext = mu.rpartition('.')[-1] if hasattr(mu, 'rpartition'):
if '?' in ext: ext = mu.rpartition('.')[-1]
ext = '' if '?' in ext:
ext = ''
else:
ext = mu.name.rpartition('.')[-1]
ext = ext.lower() if ext else 'jpg' ext = ext.lower() if ext else 'jpg'
mpath = os.path.join(self.output_dir, 'masthead_source.'+ext) mpath = os.path.join(self.output_dir, 'masthead_source.'+ext)
outfile = os.path.join(self.output_dir, 'mastheadImage.jpg') outfile = os.path.join(self.output_dir, 'mastheadImage.jpg')
if os.access(mu, os.R_OK): if hasattr(mu, 'read'):
with open(mpath, 'wb') as mfile:
mfile.write(mu.read())
elif os.access(mu, os.R_OK):
with open(mpath, 'wb') as mfile: with open(mpath, 'wb') as mfile:
mfile.write(open(mu, 'rb').read()) mfile.write(open(mu, 'rb').read())
else: else:
@ -1329,6 +1319,27 @@ class BasicNewsRecipe(Recipe):
except: except:
self.log.exception("Failed to download supplied masthead_url") self.log.exception("Failed to download supplied masthead_url")
def resolve_masthead(self):
self.masthead_path = None
try:
murl = self.get_masthead_url()
except:
self.log.exception('Failed to get masthead url')
murl = None
if murl is not None:
# Try downloading the user-supplied masthead_url
# Failure sets self.masthead_path to None
self.download_masthead(murl)
if self.masthead_path is None:
self.log.info("Synthesizing mastheadImage")
self.masthead_path = os.path.join(self.output_dir, 'mastheadImage.jpg')
try:
self.default_masthead_image(self.masthead_path)
except:
self.log.exception('Failed to generate default masthead image')
self.masthead_path = None
def default_cover(self, cover_file): def default_cover(self, cover_file):
''' '''
Create a generic cover for recipes that dont have a cover Create a generic cover for recipes that dont have a cover