aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/DownloadConfigPane.java
blob: 0a9062cf5b7554ac57d52db3fcc4e7498e17e995 (plain) (blame)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;

public class DownloadConfigPane extends JFrame{
    private JPanel mainPanel;
    private JTable outputTable;
    private JTextField urlField;
    private JTextField fromField;
    private JTextField toField;
    private JButton loadFromFileButton;
    private JLabel fromLabel;
    private JLabel toLabel;
    private JLabel urlLabel;
    private JButton addButton;
    private JButton saveButton;
    private JButton removeButton;
    private JScrollPane tableScrollPane;
    private JCheckBox fullVideoCheckBox;

    private String loadedPath;

    public DownloadConfigPane() {
        // Initialize all components
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        urlField = new JTextField();
        fromField = new JTextField("HH:MM:SS");
        toField = new JTextField("HH:MM:SS");
        loadFromFileButton = new JButton("Load From File");
        fromLabel = new JLabel("From:");
        toLabel = new JLabel("To:");
        urlLabel = new JLabel("URL");
        addButton = new JButton("Add");
        saveButton = new JButton("Save");
        removeButton = new JButton("Remove");
        fullVideoCheckBox = new JCheckBox("Full Video");
        
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(900, 500);
        initializeTable();
        setupLayout();
        this.add(mainPanel);
        this.setVisible(true);
        loadFromFileButton.addActionListener(e -> loadConfigFromFile());
        addButton.addActionListener(e -> {
            String url = urlField.getText();
            if(url.contains("youtu.be")){
                String[] split = url.split("/");
                url = "https://www.youtube.com/watch?v=" + split[split.length - 1];
            }
            else if(url.contains("playlist") || url.contains("import")) {
                String playlistUrls =
                        UI.Modal.textAreaDialog("Looks like you're trying to add a playlist or import by plaintext urls\n" +
                                        "please use a tool https://cable.ayra.ch/ytdl/playlist.php to get the individual video URLs\n" +
                                        "Delete all the text here and paste them here!",
                                "YouTube Playlist Import");
                try {
                    assert playlistUrls != null;
                    String[] urls = playlistUrls.split("\n");
                    for (String playlistUrl : urls) {
                        addURLToTable(playlistUrl, "00:00:00", "00:00:00");
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Invalid playlist URLs. Make sure" +
                            "you only have 1 URL on each line in the text area and that they are valid YouTube video URLs");
                }
            }
            String from = fromField.getText();
            String to = toField.getText();
            addURLToTable(url, from, to);

        });
        removeButton.addActionListener(e -> {
            int row = outputTable.getSelectedRow();
            if (row == -1){
                return;
            }
            DefaultTableModel model = (DefaultTableModel) outputTable.getModel();
            model.removeRow(row);
        });

        saveButton.addActionListener(e -> {
            try {
                saveConfigToFile();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
        fullVideoCheckBox.addActionListener(e -> {
            if (fullVideoCheckBox.isSelected()){
                fromField.setEnabled(false);
                fromField.setText("BEGINNING_OF_VIDEO");
                toField.setEnabled(false);
                toField.setText("END_OF_VIDEO");
            }
            else{
                fromField.setEnabled(true);
                toField.setEnabled(true);
            }
        });
    }

    private void setupLayout() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        
        // First row - URL
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(urlLabel, gbc);
        
        gbc.gridx = 1;
        gbc.gridwidth = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        mainPanel.add(urlField, gbc);
        
        // Second row - From/To fields
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(fromLabel, gbc);
        
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0.3;
        mainPanel.add(fromField, gbc);
        
        gbc.gridx = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        mainPanel.add(fullVideoCheckBox, gbc);
        
        gbc.gridx = 3;
        mainPanel.add(toLabel, gbc);
        
        gbc.gridx = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0.3;
        mainPanel.add(toField, gbc);
        
        // Third row - Add button
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 5;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        mainPanel.add(addButton, gbc);
        
        // Fourth row - Remove button
        gbc.gridy = 3;
        mainPanel.add(removeButton, gbc);
        
        // Fifth row - Table
        gbc.gridy = 4;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        tableScrollPane = new JScrollPane(outputTable);
        mainPanel.add(tableScrollPane, gbc);
        
        // Sixth row - Load and Save buttons
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.weighty = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(loadFromFileButton, gbc);
        
        gbc.gridx = 2;
        gbc.gridwidth = 3;
        mainPanel.add(saveButton, gbc);
    }
    
    private void initializeTable() {
        DefaultTableModel model = new DefaultTableModel();
        // center align the text in the table
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        outputTable = new JTable();
        outputTable.setDefaultRenderer(String.class, centerRenderer);
        model.addColumn("URL");
        model.addColumn("From");
        model.addColumn("To");
        outputTable.setModel(model);
        outputTable.getTableHeader().setReorderingAllowed(false);
    }

    private void clearTable(){
        DefaultTableModel model = (DefaultTableModel) outputTable.getModel();
        model.setRowCount(0);
    }

    private File selectTextFileChooser(){
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = chooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            return selectedFile;
        }
        return null;
    }

    private File selectDirectoryFileChooser(){
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = chooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            return selectedFile;
        }
        return null;
    }

    private void loadConfigFromFile(){
        File file = selectTextFileChooser();
        if (file == null){
            return;
        }
        clearTable();
        try{
            for (String line : Files.readAllLines(file.toPath())) {
                String[] split = line.split(",");
                String url = split[0];
                String[] timeRange = new String[2];
                if (split.length == 2){
                    timeRange = split[1].split("-");
                }
                else{
                    timeRange[0] = "00:00:00";
                    timeRange[1] = "00:00:00";
                }
                String from = timeRange[0];
                String to = timeRange[1];
                if (from.length() != 8){
                    from = "00:00:00";
                }
                if (to.length() != 8){
                    to = "00:00:00";
                }
                Object[] song = new Object[]{url, from, to};
                DefaultTableModel model = (DefaultTableModel) outputTable.getModel();
                model.addRow(song);
                // add headers to the table
            }
            loadedPath = file.getAbsolutePath();
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, "Please choose a file with the download config format");
            System.out.println("Invalid file selected");
            loadedPath = null;
        }

    }

    private void saveConfigToFile() throws IOException {
        if (loadedPath == null) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Specify a file to save");
            int userSelection = fileChooser.showSaveDialog(this);
            if (userSelection == JFileChooser.APPROVE_OPTION) {
                File fileToSave = fileChooser.getSelectedFile();
                loadedPath = fileToSave.getAbsolutePath();
                if (!loadedPath.endsWith(".txt")) {
                    loadedPath += ".txt";
                }
            } else {
                return;
            }
        }

        File saveFile = new File(loadedPath);
        FileWriter writer = new FileWriter(saveFile);
        for (int i = 0; i < outputTable.getRowCount(); i++) {
            String url = (String) outputTable.getValueAt(i, 0);
            String from = (String) outputTable.getValueAt(i, 1);
            String to = (String) outputTable.getValueAt(i, 2);
            String line = "";
            if (from.equals("00:00:00") && to.equals("00:00:00")) {
                line = url;
            } else {
                line = url + "," + from + "-" + to;
            }
            writer.write(line + System.lineSeparator());
            System.out.println(line);
        }
        writer.close();
        JOptionPane.showConfirmDialog(null, "Saved to " + loadedPath, "Saved", JOptionPane.DEFAULT_OPTION);
    }

    private void addURLToTable(String url, String from, String to){
        if (url.isEmpty()){
            return;
        }
        if (from.length() != 8){
            from = "00:00:00";
        }
        if (to.length() != 8){
            to = "00:00:00";
        }
        Object[] song = new Object[]{url, from, to};
        DefaultTableModel model = (DefaultTableModel) outputTable.getModel();
        model.addRow(song);
    }


}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage