tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,51 @@
|
|||
require 'open-uri'
|
||||
require 'rexml/document'
|
||||
|
||||
module RosettaCode
|
||||
|
||||
URL_ROOT = "http://rosettacode.org/mw"
|
||||
|
||||
def self.get_url(page, query)
|
||||
begin
|
||||
# Ruby 1.9.2
|
||||
pstr = URI.encode_www_form_component(page)
|
||||
qstr = URI.encode_www_form(query)
|
||||
rescue NoMethodError
|
||||
require 'cgi'
|
||||
pstr = CGI.escape(page)
|
||||
qstr = query.map {|k,v|
|
||||
"%s=%s" % [CGI.escape(k.to_s), CGI.escape(v.to_s)]}.join("&")
|
||||
end
|
||||
url = "#{URL_ROOT}/#{pstr}?#{qstr}"
|
||||
p url if $DEBUG
|
||||
url
|
||||
end
|
||||
|
||||
def self.get_api_url(query)
|
||||
get_url "api.php", query
|
||||
end
|
||||
|
||||
def self.category_members(category)
|
||||
query = {
|
||||
"action" => "query",
|
||||
"list" => "categorymembers",
|
||||
"cmtitle" => "Category:#{category}",
|
||||
"format" => "xml",
|
||||
"cmlimit" => 500,
|
||||
}
|
||||
while true
|
||||
url = get_api_url query
|
||||
doc = REXML::Document.new open(url)
|
||||
|
||||
REXML::XPath.each(doc, "//cm") do |task|
|
||||
yield task.attribute("title").value
|
||||
end
|
||||
|
||||
continue = REXML::XPath.first(doc, "//query-continue")
|
||||
break if continue.nil?
|
||||
cm = REXML::XPath.first(continue, "categorymembers")
|
||||
query["cmcontinue"] = cm.attribute("cmcontinue").value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
require 'rosettacode'
|
||||
|
||||
total_examples = 0
|
||||
|
||||
RosettaCode.category_members("Programming_Tasks") do |task|
|
||||
url = RosettaCode.get_url("index.php", {"action" => "raw", "title" => task})
|
||||
examples = open(url).read.scan("=={{header").length
|
||||
puts "#{task}: #{examples}"
|
||||
total_examples += examples
|
||||
end
|
||||
|
||||
puts
|
||||
puts "Total: #{total_examples}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue