#!/usr/bin/env ruby $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'djatoka' require 'trollop' require 'pp' require 'open-uri' opts = Trollop::options do banner <<-EOS This script take a Djatoka resolver base URL and returns a full URI for a particular identifier. Usage: djatoka_url [options] --resolver http://african.lanl.gov/adore-djatoka/resolver --rftid info:lanl-repo/ds/5aa182c2-c092-4596-af6e-e95d2e263de3 where options are: EOS opt :resolver, "A Djatoka resolver base URL", :required => true, :type => String opt :rftid, "Resource identifier", :type => String, :short => :i opt :level, 'Level', :type => String opt :rotate, 'Rotate', :type => String opt :region, 'Region', :type => String opt :scale, "Scale", :type => String opt :format, 'Format', :type => String opt :clayer, 'clayer', :type => String opt :smallbox, 'Creates a 75x75 image' opt :metadata, 'Output metadata' opt :levels, 'Output Levels' opt :browser, 'Set the browser to open JSON and images with', :type => String opt :square, 'Squares the image by cropping both sides' opt :topleftsquare, 'Squares the image by justifying to the top left' opt :bottomrightsquare, 'Squares the image by jutifying to the bottom right' opt :file, 'process a whole file of rtfids', :type => String opt :download, 'download the image to the current working directory' end if (!opts[:rftid] and !opts[:file]) or (opts[:rftid] and opts[:file]) Trollop::die :rftid, "You must specify either --rftid or --file" end if opts[:rftid] rftids = [opts[:rftid]] elsif opts[:file] Trollop::die :file, "File must exist" if !File.exists?(opts[:file]) rftids = File.read(opts[:file]).split("\n") end resolver = Djatoka::Resolver.new(opts[:resolver]) rftids.each do |rftid| region = resolver.region(rftid) [:level, :rotate, :region, :scale, :format, :clayer].each do |param| region.send(param, opts[param]) if opts[param] end if opts[:smallbox] region.smallbox end if opts[:square] region.square end if opts[:topleftsquare] region.top_left_square end if opts[:bottomrightsquare] region.bottom_right_square end if opts[:metadata] or opts[:levels] metadata = resolver.metadata(rftid) metadata.perform if opts[:metadata] puts metadata.url unless opts[:file] `#{opts[:browser]} "#{metadata.url}"` if opts[:browser] pp metadata.response #unless opts[:file] if metadata.response.nil? or metadata.response.empty? pp rftid + ": empty" end end if opts[:levels] pp metadata.all_levels unless opts[:file] end end pp region.query unless opts[:file] puts region.url unless opts[:file] `#{opts[:browser]} "#{region.url}"` if opts[:browser] if opts[:download] puts 'downloading...' image = open(region.url).read File.open(rftid + '.jpg', 'w') do |fh| fh.puts image end end end