[Docker] Clean up seeding script

This commit is contained in:
Earlopain 2022-10-20 18:39:45 +02:00
parent 982568a603
commit 0de7135df9
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897

View File

@ -13,7 +13,7 @@ end
puts "== Seeding database with sample content ==\n" puts "== Seeding database with sample content ==\n"
# Uncomment to see detailed logs # Uncomment to see detailed logs
#ActiveRecord::Base.logger = ActiveSupport::Logger.new($stdout) # ActiveRecord::Base.logger = ActiveSupport::Logger.new($stdout)
admin = User.find_or_create_by!(name: "admin") do |user| admin = User.find_or_create_by!(name: "admin") do |user|
user.created_at = 2.weeks.ago user.created_at = 2.weeks.ago
@ -41,41 +41,40 @@ ForumCategory.find_or_create_by!(id: Danbooru.config.alias_implication_forum_cat
category.can_view = 0 category.can_view = 0
end end
unless Rails.env.test? def api_request(path)
CurrentUser.user = admin response = HTTParty.get("https://e621.net#{path}", {
CurrentUser.ip_addr = "127.0.0.1" headers: { "User-Agent" => "e621ng/seeding" },
resources = YAML.load_file Rails.root.join("db", "seeds.yml")
url = "https://e621.net/posts.json?limit=#{ENV.fetch("SEED_POST_COUNT", 100)}&tags=id:#{resources["post_ids"].join(",")}"
response = HTTParty.get(url, {
headers: {"User-Agent" => "e621ng/seeding"}
}) })
json = JSON.parse(response.body) JSON.parse(response.body)
end
def import_posts
resources = YAML.load_file Rails.root.join("db/seeds.yml")
json = api_request("/posts.json?limit=#{ENV.fetch('SEED_POST_COUNT', 100)}&tags=id:#{resources['post_ids'].join(',')}")
json["posts"].each do |post| json["posts"].each do |post|
puts post["file"]["url"] puts post["file"]["url"]
data = Net::HTTP.get(URI(post["file"]["url"]))
file = Tempfile.new.binmode
file.write data
post["tags"].each do |category, tags| post["tags"].each do |category, tags|
Tag.find_or_create_by_name_list(tags.map {|tag| category + ":" + tag}) Tag.find_or_create_by_name_list(tags.map { |tag| "#{category}:#{tag}" })
end end
md5 = Digest::MD5.hexdigest(data)
service = UploadService.new({ service = UploadService.new({
uploader_id: CurrentUser.id, uploader: CurrentUser.user,
uploader_ip_addr: CurrentUser.ip_addr, uploader_ip_addr: CurrentUser.ip_addr,
file: file, direct_url: post["file"]["url"],
tag_string: post["tags"].values.flatten.join(" "), tag_string: post["tags"].values.flatten.join(" "),
source: post["sources"].join("\n"), source: post["sources"].join("\n"),
description: post["description"], description: post["description"],
rating: post["rating"], rating: post["rating"],
md5: md5, })
md5_confirmation: md5
})
service.start! service.start!
end end
end end
unless Rails.env.test?
CurrentUser.user = admin
CurrentUser.ip_addr = "127.0.0.1"
import_posts
end