eBooru/db/seeds.rb

94 lines
2.6 KiB
Ruby
Raw Normal View History

2019-07-19 07:23:31 -04:00
# frozen_string_literal: true
2013-04-16 23:23:02 -04:00
2019-07-19 07:23:31 -04:00
require "digest/md5"
require "net/http"
require "tempfile"
# Uncomment to see detailed logs
2022-10-20 12:39:45 -04:00
# ActiveRecord::Base.logger = ActiveSupport::Logger.new($stdout)
2019-07-19 07:23:31 -04:00
admin = User.find_or_create_by!(name: "admin") do |user|
user.created_at = 2.weeks.ago
user.password = "e621test"
user.password_confirmation = "e621test"
user.password_hash = ""
2019-09-09 11:35:50 -04:00
user.email = "admin@e621.net"
2019-07-19 07:23:31 -04:00
user.can_upload_free = true
user.can_approve_posts = true
2019-07-19 07:23:31 -04:00
user.level = User::Levels::ADMIN
2011-07-03 19:12:31 -04:00
end
User.find_or_create_by!(name: Danbooru.config.system_user) do |user|
user.password = "ae3n4oie2n3oi4en23oie4noienaorshtaioresnt"
user.password_confirmation = "ae3n4oie2n3oi4en23oie4noienaorshtaioresnt"
user.password_hash = ""
user.email = "system@e621.net"
user.can_upload_free = true
user.can_approve_posts = true
user.level = User::Levels::JANITOR
end
ForumCategory.find_or_create_by!(name: "Tag Alias and Implication Suggestions") do |category|
2021-05-14 09:04:03 -04:00
category.can_view = 0
end
2022-10-20 12:39:45 -04:00
def api_request(path)
response = Faraday.get("https://e621.net#{path}", nil, user_agent: "e621ng/seeding")
2022-10-20 12:39:45 -04:00
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(',')}")
2019-09-09 11:35:50 -04:00
2021-04-05 09:23:02 -04:00
json["posts"].each do |post|
puts post["file"]["url"]
2019-09-09 11:35:50 -04:00
post["tags"].each do |category, tags|
2022-10-20 12:39:45 -04:00
Tag.find_or_create_by_name_list(tags.map { |tag| "#{category}:#{tag}" })
end
2019-09-09 11:35:50 -04:00
service = UploadService.new({
2022-10-20 12:39:45 -04:00
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"],
})
2019-09-09 11:35:50 -04:00
service.start!
end
2011-10-16 01:40:42 -04:00
end
2022-10-20 12:39:45 -04:00
def import_mascots
api_request("/mascots.json").each do |mascot|
puts mascot["url_path"]
Mascot.create!(
creator: CurrentUser.user,
mascot_file: Downloads::File.new(mascot["url_path"]).download!,
display_name: mascot["display_name"],
background_color: mascot["background_color"],
artist_url: mascot["artist_url"],
artist_name: mascot["artist_name"],
available_on_string: Danbooru.config.app_name,
active: mascot["active"],
)
end
end
2022-10-20 12:39:45 -04:00
unless Rails.env.test?
CurrentUser.user = admin
CurrentUser.ip_addr = "127.0.0.1"
begin
import_posts
import_mascots
rescue StandardError => e
puts "--------"
puts "#{e.class}: #{e.message}"
puts "Failure during seeding, continuing on..."
puts "--------"
end
2022-10-20 12:39:45 -04:00
end