From 503b6976532ad4b3235995dc3890f830675c712c Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 23 May 2009 11:55:53 -0400 Subject: [PATCH] RocketBook (rb) input. --- src/calibre/customize/builtins.py | 3 +- src/calibre/ebooks/rb/__init__.py | 11 +++ src/calibre/ebooks/rb/input.py | 24 ++++++ src/calibre/ebooks/rb/reader.py | 131 ++++++++++++++++++++++++++++++ src/calibre/ebooks/txt/input.py | 1 - 5 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 src/calibre/ebooks/rb/__init__.py create mode 100644 src/calibre/ebooks/rb/input.py create mode 100644 src/calibre/ebooks/rb/reader.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 8e0f243056..4524f1f04c 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -321,6 +321,7 @@ from calibre.ebooks.lit.input import LITInput from calibre.ebooks.fb2.input import FB2Input from calibre.ebooks.fb2.output import FB2Output from calibre.ebooks.odt.input import ODTInput +from calibre.ebooks.rb.input import RBInput from calibre.ebooks.rtf.input import RTFInput from calibre.ebooks.html.input import HTMLInput from calibre.ebooks.comic.input import ComicInput @@ -351,7 +352,7 @@ from calibre.devices.bebook.driver import BEBOOK, BEBOOK_MINI plugins = [HTML2ZIP, EPUBInput, MOBIInput, PDBInput, PDFInput, HTMLInput, TXTInput, OEBOutput, TXTOutput, PDFOutput, LITInput, ComicInput, FB2Input, FB2Output, ODTInput, RTFInput, EPUBOutput, RecipeInput, PMLInput, - PMLOutput, MOBIOutput, PDBOutput, LRFOutput, LITOutput] + PMLOutput, MOBIOutput, PDBOutput, LRFOutput, LITOutput, RBInput] plugins += [PRS500, PRS505, PRS700, CYBOOKG3, KINDLE, KINDLE2, BLACKBERRY, EB600, JETBOOK, BEBOOK, BEBOOK_MINI] plugins += [x for x in list(locals().values()) if isinstance(x, type) and \ diff --git a/src/calibre/ebooks/rb/__init__.py b/src/calibre/ebooks/rb/__init__.py new file mode 100644 index 0000000000..7c048a95c8 --- /dev/null +++ b/src/calibre/ebooks/rb/__init__.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2009, John Schember ' +__docformat__ = 'restructuredtext en' + +HEADER = '\xb0\x0c\xb0\x0c\x02\x00NUVO\x00\x00\x00\x00' + +class RocketBookError(Exception): + pass + diff --git a/src/calibre/ebooks/rb/input.py b/src/calibre/ebooks/rb/input.py new file mode 100644 index 0000000000..8b05c1d42e --- /dev/null +++ b/src/calibre/ebooks/rb/input.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2009, John Schember ' +__docformat__ = 'restructuredtext en' + +import os + +from calibre.ebooks.rb.reader import Reader +from calibre.customize.conversion import InputFormatPlugin + +class RBInput(InputFormatPlugin): + + name = 'RB Input' + author = 'John Schember' + description = 'Convert RB files to HTML' + file_types = set(['rb']) + + def convert(self, stream, options, file_ext, log, + accelerators): + reader = Reader(stream, log, options.input_encoding) + opf = reader.extract_content(os.getcwd()) + + return opf diff --git a/src/calibre/ebooks/rb/reader.py b/src/calibre/ebooks/rb/reader.py new file mode 100644 index 0000000000..bc70905539 --- /dev/null +++ b/src/calibre/ebooks/rb/reader.py @@ -0,0 +1,131 @@ +import os.path +import zlib +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2009, John Schember ' +__docformat__ = 'restructuredtext en' + +import os +import struct +from urllib import unquote as urlunquote + +from calibre import CurrentDir +from calibre.ebooks.rb import HEADER +from calibre.ebooks.rb import RocketBookError +from calibre.ebooks.metadata.rb import get_metadata +from calibre.ebooks.metadata.opf2 import OPFCreator + +class RBToc(list): + + class Item(object): + + def __init__(self, name='', size=0, offset=0, flags=0): + self.name = name + self.size = size + self.offset = offset + self.flags = flags + + +class Reader(object): + + def __init__(self, stream, log, encoding=None): + self.stream = stream + self.log = log + self.encoding = encoding + + self.verify_file() + + self.mi = get_metadata(self.stream) + self.toc = self.get_toc() + + def read_i32(self): + return struct.unpack('