Commits

Josep Batalle committed f9f584e

Javascript functions implemented

Comments (0)

Files changed (3)

 Amarok.Window.ToolsMenu.updateList['triggered()'].connect(updateListJson);
 
 function updateListJson() {
-//    var state = executeScript("default");
-    var state = curlAuth("default");
-    listSongs(state);
+    var authToken = curlAuth("default");
+    listSongs(authToken, "default");
     getCookie("default");
     update = false;
     newItems();
-    if(state == '')
-      var state = curl("option1");
-    if(state != '')
+    if(authToken == ''){
+      var authToken = curlAuth("option1");
+      listSongs(authToken, "option1");
+	    getCookie("option1");
+    }
+    if(authToken != '')
       Amarok.alert("Your music list is updated... Restart Amarok please.");
 }
 
     p.waitForFinished()
     var Response = p.readAllStandardOutput();
     var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var checkPython = textStream.readAll();
-    if (checkPython) return true;
+    var checkCurl = textStream.readAll();
+    if (checkCurl) return true;
     else false;
 }
 
 function doCurl(args, dirScript){
-	var p = new QProcess();
- 	p.setWorkingDirectory(dirScript);
- 	p.start("curl", args, QIODevice.ReadOnly);
-	p.waitForFinished()
- 	var Response = p.readAllStandardOutput();
-	var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-	var tinyURL = textStream.readAll();
-	return tinyURL;
+    var p = new QProcess();
+    p.setWorkingDirectory(dirScript);
+    p.start("curl", args, QIODevice.ReadOnly);
+    p.waitForFinished()
+    var Response = p.readAllStandardOutput();
+    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
+    return textStream.readAll();
+}
+
+function getStreamByCurl(args, dirScript) {
+    var p = new QProcess();
+    p.setWorkingDirectory(dirScript);
+    p.start("curl", args, QIODevice.ReadOnly);
+    p.waitForFinished()
+    return p.readAllStandardOutput();
 }
 
 function curlAuth(type) {
-    type = "default";
     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 nameScript = "google_music.sh";
     var args = new Array();
     var clientLoginUrl = "https://www.google.com/accounts/ClientLogin";
-    var service = "sj";
     var email = Config["gpUserID"];
     var password = Config["gpPass"];
 
-    //   args[0] = clientLoginUrl+" --data-urlencode Email="+email+" --data-urlencode Passwd="+password+" -d accountType=GOOGLE -d service=sj";
     args[0] = clientLoginUrl;
     args[1] = "--data-urlencode";
     args[2] = "Email=" + email;
     args[6] = "accountType=GOOGLE";
     args[7] = "-d";
     args[8] = "service=sj";
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("curl", args, QIODevice.ReadOnly);
-    p.waitForFinished()
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    var authResponse = tinyURL;
-    Amarok.debug(tinyURL);
-    var auth = tinyURL;
+
+    var authResponse = doCurl(args, dirScript);
+/*
+    var auth = authResponse;
     var re = new RegExp("SID=(.+)", "g");
     var myArray = auth.match(re);
     var SID = myArray[0].split("SID=");
     var LSID = myArray[1].split("SID=");
     Amarok.debug(SID[1]);
     Amarok.debug(LSID[1]);
-
+*/
     m = auth.match(/SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)/)
     var SID = m[1];
     var LSID = m[2];
     var Auth = m[3];
-	return Auth;
+    return Auth;
 }
 
-function listSongs(AuthToken) {
-//    curl--header "Authorization: GoogleLogin auth=${authToken}"
-//    https: //www.googleapis.com/sj/v1beta1/tracks > ListSongs.json
-    type = "default";
+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
-    var nameScript = "google_music.sh";
     var args = new Array();
 
     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 p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("curl", args, QIODevice.ReadOnly);
-    p.waitForFinished()
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
+    var Response = getStreamByCurl(args, dirScript);
+/*    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");
     file.open(QIODevice.WriteOnly);
     file.write(Response);
 }
 
 function getCookie(type) {
-var email = Config["gpUserID"];
+    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 nameScript = "google_music.sh";
     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";
+    args[3] = "cookie.txt";
+    args[4] = "https://accounts.google.com/ServiceLogin?service=sj";
 
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("curl", args, QIODevice.ReadOnly);
-    p.waitForFinished()
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    var listSongs = tinyURL;
-    Amarok.debug(tinyURL);
-    var auth = tinyURL;
+    var authResponse = doCurl(args, dirScript);
+    var auth = authResponse;
     var re = new RegExp('id="dsh" value="(.*)"', "g");
     var myArray = auth.match(re);
-var dsh = myArray[0].split('value="');
-var dsh = dsh[1].split('"');
-Amarok.debug(dsh[0]);
-var DSH = dsh[0];
+    var dsh = myArray[0].split('value="');
+    var dsh = dsh[1].split('"');
+    Amarok.debug(dsh[0]);
+
     var re = new RegExp('  value="(.*)"', "g");
     var myArray = auth.match(re);
-Amarok.debug(myArray[0]);
-var galx = myArray[0].split('value="');
-var galx = galx[1].split('"');
-Amarok.debug(galx[0]);
+    var galx = myArray[0].split('value="');
+    var galx = galx[1].split('"');
+    Amarok.debug(galx[0]);
 
-var GALX = galx[0];
-getCookie2(DSH, GALX);
+    var DSH = dsh[0];
+    var GALX = galx[0];
+    getCookie2(DSH, GALX, dirScript);
 }
 
-function getCookie2(DSH, GALX){
-	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 email = Config["gpUserID"];
+function getCookie2(DSH, GALX, dirScript) {
+    var email = Config["gpUserID"];
     var password = Config["gpPass"];
-	var args = new Array();
-    args[0] = "-b";
-    args[1] = "cookie.txt";
-    args[2] = "-c";
-args[3] = "cookie.txt";
-args[4] = "-d";
-args[5] = "service=sj&dsh="+DSH+"&GALX="+GALX+"&pstMsg=1&dnConn=&checkConnection=youtube%3A138%3A1&checkedDomains=youtube&timeStmp=&secTok=&Email="+email+"&PersistentCookie=no&Passwd="+password+"&signIn=Sign+in";
-args[6] = "-X";
-args[7] = "POST";
-args[8] = "https://accounts.google.com/ServiceLoginAuth";
-
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("curl", args, QIODevice.ReadOnly);
-    p.waitForFinished()
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    var listSongs = tinyURL;
-    Amarok.debug(tinyURL);
-    var auth = tinyURL;
-	}
-	
-	function getSong(songId){
-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();
+    var args = new Array();
     args[0] = "-b";
     args[1] = "cookie.txt";
     args[2] = "-c";
-args[3] = "cookie.txt";
-args[4] = "https://music.google.com/music/play?u=0&songid="+songId+"&pt=e";
-///args[6] = ">";
-//args[7] = "url.json";
-
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("curl", args, QIODevice.ReadOnly);
-    p.waitForFinished()
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    var listSongs = tinyURL;
-    Amarok.debug(tinyURL);
-    return tinyURL;
+    args[3] = "cookie.txt";
+    args[4] = "-d";
+    args[5] = "service=sj&dsh=" + DSH + "&GALX=" + GALX + "&pstMsg=1&dnConn=&checkConnection=youtube%3A138%3A1&checkedDomains=youtube&timeStmp=&secTok=&Email=" + email + "&PersistentCookie=no&Passwd=" + password + "&signIn=Sign+in";
+    args[6] = "-X";
+    args[7] = "POST";
+    args[8] = "https://accounts.google.com/ServiceLoginAuth";
+
+    var authResponse = doCurl(args, dirScript);
+    var auth = authResponse;
 }
 
-function executeScript(type) {
-    Config["gpUserID"] = Amarok.Script.readConfig("gpUserID", "");
-    Config["gpPass"] = Amarok.Script.readConfig("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 nameScript = "google_music.sh";
+function getSong(songId) {
+    var dirScript = ".kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default
 
     var args = new Array();
-    args[0] = nameScript;
-    args[1] = "getList";
-    args[2] = Config["gpUserID"];
-    args[3] = Config["gpPass"];
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("sh", args, QIODevice.ReadOnly);
-    p.waitForFinished();
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    return tinyURL;
-}
-
-function getUrl(id) {
-    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 nameScript = "google_music.sh"; //getUrl
-    var args = new Array();
-    args[0] = nameScript;
-    args[1] = "getUrlSong";
-    args[2] = id;
-
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("sh", args, QIODevice.ReadOnly);
-    p.waitForFinished();
-    var Response = p.readAllStandardOutput();
-    var textStream = new QTextStream(Response, QIODevice.ReadOnly);
-    var tinyURL = textStream.readAll();
-    return tinyURL;
+    args[0] = "-b";
+    args[1] = "cookie.txt";
+    args[2] = "-c";
+    args[3] = "cookie.txt";
+    args[4] = "https://music.google.com/music/play?u=0&songid=" + songId + "&pt=e";
 
+    var authResponse = doCurl(args, dirScript);
+    var song = (new Function("return " + authResponse))();
+    Amarok.debug(song.url);
+    return song.url;
 }
 
-
 function ImportJsonFile(json_file) {
     if (json_file != null) {
         eval("var JSON_obj = " + ReadTextFile(json_file));
 var delayedArgs = null;
 var update = true;
 
-//var myRadioService = new RadioService("RADIO SERVICE", "aaa", "HTML DEscription", "aaaaaa", "Message");
 var myRadioService = new RadioService("Google Music SERVICE", "", "Listen your uploaded Music!", "", "You need a Google Music Account");
 var categoryName = "Google Music";
 var categoryImageFullPath = ScriptBaseDir() + "/Defaults/Category.default.image.png";
     onPopulating(delayedArgs[0], delayedArgs[1], delayedArgs[2]);
 }
 
-
 function GoogleMusicforAmarok() {
     ScriptableServiceScript.call(this, "Google Music for Amarok", 2, "Songs allocated in Google music", "GoogleMusic2", false);
 }
 }
 
 function onPopulating(level, callbackData, filter) {
+	if(!detectCurl){
+		Amarok.alert("Curl is not installed in the system!");
+		return;
+		}
     if (update == true) {
         delayedArgs = [level, callbackData, filter];
         newItems();
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.