3 # Part of the Objavi2 package. This script imports e-books into Booki
5 # Copyright (C) 2009 Douglas Bagnall
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 from urllib2
import urlopen
, HTTPError
24 from urlparse
import urlsplit
25 from urllib
import unquote
28 from objavi
import epub
29 from objavi
.book_utils
import log
30 from objavi
.cgi_utils
import output_blob_and_exit
, parse_args
, print_template_and_exit
, output_blob_and_shut_up
31 from objavi
.cgi_utils
import is_name
, is_utf8
, is_url
32 from objavi
import config
34 IA_EPUB_URL
= "http://www.archive.org/download/%s/%s.epub"
36 def print_form_and_exit(booklink
):
37 print_template_and_exit('templates/espri.html',
38 {'booklink': booklink
, }
41 def async_start(content
, mimetype
):
42 """Begin (and in many cases, finish) http output.
43 In asynchronous modes, fork and close down stdout.
45 output_blob_and_shut_up(content
, mimetype
)
46 log(sys
.stdout
, sys
.stderr
, sys
.stdin
)
51 #log(sys.stdout, sys.stderr, sys.stdin)
54 def async_callback(callback_url
, **kwargs
):
55 """Call the callback url with each message."""
58 log('child %s is doing callback with message %r' % (pid
, kwargs
, ))
60 from urllib2
import urlopen
, URLError
61 from urllib
import urlencode
62 data
= urlencode(kwargs
)
64 f
= urlopen(callback_url
, data
)
69 log("ERROR in callback:\n %r\n %s %s" % (e
.url
, e
.code
, e
.msg
))
73 def espri(epuburl
, zipurl
):
82 e
.make_bookizip(zipurl
)
84 def ia_espri(book_id
):
85 epuburl
= IA_EPUB_URL
% (book_id
, book_id
)
87 zipurl
= '%s/%s.zip' % (config
.BOOKI_BOOK_DIR
, book_id
)
88 espri(epuburl
, zipurl
)
91 def inet_espri(epuburl
):
92 filename
= '_'.join(unquote(os
.path
.basename(urlsplit(epuburl
).path
)).split())
93 if filename
.lower().endswith('.epub'):
94 filename
= filename
[:-5]
95 zipurl
= '%s/%s-%s.zip' % (config
.BOOKI_BOOK_DIR
, filename
, time
.strftime('%F_%T'))
96 espri(epuburl
, zipurl
)
99 def wikibooks_espri(book_id
):
100 epuburl
= IA_EPUB_URL
% (book_id
, book_id
)
102 zipurl
= '%s/%s.zip' % (config
.BOOKI_BOOK_DIR
, book_id
)
103 espri(epuburl
, zipurl
)
109 'archive.org': {'function': ia_espri
},
110 'url': {'function': inet_espri
},
111 'wikibooks': {'function': wikibooks_espri
},
114 "source": SOURCES
.__contains
__,
116 "url": is_url
, #obsolete
117 'mode': ('zip', 'html', 'callback').__contains
__,
121 def ensure_backwards_compatibility(args
):
122 """Mutate args to match previous API"""
124 args
['source'] = 'url'
125 args
['book'] = args
['url']
126 if 'source' not in args
:
127 args
['source'] = 'archive.org'
130 if __name__
== '__main__':
132 args
= parse_args(ARG_VALIDATORS
)
133 ensure_backwards_compatibility(args
)
134 mode
= args
.get('mode', 'html')
135 book
= args
.get('book')
136 source
= args
.get('source', 'archive.org')
137 source_fn
= SOURCES
.get(source
)['function']
139 if mode
== 'callback':
140 callback_url
= args
['callback']
141 async_start('OK, got it... will call %r when done' % (callback_url
,),
147 url
= source_fn(book
)
148 book_link
= '<p>Download <a href="%s">%s</a>.</p>' % (url
, url
)
150 traceback
.print_exc()
152 book_link
= '<p>Error: <b>%s</b> when trying to get <b>%s</b></p>' % (e
, book
)
159 if mode
== 'callback':
160 async_callback(callback_url
, url
=url
)
162 elif mode
== 'zip' and url
is not None:
166 output_blob_and_exit(data
, config
.BOOKIZIP_MIMETYPE
,
167 os
.path
.basename(url
))
170 print_form_and_exit(book_link
)