[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"
# 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|
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
end
unless Rails.env.test?
CurrentUser.user = admin
CurrentUser.ip_addr = "127.0.0.1"
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"}
def api_request(path)
response = HTTParty.get("https://e621.net#{path}", {
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|
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|
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
md5 = Digest::MD5.hexdigest(data)
service = UploadService.new({
uploader_id: CurrentUser.id,
uploader_ip_addr: CurrentUser.ip_addr,
file: file,
tag_string: post["tags"].values.flatten.join(" "),
source: post["sources"].join("\n"),
description: post["description"],
rating: post["rating"],
md5: md5,
md5_confirmation: md5
})
uploader: CurrentUser.user,
uploader_ip_addr: CurrentUser.ip_addr,
direct_url: post["file"]["url"],
tag_string: post["tags"].values.flatten.join(" "),
source: post["sources"].join("\n"),
description: post["description"],
rating: post["rating"],
})
service.start!
end
end
unless Rails.env.test?
CurrentUser.user = admin
CurrentUser.ip_addr = "127.0.0.1"
import_posts
end