1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import com.formdev.flatlaf.FlatIntelliJLaf;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import static UI.Modal.showTextFileChooser;
public class Main extends JFrame {
private static String completedDir;
String textPath = "";
static JTextArea outputArea = new JTextArea("");
JPanel panel = new JPanel();
JScrollPane scrollPane;
JButton songsGen = new JButton("Generate text file");
JButton editButton = new JButton("Edit Tags");
JButton startButton = new JButton("Set .txt File");
JButton configureDownloadButton = new JButton("Configure Download File Interactively");
JButton setOutputDirButton = new JButton("Set MP3 Output Directory");
JCheckBox defaultFileBox = new JCheckBox("Use location of last file");
JProgressBar progressBar = new JProgressBar();
JLabel title = new JLabel("YouTube to MP3 Auto Tagging [v1.5]");
Boolean readyState = false;
Configuration config = new Configuration();
HashMap<String, String> configuration;
public Main() {
initializeComponents();
initializeActionsListeners();
config.createConfigurationFile();
configuration = config.readConfigurationData();
if(configuration.containsKey("outputPath") && !configuration.get("outputPath").isEmpty()){
completedDir = configuration.get("outputPath");
}
else{
createDefaultCompletedDirectories();
completedDir = System.getProperty("user.dir") + "/completed";
}
outputArea.setText(outputArea.getText() + "\nOutput Directory set as: " + completedDir);
}
public static void main(String[] args) {
// Launch GUI when no args provided
if(args.length == 0) {
FlatIntelliJLaf.setup();
new Main().setVisible(true);
}
//TODO: Pass to Command Handler to run job otherwise...
}
/**
* Calculate the percentage for progress bar
* @param current The current number of songs downloaded
* @param total The total number of songs to download
* @return The percentage of songs downloaded
*/
private int calculatePercentage(int current, int total) {
return (int) (((double) current / (double) total) * 100);
}
public void downloadAndTag(){
ArrayList<String> songs = FileUtility.txtToList(textPath);
int totalSongs = songs.size();
int songsProcessed = 0;
for(String line: songs){
System.out.println(line);
if(line.contains(",")){
String[] parts = line.split(",");
String url = parts[0];
String stamp = parts[1];
Downloader downloader = new Downloader(completedDir, outputArea);
try{
if(!downloader.download(url, stamp)){
UI.Modal.showError("Error downloading song: " + url + " at timestamp: " + stamp);
}
}
catch (Exception e){
UI.Modal.showError("We were unable to download a song, please check to logs " + e);
return;
}
}
else{
Downloader downloader = new Downloader(completedDir, outputArea);
try{
if(!downloader.download(line)){
UI.Modal.showError("Error downloading song: " + line);
}
}
catch (Exception e){
UI.Modal.showError("We were unable to download a song, please check to logs " + e);
return;
}
}
songsProcessed++;
progressBar.setValue(calculatePercentage(songsProcessed, totalSongs));
}
}
/**
* Initialize all GUI components
*/
private void initializeComponents() {//Initiate GUI components
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setIconImage(new ImageIcon("icon.png").getImage());
this.add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
scrollPane = new JScrollPane(outputArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
outputArea.setEditable(true);
outputArea.setLineWrap(true);
DefaultCaret caret = (DefaultCaret) outputArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.setBorder(BorderFactory.createEmptyBorder(25, 10, 20, 10));
startButton.setAlignmentX(CENTER_ALIGNMENT);
startButton.setSize(new Dimension(300, 20));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
defaultFileBox.setAlignmentX(Component.CENTER_ALIGNMENT);
setOutputDirButton.setAlignmentX(Component.CENTER_ALIGNMENT);
editButton.setAlignmentX(Component.CENTER_ALIGNMENT);
songsGen.setAlignmentX(Component.CENTER_ALIGNMENT);
progressBar.setStringPainted(true);
progressBar.setFont(new Font("Verdana", Font.PLAIN, 12));
title.setFont(new Font("Verdana", Font.BOLD, 16));
panel.add(title);
panel.add(Box.createVerticalStrut(10));
panel.add(progressBar);
panel.add(Box.createVerticalStrut(10));
panel.add(startButton);
panel.add(defaultFileBox);
panel.add(setOutputDirButton);
panel.add(Box.createVerticalStrut(8));
panel.add(scrollPane);
panel.add(Box.createVerticalStrut(5));
panel.add(editButton);
panel.add(Box.createVerticalStrut(5));
outputArea.setEditable(false);
configureDownloadButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(configureDownloadButton);
this.setSize(550, 450);
this.setTitle("YTMP3Tagger");
}
/**
* Initialize all action listeners for buttons
*/
private void initializeActionsListeners() { //Add all actionlisteners for buttons
defaultFileBox.addActionListener(e -> useLastInputTextFileLocation());
startButton.addActionListener(e -> startDownloadTagJobs());
editButton.addActionListener(e -> new TagEditorScreen().setVisible(true));
configureDownloadButton.addActionListener(e -> new DownloadConfigPane().setVisible(true));
setOutputDirButton.addActionListener(e -> chooseOutputDirectory());
}
private void writeFileContentsToOutputArea(String path){
File file = new File(path);
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
outputArea.setText(outputArea.getText() + "\n" + line);
}
} catch (FileNotFoundException e) {
UI.Modal.showError("Unable to display contents of input file? Did you delete it?");
}
}
/**
* Create the directories for completed files
*/
public void createDefaultCompletedDirectories(){
Path completedDirPath = Paths.get( System.getProperty("user.dir") + "/completed");
try {
Files.createDirectories(completedDirPath);
} catch (IOException e) {
UI.Modal.showError("Unable to create directories for completed files");
}
}
/**
* Set the text file input path to the location that was used last time
*/
private void useLastInputTextFileLocation(){
if (defaultFileBox.isSelected()) {
File file = new File(configuration.get("lastFile"));
if (!file.exists()) {
defaultFileBox.setSelected(false);
UI.Modal.showError("Unable to find the location of your previous input file " +
"(Is this your first time using the app?)" +
"\nPlease select a new file using the \"Set .txt File\" button");
return;
}
textPath = configuration.get("lastFile");
readyState = true;
startButton.setText("Start Download");
outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
writeFileContentsToOutputArea(configuration.get("lastFile"));
System.out.println("Ready to begin downloading. Press the button");
} else {
readyState = false;
startButton.setText("Set .txt File");
textPath = "";
}
}
/**
* Deletes any possible remaining files from previous jobs
*/
private void cleanRemainingFiles(){
FileUtility.deleteALlFileOfType(System.getProperty("user.dir"), "webm");
FileUtility.deleteALlFileOfType(System.getProperty("user.dir"), "json");
FileUtility.deleteALlFileOfType(System.getProperty("user.dir"), "mp3");
}
/**
* Starts the download and tagging process
*/
private void startDownloadTagJobs(){
cleanRemainingFiles();
if (!readyState) {
outputArea.setText(outputArea.getText() + "\n" + "txt path has not been set. Launching chooserPane");
System.out.println(".txt path has not been set. Launching chooserPane");
String path = showTextFileChooser();
textPath = path;
if(path == null){
UI.Modal.showWarning("No text file was selected. Aborting operation");
return;
}
config.modifyConfigurationValue("lastFile", path);
configuration = config.readConfigurationData();
try {
if (!textPath.isEmpty()) {
UI.Modal.showWarning("File has been set.\nMake sure you add a new line for each URL");
readyState = true;
writeFileContentsToOutputArea(textPath);
startButton.setText("Start Download");
outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
System.out.println("Ready to begin downloading. Press the button");
}
} catch (Exception ex) {
}
} else {
outputArea.setText(outputArea.getText() + "\n\n" + "Files will be saved to: " + completedDir);
Runnable runnable = () -> {
outputArea.setText("");
startButton.setEnabled(false);
downloadAndTag();
startButton.setEnabled(true);
};
Thread thread = new Thread(runnable);
thread.start();
}
}
public void chooseOutputDirectory(){
completedDir = UI.Modal.showDirectoryChooser(configuration.get("outputPath"));
if (completedDir == null) {
outputArea.setText(outputArea.getText() + "\n" + "No directory was selected. No changes were made.");
}
else{
config.modifyConfigurationValue("outputPath", completedDir);
configuration = config.readConfigurationData();
outputArea.setText(outputArea.getText() + "\n" + "Output directory set as: " + completedDir);
}
}
}
|