- Added file already existing handling
- Switch to Executor Service to handle only 5 SwingWorkers at a time - Added chromedriver.exe task killer at window close
This commit is contained in:
		
							parent
							
								
									f8dd7be711
								
							
						
					
					
						commit
						8d00ff7f90
					
				@ -8,6 +8,7 @@ import org.apache.logging.log4j.Level;
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
import org.openqa.selenium.By;
 | 
			
		||||
import org.openqa.selenium.ElementClickInterceptedException;
 | 
			
		||||
import org.openqa.selenium.WebDriver;
 | 
			
		||||
import org.openqa.selenium.WebElement;
 | 
			
		||||
import org.openqa.selenium.chrome.ChromeDriver;
 | 
			
		||||
@ -74,7 +75,12 @@ public class CrawlerUtil {
 | 
			
		||||
        if(!driverSeasonsAndEpisodes.getCurrentUrl().equals(url)){
 | 
			
		||||
            driverSeasonsAndEpisodes.get(url);
 | 
			
		||||
        }
 | 
			
		||||
        try{
 | 
			
		||||
        driverSeasonsAndEpisodes.findElement(By.xpath("/html/body/div[4]/div[1]/div[2]/ul/li[2]/a")).click();
 | 
			
		||||
        }catch(ElementClickInterceptedException e){
 | 
			
		||||
            driverSeasonsAndEpisodes.findElement(By.id("cmpbntyestxt")).click();
 | 
			
		||||
            driverSeasonsAndEpisodes.findElement(By.xpath("/html/body/div[4]/div[1]/div[2]/ul/li[2]/a")).click();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,7 @@ public class DownloadTask extends SwingWorker<Void, Void> {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return episodeWrapper.getTitle();
 | 
			
		||||
        return episodeWrapper.getTitle()+" : " + getState();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /** The bytes size of the file to download */
 | 
			
		||||
@ -59,19 +59,36 @@ public class DownloadTask extends SwingWorker<Void, Void> {
 | 
			
		||||
            
 | 
			
		||||
            ConnectionUtil util = new ConnectionUtil();
 | 
			
		||||
            util.prepare(downloadURL);
 | 
			
		||||
            fileSize = util.getContentLength();
 | 
			
		||||
             
 | 
			
		||||
            InputStream inputStream = util.getInputStream();
 | 
			
		||||
            
 | 
			
		||||
            String fixedTitle = episodeWrapper.getTitle().replace(":", " ");
 | 
			
		||||
            
 | 
			
		||||
            File outputFile = new File(SettingsRipperPanel.DOWNLOADFOLDER, fixedTitle+".mp4");
 | 
			
		||||
            File outputShowFolder = new File(SettingsRipperPanel.DOWNLOADFOLDER, episodeWrapper.getShow());
 | 
			
		||||
            if(!outputShowFolder.exists()){
 | 
			
		||||
                outputShowFolder.mkdir();
 | 
			
		||||
            }
 | 
			
		||||
            File outputSeasonFolder = new File(outputShowFolder, episodeWrapper.getSeason());
 | 
			
		||||
            if(!outputSeasonFolder.exists()){
 | 
			
		||||
                outputSeasonFolder.mkdir();
 | 
			
		||||
            }
 | 
			
		||||
            File outputFile = new File(outputSeasonFolder, fixedTitle+".mp4");
 | 
			
		||||
            if(outputFile.exists()){
 | 
			
		||||
                if(outputFile.length() == fileSize){
 | 
			
		||||
                    done();
 | 
			
		||||
                    return null;
 | 
			
		||||
                }else{
 | 
			
		||||
                    outputFile.delete();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
 | 
			
		||||
                byte[] buffer = new byte[BUFFER_SIZE];
 | 
			
		||||
                int bytesRead;
 | 
			
		||||
                totalBytesRead = 0;
 | 
			
		||||
                percentCompleted = 0;
 | 
			
		||||
                int oldPercentCompleted = 0;
 | 
			
		||||
                fileSize = util.getContentLength();
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
                while ((bytesRead = inputStream.read(buffer)) != -1) {
 | 
			
		||||
                    outputStream.write(buffer, 0, bytesRead);
 | 
			
		||||
@ -79,17 +96,13 @@ public class DownloadTask extends SwingWorker<Void, Void> {
 | 
			
		||||
                    percentCompleted = (int) (totalBytesRead * 100 / fileSize);
 | 
			
		||||
                    firePropertyChange(Long.toString(episodeWrapper.getId()), oldPercentCompleted, percentCompleted);
 | 
			
		||||
                    oldPercentCompleted = percentCompleted;
 | 
			
		||||
                    //setProgress(percentCompleted);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
            util.disconnect();
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
            // Enable MetaDataWriter
 | 
			
		||||
            // MetaDataWriter mdp = new MetaDataWriter();
 | 
			
		||||
            // mdp.writeMetadata(outputFile.getAbsolutePath(), episodeWrapper.getTitle(), episodeWrapper.getShow(), episodeWrapper.getShow(), episodeWrapper.getEpisode());
 | 
			
		||||
            
 | 
			
		||||
        } catch (IOException ex) {
 | 
			
		||||
            cancel(true);          
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,8 @@ import java.beans.PropertyChangeListener;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.concurrent.ExecutorService;
 | 
			
		||||
import java.util.concurrent.Executors;
 | 
			
		||||
import javax.swing.BorderFactory;
 | 
			
		||||
import javax.swing.DefaultListModel;
 | 
			
		||||
import javax.swing.JLabel;
 | 
			
		||||
@ -39,6 +41,8 @@ public class DownloadRipperPanel implements PropertyChangeListener{
 | 
			
		||||
    /** The information panel for the currently selected task */
 | 
			
		||||
    private DownloadTaskInformationPanel infoPanel;
 | 
			
		||||
    
 | 
			
		||||
    private final ExecutorService threadPool = Executors.newFixedThreadPool(5);
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a download panel
 | 
			
		||||
     */
 | 
			
		||||
@ -126,7 +130,8 @@ public class DownloadRipperPanel implements PropertyChangeListener{
 | 
			
		||||
     */
 | 
			
		||||
    public void addTask(DownloadTask task){
 | 
			
		||||
        downloadTasks.add(task);
 | 
			
		||||
        task.execute();
 | 
			
		||||
        threadPool.submit(task);
 | 
			
		||||
        //task.execute();
 | 
			
		||||
        listModel.addElement(task);
 | 
			
		||||
        updateTaskCount();
 | 
			
		||||
        task.addPropertyChangeListener(this);
 | 
			
		||||
 | 
			
		||||
@ -6,8 +6,15 @@ import com.greinet.tvtotalripper.download.DownloadTask;
 | 
			
		||||
import java.awt.Dimension;
 | 
			
		||||
import java.awt.event.ActionEvent;
 | 
			
		||||
import java.awt.event.ActionListener;
 | 
			
		||||
import java.awt.event.WindowAdapter;
 | 
			
		||||
import java.awt.event.WindowEvent;
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStreamReader;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
import javax.swing.JComponent;
 | 
			
		||||
import javax.swing.JFrame;
 | 
			
		||||
@ -45,7 +52,28 @@ public class RipperWindow {
 | 
			
		||||
     */
 | 
			
		||||
    public RipperWindow(){
 | 
			
		||||
        JFrame frame = new JFrame("TV Total Ripper");
 | 
			
		||||
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 | 
			
		||||
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 | 
			
		||||
        frame.addWindowListener(new WindowAdapter() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void windowClosing(WindowEvent e) {
 | 
			
		||||
                
 | 
			
		||||
                try {
 | 
			
		||||
                    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c","taskkill /F /IM chromedriver.exe /T");
 | 
			
		||||
                    builder.redirectErrorStream(true);
 | 
			
		||||
                    Process p = builder.start();
 | 
			
		||||
                    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
 | 
			
		||||
                    String line;
 | 
			
		||||
                    while (true) {
 | 
			
		||||
                        line = r.readLine();
 | 
			
		||||
                        if (line == null) { break; }
 | 
			
		||||
                    }
 | 
			
		||||
                } catch (IOException ex) {
 | 
			
		||||
                    Logger.getLogger(RipperWindow.class.getName()).log(Level.SEVERE, null, ex);
 | 
			
		||||
                }
 | 
			
		||||
                System.exit(0);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        tabbedPane = new JTabbedPane();
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ public class SettingsRipperPanel {
 | 
			
		||||
    /** The file chooser to select the download folder */
 | 
			
		||||
    private JFileChooser fileChooserDownloadFolder;
 | 
			
		||||
    /** The download folder */
 | 
			
		||||
    public static File DOWNLOADFOLDER = new File(".");
 | 
			
		||||
    public static File DOWNLOADFOLDER = new File("D:/Users/Andreas/Videos/MySpass/");
 | 
			
		||||
    /** The JButton to open the file chooser dialog */
 | 
			
		||||
    private final JButton button;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user