Created
February 4, 2013 20:59
-
-
Save chrjsorg/4709678 to your computer and use it in GitHub Desktop.
Yet another mechanize snippet. I'm getting used to it.
Gets the played songs count of $USER
Requires: rubygems, mechanize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# encoding: utf-8 | |
# This is just a proof of concept, not for general usage. | |
require 'rubygems' | |
require 'mechanize' | |
last_fm_url = 'http://last.fm/user/' | |
user = ARGV[0] | |
a = Mechanize.new { |agent| | |
agent.user_agent_alias = 'Linux Firefox' | |
} | |
if(ARGV.length < 1) | |
puts "feed me a user within the first agument" | |
Process.exit | |
end | |
begin | |
page = a.get(last_fm_url + user) | |
numbers = page.parser.xpath("//*[@class='flip']").inner_html | |
if(numbers.length < 1) | |
puts "User not existing, or didn't listen to any songs" | |
Process.exit | |
end | |
puts numbers | |
rescue Mechanize::ResponseCodeError => ex | |
case ex.response_code | |
when '404' then | |
puts 'User not found' | |
else | |
puts 'Error:' + ex.message | |
end | |
Process.exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment