capitalist (owner)

Fork Of

gist: 128400 by feldpost Print To Kindle

Revisions

gist: 128460 Download_button fork
public
Public Clone URL: git://gist.github.com/128460.git
Embed All Files: show embed
.kindle_export.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# PRINT TO KINDLE in Mac OSX (for Kindle DX)
#
# HUH?
# - Kindle file depository:
# - drag any supported Kindle file to a local folder
# - file will be automatically transfered whenever Kindle is connected
# - "Print To Kindle" Print menu option (PDF):
# - when Kindle is connected it will print as PDF straight to the Kindle (from anywhere)
# - when Kindle is not connected, it will print to the local folder and is transfered as soon as Kindle is connected
 
# SETUP:
# 1. Create a local Kindle folder. Default is:
# ~/Desktop/Kindle
#
# 2. Set your CONFIGURATION options below
#
# 3. Save as .kindle_export.rb to your home folder
#
# 4. Set up a cron job (if you want automatic transfers):
# crontab -e
# */5 * * * * ruby ~/.kindle_export.rb >/dev/null 2>&1
#
# 5. Download the "Print to Kindle" Automator action at:
# http://www.feldpost.com/mementos/assets/print_to_kindle.zip
#
# 6. Unzip and drag Automator action to:
# ~/Library/PDF Services
#
# TODO:
#
# If a file is unsupported by the Kindle, it should be automatically emailed
# to the free Kindle conversion service.
 
 
 
CONFIGURATION = {
  # a local folder or drive that you will use to store your Kindle-bound documents
  :local_kindle_folder => "/Users/#{ENV['USER']}/Desktop/Kindle",
  # The username you have set up for your kindle email account
  :kindle_email_name => '',
  # Your kindle drive -- in most cases the kindle will mount to /Volumes/Kindle
  :kindle_drive => '/Volumes/Kindle'
}
 
module Kindle
  class Export
    
    require 'fileutils'
    
    SUPPORTED_FILE_FORMATS = {
      :documents => ['mobi', 'prc', 'pdf', 'txt', 'azw', 'azw1'],
      :music => ['mp3'],
      :audible => ['aa', 'aax']
    }
 
    def self.run(source_file,options)
      export = new(options)
      export.prepare(source_file) if source_file
      export.run
    end
 
    def initialize(options)
      @kindle_drive = options[:kindle_drive]
      @local_kindle_folder = options[:local_kindle_folder]
      @kindle_email_name = options[:kindle_email_name]
    end
    
    def prepare(source_file)
      file_name = File.basename(source_file)
      FileUtils.mv source_file, File.join(@local_kindle_folder,file_name), :force => true
    end
 
    def run
      return false unless kindle_is_connected?
      process_files if files_to_be_processed?
    end
 
    protected
 
    def kindle_is_connected?
      File.directory? @kindle_drive
    end
 
    def process_files
      files_to_be_processed.each do |file|
        transfer_to_kindle_or_email_for_conversion file
      end
    end
 
    def files_to_be_processed
      @files_to_be_processed ||= Dir.entries(@local_kindle_folder).reject {|f| f.match(/^\./) }.map {|file| File.join(@local_kindle_folder,file) }
    end
 
    def files_to_be_processed?
      !files_to_be_processed.empty?
    end
 
    def transfer_to_kindle_or_email_for_conversion(file)
      target_folder = map_folder_to File.extname(file)
 
      if target_folder
        FileUtils.mv file, target_folder, :force => true
      else
        send_file_for_email_conversion file
      end
      
    end
    
    def send_file_for_email_conversion(file)
      begin
        #TODO: send email to free.kindle.com for conversion
      rescue
        #emailing didnt work
      ensure
        # at this point we should probably delete or move the file, so it won't try to process at every execution
        # FileUtils.rm file
      end
    end
    
    def map_folder_to(file_extension)
      return nil unless file_extension
      file_extension = file_extension.delete('.')
      folder = SUPPORTED_FILE_FORMATS.find(proc{[]}) { |folder_map| folder_map[1].include?(file_extension) }[0]
      folder ? File.join(@kindle_drive,folder.to_s) : nil
    end
    
    def conversion_email
      '%s@free.kindle.com' % @kindle_email_name
    end
 
  end
end
 
Kindle::Export.run(ARGV[0],CONFIGURATION)