Class: Gettc::Downloader
- Inherits:
-
Object
- Object
- Gettc::Downloader
- Defined in:
- lib/gettc/download.rb
Constant Summary collapse
- ROOT =
"https://community.topcoder.com"
- LIMIT =
10
Instance Method Summary collapse
- #download(url) ⇒ Object
- #download_detail(problem_id, round_id) ⇒ Object
- #download_solution(problem_id, round_id, solution_id) ⇒ Object
- #download_statement(problem_id) ⇒ Object
-
#initialize(account) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(account) ⇒ Downloader
Returns a new instance of Downloader.
82 83 84 85 86 |
# File 'lib/gettc/download.rb', line 82 def initialize(account) @account = account @proxy = get_proxy @cookie = end |
Instance Method Details
#download(url) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gettc/download.rb', line 88 def download(url) uri = url unless uri.is_a?(URI) uri = url.start_with?("http") ? URI.parse(url) : URI.join(ROOT, url) end connect(uri) do |http| LIMIT.times do request = Net::HTTP::Get.new(uri.request_uri) request["cookie"] = @cookie response = http.request(request) if response.is_a?(Net::HTTPSuccess) raise BadResponseError.new(request, response) if block_given? && !yield(response.body) return response.body end raise HttpError.new(request, response) unless response.is_a?(Net::HTTPMovedPermanently) uri = URI.parse(response["location"]) end raise DownloadError.new("Tried #{LIMIT} times without success") end end |
#download_detail(problem_id, round_id) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/gettc/download.rb', line 121 def download_detail(problem_id, round_id) download("/tc?module=ProblemDetail&rd=#{round_id}&pm=#{problem_id}") do |body| body.match("Problem Detail") body.match("Problem Name") body.match("Used As") body.match("Categories") body.match("Top Submission") end end |
#download_solution(problem_id, round_id, solution_id) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/gettc/download.rb', line 131 def download_solution(problem_id, round_id, solution_id) download("/stat?c=problem_solution&cr=#{solution_id}&rd=#{round_id}&pm=#{problem_id}") do |body| body.match("<TITLE>TopCoder Statistics - Problem Solution</TITLE>") body.match("System Test Results") end end |
#download_statement(problem_id) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/gettc/download.rb', line 113 def download_statement(problem_id) download("/stat?c=problem_statement&pm=#{problem_id}") do |body| body.match("<h3>Problem Statement</h3>") end rescue BadResponseError => error raise IDNotAvailable.new(problem_id, error.request, error.response) end |