X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=bin%2Fuploader.rb;h=e8fa6eddf4ea04e914deadac2b2a44a207fc48af;hb=8ec74dec6d3b971a050d2a14979783ccd54f06a2;hp=aa56f49f08fe54f34ba3976404d643a8a9bad3ff;hpb=ecf9b1a88198581aa2bbede0486ca93116d59d73;p=pettanr%2Fpettanr.git diff --git a/bin/uploader.rb b/bin/uploader.rb index aa56f49f..e8fa6edd 100644 --- a/bin/uploader.rb +++ b/bin/uploader.rb @@ -7,23 +7,23 @@ require 'json' require 'base64' if ARGV.size < 1 - puts "uploader.rb host dirname attr.json lsname auth" + puts "uploader.rb host dirname license_name email token" puts "upload on child dirs" exit end host = ARGV[0] dirname = ARGV[1] -attr_fn = ARGV[2] -lsname = ARGV[3] +lsname = ARGV[2] +email = ARGV[3] auth = ARGV[4] class Uploader - def initialize host, dirname, attr_fn, lsname, auth + def initialize host, dirname, lsname, email, auth @host = host @dirname = dirname - @attr_fn = attr_fn @lsname = lsname + @email = email @auth = auth @licenses = self.licenses @license_groups = self.license_groups @@ -32,18 +32,18 @@ class Uploader def licenses res = {} u = @host + 'licenses.json' - r = RestClient.get(u) + r = RestClient.get(u, {email: @email, token: @auth}) ary = JSON.parse r ary['list'].each do |l| res[l['name']] = l end res end - + def license_groups res = {} u = @host + 'license_groups.json' - r = RestClient.get(u) + r = RestClient.get(u, {email: @email, token: @auth}) ary = JSON.parse r ary['list'].each do |l| res[l['id'].to_i] = l @@ -51,20 +51,15 @@ class Uploader res end - def each_subdir - Dir.glob File.expand_path(@dirname) + '/*' do |filename| - if File.directory?(filename) - yield filename - end - end - end - def each_op Dir.glob File.expand_path(@dirname) + '/*' do |fn| ext = File.extname(fn).downcase + bn = File.basename(fn, ".*") case ext when '.png', '.gif', '.jpeg' - yield fn + custom_attr_file = File.dirname(fn) + '/' + bn + '.json' + attr = JSON.parse(File.open(custom_attr_file).read) + yield fn, attr end end end @@ -75,8 +70,8 @@ class Uploader u = @host + 'original_pictures' begin r = RestClient.post(u, - {:original_picture => {:file => b}, :auth_token => @auth}.to_json, - :content_type => :json, :accept => :json + {:original_picture => {:file => b}}.to_json, + :content_type => :json, :accept => :json, :email => @email, :token => @auth ) o = JSON.parse r oid = o['id'] @@ -95,41 +90,14 @@ class Uploader :license_id => lsid, :license_group_module_name => en, :license_settings => attr, - :auth_token => @auth - } - begin - r = RestClient.post(u, - param.to_json, - :content_type => :json, :accept => :json - ) - rp = JSON.parse r - rescue - puts ' failed: ' + u.to_s - puts param - puts r.to_s - end - rp - end - - #confirm - def resource_picture oid, lgid, lsid, en, formname, attr - rp = nil - u = @host + en.tableize + '/attributes' - attr['license_id'] = lsid - param = { - :original_picture_id => oid, - :original_picture_license_group => { - :original_picture_id => oid, :license_group_id => lgid - }, - :auth_token => @auth } - param[:attributes] = attr begin r = RestClient.post(u, param.to_json, - :content_type => :json, :accept => :json + :content_type => :json, :accept => :json, :email => @email, :token => @auth ) - rp = JSON.parse r + resp = JSON.parse r + rp = resp['item'] rescue puts ' failed: ' + u.to_s puts param @@ -147,12 +115,11 @@ class Uploader :original_picture_id => oid, :license_group_id => lgid }, :resource_picture => rp, - :auth_token => @auth } begin r = RestClient.post(u, attr.to_json, - :content_type => :json, :accept => :json + :content_type => :json, :accept => :json, :email => @email, :token => @auth ) res = JSON.parse r rescue @@ -162,8 +129,7 @@ class Uploader end def publish_all - attr = JSON.parse(File.open(@attr_fn).read) - each_op do |imgfile| + each_op do |imgfile, attr| puts 'uploading: ' + imgfile oid = self.upload imgfile next unless oid @@ -186,7 +152,6 @@ class Uploader end -u = Uploader.new host, dirname, attr_fn, lsname, auth - +u = Uploader.new host, dirname, lsname, email, auth u.publish_all