Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function request(url,type,params) {
- customdebug(url); // print presentet URL
- customdebug(type); // print presentet type
- if (type === "POST") {
- var body = '<form method="post" action="' + url + '" id="form">';
- for (var param in params) {
- body += '<input type="text" name="' + param + '" value="' + params[param] + '">';
- }
- body += '</form>';
- customdebug("Sending this: "+body);
- }
- var xhr = new XMLHttpRequest(); // create new XMLHttpRequest
- //xhr.setRequestHeader("User-Agent", "Music-App/"+appVersion);
- //xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- xhr.onreadystatechange = function() {
- if (xhr.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
- showRequestInfo("Headers -->");
- showRequestInfo(xhr.getAllResponseHeaders ());
- showRequestInfo("Last modified -->");
- showRequestInfo(xhr.getResponseHeader ("Last-Modified"));
- xhr.send(body); // send the data
- // did it work?
- if (xhr.readyState==4 && xhr.status === 200) {
- customdebug("Response = " + xhr.responseText);
- }
- else {
- // This is very handy for finding out why your web service won't talk to you
- customdebug("Status: " + xhr.status + ", Status Text: " + xhr.statusText + ", Ready state: "+xhr.readyState);
- }
- customdebug("Response = " + xhr.responseText);
- return xhr.responseXML;
- }
- else if (xhr.readyState == XMLHttpRequest.DONE) {
- var a = xhr.responseXML.xhrumentElement;
- for (var ii = 0; ii < a.childNodes.length; ++ii) {
- showRequestInfo(a.childNodes[ii].nodeName);
- }
- showRequestInfo("Headers -->");
- showRequestInfo(xhr.getAllResponseHeaders ());
- showRequestInfo("Last modified -->");
- showRequestInfo(xhr.getResponseHeader ("Last-Modified"));
- customdebug("Response = " + xhr.responseText);
- }
- }
- xhr.open(type, url, true); // send the data (only async supported)
- }
- function authenticate(username,password) {
- // send to scrobble_url
- var signature = auth_signature(username,password);
- // create array of needed parameters for authentication
- var params = new Array();
- params[0] = "method=auth.getMobileSession";
- params[1] = "api_key="+api_key;
- params[2] = "api_sig="+signature;
- params[3] = "username="+username;
- params[4] = "password="+password;
- var answer = request(scrobble_urls,"POST",params); // send the request to the function
- customdebug("Request answered: "+answer);
- var loginstate = answer.getElementsByTagName("status").innerHTML; // get the status from the answer
- // get the key
- var lfm = answer.getElementsByTagName("lfm");
- var session = answer.getElementsByTagName("session");
- var session_key = session.getElementsByTagName("key");
- customdebug("Session key: "+session_key);
- customdebug("Login answer: "+loginstate);
- return loginstate;
- }
- // mobile authentication
- function auth_signature(username,password) {
- var signature = Qt.md5("api_key"+api_key+"methodauth.getMobileSessionpassword"+password+"username"+username+secret_key)
- customdebug("Signature: "+signature);
- return signature
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement