Skip to content

Commit

Permalink
Merge pull request #48 from QWYNG/rewrite_code
Browse files Browse the repository at this point in the history
i did not like a lot of things so i rewrote it
  • Loading branch information
QWYNG committed Sep 23, 2023
2 parents 3faf2e2 + dfd5972 commit aa97849
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 50 deletions.
2 changes: 0 additions & 2 deletions green_day.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'faraday'
spec.add_dependency 'faraday-cookie_jar'
spec.add_dependency 'nokogiri'
spec.add_dependency 'parallel'
spec.add_dependency 'thor'
spec.add_dependency 'webrick'
spec.metadata['rubygems_mfa_required'] = 'true'
end
26 changes: 17 additions & 9 deletions lib/green_day/atcoder_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@ module GreenDay
class AtcoderClient
ATCODER_ENDPOINT = 'https://atcoder.jp'
COOKIE_FILE_NAME = '.cookie-store'
attr_reader :client, :cookie_jar
attr_reader :conn, :cookie_jar

def self.login(username, password)
new.login(username, password)
end

def self.get_parsed_body(path)
new.get_parsed_body(path)
end

def initialize
@cookie_jar = create_or_load_cookie_jar
@client = Faraday.new(url: ATCODER_ENDPOINT) do |builder|
@conn = Faraday.new(url: ATCODER_ENDPOINT) do |builder|
builder.use :cookie_jar, jar: cookie_jar
builder.request :url_encoded
builder.adapter :net_http
end
end

def get_parsed_body(path)
res = client.get(path)
res = conn.get(path)
Nokogiri::HTML.parse(res.body)
end

def login(username, password)
csrf_token = obtain_atcoder_csrf_token

client.post('/login',
username:,
password:,
csrf_token:)
conn.post('/login',
username:,
password:,
csrf_token:)

unless login_succeed?
## ex error:Username or Password is incorrect
Expand All @@ -55,11 +63,11 @@ def create_or_load_cookie_jar
end

def obtain_atcoder_csrf_token
get_login_response = client.get('/login')
get_login_response = conn.get('/login')
login_html = Nokogiri::HTML.parse(get_login_response.body)
login_html.at('input[name="csrf_token"]')['value']
rescue StandardError
raise Error, 'cant get_csrf_token'
raise Error, 'can not get_csrf_token'
end

def login_succeed?
Expand Down
36 changes: 17 additions & 19 deletions lib/green_day/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def login
print 'password:'
password = $stdin.noecho { |stdin| stdin.gets(chomp: true) }.tap { puts }

AtcoderClient.new.login(username, password)
AtcoderClient.login(username, password)
puts(
"Successfully created #{AtcoderClient::COOKIE_FILE_NAME}"
.colorize(:green)
Expand All @@ -26,38 +26,36 @@ def login

desc 'new [contest name]', 'create contest workspace and spec'
def new(contest_name)
contest = Contest.new(contest_name, AtcoderClient.new)
contest = Contest.new(contest_name)
FileUtils.makedirs("#{contest.name}/spec")

Parallel.each(contest.tasks, in_threads: THREAD_COUNT) do |task|
create_submit_file(task)
create_spec_file(task)
end
contest.tasks.map do |task|
create_files_in_thread(task)
end.each(&:join)

puts "Successfully created #{contest.name} directory".colorize(:green)
end

private

def create_submit_file(task)
File.open(submit_file_path(task), 'w')
def create_files_in_thread(task)
Thread.new do
create_task_file(task)
create_task_spec_file(task)
end
end

def create_spec_file(task)
test =
TestBuilder.build_test(
submit_file_path(task),
task.sample_answers
)
File.write(spec_file_path(task), test)
def create_task_file(task)
FileUtils.touch(task_file_name(task))
end

def submit_file_path(task)
"#{task.contest.name}/#{task.name}.rb"
def create_task_spec_file(task)
test_content = TestBuilder.build_test(task_file_name(task), task.sample_answers)
File.write("#{task.contest.name}/spec/#{task.name}_spec.rb", test_content)
end

def spec_file_path(task)
"#{task.contest.name}/spec/#{task.name}_spec.rb"
def task_file_name(task)
"#{task.contest.name}/#{task.name}.rb"
end
end
end
18 changes: 6 additions & 12 deletions lib/green_day/contest.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
# frozen_string_literal: true

require 'forwardable'
require_relative 'task'

module GreenDay
class Contest
extend Forwardable
delegate get_parsed_body: :@client

attr_reader :name, :tasks

def initialize(contest_name, client)
@client = client
def initialize(contest_name)
@name = contest_name

task_names_and_paths = fetch_task_names_and_paths
@tasks =
Parallel.map(task_names_and_paths,
in_threads: THREAD_COUNT) do |task_name, task_path|
Task.new(self, task_name, task_path, @client)
@tasks = fetch_task_names_and_paths.map do |task_name, task_path|
Thread.new do
Task.new(self, task_name, task_path)
end
end.map(&:value)
end

private

def fetch_task_names_and_paths
body = get_parsed_body("contests/#{name}/tasks")
body = AtcoderClient.get_parsed_body("contests/#{name}/tasks")
task_elements = body.search('tbody tr td:first a')

task_elements.to_h do |element|
Expand Down
10 changes: 2 additions & 8 deletions lib/green_day/task.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# frozen_string_literal: true

require 'forwardable'

module GreenDay
class Task
extend Forwardable
delegate get_parsed_body: :@client

attr_reader :contest, :name, :path, :sample_answers

def initialize(contest, name, path, client)
@client = client
def initialize(contest, name, path)
@contest = contest
@name = name
@path = path
Expand All @@ -26,7 +20,7 @@ def create_sample_answers_hash
end

def fetch_inputs_and_outputs
body = get_parsed_body(path)
body = AtcoderClient.get_parsed_body(path)
samples = body.css('.lang-ja > .part > section > pre').map { |e| e.children.text }

inputs, outputs = samples.partition.with_index { |_sample, i| i.even? }
Expand Down

0 comments on commit aa97849

Please sign in to comment.