KG updates

This commit is contained in:
GRiker 2011-01-13 04:00:45 -07:00
commit b81e4923d0
10 changed files with 426 additions and 34 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

@ -8,12 +8,13 @@ __docformat__ = 'restructuredtext en'
globeandmail.com globeandmail.com
''' '''
import re
from calibre.web.feeds.news import BasicNewsRecipe from calibre.web.feeds.news import BasicNewsRecipe
class AdvancedUserRecipe1287083651(BasicNewsRecipe): class AdvancedUserRecipe1287083651(BasicNewsRecipe):
title = u'Globe & Mail' title = u'Globe & Mail'
__license__ = 'GPL v3' __author__ = 'Kovid Goyal'
__author__ = 'Szing'
oldest_article = 2 oldest_article = 2
no_stylesheets = True no_stylesheets = True
max_articles_per_feed = 100 max_articles_per_feed = 100
@ -38,24 +39,19 @@ class AdvancedUserRecipe1287083651(BasicNewsRecipe):
(u'Sports', u'http://www.theglobeandmail.com/auto/?service=rss') (u'Sports', u'http://www.theglobeandmail.com/auto/?service=rss')
] ]
keep_only_tags = [ preprocess_regexps = [
dict(name='h1'), (re.compile(r'<head.*?</head>', re.DOTALL), lambda m: '<head></head>'),
dict(name='h2', attrs={'id':'articletitle'}), (re.compile(r'<script.*?</script>', re.DOTALL), lambda m: ''),
dict(name='p', attrs={'class':['leadText', 'meta', 'leadImage', 'redtext byline', 'bodyText']}), ]
dict(name='div', attrs={'class':['news','articlemeta','articlecopy']}),
dict(name='id', attrs={'class':'article'}),
dict(name='table', attrs={'class':'todays-market'}),
dict(name='header', attrs={'id':'leadheader'})
]
remove_tags_before = dict(name='h1')
remove_tags = [ remove_tags = [
dict(name='div', attrs={'id':['tabInside', 'ShareArticles', 'topStories']}) dict(name='div', attrs={'id':['ShareArticles', 'topStories']}),
] dict(href=lambda x: x and 'tracking=' in x),
{'class':['articleTools', 'pagination', 'Ads', 'topad',
#this has to be here or the text in the article appears twice. 'breadcrumbs', 'footerNav', 'footerUtil', 'downloadlinks']}]
remove_tags_after = [dict(id='article')]
#Use the mobile version rather than the web version #Use the mobile version rather than the web version
def print_version(self, url): def print_version(self, url):
return url + '&service=mobile' return url.rpartition('?')[0] + '?service=mobile'

View File

@ -190,12 +190,11 @@ class RTFInput(InputFormatPlugin):
return name return name
def rasterize_wmf(self, name): def rasterize_wmf(self, name):
raise ValueError('Conversion of WMF images not supported') from calibre.utils.wmf.parse import wmf_unwrap
from calibre.utils.wmf import extract_raster_image
with open(name, 'rb') as f: with open(name, 'rb') as f:
data = f.read() data = f.read()
data = extract_raster_image(data) data = wmf_unwrap(data)
name = name.replace('.wmf', '.jpg') name = name.replace('.wmf', '.png')
with open(name, 'wb') as f: with open(name, 'wb') as f:
f.write(data) f.write(data)
return name return name

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from PyQt4.Qt import QDialog
from calibre.gui2.dialogs.drm_error_ui import Ui_Dialog
class DRMErrorMessage(QDialog, Ui_Dialog):
def __init__(self, parent=None, title=None):
QDialog.__init__(self, parent)
self.setupUi(self)
if title is not None:
t = unicode(self.msg.text())
self.msg.setText('<h2>%s</h2>%s'%(title, t))
self.resize(self.sizeHint())

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>417</width>
<height>235</height>
</rect>
</property>
<property name="windowTitle">
<string>This book is DRMed</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>132</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../../../resources/images.qrc">:/images/document-encrypt.png</pixmap>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="msg">
<property name="text">
<string>&lt;p&gt;This book is locked by &lt;b&gt;DRM&lt;/b&gt;. To learn more about DRM and why you cannot read or convert this book in calibre,
&lt;a href=&quot;http://bugs.calibre-ebook.com/wiki/DRM&quot;&gt;click here&lt;/a&gt;.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../../../../resources/images.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -468,12 +468,8 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
try: try:
if 'calibre.ebooks.DRMError' in job.details: if 'calibre.ebooks.DRMError' in job.details:
if not minz: if not minz:
d = error_dialog(self, _('Conversion Error'), from calibre.gui2.dialogs.drm_error import DRMErrorMessage
_('<p>Could not convert: %s<p>It is a ' d = DRMErrorMessage(self, job.description.split(':')[-1])
'<a href="%s">DRM</a>ed book. You must first remove the '
'DRM using third party tools.')%\
(job.description.split(':')[-1],
'http://bugs.calibre-ebook.com/wiki/DRM'))
d.setModal(False) d.setModal(False)
d.show() d.show()
self._modeless_dialogs.append(d) self._modeless_dialogs.append(d)

View File

@ -627,9 +627,8 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
QApplication.processEvents() QApplication.processEvents()
if worker.exception is not None: if worker.exception is not None:
if isinstance(worker.exception, DRMError): if isinstance(worker.exception, DRMError):
error_dialog(self, _('DRM Error'), from calibre.gui2.dialogs.drm_error import DRMErrorMessage
_('<p>This book is protected by <a href="%s">DRM</a>') DRMErrorMessage(self).exec_()
%'http://wiki.mobileread.com/wiki/DRM').exec_()
else: else:
r = getattr(worker.exception, 'reason', worker.exception) r = getattr(worker.exception, 'reason', worker.exception)
error_dialog(self, _('Could not open ebook'), error_dialog(self, _('Could not open ebook'),

View File

@ -0,0 +1,269 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import sys, struct
class WMFHeader(object):
'''
For header documentation, see
http://www.skynet.ie/~caolan/publink/libwmf/libwmf/doc/ora-wmf.html
'''
def __init__(self, data, log, verbose):
self.log, self.verbose = log, verbose
offset = 0
file_type, header_size, windows_version = struct.unpack_from('<HHH', data)
offset += 6
if header_size != 9:
raise ValueError('Not a WMF file')
file_size, num_of_objects = struct.unpack_from('<IH', data, offset)
if file_size * 2 != len(data):
# file size is in 2-byte units
raise ValueError('WMF file header specifies incorrect file size')
offset += 6
self.records_start_at = header_size * 2
class DIBHeader(object):
'''
See http://en.wikipedia.org/wiki/BMP_file_format
'''
def __init__(self, raw):
hsize = struct.unpack('<I', raw[:4])[0]
if hsize == 40:
parts = struct.unpack('<IiiHHIIIIII', raw[:hsize])
for i, attr in enumerate((
'header_size', 'width', 'height', 'color_planes',
'bits_per_pixel', 'compression', 'image_size',
'hres', 'vres', 'ncols', 'nimpcols'
)):
setattr(self, attr, parts[i])
elif hsize == 12:
parts = struct.unpack('<IHHHH', raw[:hsize])
for i, attr in enumerate((
'header_size', 'width', 'height', 'color_planes',
'bits_per_pixel')):
setattr(self, attr, parts[i])
else:
raise ValueError('Unsupported DIB header type of size: %d'%hsize)
self.bitmasks_size = 12 if getattr(self, 'compression', 0) == 3 else 0
self.color_table_size = 0
if self.bits_per_pixel != 24:
# See http://support.microsoft.com/kb/q81498/
# for all the gory Micro and soft details
self.color_table_size = getattr(self, 'ncols', 0) * 4
class WMF(object):
def __init__(self, log=None, verbose=0):
if log is None:
from calibre.utils.logging import default_log as log
self.log = log
self.verbose = verbose
self.map_mode = None
self.window_origin = None
self.window_extent = None
self.bitmaps = []
self.function_map = { # {{{
30: 'SaveDC',
53: 'RealizePalette',
55: 'SetPalEntries',
79: 'StartPage',
80: 'EndPage',
82: 'AbortDoc',
94: 'EndDoc',
258: 'SetBkMode',
259: 'SetMapMode',
260: 'SetROP2',
261: 'SetRelabs',
262: 'SetPolyFillMode',
263: 'SetStretchBltMode',
264: 'SetTextCharExtra',
295: 'RestoreDC',
298: 'InvertRegion',
299: 'PaintRegion',
300: 'SelectClipRegion',
301: 'SelectObject',
302: 'SetTextAlign',
313: 'ResizePalette',
332: 'ResetDc',
333: 'StartDoc',
496: 'DeleteObject',
513: 'SetBkColor',
521: 'SetTextColor',
522: 'SetTextJustification',
523: 'SetWindowOrg',
524: 'SetWindowExt',
525: 'SetViewportOrg',
526: 'SetViewportExt',
527: 'OffsetWindowOrg',
529: 'OffsetViewportOrg',
531: 'LineTo',
532: 'MoveTo',
544: 'OffsetClipRgn',
552: 'FillRegion',
561: 'SetMapperFlags',
564: 'SelectPalette',
1040: 'ScaleWindowExt',
1042: 'ScaleViewportExt',
1045: 'ExcludeClipRect',
1046: 'IntersectClipRect',
1048: 'Ellipse',
1049: 'FloodFill',
1051: 'Rectangle',
1055: 'SetPixel',
1065: 'FrameRegion',
1352: 'ExtFloodFill',
1564: 'RoundRect',
1565: 'PatBlt',
2071: 'Arc',
2074: 'Pie',
2096: 'Chord',
3379: 'SetDibToDev',
247: 'CreatePalette',
248: 'CreateBrush',
322: 'DibCreatePatternBrush',
496: 'DeleteObject',
505: 'CreatePatternBrush',
762: 'CreatePenIndirect',
763: 'CreateFontIndirect',
764: 'CreateBrushIndirect',
765: 'CreateBitmapIndirect',
804: 'Polygon',
805: 'Polyline',
1078: 'AnimatePalette',
1313: 'TextOut',
1336: 'PolyPolygon',
1574: 'Escape',
1583: 'DrawText',
1790: 'CreateBitmap',
1791: 'CreateRegion',
2338: 'BitBlt',
2368: 'DibBitblt',
2610: 'ExtTextOut',
2851: 'StretchBlt',
2881: 'DibStretchBlt',
3907: 'StretchDIBits'
} # }}}
def __call__(self, stream_or_data):
data = stream_or_data
if hasattr(data, 'read'):
data = data.read()
self.log.filter_level = self.log.DEBUG
self.header = WMFHeader(data, self.log, self.verbose)
offset = self.header.records_start_at
hsize = struct.calcsize('<IH')
self.records = []
while offset < len(data)-6:
size, func = struct.unpack_from('<IH', data, offset)
size *= 2 # Convert to bytes
offset += hsize
params = ''
delta = size - hsize
if delta > 0:
params = data[offset:offset+delta]
offset += delta
func = self.function_map.get(func, func)
if self.verbose > 3:
self.log.debug('WMF Record:', size, func)
self.records.append((func, params))
for rec in self.records:
f = getattr(self, rec[0], None)
if callable(f):
f(rec[1])
elif self.verbose > 2:
self.log.debug('Ignoring record:', rec[0])
self.has_raster_image = len(self.bitmaps) > 0
def SetMapMode(self, params):
if len(params) == 2:
self.map_mode = struct.unpack('<H', params)[0]
else:
self.log.warn('Invalid SetMapMode param')
def SetWindowOrg(self, params):
if len(params) == 4:
self.window_origin = struct.unpack('<HH', params)
elif len(params) == 8:
self.window_origin = struct.unpack('<II', params)
elif len(params) == 16:
self.window_origin = struct.unpack('<LL', params)
else:
self.log.warn('Invalid SetWindowOrg param', repr(params))
def SetWindowExt(self, params):
if len(params) == 4:
self.window_extent = struct.unpack('<HH', params)
elif len(params) == 8:
self.window_extent = struct.unpack('<II', params)
elif len(params) == 16:
self.window_extent = struct.unpack('<LL', params)
else:
self.log.warn('Invalid SetWindowExt param', repr(params))
def DibStretchBlt(self, raw):
offset = 0
fmt = '<IHHHHHHHH'
raster_op, src_height, src_width, y_src, x_src, dest_height, \
dest_width, y_dest, x_dest = struct.unpack_from('<IHHHHHHHH', raw, offset)
offset += struct.calcsize(fmt)
bmp_data = raw[offset:]
bmp = self.create_bmp_from_dib(bmp_data)
self.bitmaps.append(bmp)
def create_bmp_from_dib(self, raw):
size = len(raw) + 14
dh = DIBHeader(raw)
pixel_array_offset = dh.header_size + dh.bitmasks_size + \
dh.color_table_size
parts = ['BM', struct.pack('<I', size), '\0'*4, struct.pack('<I',
pixel_array_offset)]
return ''.join(parts) + raw
def to_png(self):
bmps = list(sorted(self.bitmaps, key=lambda x: len(x)))
bmp = bmps[-1]
from calibre.utils.magick import Image
img = Image()
img.load(bmp)
return img.export('png')
def wmf_unwrap(wmf_data):
'''
Return the largest embedded raster image in the WMF.
The returned data is in PNG format.
'''
w = WMF()
w(wmf_data)
if not w.has_raster_image:
raise ValueError('No raster image found in the WMF')
return w.to_png()
if __name__ == '__main__':
wmf = WMF(verbose=4)
wmf(open(sys.argv[-1], 'rb'))
open('/t/test.bmp', 'wb').write(wmf.bitmaps[0])

View File

@ -982,9 +982,12 @@ class ZipFile:
zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH]) zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
if fname != zinfo.orig_filename: if fname != zinfo.orig_filename:
raise BadZipfile, \ print ('WARNING: Header (%r) and directory (%r) filenames do not'
'File name in directory "%s" and header "%s" differ.' % ( ' match inside ZipFile')%(fname, zinfo.orig_filename)
zinfo.orig_filename, fname) print 'Using directory filename %r'%zinfo.orig_filename
#raise BadZipfile, \
# 'File name in directory "%r" and header "%r" differ.' % (
# zinfo.orig_filename, fname)
# check for encrypted flag & handle password # check for encrypted flag & handle password
is_encrypted = zinfo.flag_bits & 0x1 is_encrypted = zinfo.flag_bits & 0x1

View File

@ -700,10 +700,17 @@ class BasicNewsRecipe(Recipe):
for attr in self.remove_attributes: for attr in self.remove_attributes:
for x in soup.findAll(attrs={attr:True}): for x in soup.findAll(attrs={attr:True}):
del x[attr] del x[attr]
for base in list(soup.findAll(['base', 'iframe'])): for base in list(soup.findAll(['base', 'iframe', 'canvas', 'embed',
'command', 'datalist', 'video', 'audio'])):
base.extract() base.extract()
ans = self.postprocess_html(soup, first_fetch) ans = self.postprocess_html(soup, first_fetch)
# Nuke HTML5 tags
for x in ans.findAll(['article', 'aside', 'header', 'footer', 'nav',
'figcaption', 'figure', 'section']):
x.name = 'div'
if job_info: if job_info:
url, f, a, feed_len = job_info url, f, a, feed_len = job_info
try: try: