commit 63c5d0ef580b214c57f00e8e9cc6e2ea87f7eb7c Author: aj Date: Sun May 26 18:30:35 2019 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a87e09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,80 @@ + +# Directories # +/build/ +/bin/ +target/ + +# OS Files # +.DS_Store + +*.class + +# Package Files # +*.war +*.ear +*.db + +###################### +# Windows +###################### + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + +###################### +# OSX +###################### + +.DS_Store +.svn + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + + +###################### +# Eclipse +###################### + +*.pydevproject +.project +.metadata +bin/** +tmp/** +tmp/**/* +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath +/src/main/resources/rebel.xml +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + +.gradle +/build/ +/buildSrc/build +/buildSrc/subprojects/*/build +/subprojects/*/build +/subprojects/docs/src/samples/*/*/build +/subprojects/internal-android-performance-testing/build-android-libs + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..f3bde67 --- /dev/null +++ b/build.gradle @@ -0,0 +1,49 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * This generated file contains a sample Java Library project to get you started. + * For more details take a look at the Java Libraries chapter in the Gradle + * user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html + */ + +plugins { + id 'java' + id 'application' + id 'eclipse' + + id 'com.github.johnrengelman.shadow' version '5.0.0' + id 'org.openjfx.javafxplugin' version '0.0.7' +} + +application { + mainClassName = 'sarsoo.spotframework.fx.SpotFramework' +} + +javafx { + version = "12.0.1" + modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics', 'javafx.web' ] +} + +version = '0.1.0' + +sourceCompatibility = 11 +targetCompatibility = 11 + +repositories { + mavenCentral() +} + +dependencies { + // The production code uses the SLF4J logging API at compile time + compileOnly 'org.slf4j:slf4j-api:1.7.25' + + implementation 'com.mashape.unirest:unirest-java:1.4.9' + implementation 'org.json:json:20180813' + + testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2') + testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2') +} + +test { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..457aad0 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..75b8c7c --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..af6708f --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..0f8d593 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..18813d4 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,10 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/5.0/userguide/multi_project_builds.html + */ + +rootProject.name = 'spotframework' diff --git a/src/main/java/sarsoo/spotframework/fx/SpotFramework.java b/src/main/java/sarsoo/spotframework/fx/SpotFramework.java new file mode 100644 index 0000000..0e06116 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/fx/SpotFramework.java @@ -0,0 +1,26 @@ +package sarsoo.spotframework.fx; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class SpotFramework extends Application{ + + @Override + public void start(Stage stage) throws Exception { + FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/RootPane.fxml")); + + Parent root = (Parent) loader.load(); + + Scene scene = new Scene(root, 1000, 800); + + stage.setMinHeight(800); + stage.setMinWidth(960); + stage.setTitle("spotframework"); + stage.setScene(scene); + stage.show(); + } + +} diff --git a/src/main/java/sarsoo/spotframework/fx/controller/RootController.java b/src/main/java/sarsoo/spotframework/fx/controller/RootController.java new file mode 100644 index 0000000..f59e1d7 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/fx/controller/RootController.java @@ -0,0 +1,11 @@ +package sarsoo.spotframework.fx.controller; + +import javafx.fxml.FXML; +import javafx.scene.control.TabPane; + +public class RootController { + + @FXML + private TabPane tabPane; + +} diff --git a/src/main/java/sarsoo/spotframework/model/Album.java b/src/main/java/sarsoo/spotframework/model/Album.java new file mode 100644 index 0000000..a758ea3 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/Album.java @@ -0,0 +1,80 @@ +package sarsoo.spotframework.model; + +import java.util.ArrayList; +import java.util.stream.Collectors; + +public class Album extends SpotifyObject { + + private AlbumType type; + private ArrayList artists; + private ArrayList genres; + + private ArrayList images; + + private String label; + + private ArrayList tracks; + + public Album(String name, String id, String uri, String url, AlbumType type, ArrayList artists) { + this.name = name; + this.id = id; + this.uri = uri; + this.url = url; + this.type = type; + this.artists = artists; + } + + public AlbumType getType() { + return type; + } + + public void setType(AlbumType type) { + this.type = type; + } + + public ArrayList getArtists() { + return artists; + } + + public void setArtists(ArrayList artists) { + this.artists = artists; + } + + public ArrayList getGenres() { + return genres; + } + + public void setGenres(ArrayList genres) { + this.genres = genres; + } + + public ArrayList getImages() { + return images; + } + + public void setImages(ArrayList images) { + this.images = images; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public ArrayList getTracks() { + return tracks; + } + + public void setTracks(ArrayList tracks) { + this.tracks = tracks; + } + + @Override + public String toString() { + return "album: " + name + " / " + String.join(", ", artists.stream().map(Artist::getName).collect(Collectors.toList())); + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/AlbumType.java b/src/main/java/sarsoo/spotframework/model/AlbumType.java new file mode 100644 index 0000000..196a514 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/AlbumType.java @@ -0,0 +1,9 @@ +package sarsoo.spotframework.model; + +public enum AlbumType { + + ALBUM, + SINGLE, + COMPILATION + +} diff --git a/src/main/java/sarsoo/spotframework/model/Artist.java b/src/main/java/sarsoo/spotframework/model/Artist.java new file mode 100644 index 0000000..e557d5c --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/Artist.java @@ -0,0 +1,38 @@ +package sarsoo.spotframework.model; + +import java.util.ArrayList; + +public class Artist extends SpotifyObject { + + private ArrayList images; + + private ArrayList genres; + + public Artist(String name, String id, String uri, String url) { + this.name = name; + this.id = id; + this.uri = uri; + this.url = url; + } + + public ArrayList getImages() { + return images; + } + + public void setImages(ArrayList images) { + this.images = images; + } + + public ArrayList getGenres() { + return genres; + } + + public void setGenres(ArrayList genres) { + this.genres = genres; + } + + @Override + public String toString() { + return "artist: " + name; + } +} diff --git a/src/main/java/sarsoo/spotframework/model/Image.java b/src/main/java/sarsoo/spotframework/model/Image.java new file mode 100644 index 0000000..6395b50 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/Image.java @@ -0,0 +1,27 @@ +package sarsoo.spotframework.model; + +public class Image { + + private int height; + private String url; + private int width; + + public Image(String url, int height, int width) { + this.url = url; + this.height = height; + this.width = width; + } + + public int getHeight() { + return height; + } + + public String getUrl() { + return url; + } + + public int getWidth() { + return width; + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/Playlist.java b/src/main/java/sarsoo/spotframework/model/Playlist.java new file mode 100644 index 0000000..e4dce8c --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/Playlist.java @@ -0,0 +1,82 @@ +package sarsoo.spotframework.model; + +import java.util.ArrayList; + +public class Playlist extends SpotifyObject { + + private Boolean collaborative; + private Boolean isPublic; + + private String description; + + private User owner; + + private ArrayList images; + + private ArrayList tracks; + + public Playlist(String name, String id, String uri, String url, Boolean collaborative, Boolean isPublic, User owner, ArrayList images) { + this.name = name; + this.id = id; + this.uri = uri; + this.url = url; + this.collaborative = collaborative; + this.isPublic = isPublic; + this.owner = owner; + this.images = images; + } + + public Boolean getCollaborative() { + return collaborative; + } + + public void setCollaborative(Boolean collaborative) { + this.collaborative = collaborative; + } + + public Boolean getIsPublic() { + return isPublic; + } + + public void setIsPublic(Boolean isPublic) { + this.isPublic = isPublic; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public User getOwner() { + return owner; + } + + public void setOwner(User owner) { + this.owner = owner; + } + + public ArrayList getImages() { + return images; + } + + public void setImages(ArrayList images) { + this.images = images; + } + + public ArrayList getTracks() { + return tracks; + } + + public void setTracks(ArrayList tracks) { + this.tracks = tracks; + } + + @Override + public String toString() { + return "playlist: " + name + " / " + owner.getName(); + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/PlaylistTrack.java b/src/main/java/sarsoo/spotframework/model/PlaylistTrack.java new file mode 100644 index 0000000..30b89ea --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/PlaylistTrack.java @@ -0,0 +1,43 @@ +package sarsoo.spotframework.model; + +import java.time.LocalDateTime; +import java.util.ArrayList; + +public class PlaylistTrack extends Track { + + private LocalDateTime addedAt; + private User addedBy; + private Boolean isLocal; + + public PlaylistTrack(String name, String id, String uri, String url, Album album, LocalDateTime addedAt, User addedBy, Boolean isLocal,ArrayList artists) { + super(name, id, uri, url, album, artists); + this.setAddedAt(addedAt); + this.setAddedBy(addedBy); + this.setIsLocal(isLocal); + } + + public LocalDateTime getAddedAt() { + return addedAt; + } + + public void setAddedAt(LocalDateTime addedAt) { + this.addedAt = addedAt; + } + + public User getAddedBy() { + return addedBy; + } + + public void setAddedBy(User addedBy) { + this.addedBy = addedBy; + } + + public Boolean getIsLocal() { + return isLocal; + } + + public void setIsLocal(Boolean isLocal) { + this.isLocal = isLocal; + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/SpotifyObject.java b/src/main/java/sarsoo/spotframework/model/SpotifyObject.java new file mode 100644 index 0000000..b93c166 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/SpotifyObject.java @@ -0,0 +1,44 @@ +package sarsoo.spotframework.model; + +public class SpotifyObject { + + protected String uri; + protected String id; + + protected String url; + + protected String name; + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/Track.java b/src/main/java/sarsoo/spotframework/model/Track.java new file mode 100644 index 0000000..f67c690 --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/Track.java @@ -0,0 +1,38 @@ +package sarsoo.spotframework.model; + +import java.util.ArrayList; +import java.util.stream.Collectors; + +public class Track extends SpotifyObject { + + private ArrayList artists = new ArrayList<>(); + private Album album; + + public Track(String name, String id, String uri, String url, Album album, ArrayList artists) { + this.name = name; + this.id = id; + this.uri = uri; + this.url = url; + this.album = album; + this.artists = artists; + } + + public ArrayList getArtists() { + return artists; + } + public void setArtists(ArrayList artists) { + this.artists = artists; + } + public Album getAlbum() { + return album; + } + public void setAlbum(Album album) { + this.album = album; + } + + @Override + public String toString() { + return "track: " + name + " / " + album.getName() + " / " + String.join(", ", artists.stream().map(Artist::getName).collect(Collectors.toList())); + } + +} diff --git a/src/main/java/sarsoo/spotframework/model/User.java b/src/main/java/sarsoo/spotframework/model/User.java new file mode 100644 index 0000000..ff450da --- /dev/null +++ b/src/main/java/sarsoo/spotframework/model/User.java @@ -0,0 +1,19 @@ +package sarsoo.spotframework.model; + +public class User extends SpotifyObject { + + public User(String name, String id, String uri, String url) { + + this.name = name; + this.id = id; + this.uri = uri; + this.url = url; + + } + + @Override + public String toString() { + return "user: " + name; + } + +} diff --git a/src/main/java/sarsoo/spotframework/spotify/SpotNetwork.java b/src/main/java/sarsoo/spotframework/spotify/SpotNetwork.java new file mode 100644 index 0000000..9dadfbc --- /dev/null +++ b/src/main/java/sarsoo/spotframework/spotify/SpotNetwork.java @@ -0,0 +1,247 @@ +package sarsoo.spotframework.spotify; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import com.mashape.unirest.http.HttpResponse; +import com.mashape.unirest.http.JsonNode; +import com.mashape.unirest.http.Unirest; +import com.mashape.unirest.http.exceptions.UnirestException; +import com.mashape.unirest.request.HttpRequest; + +import sarsoo.spotframework.model.Album; +import sarsoo.spotframework.model.AlbumType; +import sarsoo.spotframework.model.Artist; +import sarsoo.spotframework.model.Image; +import sarsoo.spotframework.model.Playlist; +import sarsoo.spotframework.model.PlaylistTrack; +import sarsoo.spotframework.model.Track; +import sarsoo.spotframework.model.User; + +public class SpotNetwork { + + private String userName; + private String accessToken; + + public SpotNetwork(String userName, String accessToken) { + this.userName = userName; + this.accessToken = accessToken; + } + + public SpotNetwork(String accessToken) { + this.accessToken = accessToken; + } + + public ArrayList getUserPlaylists() { + List list = makePagedGetRequest("https://api.spotify.com/v1/me/playlists"); + + ArrayList playlists = new ArrayList<>(); + + list.stream().forEach(t -> playlists.add(parseSimplifiedPlaylist(t))); + + return playlists; + } + + public ArrayList getPlaylistTracks(String playlistId) { + List list = makePagedGetRequest(String.format("https://api.spotify.com/v1/playlists/%s/tracks", playlistId)); + + ArrayList tracks = new ArrayList<>(); + + list.stream().forEach(t -> tracks.add(parsePlaylistTrack(t))); + + return tracks; + } + + private ArrayList makePagedGetRequest(String url) { + + HttpRequest request; + try { + request = Unirest.get(url).header("Accept", "application/json"); + + request.header("Authorization", "Bearer " + accessToken); + + HttpResponse response = request.asJson(); + + if (response.getStatus() >= 200 && response.getStatus() < 300) { + + JSONObject obj = new JSONObject(response.getBody().toString()); + + ArrayList list = new ArrayList<>(); + + for (Object jsonObj : obj.getJSONArray("items")) { + list.add((JSONObject) jsonObj); + } + + try { + String next = obj.getString("next"); + list.addAll(makePagedGetRequest(next)); + } catch (JSONException e) { + System.out.println("end"); + } + + return list; + + } else { + JSONObject obj = new JSONObject(response.getBody().toString()); + + System.out.println(obj); + +// throw new ApiCallException(method, obj.getInt("error"), obj.getString("message")); + } + } catch (UnirestException e) { + e.printStackTrace(); + } + + return null; + + } + + private JSONObject makeGetRequest(String method, HashMap parameters, + HashMap headers) { + + HttpRequest request; + try { + request = Unirest.get("https://api.spotify.com/v1/" + method).header("Accept", "application/json"); + + request.header("Authorization", accessToken); + + if (headers != null) { + for (String key : headers.keySet()) { + request = request.header(key, headers.get(key)); + } + } + + if (parameters != null) { + for (String key : parameters.keySet()) { + request = request.queryString(key, parameters.get(key)); + } + } + + HttpResponse response = request.asJson(); + + if (response.getStatus() >= 200 && response.getStatus() < 300) { + + return new JSONObject(response.getBody().toString()); + + } else { + JSONObject obj = new JSONObject(response.getBody().toString()); + +// throw new ApiCallException(method, obj.getInt("error"), obj.getString("message")); + } + } catch (UnirestException e) { + e.printStackTrace(); + } + + return null; + + } + + private Playlist parseSimplifiedPlaylist(JSONObject object) { + + JSONObject ownerObj = object.getJSONObject("owner"); + + User owner = new User(ownerObj.getString("display_name"), ownerObj.getString("id"), ownerObj.getString("uri"), + ownerObj.getString("href")); + + JSONArray imageObj = object.getJSONArray("images"); + + ArrayList images = new ArrayList<>(); + for (Object i : imageObj) { + JSONObject image = (JSONObject) i; + + System.out.println(image.get("height")); + + int height = 0; + int width = 0; + + try { + height = image.getInt("height"); + width = image.getInt("width"); + } catch (JSONException e) { + System.out.println("null image dimensions"); + } + + images.add(new Image(image.getString("url"), height, width)); + } + + Playlist playlist = new Playlist(object.getString("name"), object.getString("id"), object.getString("uri"), + object.getString("href"), object.getBoolean("collaborative"), object.getBoolean("public"), owner, + images); + + return playlist; + } + + private Artist parseSimplifiedArtist(JSONObject object) { + return new Artist(object.getString("name"), object.getString("id"), object.getString("uri"), + object.getString("href")); + } + + private Album parseSimplifiedAlbum(JSONObject object) { + + AlbumType type = null; + + switch (object.getString("album_type")) { + case "album": + type = AlbumType.ALBUM; + break; + case "single": + type = AlbumType.SINGLE; + break; + case "compilation": + type = AlbumType.COMPILATION; + break; + } + + ArrayList artists = new ArrayList<>(); + + JSONArray artistObj = object.getJSONArray("artists"); + + for (Object i : artistObj) { + JSONObject artist = (JSONObject) i; + artists.add(parseSimplifiedArtist(artist)); + } + + Album album = new Album(object.getString("name"), object.getString("id"), object.getString("uri"), + object.getString("href"), type, artists); + + return album; + } + + private User parseUser(JSONObject object) { + return new User(object.getString("id"), object.getString("id"), object.getString("uri"), + object.getString("href")); + } + + private PlaylistTrack parsePlaylistTrack(JSONObject object) { + + JSONObject trackObj = object.getJSONObject("track"); + + Album album = parseSimplifiedAlbum(trackObj.getJSONObject("album")); + + ArrayList artists = new ArrayList<>(); + + JSONArray artistObj = trackObj.getJSONArray("artists"); + + for (Object i : artistObj) { + JSONObject artist = (JSONObject) i; + artists.add(parseSimplifiedArtist(artist)); + } + + PlaylistTrack track = new PlaylistTrack(trackObj.getString("name"), trackObj.getString("id"), + trackObj.getString("uri"), trackObj.getString("href"), album, + LocalDateTime.ofInstant(Instant.parse(object.getString("added_at")), ZoneId.systemDefault()), parseUser(object.getJSONObject("added_by")), + trackObj.getBoolean("is_local"), artists); + + return track; + } + +} diff --git a/src/main/resources/sarsoo/spotframework/fx/styles/mainPane.css b/src/main/resources/sarsoo/spotframework/fx/styles/mainPane.css new file mode 100644 index 0000000..f0bcf46 --- /dev/null +++ b/src/main/resources/sarsoo/spotframework/fx/styles/mainPane.css @@ -0,0 +1,161 @@ +.vBoxStats{ + -fx-background-color: #001a44; +} + +.gridPane{ + + -fx-background-color: #a2aec1; +} + +.vBoxStatsLabel{ + + -fx-text-fill: white; +} + +.pieChart{ + + -fx-start-angle: 90; +} + +.chart-vertical-grid-lines { + -fx-stroke: #404040; +} + +.chart-horizontal-grid-lines { + -fx-stroke: #404040; +} + +.chart-bar { + -fx-background-color: #202124; +} + +.chart-plot-background { + -fx-background-color: transparent; +} + +.chart-pie-label { + -fx-fill: white; +} + +.chart-legend { + -fx-background-color: transparent; +} + +.chart-title { + -fx-text-fill: white; +} + +.button { + -fx-background-color: #4D4D4D; + -fx-text-fill: white; +} + +.text-field { + -fx-background-color: #4D4D4D; + -fx-text-fill: white; +} + +.menu-button { + -fx-background-color: #4D4D4D; + -fx-text-fill: white; +} + +.choice-box { + -fx-background-color: #4D4D4D; + -fx-text-fill: white; +} + +.backGround { + -fx-background-color: #202124; +} + +.titled-pane { + -fx-background-color: #202124; +} + +.highlight { + -fx-background-color: #131313; +} + +.split-pane { + -fx-background-color: #202124; +} + +.split-pane:horizontal > .split-pane-divider { + -fx-background-color: #4D4D4D; +} + +.split-pane:vertical > .split-pane-divider { + -fx-background-color: #4D4D4D; +} + +.text-area { + -fx-background-color: #202124; +} + +.menu-bar { + -fx-background-color: #202124; +} + +.menu { + -fx-background-color: #202124; +} + +.menu-item { + -fx-background-color: #202124; +} + +.context-menu { + -fx-background-color: #202124; +} + +.menu-item:focused { + -fx-background-color: #4D4D4D; +} + +.check-box { + -fx-text-fill: white; +} + +.label { + -fx-text-fill: white; +} + +.tab-pane .tab-header-area .tab-header-background { + -fx-opacity: 0; +} + +.tab-pane +{ + -fx-tab-min-width:70px; +} + +.tool-bar { + -fx-background-color: #131313; +} + +.tab{ + -fx-background-insets: 0 1 0 1,0,0; +} +.tab-pane .tab +{ + -fx-background-color: #202124; + +} + +.tab-pane .tab:selected +{ + -fx-background-color: #3c3c3c; +} + +.tab .tab-label { + -fx-alignment: CENTER; + -fx-text-fill: #828282; + //-fx-font-size: 12px; + -fx-font-weight: bold; +} + +.tab:selected .tab-label { + -fx-alignment: CENTER; + -fx-text-fill: white; +} \ No newline at end of file diff --git a/src/main/resources/sarsoo/spotframework/fx/ui/RootPane.fxml b/src/main/resources/sarsoo/spotframework/fx/ui/RootPane.fxml new file mode 100644 index 0000000..0705714 --- /dev/null +++ b/src/main/resources/sarsoo/spotframework/fx/ui/RootPane.fxml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
+
diff --git a/src/test/java/sarsoo/spotframework/spotify/SpotNetworkTest.java b/src/test/java/sarsoo/spotframework/spotify/SpotNetworkTest.java new file mode 100644 index 0000000..ba4cb26 --- /dev/null +++ b/src/test/java/sarsoo/spotframework/spotify/SpotNetworkTest.java @@ -0,0 +1,34 @@ +package sarsoo.spotframework.spotify; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.Scanner; + +import org.junit.jupiter.api.Test; + +import sarsoo.spotframework.model.Playlist; +import sarsoo.spotframework.model.PlaylistTrack; + +class SpotNetworkTest { + + @Test + void test() { + + Scanner scan = new Scanner(System.in); + + SpotNetwork net = new SpotNetwork(scan.nextLine()); + ArrayList playlists = net.getUserPlaylists(); + + ArrayList tracks = net.getPlaylistTracks(playlists.get(0).getId()); + + tracks.stream().forEach(t -> System.out.println(t.getAddedAt())); + + System.out.println(tracks.size()); +// .stream().forEach(System.out::println); + + scan.close(); + assertTrue(true); + } + +}