Josep Batalle avatar Josep Batalle committed 93535d3

ListSong functions and get Auth using only javascript! Implemented

Comments (0)

Files changed (2)

 Amarok.Window.ToolsMenu.updateList['triggered()'].connect(updateListJson);
 
 function updateListJson() {
-    var authToken = curlAuth("default");
-    listSongs(authToken, "default");
-    getCookie("default");
+    var authToken = curlAuth();
+    listSongs(authToken);
+    getCookie();
     update = false;
     newItems();
     if(authToken == ''){
-      var authToken = curlAuth("option1");
-      listSongs(authToken, "option1");
-	    getCookie("option1");
+      Amarok.alert("Problem with Authentication");
     }
     if(authToken != '')
       Amarok.alert("Your music list is updated... Restart Amarok please.");
     else false;
 }
 
-function doCurl(args, dirScript){
+function doCurl(args) {
     var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
+    p.setWorkingDirectory(Amarok.Info.scriptPath());
     p.start("curl", args, QIODevice.ReadOnly);
     p.waitForFinished()
     var Response = p.readAllStandardOutput();
     return textStream.readAll();
 }
 
-function getStreamByCurl(args, dirScript) {
+function getStreamByCurl(args) {
     var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
+    p.setWorkingDirectory(Amarok.Info.scriptPath());
     p.start("curl", args, QIODevice.ReadOnly);
     p.waitForFinished()
     return p.readAllStandardOutput();
 }
 
-function curlAuth(type) {
-    if (type == "default") var dirScript = ".kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default
-    else var dirScript = "../.kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default other systems
+function curlAuth() {
     var args = new Array();
     var clientLoginUrl = "https://www.google.com/accounts/ClientLogin";
     var email = Config["gpUserID"];
     args[7] = "-d";
     args[8] = "service=sj";
 
-    var authResponse = doCurl(args, dirScript);
-/*
+    var authResponse = doCurl(args);
+    /*
     var auth = authResponse;
     var re = new RegExp("SID=(.+)", "g");
     var myArray = auth.match(re);
     Amarok.debug(SID[1]);
     Amarok.debug(LSID[1]);
 */
-    m = auth.match(/SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)/)
+    m = authResponse.match(/SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)/)
     var SID = m[1];
     var LSID = m[2];
     var Auth = m[3];
     return Auth;
 }
 
-function listSongs(AuthToken, type) {
-    if (type == "default") var dirScript = ".kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default
-    else var dirScript = "../.kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default other systems
+function listSongs(AuthToken) {
     var args = new Array();
-
+    Amarok.debug(AuthToken);
     args[0] = "--header";
     args[1] = "Authorization: GoogleLogin auth=" + AuthToken;
     args[2] = "https://www.googleapis.com/sj/v1beta1/tracks";
+    args[3] = ">";
+    args[4] = "ListSongs.json";
 
-    var Response = getStreamByCurl(args, dirScript);
-/*    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
+    var Response = getStreamByCurl(args);
+    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
     var tinyURL = textStream.readAll();
     var listSongs = tinyURL;
-    Amarok.debug(tinyURL);
-*/
-    var file = new QFile(".kde4/share/apps/amarok/scripts/google_music/ListSongs.json");
+    //    Amarok.debug(tinyURL);
+    Amarok.debug(Amarok.Info.scriptPath());
+    var file = new QFile(Amarok.Info.scriptPath() + "/ListSongs.json");
     file.open(QIODevice.WriteOnly);
     file.write(Response);
     file.close();
     var email = Config["gpUserID"];
     var password = Config["gpPass"];
 
-    if (type == "default") var dirScript = ".kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default
-    else var dirScript = "../.kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default other systems
     var args = new Array();
-
     args[0] = "-b";
     args[1] = "cookie.txt";
     args[2] = "-c";
     args[3] = "cookie.txt";
     args[4] = "https://accounts.google.com/ServiceLogin?service=sj";
 
-    var authResponse = doCurl(args, dirScript);
+    var authResponse = doCurl(args);
     var auth = authResponse;
     var re = new RegExp('id="dsh" value="(.*)"', "g");
     var myArray = auth.match(re);
 
     var DSH = dsh[0];
     var GALX = galx[0];
-    getCookie2(DSH, GALX, dirScript);
+    getCookie2(DSH, GALX);
 }
 
-function getCookie2(DSH, GALX, dirScript) {
+function getCookie2(DSH, GALX) {
     var email = Config["gpUserID"];
     var password = Config["gpPass"];
     var args = new Array();
     args[7] = "POST";
     args[8] = "https://accounts.google.com/ServiceLoginAuth";
 
-    var authResponse = doCurl(args, dirScript);
+    var authResponse = doCurl(args);
     var auth = authResponse;
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.