1 #!/usr/bin/env ruby 2 3 require 'rubygems' 4 require 'mechanize' 5 6 HELP_STRING =<<EOS 7 8 Tool for fetching wordpress.com weblog statistics. Usage: 9 10 wls.rb [username] [pwd] 11 12 where 'user' is your wordpress user name and 'pwd' is your 13 password respectively. 14 15 EOS 16 17 if not ARGV.grep(/-h|--help/).empty? 18 puts HELP_STRING 19 exit(0) 20 end 21 22 # try to access the weblog statistics page 23 user = 'muharem' 24 password = nil # set your password here if you dislike being prompted for it 25 26 if ARGV[0] 27 user = ARGV[0] 28 end 29 if ARGV[1] 30 password = ARGV[1] 31 end 32 33 stats_url = "http://#{user}.wordpress.com/wp-admin/index.php?page=stats" 34 35 # instantiate/initialise web agent .. 36 agent = WWW::Mechanize.new 37 agent.user_agent_alias = 'Mac Safari' 38 # .. and get the weblog statistics page 39 page = agent.get(stats_url) 40 41 # did we get back the login form? 42 if (page.title.strip.split[-1] == 'Login') 43 # yes, fill it in and submit it 44 loginf = page.form('loginform') 45 loginf.log = user 46 if not password 47 print "Enter your wordpress.com password: " 48 password = $stdin.gets.chomp 49 end 50 loginf.pwd = password 51 agent.submit(loginf, loginf.buttons.first) 52 end 53 54 # now get the actual weblog statistics page 55 page = agent.get_file(stats_url) 56 # parse it! 57 doc = Hpricot(page) 58 59 # search for the div elements that contain the statistics data 60 stats_divs = doc.search("//div[@class='statsdiv']") 61 stats_divs.each do |div| 62 heading = div.search("h3/a/text()") 63 # we are only interested in the statistics for today 64 day = div.search("h4/text()").first 65 if (heading and day) 66 heading = "==== #{heading} (#{day.inner_text.downcase}) ====".center(50) 67 puts "\n#{heading}\n" 68 # find the table with today's statistics data 69 tab = div.search("table").first 70 if tab 71 # extract the statistics data from the <tr> elements 72 tab.search("tr").each do |tr| 73 what = tr.search("td[@class='label']") 74 views = tr.search("td[@class='views']") 75 whats = what.inner_text.strip() 76 if not whats.empty? 77 views = views.inner_text.strip() 78 printf("%s -- %5s\n", whats.center(45), views) 79 end 80 end 81 end 82 end 83 end 84 # grab the div with the general (weblog level) statistics data 85 gbdiv = doc.search("//div[@id='generalblog']") 86 # find the <p> element with the number of views today 87 vtoday = gbdiv.search("p").find { |p| p.inner_text.index('Views today') } 88 if vtoday 89 printf("\n%s\n\n", "=> #{vtoday.inner_text.strip} <=".center(45)) 90 else 91 puts "\n\n!! No weblog statistics data found." 92 puts " Did you enter a wrong user name and/or password?" 93 end