Advertisement
DanielHolm

scrobble.js #2

Jul 2nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function request(url,type,params) {
  2.     customdebug(url); // print presentet URL
  3.     customdebug(type); // print presentet type
  4.  
  5.     if (type === "POST") {
  6.         var body = '<form method="post" action="' + url + '" id="form">';
  7.         for (var param in params) {
  8.             body += '<input type="text" name="' + param + '" value="' + params[param] + '">';
  9.         }
  10.         body += '</form>';
  11.         customdebug("Sending this: "+body);
  12.     }
  13.  
  14.     var xhr = new XMLHttpRequest(); // create new XMLHttpRequest
  15.     //xhr.setRequestHeader("User-Agent", "Music-App/"+appVersion);
  16.     //xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  17.  
  18.     xhr.onreadystatechange = function() {
  19.         if (xhr.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
  20.             showRequestInfo("Headers -->");
  21.             showRequestInfo(xhr.getAllResponseHeaders ());
  22.             showRequestInfo("Last modified -->");
  23.             showRequestInfo(xhr.getResponseHeader ("Last-Modified"));
  24.             xhr.send(body); // send the data
  25.  
  26.             // did it work?
  27.             if (xhr.readyState==4 && xhr.status === 200) {
  28.                 customdebug("Response = " + xhr.responseText);
  29.             }
  30.  
  31.             else {
  32.                 // This is very handy for finding out why your web service won't talk to you
  33.                 customdebug("Status: " + xhr.status + ", Status Text: " + xhr.statusText + ", Ready state: "+xhr.readyState);
  34.             }
  35.  
  36.             customdebug("Response = " + xhr.responseText);
  37.             return xhr.responseXML;
  38.         }
  39.         else if (xhr.readyState == XMLHttpRequest.DONE) {
  40.             var a = xhr.responseXML.xhrumentElement;
  41.             for (var ii = 0; ii < a.childNodes.length; ++ii) {
  42.                 showRequestInfo(a.childNodes[ii].nodeName);
  43.             }
  44.             showRequestInfo("Headers -->");
  45.             showRequestInfo(xhr.getAllResponseHeaders ());
  46.             showRequestInfo("Last modified -->");
  47.             showRequestInfo(xhr.getResponseHeader ("Last-Modified"));
  48.             customdebug("Response = " + xhr.responseText);
  49.         }
  50.     }
  51.     xhr.open(type, url, true); // send the data (only async supported)
  52. }
  53.  
  54. function authenticate(username,password) {
  55.     // send to scrobble_url
  56.     var signature = auth_signature(username,password);
  57.  
  58.     // create array of needed parameters for authentication
  59.     var params = new Array();
  60.     params[0] = "method=auth.getMobileSession";
  61.     params[1] = "api_key="+api_key;
  62.     params[2] = "api_sig="+signature;
  63.     params[3] = "username="+username;
  64.     params[4] = "password="+password;
  65.  
  66.     var answer = request(scrobble_urls,"POST",params); // send the request to the function
  67.     customdebug("Request answered: "+answer);
  68.  
  69.     var loginstate = answer.getElementsByTagName("status").innerHTML; // get the status from the answer
  70.  
  71.     // get the key
  72.     var lfm = answer.getElementsByTagName("lfm");
  73.     var session = answer.getElementsByTagName("session");
  74.     var session_key = session.getElementsByTagName("key");
  75.  
  76.     customdebug("Session key: "+session_key);
  77.     customdebug("Login answer: "+loginstate);
  78.  
  79.     return loginstate;
  80. }
  81.  
  82. // mobile authentication
  83. function auth_signature(username,password) {
  84.     var signature = Qt.md5("api_key"+api_key+"methodauth.getMobileSessionpassword"+password+"username"+username+secret_key)
  85.     customdebug("Signature: "+signature);
  86.     return signature
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement