-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-notes.rb
64 lines (52 loc) · 2.34 KB
/
create-notes.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
# frozen_string_literal: true
require_relative '../lib/mixins/google_api_support'
usage 'create-notes [options] html_agenda'
aliases :create_notes, :cn
summary 'create notes from a meeting agenda'
description 'Create a working agenda and a space for meeting notes from the given HTML meeting agenda.'
class CreateNotes < ::Nanoc::CLI::CommandRunner
include GoogleApiSupport
def run
require 'nokogiri'
require 'google/apis/drive_v2'
@config = Nanoc::Int::ConfigLoader.new.new_from_cwd
# Extract arguments
if arguments.length != 1
raise Nanoc::Int::Errors::GenericTrivial, "usage: #{command.usage}"
end
agenda_path = arguments[0]
if !File.exist?(agenda_path) || File.extname(agenda_path) != '.html'
raise(
Nanoc::Int::Errors::GenericTrivial,
"The meeting notes were not created because '#{agenda_path}' was not found " \
'or is not an HTML file.'
)
end
# Setup notifications
Nanoc::Int::NotificationCenter.on(:file_created) do |file_path|
Nanoc::CLI::Logger.instance.file(:high, :create, file_path)
end
agenda = Nokogiri::HTML(File.read(agenda_path))
file_metadata = {
title: agenda.title,
mime_type: 'application/vnd.google-apps.document'
}
service = Google::Apis::DriveV2::DriveService.new
service.client_options.application_name = @config[:google][:application_name]
service.client_options.log_http_requests = debug?
service.authorization = user_credentials_for(Google::Apis::DriveV2::AUTH_DRIVE_FILE)
doc = service.insert_file(file_metadata,
convert: true,
fields: 'alternateLink,createdDate,description,embedLink,etag,iconLink,id,kind,lastModifyingUser(displayName,emailAddress,kind,picture),mimeType,modifiedDate,title,version',
upload_source: agenda_path,
content_type: 'text/html')
service.insert_child(@config[:google][:agenda_subfolder_id], doc) unless debug?
Nanoc::Int::NotificationCenter.post(:file_created, doc.alternate_link)
docs_cache = @config[:cache][:meeting_notes]
FileUtils.mkdir_p(docs_cache)
doc_file = File.join(docs_cache, "#{doc.id}.yaml")
File.write(doc_file, YAML.dump(doc.to_h))
Nanoc::Int::NotificationCenter.post(:file_created, doc_file)
end
end
runner CreateNotes