Commits

Josep Batalle  committed f939574

Final Version 1.2

  • Participants
  • Parent commits 3a08657

Comments (0)

Files changed (7)

 
 This script allows you to listen your music allocated in your account in Google Music in your Amarok player.
 
-INSTRUCTIONS
+== INSTRUCTIONS ==
 Download this script and put it into scripts directory (usually ~/.kde/share/apps/amarok/scripts/). Then, active the script in your Amarok 
 configuration.
 
-IMPORTANT
-You need to give execution permissions to the google_music.sh file.
-
-You can use the following command: 
-
-  chmod +x ~/.kde4/share/apps/amarok/scripts/google_music/google_music.sh
-  
-or if you stay inside the folder:
-
-  chmod +x google_music.sh
+== IMPORTANT ==
+You need to have installed curl.
 
 Once the plugin is installed, you can see a new option in Tools Menu. Here you have to put your google account. Then, you can download the 
-list of songs pushing the button allocated in the Tools Menu ("Update Google Music List"). After a few seconds, AMarok shows an alert in 
+list of songs pushing the button allocated in the Tools Menu ("Update Google Music List"). After a few seconds, Amarok shows an alert in 
 order to confirm the correct downloaded. Then restart Amarok to check the list of songs downloaded.
 
 At this moment is only a basic system, works fine but needs more little changes in order to improve the reliability.
+/*
+ *		This Script allows you to listen the music allocated in google
+ *		music account using Amarok
+ *
+ * Copyright (C) 
+ *
+ *  (C)  2013 Josep Batalle Oronich <jbatalle3@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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ *
+ * Version: 1.2
+ */
+
 var Config = Array();
 var helpWindow = new helpWindow();
 

File docs/CHANGELOG.txt

+v1.2:
+- Bash script functions passed to javascript.
 v1.0:
 - First script release.

File docs/README.txt

 configuration.
 
 IMPORTANT
-You need to give execution permissions to the google_music.sh file.
-
-You can use the following command: 
-
-  chmod +x ~/.kde4/share/apps/amarok/scripts/google_music/google_music.sh
-  
-or if you stay inside the folder:
-
-  chmod +x google_music.sh
+You need to have installed curl.
 
 Once the plugin is installed, you can see a new option in Tools Menu. Here you have to put your google account. Then, you can download the 
-list of songs pushing the button allocated in the Tools Menu ("Update Google Music List"). After a few seconds, AMarok shows an alert in 
+list of songs pushing the button allocated in the Tools Menu ("Update Google Music List"). After a few seconds, Amarok shows an alert in 
 order to confirm the correct downloaded. Then restart Amarok to check the list of songs downloaded.
 
 At this moment is only a basic system, works fine but needs more little changes in order to improve the reliability.
+/*
+ *		This Script allows you to listen the music allocated in google
+ *		music account using Amarok
+ *
+ * Copyright (C) 
+ *
+ *  (C)  2013 Josep Batalle Oronich <jbatalle3@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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ *
+ * Version: 1.2
+ */
+
 function detectCurl() {
     var p = new QProcess();
     p.start("which", ["curl"], QIODevice.ReadOnly);
     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 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";
-    
-    var args = new Array();
-    args[0] = nameScript;
-    args[1] = "getList";
-    args[2] = Config["gpUserID"];
-    args[3] = Config["gpPass"];
+function doCurl(args) {
     var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("sh", args, QIODevice.ReadOnly);
-    p.waitForFinished();
+    p.setWorkingDirectory(Amarok.Info.scriptPath());
+    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;
+    return textStream.readAll();
+}
+
+function getStreamByCurl(args) {
+    var p = new QProcess();
+    p.setWorkingDirectory(Amarok.Info.scriptPath());
+    p.start("curl", args, QIODevice.ReadOnly);
+    p.waitForFinished()
+    return p.readAllStandardOutput();
 }
 
-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
+function curlAuth() {
     var args = new Array();
-    args[0] = nameScript;
-    args[1] = "getUrlSong";
-    args[2] = id;
+    var clientLoginUrl = "https://www.google.com/accounts/ClientLogin";
+    var email = Config["gpUserID"];
+    var password = Config["gpPass"];
 
-    var p = new QProcess();
-    p.setWorkingDirectory(dirScript);
-    p.start("sh", args, QIODevice.ReadOnly);
-    p.waitForFinished();
-    var Response = p.readAllStandardOutput();
+    args[0] = clientLoginUrl;
+    args[1] = "--data-urlencode";
+    args[2] = "Email=" + email;
+    args[3] = "--data-urlencode";
+    args[4] = "Passwd=" + password;
+    args[5] = "-d";
+    args[6] = "accountType=GOOGLE";
+    args[7] = "-d";
+    args[8] = "service=sj";
+
+    var authResponse = doCurl(args);
+    /*
+    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 = 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) {
+    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);
     var textStream = new QTextStream(Response, QIODevice.ReadOnly);
     var tinyURL = textStream.readAll();
-    return tinyURL;
+    var listSongs = tinyURL;
+    //    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();
 }
 
+function getCookie(type) {
+    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] = "https://accounts.google.com/ServiceLogin?service=sj";
+
+    var authResponse = doCurl(args);
+    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 re = new RegExp('  value="(.*)"', "g");
+    var myArray = auth.match(re);
+    var galx = myArray[0].split('value="');
+    var galx = galx[1].split('"');
+    Amarok.debug(galx[0]);
+
+    var DSH = dsh[0];
+    var GALX = galx[0];
+    getCookie2(DSH, GALX);
+}
+
+function getCookie2(DSH, GALX) {
+    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 authResponse = doCurl(args);
+    var auth = authResponse;
+}
+
+function getSong(songId) {
+    var dirScript = ".kde4/share/apps/amarok/scripts/google_music/"; //~/Documents by default
+
+    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";
+
+    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));
-    	return JSON_obj;
-    }
-    else{
-    	return null;    	
+    if (json_file != null) {
+        eval("var JSON_obj = " + ReadTextFile(json_file));
+        return JSON_obj;
+    } else {
+        return null;
     }
 }
 
+/*
+ *		This Script allows you to listen the music allocated in google
+ *		music account using Amarok
+ *
+ * Copyright (C) 
+ *
+ *  (C)  2013 Josep Batalle Oronich <jbatalle3@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; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ *
+ * Version: 1.2
+ */
+
 Importer.loadQtBinding("qt.core");
 Importer.loadQtBinding("qt.gui");
 Importer.include("config.js");
 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();
     var google_for_amarok = "Google Music";
     if (Amarok.Playlist.trackAt(row).album == google_for_amarok) {
         //  Amarok.alert(Amarok.Playlist.trackAt(row).album);
-        var uri = getUrl(Amarok.Playlist.trackAt(row).path);
+//        var uri = getUrl(Amarok.Playlist.trackAt(row).path);
+        var uri = getSong(Amarok.Playlist.trackAt(row).path);
+
         uri = uri.replace(/[\n\r]/g, ''); //delete carrier return
         Amarok.debug(uri);
         Amarok.Playlist.addMedia(new QUrl(uri));
 X-KDE-PluginInfo-Author=Josep Batalle
 X-KDE-PluginInfo-Email=jbatalle@gmail.com
 X-KDE-PluginInfo-Name=Google Music for Amarok
-X-KDE-PluginInfo-Version=1.0
+X-KDE-PluginInfo-Version=1.2
 X-KDE-PluginInfo-Category=Scriptable Service
 X-KDE-PluginInfo-Depends=Amarok2
 X-KDE-PluginInfo-License=LGPL