From b0ab0e4e446dc0af6a88587b9a90db1c9c967622 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sat, 9 May 2015 17:01:15 +0900 Subject: [PATCH] fix: uploader --- script/uploader.rb | 96 +++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/script/uploader.rb b/script/uploader.rb index 86310820..aa56f49f 100644 --- a/script/uploader.rb +++ b/script/uploader.rb @@ -7,19 +7,23 @@ require 'json' require 'base64' if ARGV.size < 1 - puts "uploader.rb host dirname auth" + puts "uploader.rb host dirname attr.json lsname auth" puts "upload on child dirs" exit end host = ARGV[0] dirname = ARGV[1] -auth = ARGV[2] +attr_fn = ARGV[2] +lsname = ARGV[3] +auth = ARGV[4] class Uploader - def initialize host, dirname, auth + def initialize host, dirname, attr_fn, lsname, auth @host = host @dirname = dirname + @attr_fn = attr_fn + @lsname = lsname @auth = auth @licenses = self.licenses @license_groups = self.license_groups @@ -30,7 +34,7 @@ class Uploader u = @host + 'licenses.json' r = RestClient.get(u) ary = JSON.parse r - ary.each do |l| + ary['list'].each do |l| res[l['name']] = l end res @@ -41,7 +45,7 @@ class Uploader u = @host + 'license_groups.json' r = RestClient.get(u) ary = JSON.parse r - ary.each do |l| + ary['list'].each do |l| res[l['id'].to_i] = l end res @@ -55,26 +59,12 @@ class Uploader end end - def each_opset - self.each_subdir do |filename| - imgfile = nil - lsname = nil - formname = nil - attr = nil - Dir.glob(filename + '/*') do |fn| - ext = File.extname(fn).downcase - case ext - when '.json' - json = JSON.parse(File.open(fn).read) - lsname = json["license_name"] - formname = json["formname"] - attr = json["attributes"] - when '.png', '.gif', '.jpeg' - imgfile = fn - end - end - if imgfile and lsname and formname and attr - yield imgfile, lsname, formname, attr + def each_op + Dir.glob File.expand_path(@dirname) + '/*' do |fn| + ext = File.extname(fn).downcase + case ext + when '.png', '.gif', '.jpeg' + yield fn end end end @@ -96,6 +86,32 @@ class Uploader oid end + def original_picture_license oid, lgid, lsid, en, attr + rp = nil + u = @host + '/original_picture_licenses' + param = { + :original_picture_id => oid, + :license_group_id => lgid, + :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' @@ -130,16 +146,7 @@ class Uploader :original_picture_license_group => { :original_picture_id => oid, :license_group_id => lgid }, - :resource_picture => { - :original_picture_id => oid, - :license_id => rp['license_id'], - :artist_name => rp['artist_name'], - :system_picture_id => rp['system_picture_id'], - :license_group_classname => rp['license_group_classname'], - :license_group_settings => rp['license_group_settings'], - :credit_picture_settings => rp['credit_picture_settings'], - :license_settings => rp['license_settings'] - }, + :resource_picture => rp, :auth_token => @auth } begin @@ -155,32 +162,31 @@ class Uploader end def publish_all - each_opset do |imgfile, lsname, formname, attr| + attr = JSON.parse(File.open(@attr_fn).read) + each_op do |imgfile| puts 'uploading: ' + imgfile oid = self.upload imgfile next unless oid puts 'success: ' + oid.to_s - ls = @licenses[lsname] + ls = @licenses[@lsname] unless ls - puts 'not detect licenses: ' + lsname.to_s + puts 'not detect licenses: ' + @lsname.to_s next end lsid = ls['id'] lgid = ls['license_group_id'].to_i lg = @license_groups[lgid] - en = lg['classname'] - puts 'building: ' + formname - rp = self.resource_picture oid, lgid, lsid, en, formname, attr - next unless rp - puts 'publishing: ' + lsname - r = self.publish oid, lgid, rp + en = lg['module_name'] + confirm = self.original_picture_license oid, lgid, lsid, en, attr + puts 'publishing: ' + @lsname.to_s + r = self.publish oid, lgid, confirm puts 'success: ' + r['id'].to_s end end end -u = Uploader.new host, dirname, auth +u = Uploader.new host, dirname, attr_fn, lsname, auth u.publish_all -- 2.11.0