#!/usr/bin/env ruby # # Author: FernyB # URL: http://fernyb.net/ # # A simple way to update your status on myspace.com # # For More Information # Visit: http://fernyb.net/myspace/api # require 'rubygems' require 'hpricot' require 'net/http' require 'uri' class MySpace Version = '0.0.1' Host = 'http://mobile.myspace.com' # Initialize the class with the username and password # Username is the email address you use for your myspace login # def initialize(user = {}) @username = user[:username] @password = user[:password] @html = nil @cookie = nil @response_code = nil @headers = {} @page_link = {:name => nil, :href => nil} @mood_options = {} end # Returns the username it was initialized with # def username @username end # Returns the username it was initialized with # def password @password end # Returns the cookie def cookie @cookie end # Returns the html def html @html end # Returns the response code # ( We will always like to have 200 but incases we don't get that # we can know it wasn't a 200 response and we can then take some sort of action ) # def response_code @response_code end # Parse the HTML to retrieve only links # def parse hp = Hpricot(@html) @linksfound = [] @link = {} @link["profile"] (hp/:a).each do |link| if link.innerHTML =~ /^\d+/ @link["profile"] = link[:href] if link[:accesskey] == "1" end end @link["profile"] end # Parse profile page to retrieve name and href for a numbered link # def parse_page_profile(item_number, param = {}) @page_link = {:name => nil, :href => nil} hp = Hpricot(@html) (hp/:a).each do |link| html_text = link.innerHTML if html_text =~ /^\d+/ if html_text[item_number.to_s.index(item_number.to_s), html_text.index(".")] == item_number.to_s @page_link = {:name => link.innerHTML, :href => Host + link[:href].to_s} end end end @page_link if param[:follow] === true http_get_page({:href => @page_link[:href]}) end end # Do A HTTP GET Request # def http_get_page(http = {}) headers = set_headers # Set HTTP Headers headers["Cookie"] = @cookie # Set the cookie request_url = URI.parse(http[:href].to_s) get_params = request_url.to_s.split("?", 2) get_url_params = get_params[1] get_uri = request_url.path + "?" + get_url_params post_request = Net::HTTP::Get.new(request_url.path) headers.to_a.each do |k, v| post_request.[]= k, v end h = Net::HTTP.new(request_url.host, 80) resp, data = h.get(get_uri, post_request) @response_code = resp.code @html = data end # Parse the status update page to retrieve the html form # def submit_update_status(user = {}) #html = Hpricot(open("./status_mood.html")) html = Hpricot(@html) form = html.search("//form") action = form.first['action'] method = form.first['method'] status_text = (form/"input#statusText") querystring = (form/"input#querystring") # Get the Select Options for Mood Status select_option = (form/"select#moodSelect") mood_option_name = select_option.first['name'] # The Name Of the HTML Select Field @mood_options = {} select_option.search('//option') do |option| option_value = option.attributes['value'] option_text = option.innerHTML @mood_options[option_value.to_s] = option_text.to_s end # Buld The Query String # "statusText=".urlencode(trim($message_text))."&moodSelect=0&querystring=". urlencode($query_string); message = user[:status] mood_status_value = "0" action_url = Host + action params = { status_text.first['name'].to_s => message, mood_option_name.to_s => mood_status_value, querystring.first['name'].to_s => querystring.first['value'].to_s } # Prepare to post status update @headers = set_headers # Set Http Headers @headers['Cookie'] = @cookie action_url = URI.parse(action_url) post_request = Net::HTTP::Post.new(action_url.path) post_request.set_form_data(params) @headers.to_a.each do |k, v| post_request.[]= k, v end res = Net::HTTP.new(action_url.host, action_url.port) res.start {|http| resbody = http.request(post_request) #@cookie = resbody.response.get_fields('Set-Cookie') @response_code = resbody.response.code @html = resbody.response.read_body } # end # This method will update your status. # You must pass your username, password and message # Hash: # :username, :password, :message # def self.update_status(user = {}) @myspace = self.new(:username => user[:username], :password => user[:password]) @myspace.login @myspace.parse @myspace.get_page_profile @myspace.parse_page_profile(7, {:follow => true}) @myspace.submit_update_status({:status => user[:status]}) @myspace.response_code end # Retrieve the Profile HTML page # def get_page_profile request_url = Host + @link['profile'].to_s profile_url = request_url headers = set_headers # Set HTTTP Headers headers["Cookie"] = @cookie request_url = URI.parse(request_url.to_s) get_params = request_url.to_s.split("?", 2) get_url_params = get_params[1] get_uri = request_url.path + "?" + get_url_params post_request = Net::HTTP::Get.new(request_url.path) headers.to_a.each do |k, v| post_request.[]= k, v end h = Net::HTTP.new(request_url.host, 80) resp, data = h.get(get_uri, post_request) @response_code = resp.code @html = data end # Login into MySpace # def login login_url = "http://mobile.myspace.com/domestic/login.wap" set_headers # Set Http Headers params = { "emailTextBox" => "#{@username}", "passwordTextBox" => "#{@password}", "rememberMe" => "true", "loginCommand" => "Log In", "querystring" => "jklaajkkjwioppznzknllkk=&friendid=-1&useimages=True&ispost=True" } login_url = URI.parse(login_url) post_request = Net::HTTP::Post.new(login_url.path) post_request.set_form_data(params) @headers.to_a.each do |k, v| post_request.[]= k, v end res = Net::HTTP.new(login_url.host, login_url.port) res.start {|http| resbody = http.request(post_request) @cookie = resbody.response.get_fields('Set-Cookie') @html = resbody.response.read_body } end private # Set http headers def set_headers @headers = { "User-Agent" => "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3", "Accept" => "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language" => "en-us,en;q=0.5", "Accept-Encoding" => "utf-8", "Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Keep-Alive" => "0", "Connection" => "close", "Referer" => "http://mobile.myspace.com/" } end end