fork download
  1. # encoding:UTF-8
  2. require "rubygems"
  3. require "mechanize"
  4. require "pp"
  5. require "pry"
  6. require "json"
  7.  
  8. def list_url(page_no)
  9. #"http://w...content-available-to-author-only...d.net/tlpd/games/index.php?section=hots#tblt-2452-#{page_no}-1-DESC"
  10. "http://w...content-available-to-author-only...d.net/tlpd/games/index.php?section=hots&action=Update&tabulator_order_col=1&tabulator_order_desc=1&tabulator_page=#{page_no}"
  11. end
  12.  
  13. def fetch_page(url)
  14. if @agent.nil?
  15. @agent = Mechanize.new
  16. @agent.user_agent_alias = 'Linux Firefox'
  17. end
  18. @agent.get(url)
  19. end
  20.  
  21. def select_table(doc)
  22. doc.search("table#tblt_table")
  23. end
  24.  
  25. def go_prev_page(doc)
  26. @agent.click(doc.link_with(:text => "<"))
  27. end
  28.  
  29. def get_matches(table)
  30. rows = table.search("tr").collect do |raw_row|
  31. raw_row.search("td").map{|e| e.text.strip }
  32. end
  33. rows.shift
  34. rows.map{|r|
  35. {
  36. :date => r[1],
  37. :league => r[2],
  38. :map => r[3],
  39. :winner => r[4],
  40. :loser => r[5]
  41. }
  42. }.reverse
  43. end
  44.  
  45. begin_page, end_page = ARGV.map{|e| e.to_i}
  46.  
  47. match_list = []
  48. doc = fetch_page list_url end_page
  49.  
  50. (begin_page..end_page).reverse_each do |page_no|
  51. print "collecting page #{page_no}..."
  52.  
  53. matches = get_matches( select_table(doc) )
  54. matches.each{ |match|
  55. match_list << match
  56. }
  57. puts "#{match_list.length}"
  58.  
  59.  
  60. go_prev_page(doc) if page_no != 1
  61. end
  62.  
  63. File.open("matches.json", "w"){|f|
  64. f.puts JSON.generate(match_list)
  65. }
Runtime error #stdin #stdout #stderr 0.03s 8376KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mechanize (LoadError)
	from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
	from prog.rb:3:in `<main>'