Advertisement
DanielHolm

scrobble.js

Jul 1st, 2013
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (C) 2013   Daniel Holm <d.holmen@gmail.com>
  3.  *                      Victor Thompson <victor.thompson@gmail.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; version 3.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. // VARIABLES
  19. var api_key = "07c14de06e622165b5b4d55deb85f4da"
  20. var secret_key = "14125657da06bcb14919e23e2f09de32"
  21. var scrobble_urls = "https://ws.audioscrobbler.com/2.0/" // secure
  22. var scrobble_urlp = "http://ws.audioscrobbler.com/2.0/" // plain
  23.  
  24. // FUNCTIONS
  25.  
  26. // Custom debug funtion that's easier to shut off
  27. function customdebug(text) {
  28.     var debug = "1"; // set to "0" for not debugging
  29.     if (debug === "1") {
  30.         console.debug("Debug: "+text);
  31.     }
  32. }
  33.  
  34. function request(URL,TYPE) {
  35.     customdebug(URL); // print presentet URL
  36.     customdebug(TYPE); // print presentet URL
  37.  
  38.     var xhr = new XMLHttpRequest(); // create new XMLHttpRequest
  39.     var encodedURL = encodeURIComponent(URL); // Make sure whatever you post is URI encoded
  40.     xhr.open(TYPE, encodedURL, true); // only async supported
  41.     xhr.send(); // send the data
  42.  
  43.     //xhr.setRequestHeader("User-Agent", "Music-App/"+appVersion);
  44.     //xhr.setRequestHeader('Content-Type', 'text/xml');
  45.  
  46.     // did it work?
  47.     if (xhr.readyState==4 && xhr.status === 200) {
  48.         customdebug("Response = " + xhr.responseText);
  49.     }
  50.  
  51.     else {
  52.         // This is very handy for finding out why your web service won't talk to you
  53.         customdebug("Status: " + xhr.status + ", Status Text: " + xhr.statusText + ", Ready state: "+xhr.readyState);
  54.     }
  55.  
  56.     return xhr.responseText;
  57. }
  58.  
  59. function authenticate(username,password) {
  60.     // send to scrobble_url
  61.     var signature = auth_signature(username,password);
  62.     var params = "?method=auth.getMobileSession&api_key="+api_key+"&api_sig="+signature+"&username="+username+"&password="+password;
  63.     var lastfmURL = scrobble_urls+params;
  64.  
  65.     customdebug("URL to auth: "+lastfmURL);
  66.  
  67.     var answer = request(lastfmURL,"POST"); // send the request to the function
  68.     customdebug("Request answered: "+answer);
  69.  
  70.     var loginstate = answer.getElementsByTagName("status").innerHTML; // get the status from the answer
  71.  
  72.     // get the key
  73.     var lfm = answer.getElementsByTagName("lfm");
  74.     var session = answer.getElementsByTagName("session");
  75.     var session_key = session.getElementsByTagName("key");
  76.  
  77.     customdebug("Session key: "+session_key);
  78.     customdebug("Login answer: "+loginstate);
  79.  
  80.     return loginstate;
  81. }
  82.  
  83. // mobile authentication
  84. function auth_signature(username,password) {
  85.     var signature = Qt.md5("api_key"+api_key+"methodauth.getMobileSessionpassword"+password+"username"+username+secret_key)
  86.     customdebug("Signature: "+signature);
  87.     return signature
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement