Permalink
Browse files

Uploaded a new tool: RL_participants_countries.rb. It is using Mechan…

…ize and Nokogiri. It outputs text data for ChartGo (http://www.chartgo.com/).
  • Loading branch information...
1 parent 732eea7 commit 24a42d0a6a783e3c3f403ebdb4c86d58611fadac @ashbb committed Dec 25, 2010
View
@@ -0,0 +1,83 @@
+RubyLearning.org Participants' Countries
+--------------------------------------
+
+Snapshot
+---------
+![Ruby_Metaprogramming_5th_batch.png](https://github.com/ashbb/ruby_learning_participants/raw/master/Ruby_Metaprogramming_5th_batch.png)
+
+
+Requirement
+------------
+
+ gem install mechanize
+ gem install nokogiri
+
+
+Usage
+------
+
+- Set your login user name, password and course name at line 10-12 of RL_participants_countries.rb
+
+ #######################################
+ login_form['username'] = "*****" <--- input your login user name, i.e. "asakawa"
+ login_form['password'] = "*****" <--- input your login password, i.e. "12345"
+ course = "*****" <--- input course name, i.e. "Core Ruby 20th batch"
+ #teachers = {'India' => 1, 'Japan' => 1, 'United States' => 1}
+ #######################################
+
+- Execute RL_participants_countries.rb
+
+ ruby RL_participants_countries.rb
+
+- Will get the output like this:
+
+<pre>
+>ruby RL_participants_countries.rb
+United States
+Brazil
+Germany
+Spain
+Canada
+United Kingdom
+Australia
+Italy
+Austria
+Mexico
+India
+Chile
+New Zealand
+Greece
+Poland
+Cambodia
+Ukraine
+Japan
+Venezuela
+
+18
+7
+5
+4
+4
+3
+3
+3
+2
+2
+2
+1
+1
+1
+1
+1
+1
+1
+1
+>Exit code: 0
+</pre>
+
+- Open http://www.chartgo.com/ with your browser and input the data (copy and paste above output data) by hand.
+
+You will get a chart!
+
+Good luck,
+ashbb
File renamed without changes.
@@ -0,0 +1,48 @@
+require 'rubygems'
+require 'mechanize'
+require 'nokogiri'
+
+agent = Mechanize.new
+login_page = agent.get("http://rubylearning.org/class/login/index.php")
+login_form = login_page.forms.first
+
+#######################################
+login_form['username'] = "your login name"
+login_form['password'] = "your login password"
+course = "Core Ruby 20th batch"
+teachers = {'India' => 1, 'Japan' => 1, 'United States' => 1}
+#######################################
+
+agent.submit(login_form)
+main_page = agent.get('http://rubylearning.org/class/course/index.php')
+course_link = main_page.link_with(:text => course)
+course_page = agent.get(course_link.href)
+participants_link = course_page.link_with(:text => 'Participants')
+participants_page = agent.get(participants_link.href)
+
+all_text = participants_page.search("//div[@id='showall']").inner_text
+all_link = participants_page.link_with(:text => all_text)
+all_page = agent.get(all_link.href)
+
+list, countries = [], []
+doc = Nokogiri all_page.body
+doc.search("//td[@class='cell c3']").each do |e|
+ n = e.inner_text.index(',')
+ n = 0 unless n
+ list << e.inner_text[0..n-1]
+end
+
+list.sort!
+
+until list.empty?
+ country, rest = list.partition{|e| list.first == e}
+ countries << [country.first, country.length]
+ list = rest
+end
+
+countries = countries.collect{|c, n| [c, teachers[c] ? n-teachers[c] : n]}
+countries = countries.sort_by{|c, n| n}.collect{|c, n| [c, n] unless n.zero?}.reverse - [nil]
+
+puts countries.map(&:first)
+puts
+puts countries.map(&:last)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 24a42d0

Please sign in to comment.