bin/mapsnatcher in mapsnatcher-0.1 vs bin/mapsnatcher in mapsnatcher-0.1.1

- old
+ new

@@ -1,15 +1,15 @@ #!/usr/bin/env ruby require "fileutils" -require "yaml" require "open-uri" require "net/http" require "rmagick" include Magick TEMP_DIR = "./.ms.tmp" +TILES_DIR = "./tiles" # False if URL returns a 200 # # @param url [String] the url to be checked # @return [Boolean] whether or not the URL is invalid @@ -32,10 +32,11 @@ puts puts " Place XXX and YYY in place of where the respective coordinate numbers would be" puts " in the URL. The first number is typically the zoom level, which is constant." puts puts " [-b] find outer bounds of map automatically" + puts " [-s] save tiles to a directory ./tiles/" exit 0 end if %w[-v --version].include? ARGV.first puts "mapsnatcher #{File.read(File.expand_path("../../VERSION", __FILE__)).strip}" @@ -49,14 +50,20 @@ print "It looks like mapsnatcher didn't close properly. Use backed up files? [Y/n] " FileUtils.rm_f(Dir.glob("#{TEMP_DIR}/*")) if STDIN.gets.chomp =~ /n.*/i puts end -if ARGV.include? "-b" +save_tiles = false +if (args = ARGV).include? "-s" + args -= ["-s"] + save_tiles = true +end + +if args.include? "-b" # Automatic boundary mode - url = (ARGV - ["-b"]).first + url = (args - ["-b"]).first # CLI input function # # @return [Array] user input coordinates def get_coords @@ -102,11 +109,11 @@ x_range, y_range = (x_low..x_high), (y_low..y_high) else # User input coordinate mode - url = ARGV.first + url = args.first # CLI input function # # @return [Array] user input coordinate ranges def get_coords @@ -139,11 +146,12 @@ end # Use the tile at 0, 0 to calculate final image size sample = Image.from_blob(URI.open(build_url url, x_range.first, y_range.first).read).first final_size = {x: sample.columns * x_range.size, y: sample.rows * y_range.size} -puts "Image found, #{sample.format} size #{final_size[:x]}x#{final_size[:y]}" +format = sample.format +puts "Image found, #{format} size #{final_size[:x]}x#{final_size[:y]}" puts dl_counter = 0 dl_total = x_range.size * y_range.size @@ -151,20 +159,21 @@ y_range.each do |y| row = ImageList.new x_range.each do |x| print "Downloading... [#{dl_counter += 1}/#{dl_total}]\r" - temp = "#{TEMP_DIR}/#{x}_#{y}.tmp" + temp = "#{TEMP_DIR}/#{x}_#{y}.#{format.downcase}" if File.exists? temp # Load the backup file - blob = YAML.load(File.read temp) + img = Image.read(temp).first else blob = URI.open(build_url url, x, y).read - File.open(temp, "w") { |f| f.write(YAML.dump blob) } + File.open(temp, "w") { |f| f.write blob } + img = Image.from_blob(blob).first end - row.push Image.from_blob(blob).first + row.push img end stitch.push row.append(false) end @@ -186,7 +195,13 @@ print "Enter file name for map: " filename = STDIN.gets.chomp print "Saving, this may take a minute..." stitch.append(true).write filename -FileUtils.rm_rf TEMP_DIR puts "\nImage written to: #{filename}" + +if save_tiles + File.rename TEMP_DIR, TILES_DIR + puts "Tiles saved to: #{TILES_DIR.match /\w+/}/" +else + FileUtils.rm_rf TEMP_DIR +end