Class: Enwrite

Inherits:
Object
  • Object
show all
Defined in:
lib/enwrite.rb

Defined Under Namespace

Modules: Version

Constant Summary collapse

PLUGINS =
%w[hugo]

Class Method Summary collapse

Class Method Details

.runObject



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/enwrite.rb', line 30

def self.run

  options = OpenStruct.new
  options.removetags = []
  options.verbose = false
  options.debug = false
  options.outputplugin = 'hugo'
  options.configtag = '_enwrite_config'
  options.filestagprefix = '_enwrite_files'

  opts = OptionParser.new do |opts|
    def opts.version_string
      "Enwrite v#{Enwrite::Version::STRING}"
    end
    
    opts.banner = "#{opts.version_string}\n\nUsage: #{$0} [options] (at least one of -n or -s has to be specified)"

    def opts.show_usage
      puts self
      exit
    end

    def opts.show_version
      puts version_string
      exit
    end

    opts.separator "\nSearch options:"
    opts.on("-n", "--notebook NOTEBOOK",
            "Process notes from specified notebook.") do |notebook|
      options.notebook = notebook
    end
    opts.on("-t", "--tag TAG",
            "Process only notes that have this tag",
            "within the given notebook.") do |tag|
      options.tag = tag
    end
    opts.on("--remove-tags [t1,t2,t3]", Array,
            "List of tags to remove from output posts.",
            "If no argument given, defaults to --tag.") do |removetags|
      options.removetags = removetags || [options.tag]
    end
    opts.on("-s", "--search SEARCHEXP",
            "Process notes that match given search",
            "expression. If specified, --notebook",
            "and --tag are ignored.") do |searchexp|
      options.searchexp = searchexp
      options.tag = nil
      options.notebook = nil
    end
    opts.separator 'Output options:'
    opts.on("-p", "--output-plugin PLUGIN", PLUGINS,
            "Output plugin to use (Valid values: #{PLUGINS.join(', ')})") do |plugin|
      options.outputplugin = plugin
    end
    opts.on("-o", "--output-dir OUTDIR",
            "Base dir of hugo output installation") do |outdir|
      options.outdir = outdir
    end
    opts.on("--rebuild-all",
            "Process all notes that match the given",
            "conditions (normally only updated notes",
            "are processed)") { options.rebuild_all = true }
    opts.separator 'Other options:'
    opts.on("--auth [TOKEN]",
            "Force Evernote reauthentication (will",
            "happen automatically if needed). Use",
            "TOKEN if given, otherwise get one",
            "interactively.") do |forceauth|
      options.forceauth = true
      options.authtoken = forceauth
    end
    opts.on("--config-tag TAG",
            "Specify tag to determine config notes",
            "(default: #{options.configtag})") { |conftag|
      options.configtag = conftag
    }
    opts.on_tail("-v", "--verbose", "Verbose mode") { options.verbose=true }
    opts.on_tail("-d", "--debug", "Debug output mode") {
      options.debug=true
      options.verbose=true
    }
    opts.on_tail("--version", "Show version") { opts.show_version }
    opts.on_tail("-h", "--help", "Shows this help message") { opts.show_usage }
  end

  opts.parse!

  begin
    eval "require 'output/#{options.outputplugin}'"
  rescue LoadError
    error "There was an error loading output module '#{plugin}': #{e.to_s}"
    exit 1
  end

  $enwrite_verbose = options.verbose
  $enwrite_debug = options.debug

  verbose("Options: " + options.to_s)

  if not (options.notebook or options.searchexp or options.forceauth)
    error "You have to specify at least one of --notebook, --search or --auth"
    exit(1)
  end
  exps = [ options.searchexp ? options.searchexp : nil,
           options.notebook ? "notebook:#{options.notebook}" : nil,
           options.tag ? "tag:#{options.tag}" : nil,
         ].reject(&:nil?)
  searchexp = exps.join(' ')

  verbose "Output dir: #{options.outdir}"
  verbose "Search expression: #{searchexp}"

  begin
    
    # Initialize Evernote access
    Evernote_utils.init(options.forceauth, options.authtoken)

    if not searchexp # Only --auth was specified
      exit 0
    end
    
    updatecount_index = "updatecount_#{searchexp}"
    latestUpdateCount = config(updatecount_index, 0)
    if options.rebuild_all
      msg "Processing ALL notes (--rebuild-all)."
      latestUpdateCount = 0
    end

    verbose "Latest stored update count for #{searchexp}: #{latestUpdateCount}"

    currentState = Evernote_utils.noteStore.getSyncState(Evernote_utils.authToken)
    currentUpdateCount = currentState.updateCount

    verbose "Current update count for the account: #{currentUpdateCount}"

    if (currentUpdateCount > latestUpdateCount)
      msg "Checking updated Evernote contents..."

      # There are new notes, doesn't necessarily mean any of them are withing the
      # selected content, so we keep track of whether we actually produce any updates
      # in the output.
      something_updated = false

      filter = Evernote::EDAM::NoteStore::NoteFilter.new
      filter.words = searchexp
      filter.order = Evernote::EDAM::Type::NoteSortOrder::UPDATE_SEQUENCE_NUMBER
      filter.ascending = false

      spec = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new
      spec.includeTitle = true
      spec.includeCreated = true
      spec.includeTagGuids = true
      spec.includeContentLength = true
      spec.includeUpdateSequenceNum = true
      spec.includeDeleted = true

      results = Evernote_utils.noteStore.(Evernote_utils.authToken,
                                                           filter,
                                                           0,
                                                           Evernote::EDAM::Limits::EDAM_USER_NOTES_MAX,
                                                           spec)

      # Get also deleted notes so we can remove from the blog
      filter.inactive = true
      delresults = Evernote_utils.noteStore.(Evernote_utils.authToken,
                                                              filter,
                                                              0,
                                                              Evernote::EDAM::Limits::EDAM_USER_NOTES_MAX,
                                                              spec)

      # Go through the list looking for config notes, parse them and remove
      # them from the list
      enwriteconfig = { 'hugo' => {
                          'base_dir' => options.outdir,
                          'rebuild_all' => options.rebuild_all,
                        },
                      }

      if Evernote_utils.tags.include?(options.configtag)
        config_tag_guid = Evernote_utils.tags[options.configtag].guid
        results.notes.select { |note|
          note.tagGuids.include?(config_tag_guid)
        }.each { |confignotemd|
          msg "Found config note '#{confignotemd.title}'"
          confignote = Evernote_utils.getWholeNote(confignotemd)
          enml = ENML_utils.new(confignote.content)
          configtext = enml.to_text
          debug "   Config note text: '#{configtext}'"
          configyaml = YAML.load(configtext)
          debug "   Config note YAML: #{configyaml}"
          enwriteconfig.deep_merge!(configyaml)
          debug "   enwriteconfig = #{enwriteconfig}"
          results.notes.delete(confignotemd)
          results.totalNotes -= 1
        }
      end
      verbose "Final enwrite config: #{enwriteconfig}"

      files_tag = "#{options.filestagprefix}_#{options.outputplugin}"

      if Evernote_utils.tags.include?(files_tag)
        files_tag_guid = Evernote_utils.tags[files_tag].guid
        results.notes.select { |note|
          note.tagGuids.include?(files_tag_guid) &&
            note.updateSequenceNum > latestUpdateCount
        }.each { |filesnotemd|
          msg "Found files note '#{filesnotemd.title}'"
          filesnote = Evernote_utils.getWholeNote(filesnotemd)
          enml = ENML_utils.new(filesnote.content, filesnote.resources)
          files = enml.resource_files
          Dir.chdir("#{options.outdir}")
          files.each do |file|
            case
            when file[:basename] =~ /\.tar.gz$/
              f = Tempfile.new('enwrite')
              begin
                verbose "   Saving file #{file[:basename]} to #{f.path}"
                f.write(file[:data])
                f.close
                verbose "   Unpacking file #{f.path} with tar"
                ok = system("tar zxf #{f.path}")
                unless ok
                  error "   An error occurred when unpacking #{f.path}"
                end
              ensure
                f.close
                f.unlink
              end
            else
              open("#{options.outdir}/#{file[:basename]}", "w") do |f|
                verbose "   Saving file #{f.path}"
                f.write(file[:data])
              end
            end
          end
          something_updated = true
          results.notes.delete(filesnotemd)
          results.totalNotes -= 1
        }
      end

      debug "Evaluating: #{options.outputplugin.capitalize}.new(enwriteconfig[options.outputplugin])"
      writer = eval "#{options.outputplugin.capitalize}.new(enwriteconfig[options.outputplugin])"
      
      (results.notes + delresults.notes).select {
        |note| note.updateSequenceNum > latestUpdateCount
      }.sort_by {
        |note| note.updateSequenceNum
      }.each do ||
        verbose "######################################################################"
        note = Evernote_utils.getWholeNote()
        note.tagNames = note.tagNames - options.removetags
        # This either creates or deletes posts as appropriate
        writer.output_note(note)
        something_updated = true
      end
      # Persist the latest updatecount for next time
      setconfig(updatecount_index, currentUpdateCount)
    end

    if something_updated
      exit 0
    else
      msg "No updated notes that match #{searchexp}"
      exit 1
    end
  rescue Evernote::EDAM::Error::EDAMUserException => e
    #the exceptions that come back from Evernote are hard to read, but really important to keep track of
    msg = "Caught an exception from Evernote trying to create a note.  #{Evernote_utils.translate_error(e)}"
    raise msg
  rescue Evernote::EDAM::Error::EDAMSystemException => e
    #the exceptions that come back from Evernote are hard to read, but really important to keep track of
    msg = "Caught an exception from Evernote trying to create a note.  #{Evernote_utils.translate_error(e)}"
    raise msg
  end
end