~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/mobi/from_feeds.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import with_statement
2
 
__license__   = 'GPL v3'
3
 
__copyright__ = '2009, Kovid Goyal kovid@kovidgoyal.net'
4
 
__docformat__ = 'restructuredtext en'
5
 
 
6
 
'''
7
 
Convert feeds to MOBI ebook
8
 
'''
9
 
 
10
 
import sys, glob, os
11
 
from calibre.web.feeds.main import config as feeds2disk_config, USAGE, run_recipe
12
 
from calibre.ebooks.mobi.writer import config as oeb2mobi_config, oeb2mobi
13
 
from calibre.ptempfile import TemporaryDirectory
14
 
from calibre import strftime, sanitize_file_name
15
 
 
16
 
def config(defaults=None):
17
 
    c = feeds2disk_config(defaults=defaults)
18
 
    c.remove('lrf')
19
 
    c.remove('epub')
20
 
    c.remove('mobi')
21
 
    c.remove('output_dir')
22
 
    c.update(oeb2mobi_config(defaults=defaults))
23
 
    c.remove('encoding')
24
 
    c.remove('source_profile')
25
 
    c.add_opt('output', ['-o', '--output'], default=None,
26
 
              help=_('Output file. Default is derived from input filename.'))
27
 
    return c
28
 
 
29
 
def option_parser():
30
 
    c = config()
31
 
    return c.option_parser(usage=USAGE)
32
 
 
33
 
def convert(opts, recipe_arg, notification=None):
34
 
    opts.lrf  = False
35
 
    opts.epub = False
36
 
    opts.mobi = True
37
 
    if opts.debug:
38
 
        opts.verbose = 2
39
 
    parser = option_parser()
40
 
    with TemporaryDirectory('_feeds2mobi') as tdir:
41
 
        opts.output_dir = tdir
42
 
        recipe = run_recipe(opts, recipe_arg, parser, notification=notification)
43
 
        c = config()
44
 
        recipe_opts = c.parse_string(recipe.oeb2mobi_options)
45
 
        c.smart_update(recipe_opts, opts)
46
 
        opts = recipe_opts
47
 
        opf = glob.glob(os.path.join(tdir, '*.opf'))
48
 
        if not opf:
49
 
            raise Exception('Downloading of recipe: %s failed'%recipe_arg)
50
 
        opf = opf[0]
51
 
        
52
 
        if opts.output is None:
53
 
            fname = recipe.title + strftime(recipe.timefmt) + '.mobi'
54
 
            opts.output = os.path.join(os.getcwd(), sanitize_file_name(fname))
55
 
        
56
 
        print 'Generating MOBI...'
57
 
        opts.encoding = 'utf-8'
58
 
        opts.source_profile = 'Browser'
59
 
        oeb2mobi(opts, opf)
60
 
    
61
 
 
62
 
def main(args=sys.argv, notification=None, handler=None):
63
 
    parser = option_parser()
64
 
    opts, args = parser.parse_args(args)
65
 
    if len(args) != 2 and opts.feeds is None:
66
 
        parser.print_help()
67
 
        return 1
68
 
    recipe_arg = args[1] if len(args) > 1 else None
69
 
    convert(opts, recipe_arg, notification=notification)
70
 
        
71
 
    return 0
72
 
 
73
 
if __name__ == '__main__':
74
 
    sys.exit(main())
 
 
b'\\ No newline at end of file'