Quote:
Originally Posted by Starson17
There is a <div> tag in the original that has a style with a defined width that matches the width of the image before rotation, and a text-align attribute that centers the image in that width.
I don't have a nook, but try this...
|
Starson17, you are the best. The recipe works perfectly now. It is centered and everything. Thank you so, so much for all of the time you put into this. For those of you who want the final recipe here it is. Be sure to thank Starson17 for all of his hard work. Thanks again!
Spoiler:
Code:
from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ebooks.BeautifulSoup import BeautifulSoup
import re
import calibre.utils.PythonMagickWand as pw
import calibre.utils.PythonMagickWand
class Explosm(BasicNewsRecipe):
title = 'Explosm Rotated'
__author__ = 'Starson17'
description = 'Explosm'
language = 'en'
use_embedded_content= False
no_stylesheets = True
oldest_article = 24
remove_javascript = True
remove_empty_feeds = True
max_articles_per_feed = 10
feeds = [
(u'Explosm Feed', u'http://feeds.feedburner.com/Explosm')
]
keep_only_tags = [dict(name='div', attrs={'align':'center'})]
remove_tags = [dict(name='span'),
dict(name='table')]
def postprocess_html(self, soup, first):
#process all the images. assumes that the new html has the correct path
for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')):
iurl = tag['src']
print 'resizing image' + iurl
with pw.ImageMagick():
img = pw.NewMagickWand()
p = pw.NewPixelWand()
if img < 0:
raise RuntimeError('Out of memory')
if not pw.MagickReadImage(img, iurl):
severity = pw.ExceptionType(0)
msg = pw.MagickGetException(img, byref(severity))
raise IOError('Failed to read image from: %s: %s'
%(iurl, msg))
width = pw.MagickGetImageWidth(img)
height = pw.MagickGetImageHeight(img)
if( width > height ) :
print 'Rotate image'
pw.MagickRotateImage(img, p, 90)
if not pw.MagickWriteImage(img, iurl):
raise RuntimeError('Failed to save image to %s'%iurl)
pw.DestroyMagickWand(img)
for divtag in soup.findAll('div'):
del(divtag['style'])
return soup
extra_css = '''
h1{font-family:Arial,Helvetica,sans-serif; font-weight:bold;font-size:large;}
h2{font-family:Arial,Helvetica,sans-serif; font-weight:normal;font-size:small;}
p{font-family:Arial,Helvetica,sans-serif;font-size:small;}
body{font-family:Helvetica,Arial,sans-serif;font-size:small;}
'''