mirror of
https://github.com/projekteuler/projekteuler.git
synced 2026-01-27 02:28:50 +01:00
Prefill original title and content from projecteuler.net
This commit is contained in:
21
app/jobs/pull_problem_content_job.rb
Normal file
21
app/jobs/pull_problem_content_job.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'open-uri'
|
||||
|
||||
class PullProblemContentJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(problem)
|
||||
html = URI.open("https://projecteuler.net/minimal=#{problem.id}").read
|
||||
html.strip!
|
||||
|
||||
# Linked problems
|
||||
html.gsub!('<a href="problem=', '<a href="/problem=')
|
||||
# Linked about pages
|
||||
html.gsub!('<a href="about=', '<a href="/about=')
|
||||
# Linked txt resources
|
||||
html.gsub!('<a href="project/resources/', '<a href="https://projecteuler.net/project/resources/')
|
||||
# Included images
|
||||
html.gsub!('<img src="project/images/', '<img src="https://projecteuler.net/project/images/')
|
||||
|
||||
problem.update(original_content: html, pulled_at: Time.current())
|
||||
end
|
||||
end
|
||||
23
app/jobs/pull_problems_job.rb
Normal file
23
app/jobs/pull_problems_job.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'csv'
|
||||
require 'open-uri'
|
||||
|
||||
class PullProblemsJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
PULL_URL = "https://projecteuler.net/minimal=problems;csv"
|
||||
|
||||
def perform
|
||||
csv = CSV.parse(URI.open(PULL_URL), headers: [:id, :title, :date_published, :date_last_updated, :solved_by])
|
||||
csv.each do |row|
|
||||
id = row[:id].to_i
|
||||
last_updated = Time.at(row[:date_last_updated].to_i)
|
||||
problem = Problem.where(id: id).first_or_create!
|
||||
if problem.pulled_at.nil? or problem.pulled_at < last_updated
|
||||
original_title = row[:title]
|
||||
problem.update(original_title: original_title)
|
||||
# Don't update pulled_at yet until we also successfully pulled the original_content
|
||||
PullProblemContentJob.perform_later problem
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user