Class: AtCoderFriends::Scraping::Agent
- Inherits:
-
Object
- Object
- AtCoderFriends::Scraping::Agent
- 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
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#ctx ⇒ Object
readonly
Returns the value of attribute ctx.
Instance Method Summary collapse
- #common_url(path) ⇒ Object
- #config ⇒ Object
- #contest ⇒ Object
- #contest_url(path = '') ⇒ Object
-
#initialize(ctx) ⇒ Agent
constructor
A new instance of Agent.
- #lang_id(ext) ⇒ Object
- #lang_list ⇒ Object
- #lang_list_txt ⇒ Object
Methods included from Submission
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.[:debug] load_session end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
19 20 21 |
# File 'lib/at_coder_friends/scraping/agent.rb', line 19 def agent @agent end |
#ctx ⇒ Object (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 |
#config ⇒ Object
33 34 35 |
# File 'lib/at_coder_friends/scraping/agent.rb', line 33 def config ctx.config end |
#contest ⇒ Object
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_list ⇒ Object
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 . .reject { |opt| opt.value.empty? } .map do |opt| { v: opt.value, t: opt.text } end end end |
#lang_list_txt ⇒ Object
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 |