Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
* Fixed JSon splitting ...
Browse files Browse the repository at this point in the history
 * Added automated tests
  • Loading branch information
jkiddo committed Mar 31, 2014
1 parent 01a3e3e commit e80e991
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 50 deletions.
1 change: 1 addition & 0 deletions desk.gmusic.api/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
7 changes: 7 additions & 0 deletions desk.gmusic.api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<version>2.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
80 changes: 33 additions & 47 deletions desk.gmusic.api/src/main/java/gmusic/api/impl/GoogleMusicAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,37 +142,37 @@ private final String getPlaylistAssist(String jsonString) throws IOException, UR
FormBuilder builder = new FormBuilder();
builder.addFields(fields);
builder.close();
System.out.println(">>"+client.dispatchPost(new URI(HTTPS_PLAY_GOOGLE_COM_MUSIC_SERVICES_LOADPLAYLIST), builder));
return client.dispatchPost(new URI(HTTPS_PLAY_GOOGLE_COM_MUSIC_SERVICES_LOADPLAYLIST), builder);
}

private final Collection<Song> getSongs(String continuationToken) throws IOException, URISyntaxException
{
Collection<Song> chunkedCollection = new ArrayList<Song>();

Map<String, String> fields = new HashMap<String, String>();
fields.put("json", "{\"continuationToken\":\"" + continuationToken + "\"}");
// Map<String, String> fields = new HashMap<String, String>();
// fields.put("json", "{\"continuationToken\":\"" + continuationToken + "\"}");

FormBuilder form = new FormBuilder();
form.addFields(fields);
// form.addFields(fields);
form.close();

String response = client.dispatchPost(new URI(HTTPS_PLAY_GOOGLE_COM_MUSIC_SERVICES_LOADALLTRACKS), form);
int start = response.indexOf("([[") + 4;
int end = response.indexOf("window.parent['slat_progress'](1.0);");
response = response.substring(start, end);
String[] responses = response.split("\\]\\r?\\n,\\[");

for(String r : responses)
{
String[] values = splitNotInQuotes(r);

Song s = new Song();
s.setId(values[0]);
s.setTitle(values[1]);
s.setName(values[1]);
if(!Strings.isNullOrEmpty(values[2])) {
s.setAlbumArtUrl("https:"+values[2]);
if(!Strings.isNullOrEmpty(values[2]))
{
s.setAlbumArtUrl("https:" + values[2]);
}
s.setArtist(values[3]);
s.setAlbum(values[4]);
Expand All @@ -183,64 +183,50 @@ private final Collection<Song> getSongs(String continuationToken) throws IOExcep
s.setYear(toInt(values[18]));
s.setPlaycount(toInt(values[22]));
s.setRating(values[23]);
if(!Strings.isNullOrEmpty(values[24])) {
s.setCreationDate(Float.valueOf(values[24])/1000);
if(!Strings.isNullOrEmpty(values[24]))
{
s.setCreationDate(Float.valueOf(values[24]) / 1000);
}
if(!Strings.isNullOrEmpty(values[36])) {
s.setUrl("https:"+values[36]);
if(!Strings.isNullOrEmpty(values[36]))
{
s.setUrl("https:" + values[36]);
}

chunkedCollection.add(s);
}
}

return chunkedCollection;
}

private String[] splitNotInQuotes(String r) {
ArrayList<String> result = new ArrayList<String>();
boolean split=false;
String target="";

for (int i=0; i<r.length(); i++) {
char c = r.charAt(i);

if (c == '"') split = !split;

if (split || c != ',') target += c;
else {
result.add(strip(target));
target = "";
}
}

if (target.length()>0) result.add(target);
return result.toArray(new String[result.size()]);
}

private String strip (String str) {
return str.replace("\"", "");
private String[] splitNotInQuotes(String r)
{
return r.replace("\"", "").split(",");
}

private int toInt(String str) {

private int toInt(String str)
{
int retInt;
try {
retInt=Integer.valueOf(str);
try
{
retInt = Integer.valueOf(str);
}
catch (NumberFormatException e)
catch(NumberFormatException e)
{
retInt=0;
retInt = 0;
}
return retInt;
}

private long toLong(String str) {
private long toLong(String str)
{
long retLong;
try {
retLong=Long.valueOf(str);
try
{
retLong = Long.valueOf(str);
}
catch (NumberFormatException e)
catch(NumberFormatException e)
{
retLong=0;
retLong = 0;
}
return retLong;
}
Expand Down
17 changes: 14 additions & 3 deletions desk.gmusic.api/src/test/java/test/APIexample.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Collection;

Expand All @@ -38,8 +40,8 @@ public class APIexample
{
public static void main(String args[])
{
String password = "kfkhgfkhgfkhgfkhgfkhgfkg";
String username = "gkjhgkjghkjhg@gmail.com";
String password = "";
String username = "";
System.out.println(Calendar.getInstance().getTime());
IGoogleMusicAPI api = new GoogleMusicAPI();
// IGoogleMusicAPI api = new GoogleSkyJamAPI();
Expand All @@ -61,8 +63,11 @@ public static void main(String args[])
api.login(username, password);
// QueryResponse response = api.search("Jane");
// api.downloadSongs(response.getResults().getSongs());
Song _song = api.getAllSongs().iterator().next();

Song _song = new Song();
_song.setId("bc94ef2b-7d16-3815-b1cb-63fbef75b87c");
api.downloadSong(_song);

Playlists playlists = api.getAllPlaylists();
if(playlists.getMagicPlaylists() != null)
{
Expand Down Expand Up @@ -113,6 +118,12 @@ public static void main(String args[])
System.out.println(Calendar.getInstance().getTime());
}

private static void toDisk(Song song) throws MalformedURLException, IOException, URISyntaxException
{
File file = new File(new File(".") + System.getProperty("path.separator") + song.getId() + ".dl");
Files.write(Resources.toByteArray(song.getAlbumArtUrlAsURI().toURL()), file);
}

private static void populateFileWithTuneTags(File file, Song song) throws IOException
{
try
Expand Down
62 changes: 62 additions & 0 deletions desk.gmusic.api/src/test/java/test/TestOfInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package test;

import gmusic.api.impl.GoogleMusicAPI;
import gmusic.api.impl.InvalidCredentialsException;
import gmusic.api.interfaces.IGoogleMusicAPI;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.naming.directory.InvalidAttributesException;

import org.junit.Before;
import org.junit.Test;

public class TestOfInterface
{

private static final String username = "";
private static final String password = "";
private IGoogleMusicAPI api;

@Before
public void before()
{
api = new GoogleMusicAPI();
}

@Test
public void testLogin() throws IOException, URISyntaxException, InvalidCredentialsException
{
api.login(username, password);
api.getAllSongs();
}

@Test
public void testGetAllSongs() throws IOException, URISyntaxException, InvalidCredentialsException
{
api.login(username, password);
api.getAllSongs();
}

@Test
public void testGetAllPlaylists() throws IOException, URISyntaxException, InvalidCredentialsException
{
api.login(username, password);
api.getAllPlaylists();
}

@Test
public void testGetSongUrl() throws IOException, URISyntaxException, InvalidCredentialsException
{
api.login(username, password);
api.getSongURL(api.getAllSongs().iterator().next());
}

@Test
public void testDownloadSong() throws IOException, URISyntaxException, InvalidCredentialsException, InvalidAttributesException
{
api.login(username, password);
api.downloadSong(api.getAllSongs().iterator().next());
}
}

0 comments on commit e80e991

Please sign in to comment.