Class: Dbtools::Google_drive
- Inherits:
-
Thor
- Object
- Thor
- Dbtools::Google_drive
- Defined in:
- lib/tasks/googledrivetool.rb
Instance Method Summary collapse
- #changes_as_rdf(start_page_token, target_file = nil) ⇒ Object
- #download(file_id, target_dest = nil) ⇒ Object
- #export(file_id, target_format, target_dest = nil) ⇒ Object
-
#initialize(*args) ⇒ Google_drive
constructor
A new instance of Google_drive.
- #list(file_id) ⇒ Object
- #serialize_rdf(target_file = nil) ⇒ Object
- #upload(file) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Google_drive
Returns a new instance of Google_drive.
23 24 25 26 27 |
# File 'lib/tasks/googledrivetool.rb', line 23 def initialize(*args) super @gdrive = Google_Drive::Google_drive_api.new(auth=) @service = @gdrive.service end |
Instance Method Details
#changes_as_rdf(start_page_token, target_file = nil) ⇒ Object
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 |
# File 'lib/tasks/googledrivetool.rb', line 173 def changes_as_rdf(start_page_token, target_file=nil) # Check if it's not empty. if start_page_token.nil? || start_page_token.to_s.empty? STDERR.puts "Start page token cannot be nil. " return end f = if target_file.nil? STDOUT else File.open(target_file, 'w') end begin changed_files, removed_files, new_start_page_token = @gdrive.get_changes(page_token=start_page_token) googledrive2rdf = Dbtools::Converter::GoogleDrive2RDFConverter.new googledrive2rdf.serialize_as_rdf(files=changed_files) do |statement| f << statement end ensure if f != STDOUT f.flush f.close end end STDERR.puts "New page token: #{new_start_page_token}" return changed_files, removed_files, new_start_page_token end |
#download(file_id, target_dest = nil) ⇒ Object
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 |
# File 'lib/tasks/googledrivetool.rb', line 86 def download(file_id, target_dest = nil) = @service.get_file(file_id) if .mime_type.index('application/vnd.google-apps') # Default conversion formats googledoc_format_conversion = { 'application/vnd.google-apps.document' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.google-apps.presentation' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.google-apps.spreadsheet' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.google-apps.drawing' => 'image/png' } target_format = googledoc_format_conversion[.mime_type] raise FormatNotSupportedError.new("Mimetype #{.mime_type} is not supported. If you know the target conversion format, try using the `export` task.") if target_format.nil? return export(file_id, target_format, target_dest) end if target_dest.nil? @service.get_file(file_id, download_dest: STDOUT) else destination = target_dest if target_dest.is_a?(String) FileUtils.mkdir_p(target_dest) destination = File.join(target_dest, .name) end @service.get_file(file_id, download_dest: destination) return destination end end |
#export(file_id, target_format, target_dest = nil) ⇒ Object
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 |
# File 'lib/tasks/googledrivetool.rb', line 45 def export(file_id, target_format, target_dest = nil) if target_dest.nil? @service.export_file(file_id, target_format, download_dest: STDOUT) else file = @service.get_file(file_id) FileUtils.mkdir_p(target_dest) extension_mapping = { 'text/html' => '.html', 'text/plain' => '.txt', 'application/rtf' => '.rtf', 'application/vnd.oasis.opendocument.text' => '.odt', 'application/pdf' => '.pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', 'application/x-vnd.oasis.opendocument.spreadsheet' => '.ods', 'text/csv' => '.csv', 'image/jpeg' => '.jpg', 'image/png' => '.png', 'image/svg+xml' => '.svg', 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx', 'application/vnd.google-apps.script+json' => '.json' } extension_mapping.default = '' extension = extension_mapping[target_format] destination = File.join(target_dest, file.name + extension) @service.export_file(file_id, target_format, download_dest: destination) return destination end end |
#list(file_id) ⇒ Object
202 203 204 205 |
# File 'lib/tasks/googledrivetool.rb', line 202 def list(file_id) folder = @service.get_file(file_id) puts @gdrive.print_child_files(folder) end |
#serialize_rdf(target_file = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tasks/googledrivetool.rb', line 125 def serialize_rdf(target_file = nil) f = if target_file.nil? then STDOUT else File.open(target_file, 'w') end begin print_progress = [:verbose] files = @gdrive.get_tree(optional_query=[:query], verbose: print_progress) googledrive2rdf_converter = Dbtools::Converter::GoogleDrive2RDFConverter.new googledrive2rdf_converter.serialize_as_rdf(files=files, verbose: print_progress) do |statement| f << statement end ensure if f != STDOUT f.flush f.close end end end |
#upload(file) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/tasks/googledrivetool.rb', line 158 def upload(file) mime_type = `file --mime-type -b #{file}`.chomp = { name: File.basename(file), mime_type: mime_type, } .merge!({ parents: [:folder] }) if [:folder] .merge!({ name: [:filename] }) if [:filename] result = @service.create_file(, upload_source: file, content_type: mime_type, options: { retries: 3 } ) return result end |