Class: AtCoderFriends::Scraping::Agent

Inherits:
Object
  • Object
show all
Includes:
PathUtil, Authentication, CustomTest, Session, Submission, Tasks
Defined in:
lib/at_coder_friends/scraping/agent.rb

Overview

common functions for scraping

Constant Summary collapse

BASE_URL =
'https://atcoder.jp/'

Constants included from PathUtil

PathUtil::CASES_DIR, PathUtil::SMP_DIR, PathUtil::TMP_DIR

Constants included from Authentication

AtCoderFriends::Scraping::Authentication::XPATH_USERNAME

Constants included from Session

Session::SESSION_STORE_FMT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Submission

#post_submit, #submit

Methods included from PathUtil

cases_dir, contest_name, makedirs_unless, smp_dir, split_prg_path, tmp_dir

Methods included from CustomTest

#check_custom_test, #code_test, #post_custom_test

Methods included from Tasks

#fetch_all, #fetch_assignments, #fetch_problem

Methods included from Authentication

#fetch_raw, #fetch_with_auth, #post_login, #read_auth, #show_username, #username_link

Methods included from Session

#load_session, #save_session, #session_store

Constructor Details

#initialize(ctx) ⇒ Agent

Returns a new instance of Agent.



21
22
23
24
25
26
27
# File 'lib/at_coder_friends/scraping/agent.rb', line 21

def initialize(ctx)
  @ctx = ctx
  @agent = Mechanize.new
  agent.pre_connect_hooks << proc { sleep 0.1 }
  agent.log = Logger.new(STDERR) if ctx.options[:debug]
  load_session
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



19
20
21
# File 'lib/at_coder_friends/scraping/agent.rb', line 19

def agent
  @agent
end

#ctxObject (readonly)

Returns the value of attribute ctx.



19
20
21
# File 'lib/at_coder_friends/scraping/agent.rb', line 19

def ctx
  @ctx
end

Instance Method Details

#common_url(path) ⇒ Object



37
38
39
# File 'lib/at_coder_friends/scraping/agent.rb', line 37

def common_url(path)
  File.join(BASE_URL, path)
end

#configObject



33
34
35
# File 'lib/at_coder_friends/scraping/agent.rb', line 33

def config
  ctx.config
end

#contestObject



29
30
31
# File 'lib/at_coder_friends/scraping/agent.rb', line 29

def contest
  @contest ||= contest_name(ctx.path)
end

#contest_url(path = '') ⇒ Object



41
42
43
# File 'lib/at_coder_friends/scraping/agent.rb', line 41

def contest_url(path = '')
  File.join(BASE_URL, 'contests', contest, path)
end

#lang_id(ext) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/at_coder_friends/scraping/agent.rb', line 45

def lang_id(ext)
  config.dig('ext_settings', ext, 'submit_lang') || (
    msg = "      submit_lang for .\#{ext} is not specified.\n      Available languages:\n      \#{lang_list_txt || '(failed to fetch)'}\n    MSG\n    raise AppError, msg\n  )\nend\n"

#lang_listObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/at_coder_friends/scraping/agent.rb', line 62

def lang_list
  @lang_list ||= begin
    page = fetch_with_auth(contest_url('custom_test'))
    form = page.forms[1]
    sel = form.field_with(name: 'data.LanguageId')
    sel && sel
      .options
      .reject { |opt| opt.value.empty? }
      .map do |opt|
        { v: opt.value, t: opt.text }
      end
  end
end

#lang_list_txtObject



56
57
58
59
60
# File 'lib/at_coder_friends/scraping/agent.rb', line 56

def lang_list_txt
  lang_list
    &.map { |opt| "#{opt[:v]} - #{opt[:t]}" }
    &.join("\n")
end