aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/DownloadConfigPane.java
blob: 1e9abb2aea5811bbf44cf764a3c84d01bddb0754 (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
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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() {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(900, 500);
        initializeTable();
        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 initializeTable() {
        DefaultTableModel model = new DefaultTableModel();
        // center align the text in the table
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        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();
        initializeTable();
        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