1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| #!/usr/bin/env ruby
# encoding: utf-8
require 'AWS'
require 'active_support/all'
require './StringColorize.rb'
GIF_TYPE_DEFAULT = 'GIF_TYPE_DEFAULT'
GIF_TYPE_ANIMAL = 'GIF_TYPE_ANIMAL'
GIF_TYPE_DOG = 'GIF_TYPE_DOG'
IMAGE_INFO_DOMAIN_NAME = "IMAGE_INFO_DOMAIN_NAME"
GROUP_INFO_DOMAIN_NAME = "GROUP_INFO_DOMAIN_NAME"
class AWSUtils
ACCESS_KEY_ID = 'MY_ACCESS_KEY_ID'
SECRET_ACCESS_KEY = 'MY_SECRET_ACCESS_KEY'
def initialize
self.prepareSDB
self.prepareDomainSDB(IMAGE_INFO_DOMAIN_NAME)
self.prepareDomainSDB(GROUP_INFO_DOMAIN_NAME)
end
def listImageInfos
imageDomain = getDomain(IMAGE_INFO_DOMAIN_NAME)
list_item_by_domain_name(imageDomain) do |item|
list_attributes_by_item(item)
end
end
def listGroupInfos
groupDomain = getDomain(GROUP_INFO_DOMAIN_NAME)
list_item_by_domain_name(groupDomain) do |item|
list_attributes_by_item(item)
end
end
def regions()
AWS::regions.each do |region|
puts region.name
end
end
def prepareSDB()
if @sdb.nil?
puts "===> prepare sdb"
@sdb = AWS::SimpleDB.new(
:access_key_id => ACCESS_KEY_ID,
:secret_access_key => SECRET_ACCESS_KEY,
:region => 'ap-northeast-1')
end
end
def prepareDomainSDB(domain_name)
self.prepareSDB
puts "===> initialize domain " + domain_name
@sdb.domains.create(domain_name)
end
def getDomain(domain_name)
self.prepareSDB
@sdb.domains[domain_name]
end
# ==== Deletion ====
def deleteEmptyDomain(domain)
self.prepareSDB
begin
puts domain.name + " is deleted because empty"
domain.delete
rescue => ex
puts domain.name + " delete error -> " + ex.to_s
end
end
# ==== List ====
def listDomainSDB(&block)
self.prepareSDB
@sdb.domains.each do |domain|
puts "===> listing domain: " + domain.name
unless block.nil?
yield domain
end
end
end
def list_item_by_domain_name(domain, &block)
self.prepareSDB
#domain = @sdb.domains.create(domain_name)
# puts domain.items.collect(&:name) # list name
domain.items.each do |item|
puts "===> listing item: " + item.name
unless block.nil?
yield item
end
end
end
def list_attributes_by_item(item)
puts "===> listing attributes: " + item.attributes.to_h().to_s()
end
# ==== Insert ====
#
# item key -> image's md5
#
# followed by a hash contains
#
# upload_time -> ISO 8601 formatted time stamp
# title -> [title_en, title_ja, ...]
# type -> type string (Dog, Cat etc.)
# group -> [group id 1, group id 2, ...], may be nil
# link -> an Imgur link
# json -> response of uploaded image
# comment -> [], may be nil
def insert_image_info_item(title, type, md5, link, json)
self.prepareSDB
image_domain = self.getDomain(IMAGE_INFO_DOMAIN_NAME)
hash = {
:upload_time => time2ISO8601,
:title => [title],
:type => [type],
:group => [],
:link => link,
:json => json,
:comment => [],
}
puts "===> inserting: #{md5} , #{hash}"
image_domain.items.create(md5, hash)
puts "===> ok"
end
# item key -> group id, this is auto-generated
#
# followed by a hash contains
#
# upload_time -> ISO 8601 formatted time stamp
# title -> [title_en, title_ja, ...]
# images_md5 -> [md5_1, md5_2, ...]
# type -> type string (Dog, Cat etc.)
# comment -> [], may be nil
def insert_group_info_item(title, images_md5, type)
self.prepareSDB
group_domian = self.getDomain(GROUP_INFO_DOMAIN_NAME)
uniq_id = (0...8).map { (65 + rand(26)).chr }.join
hash = {
:upload_time => time2ISO8601,
:title => [title],
:images_md5 => images_md5,
:type => type,
:comment => []
}
puts "===> inserting: #{uniq_id} , #{hash}"
group_domian.items.create(uniq_id, hash)
end
def duplicated(domain, key)
!domain.items[key].attributes.to_h.empty?
end
def duplicated_in_domain_name(domain_name, key)
duplicated(getDomain(domain_name), key)
end
def time2ISO8601
Time.now.in_time_zone('Asia/Tokyo').iso8601
end
end
|