Skip to content

Commit

Permalink
add readme, fix content in atom feeds, fix options
Browse files Browse the repository at this point in the history
  • Loading branch information
blackmad committed Jan 8, 2011
1 parent f475b28 commit 77034cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
16 changes: 16 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Based on mostly unmodified code from http://code.google.com/p/python-epub-builder/

You'll want to install genshi & lxml
easy_install lxml genshi

For converting to kindle format, you need either:
ebook-convert from Calibre http://calibre-ebook.com
/Applications/calibre.app/Contents//MacOS/ebook-convert on OS X

kindlegen from http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621
--Kindlegen can be fussy about bad HTML, ebook-convert is much more forgiving

This script will follow rel=next links on atom feeds, so it can mirror an entire blogspot blog.

You can preview your books in the "Kindle Previewer", also available at http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621

36 changes: 23 additions & 13 deletions build-book.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Washington's I.T. Guy | The American Prospect
# <!******************** -->

kindlegen = '~/Downloads/kindlegen/kindlegen'
kindlegen = 'kindlegen'
ebook_convert = 'ebook-convert'

from time import strftime
Expand Down Expand Up @@ -119,16 +119,18 @@ def get_all_entries(url, entries):
def process_feed(file):
entries = []
get_all_entries(file, entries)
# book.impl.addMeta('Last Updated: ', d.feed.updated)

for entry in reversed(entries):
print 'adding: %s' % entry.title
if full_content:
content = entry_get(entry, 'content')
if not content:
content = entry_get(entry, 'summary')
add_article(entry_get(entry, 'title'),
'<html><body><h1>%s</h1><h3>%s</h3>%s</body></html>' % (
entry_get(entry, 'title'),
entry_date_str(entry),
entry_get(entry, 'summary')))
content))
else:
print entry.link
data = get_data(entry.link)
Expand Down Expand Up @@ -201,11 +203,11 @@ def add_article(title, data):
help='Use content from RSS/Atom feed, rather than fetching links')
parser.add_option('-s', '--strip', action='store_true', dest='use_boilerpipe', default=True,
help='Attempt to strip boilerplate from web content using https://boilerpipe-web.appspot.com/')
parser.add_option('-t', '--title', dest='title', default=None,
parser.add_option('-t', '--title', dest='title',
help='Book Title')
parser.add_option('-a', '--author', dest='author', default=None,
parser.add_option('-a', '--author', dest='author',
help='Book Author')
parser.add_option('-o', '--output', dest='output', default=None,
parser.add_option('-o', '--output', dest='output',
help='Base name of output file & directory')
parser.add_option('-e', '--epub', action='store_true', dest='generate_epub', default=True,
help='Generate an epub file')
Expand All @@ -218,6 +220,7 @@ def add_article(title, data):

if len(args) < 1:
print 'Must specify an input source'
parser.print_help()
sys.exit(1)

file = args[0]
Expand All @@ -232,14 +235,16 @@ def add_article(title, data):
full_content = options.full_content
print 'Full content feed? ', 'Yes' if full_content else 'No'

output_tmp = '/tmp/' + output + '_tmp'
output_tmp = '/tmp/' + os.path.basename(output) + '_tmp'
try:
os.mkdir(output_tmp)
except:
pass

book = ez_epub.Book()
sections = []
if not options.output and book_title:
output = book_title
print "Processing: " + file
print 'Output: %s' % output
if 'rss' in file or 'http' in file or 'atom' in file or 'xml' in file:
Expand All @@ -252,22 +257,27 @@ def add_article(title, data):
for row in csv_reader:
process(row)

if not book_title:
book_title = output
if not options.output:
output = book_title

print 'Title: %s' % book_title
print 'Author: %s' % author
book.title = book_title
book.authors = [author]
book.sections = sections
generate_epub = args.generate_epub or args.use_calibre
generate_epub = options.generate_epub or options.use_calibre
print 'Generating epub? ', 'Yes' if generate_epub else 'No'
book.make(book_title, generate_epub)
book.make(output, generate_epub)

if args.use_kindlegen:
if options.use_kindlegen:
kindle_cmd = '%s "%s/OEBPS/content.opf" -o "%s.mobi"' % (kindlegen, output, output)
print(kindle_cmd)
os.system(kindle_cmd)
mv_cmd = 'mv "%s/OEBPS/%s.mobi" "%s.mobi"' % (output, output, output)
print (mv_cmd)
os.system(mv_cmd)

if args.use_calibre:
if options.use_calibre:
calibre_cmd = '%s "%s.epub" -o "%s.mobi"' % (ebook_convert, output, output)
print(calibre_cmd)
os.system(calibre_cmd)

0 comments on commit 77034cb

Please sign in to comment.