Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mobipocket conversion working.

  • Loading branch information...
commit 83dfbd2fe5785f3dd4b2185d54d5e124305a5216 1 parent 578c353
@carlo authored
Showing with 114 additions and 13 deletions.
  1. +4 −3 Gemfile
  2. +8 −1 config.ru
  3. +3 −3 epub.rb
  4. +0 −6 mobi.rb
  5. +99 −0 mobipocket.rb
View
7 Gemfile
@@ -3,6 +3,7 @@ disable_rubygems
bundle_path ".gems/bundler_gems"
# List gems to bundle here:
-gem 'appengine-rack'
-gem 'dm-appengine'
-gem 'sinatra'
+gem "appengine-rack"
+gem "dm-appengine"
+gem "sinatra"
+gem "kindler"
View
9 config.ru
@@ -4,7 +4,14 @@ AppEngine::Rack.configure_app(
:version => 2
)
-require "mobi"
+require "mobipocket"
require "epub"
+get "/" do
+ "ephemera-conv. Yeeeeah, baby."
+end
+
+CACHE = AppEngine::Memcache.new
+CACHE_TIME = 300 # sec
+
run Sinatra::Application
View
6 epub.rb
@@ -1,6 +1,6 @@
-require 'sinatra'
-require 'dm-core'
+require "sinatra"
+require "dm-core"
-get '/epub/' do
+get "/epub" do
"epub"
end
View
6 mobi.rb
@@ -1,6 +0,0 @@
-require 'sinatra'
-require 'dm-core'
-
-get '/mobi/' do
- "mobi"
-end
View
99 mobipocket.rb
@@ -0,0 +1,99 @@
+require "sinatra"
+require "dm-core"
+require "mobi"
+require "appengine-apis/memcache"
+
+
+helpers do
+
+ def new_exth(type, data)
+ data += " "
+ Mobi::ExtendedHeader.new( type, data.unpack("C*") )
+ end
+
+
+ def send_book(book)
+ content_type 'application/octet-stream'
+ response['Content-Disposition'] = 'inline'
+ halt book
+ end
+
+end
+
+
+get "/mobi" do
+ CACHE.set("lol", "omg", 3)
+ # puts request.env['REMOTE_ADDR'].inspect
+ STDERR.puts mc.get("lol")
+
+ "mobi"
+end
+
+
+post "/mobi" do
+ unless params[:file] \
+ && params[:file][:type] == "text/html" \
+ && ( tmpfile = params[:file][:tempfile] ) \
+ && ( name = params[:file][:filename] ) \
+ && ( site = params[:site] ) \
+ && ( title = params[:title] )
+ halt 400, { "Content-Type" => "text/plain" }, "insufficient data"
+ end
+
+ key = [ request.env['REMOTE_ADDR'], name ].join("-")
+ if book = CACHE.get(key)
+ STDERR.puts "Cached request! FILE: #{name.inspect}; SITE: #{site}; TITLE: #{title}."
+ send_book(book)
+ else
+ STDERR.puts "New request! FILE: #{name.inspect}; SITE: #{site}; TITLE: #{title}."
+
+ while html = tmpfile.read(65536)
+ mobi = Mobi.new
+ mobi.content = html
+ mobi.name = title
+ mobi.title = title
+
+ mobi.header.type = "NEWS" # "HTML"
+ mobi.header.encoding = "UTF-8"
+ mobi.header.extended_headers << new_exth( 100, site )
+ mobi.header.extended_headers.each {|eh| mobi.header.exth_length += eh.length }
+ mobi.header.exth_count = mobi.header.extended_headers.size
+
+ io = StringIO.new
+ mobi.write(io)
+ io.seek(0)
+ book = io.read
+
+ CACHE.set(key, book, CACHE_TIME)
+ send_book(book)
+ end
+ end
+end
+
+
+
+
+
+=begin
+
+tmp_file = "_test/szde.html"
+mobi_file = "_test/test.mobi"
+
+
+mobi = Mobi.new
+mobi.content = File.read(tmp_file)
+mobi.name = "Politik kompakt: \"Afghanistan-Konflikt nicht zu gewinnen\""
+mobi.title = mobi.name
+
+mobi.header.type = "NEWS" # "HTML"
+mobi.header.encoding = "UTF-8"
+mobi.header.extended_headers << new_exth( 100, "sueddeutsche.de" )
+mobi.header.extended_headers.each do |eh|
+ mobi.header.exth_length += eh.length
+end
+
+mobi.header.exth_count = mobi.header.extended_headers.size
+
+mobi.write_file(mobi_file)
+
+=end
Please sign in to comment.
Something went wrong with that request. Please try again.