Class: Codeforces::Solutions::Downloader::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/codeforces/solutions/downloader/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



14
15
16
17
18
19
20
21
# File 'lib/codeforces/solutions/downloader/application.rb', line 14

def initialize
  @option = {
    :page_offset => 1,
    :page_limit => 5,
    :page_limit_specified => false,
    :output_directory => 'dist',
  }
end

Instance Attribute Details

#optionObject

Returns the value of attribute option.



12
13
14
# File 'lib/codeforces/solutions/downloader/application.rb', line 12

def option
  @option
end

Instance Method Details

#fetch_submission(contest_id, submission_id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/codeforces/solutions/downloader/application.rb', line 84

def fetch_submission(contest_id, submission_id)
  url = "http://codeforces.com/contest/#{contest_id}/submission/#{submission_id}"
  puts "fetch: #{url}"
  sleep 5
  body = get_body(url)
  doc = Nokogiri::HTML body
  {
    :source => doc.xpath('id("content")').search('//pre[contains(concat(" ",@class," ")," prettyprint ")]').text.strip,
    :language => doc.xpath('//table//tr')[1].search('//td')[3].text.strip,
  }
end

#fetch_submissionsArray

get submissions data from codeforces.com

Returns:

  • (Array)

    list of submissions



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/codeforces/solutions/downloader/application.rb', line 56

def fetch_submissions
  puts "pending..."

  @page_limit = @option[:page_limit]
  @page_limit = [@page_limit, get_page_limit() - @option[:page_offset] + 1].min

  puts "get submission list"
  puts "    page_limit = #{@page_limit}"
  puts "    page_offset = #{@option[:page_offset]}"
  puts "    output_directory = #{@option[:output_directory]}"

  submissions = get_submissions()

  len = submissions.length
  cnt = 0
  submissions.each {|s|
    info     = fetch_submission s[:contest_id], s[:submission_id]
    ext      = resolve_language(info[:language])
    filename = "#{@option[:output_directory]}/#{s[:submission_id]}.#{ext}"

    FileUtils.mkdir_p "#{@option[:output_directory]}"
    File.open(filename, 'w') {|f| f.write info[:source] }

    cnt += 1
    puts "save: #{filename} [#{cnt}/#{len}]"
  }
end

#parse_options(argv) ⇒ nil

parse options

Parameters:

  • argv (Array)

    array of command options

Returns:

  • (nil)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/codeforces/solutions/downloader/application.rb', line 26

def parse_options(argv)
  OptionParser.new do |option_parser|
    option_parser.version = Codeforces::Solutions::Downloader::VERSION
    # user_id
    option_parser.on('-u user-id', '--user-id user-id', 'specify codeforces user id', String) {|user_id|
      @option[:user_id] = user_id
    }
    # page offset
    option_parser.on('-o page-offset', '--page-offset page-offset', 'specify page offset (default = 0)', Integer) {|page_offset|
      @option[:page_offset] = page_offset
    }
    # page limit
    option_parser.on('-l page-limit', '--page-limit page-limit', 'specify max page number (default = 5)', Integer) {|page_limit|
      @option[:page_limit] = page_limit
      @option[:page_limit_specified] = true
    }
    # directory
    option_parser.on('-d directory', '--output-directory directory', 'specify output directory (default = dist)', String) {|output_directory|
      @option[:output_directory] = output_directory
    }
    option_parser.parse! argv
  end
  if @option[:user_id].nil?
    abort "Error: user id must be specified"
  end
  nil
end