class AtCoderFriends::Scraping::Agent
common functions for scraping
Constants
- BASE_URL
- CONTACT
Attributes
agent[R]
ctx[R]
Public Class Methods
new(ctx)
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 21 def initialize(ctx) @ctx = ctx @agent = Mechanize.new agent.user_agent = "AtCoderFriends/#{VERSION} (#{CONTACT})" agent.pre_connect_hooks << proc { sleep 0.1 } agent.log = Logger.new(STDERR) if ctx.options[:debug] load_session end
Public Instance Methods
common_url(path)
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 34 def common_url(path) File.join(BASE_URL, path) end
contest()
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 30 def contest @contest ||= ctx.path_info.contest_name end
contest_url(path = '')
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 38 def contest_url(path = '') File.join(BASE_URL, 'contests', contest, path) end
find_lang(page, langs)
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 57 def find_lang(page, langs) langs.find do |lng| page.search("div#select-lang select option[value=#{lng}]")[0] end || langs[0] end
lang_id(ext)
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 42 def lang_id(ext) [lang_id_conf(ext)].flatten end
lang_id_conf(ext)
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 46 def lang_id_conf(ext) ctx.config.dig('ext_settings', ext, 'submit_lang') || ( msg = <<~MSG submit_lang for .#{ext} is not specified. Available languages: #{lang_list_txt || '(failed to fetch)'} MSG raise AppError, msg ) end
lang_list()
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 69 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_txt()
click to toggle source
# File lib/at_coder_friends/scraping/agent.rb, line 63 def lang_list_txt lang_list &.map { |opt| "#{opt[:v]} - #{opt[:t]}" } &.join("\n") end