forked from danbooru/danbooru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.rb
205 lines (173 loc) · 4.78 KB
/
populate.rb
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
require 'set'
CurrentUser.ip_addr = "127.0.0.1"
used_names = Set.new([""])
def rand_string(n, unique = false)
string = ""
n = rand(n) + 1
while used_names.include?(string)
consonants = "bcdfghjklmnpqrstvwxz".scan(/./)
vowels = "aeiouy".scan(/./)
string = ""
n.times do
string << consonants[rand(consonants.size)]
string << vowels[rand(vowels.size)]
end
return string unless unique
end
used_names.add(string)
string
end
def rand_sentence(n, unique = false)
(0..n).map {rand_string(n, unique)}.join(" ") + "."
end
def rand_paragraph(n, unique = false)
(0..n).map {rand_sentence(n, unique)}.join(" ")
end
def rand_document(n, unique = false)
(0..n).map {rand_paragraph(n, unique)}.join("\n\n")
end
if User.count == 0
puts "Creating users"
begin
user = User.create(
:name => "admin",
:password => "password1",
:password_confirmation => "password1"
)
CurrentUser.user = user
User::Levels.constants.reject { |x| x == :ADMIN }.each do |level|
newuser = User.create(
name: level.to_s.downcase,
password: "password1",
password_confirmation: "password1"
)
newuser.promote_to!(User::Levels.const_get(level), user)
end
newuser = User.create(
:name => "banned",
:password => "password1",
:password_confirmation => "password1"
)
Ban.create(:user_id => newuser.id, :reason => "from the start", :duration => 99999)
newuser = User.create(
:name => "uploader",
:password => "password1",
:password_confirmation => "password1"
)
newuser.promote_to!(User::Levels::BUILDER, :can_upload_free => true, :skip_dmail => true)
newuser = User.create(
:name => "approver",
:password => "password1",
:password_confirmation => "password1"
)
newuser.promote_to!(User::Levels::BUILDER, :can_approve_posts => true, :skip_dmail => true)
end
0.upto(10) do |i|
User.create(
:name => rand_string(8, true),
:password => "password1",
:password_confirmation => "password1"
)
end
used_names = Set.new([""])
else
puts "Skipping users"
user = User.find_by_name("albert")
end
CurrentUser.user = User.admins.first
CurrentUser.ip_addr = "127.0.0.1"
if Upload.count == 0
puts "Creating uploads"
1.upto(50) do |i|
color1 = rand(4096).to_s(16)
color2 = rand(4096).to_s(16)
width = rand(100..2099)
height = (width * (rand(0.5) + 1)).to_i
url = "http://ipsumimage.appspot.com/#{width}x#{height},#{color1}"
tags = rand(1_000_000_000).to_s.scan(/../).join(" ")
puts url
service = UploadService.new(source: url, tag_string: tags, rating: "s")
service.start!
end
else
puts "Skipping uploads"
end
if Comment.count == 0
puts "Creating comments"
Post.all.each do |post|
rand(100).times do
Comment.create(:post_id => post.id, :body => rand_paragraph(6))
end
end
else
puts "Skipping comments"
end
if Note.count == 0
puts "Creating notes"
Post.order("random()").limit(10).each do |post|
rand(5).times do
note = Note.create(:post_id => post.id, :x => rand(post.image_width), :y => rand(post.image_height), :width => 100, :height => 100, :body => Time.now.to_f.to_s)
rand(20).times do |i|
note.update(body: rand_sentence(6))
end
end
end
else
puts "Skipping notes"
end
if Artist.count == 0
puts "Creating artists"
20.times do |i|
Artist.create(:name => rand_string(9, true))
end
used_names = Set.new([""])
else
puts "Skipping artists"
end
if TagAlias.count == 0
puts "Creating tag aliases"
20.times do |i|
TagAlias.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
end
used_names = Set.new([""])
else
puts "Skipping tag aliases"
end
if TagImplication.count == 0
puts "Creating tag implictions"
20.times do |i|
TagImplication.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
end
used_names = Set.new([""])
else
puts "Skipping tag implications"
end
if Pool.count == 0
puts "Creating pools"
1.upto(20) do |i|
pool = Pool.create(:name => rand_string(9, true))
rand(33).times do |j|
pool.add!(Post.order("random()").first)
end
end
used_names = Set.new([""])
end
if Favorite.count == 0
puts "Creating favorites"
Post.order("random()").limit(50).each do |post|
user = User.order("random()").first
Favorite.create!(post: post, user: user)
Favorite.create!(post: post, user: Currentuser.user)
end
else
puts "Skipping favorites"
end
if ForumTopic.count == 0
puts "Creating forum posts"
20.times do |i|
topic = ForumTopic.create(:title => rand_sentence(6))
rand(100).times do |j|
post = ForumPost.create(:topic_id => topic.id, :body => rand_document(6))
end
end
end