Skip to content

Instantly share code, notes, and snippets.

@MatteoRagni
Created November 20, 2018 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatteoRagni/9fe0310664102d331a9bd7742dd1c2b9 to your computer and use it in GitHub Desktop.
Save MatteoRagni/9fe0310664102d331a9bd7742dd1c2b9 to your computer and use it in GitHub Desktop.
Downloader for google drive (utility for me...)
#!/usr/bin/env ruby
require 'json'
# Position of the mounted drive
$SOURCE = "/Volumes/GoogleDrive/Il mio Drive"
# Destination of the json file
$DEST = "/tmp/result.json"
# Size for "too big" files
$XGB = 2
# Regular expression for drive files
$REGEXPDRIVE = /^(.*)\.(gdoc|gsheet|gslides|gform|gscript)$/i
#######
$FILE_COUNT = 0
$DIR_COUNT = 0
$DATA = {
"export" => {
"gdoc" => [],
"gsheet" => [],
"gslides" => [],
"gform" => [],
"gscript" => []
},
"big" => [],
"big_n" => 0,
"copy" => [],
"copy_n" => 0
}
def handle_dir(input, cwd)
$DIR_COUNT += 1
Dir.foreach(input) do |entry|
next if entry == '..' or entry == '.'
if File.file? entry
$FILE_COUNT += 1
if match = entry.match($REGEXPDRIVE)
$DATA["export"][match[2]] << "#{cwd}/#{match[1]}"
next
end
if (File.open(entry).size * 1e-9) > $XGB
$DATA["big"] << "#{cwd}/#{entry}"
$DATA["big_n"] += 1
next
end
$DATA["copy"] << "#{cwd}/#{entry}"
$DATA["copy_n"] += 1
elsif File.directory? entry
Dir.chdir(entry) do
handle_dir(".", "#{cwd}/#{entry}")
end
else
puts "ENTRY UNKNOWN :: #{cwd}/#{entry}"
end
end
end
Dir.chdir($SOURCE) do
handle_dir(".", ".")
end
File.open($DEST, "w") do |f|
f.puts JSON.generate($DATA, indent: ' ', max_nesting: false)
end
puts "STATISTICS:"
puts " - total dirs: #{$DIR_COUNT}"
puts " - total files: #{$FILE_COUNT}"
puts " - drive docs: #{$DATA["export"]["gdoc"].size}"
puts " - drive sheet: #{$DATA["export"]["gsheet"].size}"
puts " - drive form: #{$DATA["export"]["gform"].size}"
puts " - drive slides: #{$DATA["export"]["gslides"].size}"
puts " - drive scripts: #{$DATA["export"]["gscript"].size}"
puts " - direct copy: #{$DATA["copy_n"]}"
puts " - big files: #{$DATA["big_n"]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment