Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2013 Daniel Holm <d.holmen@gmail.com>
- * Victor Thompson <victor.thompson@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- // VARIABLES
- var api_key = "07c14de06e622165b5b4d55deb85f4da"
- var secret_key = "14125657da06bcb14919e23e2f09de32"
- var scrobble_urls = "https://ws.audioscrobbler.com/2.0/" // secure
- var scrobble_urlp = "http://ws.audioscrobbler.com/2.0/" // plain
- // FUNCTIONS
- // Custom debug funtion that's easier to shut off
- function customdebug(text) {
- var debug = "1"; // set to "0" for not debugging
- if (debug === "1") {
- console.debug("Debug: "+text);
- }
- }
- function request(URL,TYPE) {
- customdebug(URL); // print presentet URL
- customdebug(TYPE); // print presentet URL
- var xhr = new XMLHttpRequest(); // create new XMLHttpRequest
- var encodedURL = encodeURIComponent(URL); // Make sure whatever you post is URI encoded
- xhr.open(TYPE, encodedURL, true); // only async supported
- xhr.send(); // send the data
- //xhr.setRequestHeader("User-Agent", "Music-App/"+appVersion);
- //xhr.setRequestHeader('Content-Type', 'text/xml');
- // 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);
- }
- return xhr.responseText;
- }
- function authenticate(username,password) {
- // send to scrobble_url
- var signature = auth_signature(username,password);
- var params = "?method=auth.getMobileSession&api_key="+api_key+"&api_sig="+signature+"&username="+username+"&password="+password;
- var lastfmURL = scrobble_urls+params;
- customdebug("URL to auth: "+lastfmURL);
- var answer = request(lastfmURL,"POST"); // 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