KindleCG 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d1d82944b95dbf3604090ecdb82aec451a30964
4
+ data.tar.gz: 31029f6e48a1b38bc5761284182d30da4d9852c8
5
+ SHA512:
6
+ metadata.gz: 6b6b16eec0216586da49de7dc723956d85a83b73074c3af122d79b95eb7f8d606a1207f6f64abfdca240b9af071937098520375b4473a75ab95f8bd419943c10
7
+ data.tar.gz: 5baa89ab6bddcc950ef128395b64f4894b31fd6dce1916c54dfad6e06529824109dcbff9c092c02af32ff540c3512e911ba5c2dfe6d5390978f2da0b22428bbb
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ kindle_collections
19
+ .ruby-gemset
20
+ .rspec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/KindleCG.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'KindleCG/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "KindleCG"
8
+ spec.version = KindleCG::VERSION
9
+ spec.authors = ["Antonio Lorusso"]
10
+ spec.email = ["antonio.lorusso@gmail.com"]
11
+ spec.description = %q{Mirror your ebooks directory tree into kindle collections}
12
+ spec.summary = %q{kindlecg helps you manage your kindle collections using the filesystem directory tree}
13
+ spec.homepage = "http://antoniolorusso.com/KindleCG"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "thor", "~> 0.18.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Antonio Lorusso
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # KindleCG
2
+ ## Directory Tree to Kindle Collections generator
3
+
4
+ A command line ruby script that will generate collections in your kindle based on the directory structure of your ebooks.
5
+ The library to understand the mobi format are a straight port/refactor of the python plugin for calibre.
6
+
7
+ ## Installation
8
+
9
+ $ gem install KindleCG
10
+
11
+ ## Usage
12
+
13
+ After installing the gem you will have access to a command line tool kindlecg.
14
+ Just run it and will generate collections based on the directory structure of your ebooks in your kindle device.
15
+
16
+ *WARNING* The script is under development. It override your current collections!!!
17
+
18
+ ## Contributing
19
+
20
+ 1. Fork it
21
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
22
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
23
+ 4. Push to the branch (`git push origin my-new-feature`)
24
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/kindlecg ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ Signal.trap("INT") { exit 1 }
3
+
4
+ require 'KindleCG'
5
+ require 'KindleCG/cli'
6
+
7
+ KindleCG::CLI.start(ARGV)
@@ -0,0 +1,25 @@
1
+ require 'thor'
2
+ require 'KindleCG'
3
+
4
+ module KindleCG
5
+ class CLI < Thor
6
+ desc 'generate', 'generate and save the collections'
7
+ def generate
8
+ check
9
+
10
+ kindle = Generator.new
11
+ kindle.generate_collections
12
+ kindle.save
13
+ end
14
+
15
+ desc 'check', 'check if a kindle is attached to the computer'
16
+ def check
17
+ unless File.directory?(KindleCG.os_mountpoint)
18
+ raise Thor::Error, "Could not find any kindle attached to #{KindleCG.os_mountpoint}"
19
+ exit 1
20
+ else
21
+ say "Your kindle is attached to #{KindleCG.os_mountpoint}. Very good! :)", :green
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module KindleCG
2
+ class Collections
3
+ attr_reader :collections
4
+
5
+ def initialize
6
+ @collections = {}
7
+ end
8
+
9
+ def add(name, language = 'en-US')
10
+ collection_name = "#{name}@#{language}"
11
+ @collections[collection_name] = { items: [] }
12
+ collection_name
13
+ end
14
+
15
+ def add_item_to_collection(collection_name, ebook)
16
+ @collections[collection_name][:items] << ebook.hash
17
+ end
18
+
19
+ def to_json
20
+ JSON.generate(@collections)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,50 @@
1
+ require 'KindleCG/ebook/sectionizer'
2
+ require 'KindleCG/ebook/mobi'
3
+ require 'KindleCG/ebook/pdf'
4
+ require 'digest'
5
+ require 'forwardable'
6
+
7
+ module KindleCG
8
+ module Ebook
9
+
10
+ class EbookError < StandardError; end
11
+
12
+ class Ebook
13
+ extend Forwardable
14
+
15
+ def_delegators :@metadata, :title, :asin, :type
16
+
17
+ def initialize(file)
18
+ @file = file
19
+ @metadata = initialize_metadata(file)
20
+ end
21
+
22
+ def hash
23
+ if @metadata.type && @metadata.asin
24
+ "#%s^%s" % [@metadata.asin, @metadata.type]
25
+ else
26
+ folder = File.dirname(@file)
27
+ filename = File.basename(@file)
28
+ "*" + Digest::SHA1.hexdigest([KindleCG.device_mountpoint.to_path, folder.match(/(documents).*/)[0], filename].join("/"))
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def initialize_metadata(file)
35
+ case get_extension(file)
36
+ when /(mobi|azw)/
37
+ Mobi.new(file)
38
+ when /(pdf)/
39
+ Pdf.new(file)
40
+ else
41
+ raise EbookError, "Can't handle this book type"
42
+ end
43
+ end
44
+
45
+ def get_extension(file)
46
+ @_extension ||= File.basename(file).downcase.split('.')[-1]
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ module KindleCG
2
+ module Ebook
3
+ class Mobi
4
+
5
+ attr_reader :title, :mobi_type, :text_encoding, :exth
6
+
7
+ def initialize(file)
8
+ sections = KindleCG::Ebook::Sectionizer.new(file, 'rb')
9
+ @header = sections.load_section(0)
10
+ @len_mobi = @header[20..-1].unpack('L>')[0] + 16
11
+ end
12
+
13
+ def exth
14
+ len_exth, = @header[@len_mobi+4..-1].unpack('L>')
15
+ exth_records = @header[@len_mobi..@len_mobi+len_exth][12..-1]
16
+ _exth = {}
17
+ while exth_records.length > 8 do
18
+ rectype, reclen = exth_records.unpack('L>L>')
19
+ recdata = exth_records[8...reclen]
20
+ _exth[rectype] = recdata
21
+ exth_records = exth_records[reclen..-1]
22
+ end
23
+ _exth
24
+ end
25
+
26
+ def mobi_type
27
+ @header[24..-1].unpack('L>')
28
+ end
29
+
30
+ def title
31
+ mobi_raw = @header[0..@len_mobi]
32
+ titleoffset, titlelen = mobi_raw[84..-1].unpack('L>L>')
33
+ @header[titleoffset...titleoffset+titlelen]
34
+ end
35
+
36
+ def text_encoding
37
+ @header[28..-1].unpack('L>')
38
+ end
39
+
40
+ def type
41
+ exth[501]
42
+ end
43
+
44
+ def asin
45
+ exth[113]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module KindleCG
2
+ module Ebook
3
+ class Pdf
4
+ def initialize(file)
5
+ # TODO: not sure what to do with pdfs yet
6
+ end
7
+
8
+ def title
9
+ # TODO: implement if ever needed
10
+ end
11
+
12
+ def asin
13
+ # TODO: implement if ever needed
14
+ end
15
+
16
+ def type
17
+ # TODO: implement if ever needed
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ module KindleCG
2
+ module Ebook
3
+ class Sectionizer
4
+
5
+ attr_accessor :file, :header, :sections, :identity
6
+
7
+ def initialize(file, perm)
8
+ @file = File.open(file, perm)
9
+ @header = @file.read(78)
10
+ @sections = split_sections(@header, @file)
11
+ end
12
+
13
+ def load_section(section)
14
+ before, after = @sections[section..section+2]
15
+ @file.seek(before)
16
+ @file.read(after - before)
17
+ end
18
+
19
+ def identity
20
+ @header[0x3C...0x3C+8]
21
+ end
22
+
23
+ private
24
+
25
+ def split_sections(header, file)
26
+ num_sections, = header[76..-1].unpack 'S!>'
27
+ raw_sections = file.read(num_sections*8)
28
+ sections_unpacked = raw_sections.unpack ("L>%d" % (num_sections*2))
29
+ sections_unpacked.values_at(*(0..sections_unpacked.size).step(2)).compact + [0xfffffff]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module KindleCG
2
+ VERSION = "0.0.1"
3
+ end
data/lib/KindleCG.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'KindleCG/version'
2
+ require 'json'
3
+ require 'KindleCG/collections'
4
+ require 'KindleCG/ebook/ebook'
5
+
6
+ module KindleCG
7
+
8
+ class << self
9
+ attr_writer :device_mountpoint, :os_mountpoint
10
+
11
+ def device_mountpoint
12
+ @device_mountpoint ||= Pathname.new '/mnt/us'
13
+ end
14
+
15
+ def os_mountpoint
16
+ @os_mountpoint ||= Pathname.new '/Volumes/Kindle'
17
+ end
18
+
19
+ def system_path
20
+ device_mountpoint.join('system').to_path
21
+ end
22
+
23
+ def documents_path
24
+ device_mountpoint.join('documents').to_path
25
+ end
26
+
27
+ def os_documents_path
28
+ os_mountpoint.join('documents').to_path
29
+ end
30
+
31
+ def os_collections_path
32
+ os_mountpoint.join('system/collections.json').to_path
33
+ end
34
+ end
35
+
36
+ class Generator
37
+ def initialize
38
+ @collections = Collections.new
39
+ end
40
+
41
+ def generate_collections
42
+ generate_tree(KindleCG.os_documents_path)
43
+ @collections
44
+ end
45
+
46
+ def generate_tree(path, current_collection = nil)
47
+ Dir.foreach(path) do |item|
48
+ next if item[0] == '.'
49
+
50
+ fullpath = [path, item].join("/")
51
+
52
+ if File.directory?(fullpath)
53
+ new_collection = @collections.add(relative_path(fullpath))
54
+ generate_tree(fullpath, new_collection)
55
+ else
56
+ begin
57
+ ebook = Ebook::Ebook.new(fullpath)
58
+ @collections.add_item_to_collection(current_collection, ebook) unless current_collection.nil?
59
+ rescue Ebook::EbookError
60
+ next
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def save
67
+ IO.write(KindleCG.os_collections_path, @collections.to_json)
68
+ end
69
+
70
+ private
71
+
72
+ def relative_path(path)
73
+ _path = path.dup
74
+ _path.slice!(KindleCG.os_documents_path + '/')
75
+ _path == KindleCG.os_documents_path ? "" : _path
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: KindleCG
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Antonio Lorusso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Mirror your ebooks directory tree into kindle collections
70
+ email:
71
+ - antonio.lorusso@gmail.com
72
+ executables:
73
+ - kindlecg
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .ruby-version
79
+ - Gemfile
80
+ - KindleCG.gemspec
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/kindlecg
85
+ - lib/KindleCG.rb
86
+ - lib/KindleCG/cli.rb
87
+ - lib/KindleCG/collections.rb
88
+ - lib/KindleCG/ebook/ebook.rb
89
+ - lib/KindleCG/ebook/mobi.rb
90
+ - lib/KindleCG/ebook/pdf.rb
91
+ - lib/KindleCG/ebook/sectionizer.rb
92
+ - lib/KindleCG/version.rb
93
+ - spec/spec_helper.rb
94
+ homepage: http://antoniolorusso.com/KindleCG
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: kindlecg helps you manage your kindle collections using the filesystem directory
118
+ tree
119
+ test_files:
120
+ - spec/spec_helper.rb