/** * Ydl.java * * Created on 13. April 2008, 21:01 * author: haui * contact: haui45@web.de * * complie with * javac Ydl.java * run with * java Ydl * */ import java.util.regex.*; import java.net.*; import java.io.*; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * * @author haui */ public class Ydl extends javax.swing.JFrame { /** Creates new form mainFrame */ public Ydl() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { fileChooser = new javax.swing.JFileChooser(); errorDialog = new javax.swing.JOptionPane(); jLabel1 = new javax.swing.JLabel(); inputLink = new javax.swing.JTextField(); getButton = new javax.swing.JButton(); outputLink = new javax.swing.JTextField(); downloadButton = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); progressBar = new javax.swing.JProgressBar(); filesizeLabel = new javax.swing.JTextField(); statusLabel = new javax.swing.JTextField(); clearButton = new javax.swing.JButton(); jLabel7 = new javax.swing.JLabel(); fileChooser.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fileChooserActionPerformed(evt); } }); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Ydl"); jLabel1.setText("Enter your Link below:"); inputLink.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inputLinkActionPerformed(evt); } }); getButton.setText("Get Link!"); getButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { getButtonActionPerformed(evt); } }); outputLink.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { outputLinkActionPerformed(evt); } }); downloadButton.setText("Download!"); downloadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { downloadButtonActionPerformed(evt); } }); jLabel2.setText("Downloadlink:"); jLabel3.setText("Status information:"); jLabel4.setText("Filesize:"); jLabel5.setText("Status:"); filesizeLabel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { filesizeLabelActionPerformed(evt); } }); clearButton.setText("Clear!"); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearButtonActionPerformed(evt); } }); jLabel7.setText("Progress:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(jLabel3) .addComponent(jLabel7) .addGroup(layout.createSequentialGroup() .addComponent(getButton) .addGap(131, 131, 131) .addComponent(downloadButton, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE) .addGap(145, 145, 145) .addComponent(clearButton)) .addComponent(inputLink, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE) .addComponent(outputLink, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE) .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4) .addComponent(jLabel5)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(filesizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearButton, getButton}); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {filesizeLabel, statusLabel}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(inputLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(outputLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3) .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(filesizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(getButton) .addComponent(clearButton) .addComponent(downloadButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(32, 32, 32)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {filesizeLabel, jLabel4}); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel5, statusLabel}); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {outputLink, progressBar}); pack(); }// //GEN-END:initComponents private void inputLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputLinkActionPerformed // TODO add your handling code here: }//GEN-LAST:event_inputLinkActionPerformed private void outputLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputLinkActionPerformed // TODO add your handling code here: }//GEN-LAST:event_outputLinkActionPerformed private void activateButton(boolean bool){ downloadButton.setEnabled(bool); clearButton.setEnabled(bool); getButton.setEnabled(bool); downloadButton.paintAll(downloadButton.getGraphics()); clearButton.paintAll(clearButton.getGraphics()); getButton.paintAll(getButton.getGraphics()); } private String getDownloadLink(String input){ String line = "http://youtube.com/get_video?video_id="; String url = input; String inputLine; String magicId = ""; String downloadLink; activateButton(false); if (checkLink(input)==false){ JOptionPane.showMessageDialog(this, "Not a valid link!"); activateButton(true); clear(); return null; } try{ URL link = new URL(url); BufferedReader in = new BufferedReader( new InputStreamReader( link.openStream())); while ((inputLine = in.readLine()) != null){ if (inputLine.matches("(.)*, \"t\": \"(.)*")){ inputLine = inputLine.replaceAll("(.)*, \"t\": \"", ""); inputLine = inputLine.replaceAll("\", \"hl\": \"(.)*", ""); magicId = inputLine; break; } } in.close(); url = url.replaceAll("(http://)?(www.)?(de.)?(youtube.com/watch[?]v=)?", ""); downloadLink = line + url + "&t=" + magicId; } catch (Exception e){ activateButton(true); JOptionPane.showMessageDialog(this, "An unknown Error occured!"); return null; } if (magicId.equals("")){ JOptionPane.showMessageDialog(this, "http request error or invalid video ID!"); activateButton(true); return null; } activateButton(true); return downloadLink; } private void getButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getButtonActionPerformed // TODO add your handling code here: String url = inputLink.getText(); String downloadLink; if ((downloadLink=getDownloadLink(url)) == null){ outputLink.setText("Error!"); return; } outputLink.setText(downloadLink); }//GEN-LAST:event_getButtonActionPerformed private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed // TODO add your handling code here: String url = null; if ((url = getDownloadLink(inputLink.getText())) == null){ return; } fileChooser.showOpenDialog(jLabel1); }//GEN-LAST:event_downloadButtonActionPerformed private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooserActionPerformed // TODO add your handling code here: String url = null; url = getDownloadLink(inputLink.getText()); File f = null; f = fileChooser.getSelectedFile(); if (f == null){ return; } URLConnection conn = null; InputStream in = null; statusLabel.setText("please wait..."); statusLabel.paintAll(statusLabel.getGraphics()); outputLink.setText(url); outputLink.paintAll(outputLink.getGraphics()); clearButton.setEnabled(false); clearButton.paintAll(clearButton.getGraphics()); activateButton(false); try { URL fileToDownload = new URL(url); FileOutputStream outStream = new FileOutputStream(f); BufferedOutputStream buffOutStream = new BufferedOutputStream(outStream); conn = fileToDownload.openConnection(); int size = conn.getContentLength() / 1024; if (size == 0){ outputLink.setText("Error"); JOptionPane.showMessageDialog(this, "http request error or invalid video ID!"); activateButton(true); return; } filesizeLabel.setText(size + " KB"); filesizeLabel.paintAll(filesizeLabel.getGraphics()); in = conn.getInputStream(); byte buffer[] = new byte[1024]; int numRead; long numWritten = 0; int i=1; long steps = size*1024 / 100; progressBar.setValue(i); progressBar.paint(progressBar.getGraphics()); while((numRead = in.read(buffer)) != -1){ buffOutStream.write(buffer, 0, numRead); numWritten += numRead; if(numWritten >= i*steps){ i++; progressBar.setValue(i); progressBar.paint(progressBar.getGraphics()); statusLabel.setText(i+"%"); statusLabel.paintAll(statusLabel.getGraphics()); } } buffOutStream.close(); in.close(); } catch (Exception e){ activateButton(true); JOptionPane.showMessageDialog(this, "http request error or invalid video ID!"); return; } statusLabel.setText("done!"); activateButton(true); }//GEN-LAST:event_fileChooserActionPerformed private void clear(){ inputLink.setText(""); outputLink.setText(""); statusLabel.setText(""); filesizeLabel.setText("0 KB"); progressBar.setValue(0); } private void filesizeLabelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filesizeLabelActionPerformed // TODO add your handling code here: }//GEN-LAST:event_filesizeLabelActionPerformed private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) { clear(); } /** * * @param s - the string to be checked * @return true if the string is a valid youtube videolink, * false else */ private boolean checkLink(String s){ Pattern p = Pattern.compile("http://((www)|(de))[.]youtube[.]com/watch[?]v=.*"); Matcher m = p.matcher(s); return m.matches(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Ydl().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton clearButton; private javax.swing.JButton downloadButton; private javax.swing.JOptionPane errorDialog; private javax.swing.JFileChooser fileChooser; private javax.swing.JTextField filesizeLabel; private javax.swing.JButton getButton; private javax.swing.JTextField inputLink; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JTextField outputLink; private javax.swing.JProgressBar progressBar; private javax.swing.JTextField statusLabel; // End of variables declaration//GEN-END:variables }