Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
Initial release

Changes:
-improved UI (added SpotifyCheckboxs’, improved some tid-bits)
-Touched things up in general
  • Loading branch information
nyxaria committed Aug 28, 2016
1 parent 899e4aa commit 4d25bbc
Show file tree
Hide file tree
Showing 24 changed files with 272 additions and 263 deletions.
211 changes: 104 additions & 107 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified out/production/Spotify2Mp3/com/gedr/DownloadManager$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/DownloadManager$States.class
Binary file not shown.
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/DownloadManager.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Grid.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/HintTextField$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/HintTextField.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/LoadingLabel$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/LoadingLabel.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$2.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$3$1$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$3$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$3.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$4.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$5$1.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main$5.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/Main.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/SpotifyCheckBox.class
Binary file not shown.
Binary file modified out/production/Spotify2Mp3/com/gedr/SpotifyScroll.class
Binary file not shown.
67 changes: 52 additions & 15 deletions src/com/gedr/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import java.io.*;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Created by gedr on 30/07/2016.
*/
public class DownloadManager {

public enum States {
QUEUE, RUNNING, DONE, RETRYING, EXTRACTING_DONE, ERROR, DOWNLOADING, EXTRACTING;
QUEUE, RUNNING, DONE, RETRYING, EXTRACTING_DONE, ERROR, DOWNLOADING, EXTRACTING
}

Playlist[] playlists;
Expand All @@ -37,6 +40,8 @@ public enum States {

static File temp;

boolean downloading;

public DownloadManager(Playlist[] playlists) {
this.playlists = playlists;

Expand All @@ -45,10 +50,23 @@ public DownloadManager(Playlist[] playlists) {

public void startDownload() {
chooseOutput();
if(output != null)
if(output != null) {
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if(System.currentTimeMillis() - lastEvent > 45000) {
if(downloading) {
retryDownload();
p.destroy();
}
}
}
}, 10, 10, TimeUnit.SECONDS);
nextPlaylist();
else

} else
finish();

}


Expand Down Expand Up @@ -78,8 +96,8 @@ private void retryDownload() {
trackIndex--;
else
retrys = -1;
nextTrack();
retrys++;
nextTrack();
}

public void nextTrack() {
Expand Down Expand Up @@ -107,6 +125,7 @@ public void nextTrack() {
public String findYoutubeUri(Track track, int i) {
String url = "";
if(track.id.equals("next_track")) {
System.out.println("URL not found for \"" + track.name + "\"");
return "next_track";
}
if(!track.id.equals("")) {
Expand All @@ -115,7 +134,7 @@ public String findYoutubeUri(Track track, int i) {
String keyword = null;
switch(i) {
case -1:
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "");
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "");
keyword = keyword.replace(" ", "+");

String duration = "sp=" + ((track.duration / 60) > 4 ? "medium" : "short") + "&";
Expand All @@ -124,7 +143,7 @@ public String findYoutubeUri(Track track, int i) {
url = "https://www.googleapis.com/youtube/v3/search?" + duration + quality + "type=video&part=snippet&maxResults=1&q=" + keyword + "&key=AIzaSyDMUtaSnR0hadvSt4jPCCoPJeRh5LbiU5w";
break;
case 1:
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ") + " official lyrics " + (track.explicit ? " explicit" : "");
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ") + " lyrics " + (track.explicit ? " explicit" : "");
keyword = keyword.replace(" ", "+");

duration = "sp=" + ((track.duration / 60) > 4 ? "medium" : "short") + "&";
Expand All @@ -133,31 +152,31 @@ public String findYoutubeUri(Track track, int i) {
url = "https://www.googleapis.com/youtube/v3/search?" + duration + quality + "type=video&part=snippet&maxResults=1&order=viewCount&q=" + keyword + "&key=AIzaSyDMUtaSnR0hadvSt4jPCCoPJeRh5LbiU5w";
break;
case 2:
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ") + " lyrics" + (track.explicit ? " explicit" : "");
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0].replace("$", "s").replace("[", " ").replace("]", " ") + " official lyrics" + (track.explicit ? " explicit" : "");
keyword = keyword.replace(" ", "+");
duration = "sp=" + ((track.duration / 60) > 4 ? "medium" : "short") + "&";
//String duration = "";
url = "https://www.googleapis.com/youtube/v3/search?" + duration + "type=video&part=snippet&maxResults=1&order=viewCount&q=" + keyword + "&key=AIzaSyDMUtaSnR0hadvSt4jPCCoPJeRh5LbiU5w";
break;
case 3:
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0] + " lyrics";
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0] + " lyrics";
keyword = keyword.replace(" ", "+");
duration = "sp=" + ((track.duration / 60) > 4 ? "medium" : "short") + "&";

url = "https://www.googleapis.com/youtube/v3/search?" + duration + "type=video&part=snippet&maxResults=1&order=viewCount&q=" + keyword + "&key=AIzaSyDMUtaSnR0hadvSt4jPCCoPJeRh5LbiU5w";
break;
case 4:
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0];
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0];
keyword = keyword.replace(" ", "+");
duration = "&sp=" + ((track.duration / 60) > 4 ? "medium" : "short") + "&";

url = "https://www.googleapis.com/youtube/v3/search?type=video&part=snippet&maxResults=1&order=viewCount&q=" + keyword + duration+ "&key=AIzaSyDMUtaSnR0hadvSt4jPCCoPJeRh5LbiU5w";
break;
case 5:
if(track.artists.length > 1)
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[1];
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[1];
else
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "") + " " + track.artists[0];
keyword = track.name.replace("[", " ").replace("]", " ").replace("(","").replace(")","").replace(".", "").replace("\"","") + " " + track.artists[0];

keyword = keyword.replace(" ", "+");

Expand All @@ -178,6 +197,7 @@ public String findYoutubeUri(Track track, int i) {
if(i != -1) {
return findYoutubeUri(track, ++attempt);
} else {
System.out.println("URL not found for \"" + track.name + "\" { keyword=\"" + keyword + " }");
track.id = "next_track";
return "next_track";
}
Expand All @@ -187,10 +207,10 @@ public String findYoutubeUri(Track track, int i) {
JSONArray array = ob.getJSONArray("items");

if(array.isEmpty()) {
System.out.println("attempt " + (attempt) + " at getting URL...");
if(i != -1) {
return findYoutubeUri(track, ++attempt);
} else {
System.out.println("URL not found for \"" + track.name + "\" { keyword=\"" + keyword + " }");
track.id = "next_track";
return "next_track";
}
Expand All @@ -201,6 +221,7 @@ public String findYoutubeUri(Track track, int i) {
if(i != -1) {
return findYoutubeUri(track, ++attempt);
} else {
System.out.println("URL not found for \"" + track.name + "\" { keyword=\"" + keyword + " }");
track.id = "next_track";
return "next_track";
}
Expand Down Expand Up @@ -299,6 +320,7 @@ public void downloadTrack(String name, String artist, String url) {
if(duration > currentTrack.ytDuration - 10) {
complete = true;
updateUI(currentTrack, States.DONE, "", "");
System.out.println("\""+ name + "\" is already downloaded.");
} else {
file.delete();
}
Expand All @@ -323,6 +345,10 @@ public void downloadTrack(String name, String artist, String url) {
}
}

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();


Process p;

private String executeCommand(String[] commands) {

Expand All @@ -332,25 +358,31 @@ private String executeCommand(String[] commands) {
}
System.out.println();

Process p;

try {
p = Runtime.getRuntime().exec(commands);
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
errorGobbler.start();
outputGobbler.start();


p.waitFor();



// when finished

} catch(Exception e) {
e.printStackTrace();
return "error";
}
ses.shutdown();

return output.toString();

}

long lastEvent;
private class StreamGobbler extends Thread {
InputStream is;
String type;
Expand All @@ -370,10 +402,12 @@ public void run() {

while((line = br.readLine()) != null) {
System.out.println(line);
lastEvent = System.currentTimeMillis();
if(line.contains("Downloading webpage") || line.contains("Downloading video info webpage") || line.contains("Extracting video information") || line.contains("Downloading MPD manifest")) {
updateUI(currentTrack, States.EXTRACTING, level++ + "", "");
}
} else
if(line.contains("[download]")) {
downloading = true;
if(line.substring(11, 17).replace(" ", "").replaceAll("[^\\d.]", "").equals("100")) {
updateUI(currentTrack, States.DOWNLOADING, "100", "");
} else if(!line.substring(12, 17).replace(" ", "").replaceAll("[^\\d.]", "").equals("")) {
Expand All @@ -400,6 +434,7 @@ public void run() {
}
}
if(line.startsWith("size=")) {
downloading = false;
String time = line.substring(21, 29).replace(":", "");

int h = Integer.parseInt(time.substring(0, 2));
Expand All @@ -409,10 +444,12 @@ public void run() {
updateUI(currentTrack, States.RETRYING, s + "", "");
}
if(line.startsWith("video:")) {
downloading = false;
updateUI(currentTrack, States.DONE, "", "");
}

if(line.startsWith("ERROR: ")) {
downloading = false;
retrying = true;
updateUI(currentTrack, States.ERROR, "", "");
stop();
Expand Down
28 changes: 17 additions & 11 deletions src/com/gedr/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Grid extends JPanel {

//Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
public Grid(Playlist[] playlists) {
setPreferredSize(new Dimension((int) (Main.screen.width * .6) - 24, (playlists.length) * 29));
setPreferredSize(new Dimension((int) (Main.frame.getWidth()) - 24, (playlists.length) * 29));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setOpaque(true);
setBackground(new Color(28, 28, 28));
Expand Down Expand Up @@ -95,8 +95,8 @@ public Grid(Playlist[] playlists) {
}
}

public Grid(Track[] tracklist) {
setPreferredSize(new Dimension((int) (Main.screen.width * .6) - 24, (tracklist.length + 1) * (29 + 8)));
public Grid(Track[] tracklist, String name) {
setPreferredSize(new Dimension((int) Main.frame.getWidth(), (tracklist.length + 1) * (29 + 8)));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setOpaque(true);
setBackground(new Color(28, 28, 28));
Expand Down Expand Up @@ -124,29 +124,35 @@ public Grid(Track[] tracklist) {
grid.setOpaque(false);
JPanel flowWrap = new JPanel(new GridLayout());
flowWrap.setOpaque(false);
flowWrap.setPreferredSize(new Dimension((int) (Main.screen.width * .6 * .6) - 6, 40));
JLabel art = new JLabel("ARTIST");
art.setFont(Main.fontNormal.deriveFont(12f));
art.setForeground(Color.gray);
art.setFont(Main.fontNormal.deriveFont(12f));

//grid.setPreferredSize(new Dimension(100, 40));


JPanel Flow = new JPanel(new GridBagLayout());
//Flow.setPreferredSize(new Dimension(70, 40));
//Flow.add(loadingImg);
JLabel playlistLabel = new JLabel(name +" ");
//playlistLabel.setPreferredSize(new Dimension(300, 30));
playlistLabel.setFont(Main.fontBold.deriveFont(15f));
playlistLabel.setForeground(Color.lightGray);
Flow.setOpaque(false);
Flow.add(playlistLabel);
flowWrap.setPreferredSize(new Dimension((int) (Main.frame.getWidth() * .6) - playlistLabel.getPreferredSize().width - 6, 40));


grid.add(art);
flowWrap.add(art);
flowWrap.setAlignmentY(JPanel.BOTTOM_ALIGNMENT);
JPanel border = new JPanel(new BorderLayout());
border.add(flowWrap, BorderLayout.CENTER);
//border.add(Flow, BorderLayout.EAST);
border.add(flowWrap, BorderLayout.WEST);
border.add(playlistLabel, BorderLayout.EAST);
border.setOpaque(false);
wrap.add(border, BorderLayout.EAST);
title.add(wrap, BorderLayout.CENTER);
title.setMinimumSize(new Dimension(Main.frame.getWidth(), 30));
title.setMinimumSize(new Dimension(Main.frame.getWidth()-10, 30));
add(Box.createVerticalStrut(4));
add(title);
add(new Separator(false));
Expand All @@ -171,21 +177,21 @@ public Grid(Track[] tracklist) {
wrap.add(songLabel, BorderLayout.CENTER);
flowWrap = new JPanel(new FlowLayout(FlowLayout.LEFT));
flowWrap.setOpaque(false);
flowWrap.setPreferredSize(new Dimension((int) (Main.screen.width * .6 * .6), 30));
flowWrap.setPreferredSize(new Dimension((int) (Main.frame.getWidth() * .6), 30));
String artist = "";
for(String s : track.artists) {
artist += s + ", ";
}
artist = artist.substring(0, artist.length() - 2);
art = new JLabel(artist);
art.setPreferredSize(new Dimension((int) (Main.screen.width * .6 * .6), (int) art.getPreferredSize().getHeight()));
art.setPreferredSize(new Dimension((int) (Main.frame.getWidth() * .6), (int) art.getPreferredSize().getHeight()));

art.setForeground(new Color(240, 240, 240));
art.setFont(Main.fontNormal.deriveFont(13f));
flowWrap.add(art);
wrap.add(flowWrap, BorderLayout.EAST);
row.add(wrap, BorderLayout.CENTER);
row.setMinimumSize(new Dimension(Main.frame.getWidth(), 38));
row.setMinimumSize(new Dimension(Main.frame.getWidth()-10, 38));
add(row, "wrap");
if(i != tracklist.length)
add(new Separator(false));
Expand Down
Loading

0 comments on commit 4d25bbc

Please sign in to comment.