- Deleted unused classes
- Refined and added javadoc - Disabled MetaDataWriter
This commit is contained in:
@@ -1,81 +1,74 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.greinet.tvtotalripper.download;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author agreiner
|
||||
*/
|
||||
public class ConnectionUtil {
|
||||
|
||||
private HttpURLConnection httpConn;
|
||||
private InputStream inputStream;
|
||||
private String fileName;
|
||||
private int contentLength;
|
||||
|
||||
public void prepare(String fileUrl) throws IOException{
|
||||
URL url = new URL(fileUrl);
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
int responseCode = httpConn.getResponseCode();
|
||||
|
||||
// always check HTTP response code first
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
String disposition = httpConn.getHeaderField("Content-Disposition");
|
||||
String contentType = httpConn.getContentType();
|
||||
contentLength = httpConn.getContentLength();
|
||||
|
||||
if (disposition != null) {
|
||||
// extracts file name from header field
|
||||
int index = disposition.indexOf("filename=");
|
||||
if (index > 0) {
|
||||
fileName = disposition.substring(index + 10,
|
||||
disposition.length() - 1);
|
||||
}
|
||||
} else {
|
||||
// extracts file name from URL
|
||||
fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1,
|
||||
fileUrl.length());
|
||||
}
|
||||
|
||||
// output for debugging purpose only
|
||||
System.out.println("Content-Type = " + contentType);
|
||||
System.out.println("Content-Disposition = " + disposition);
|
||||
System.out.println("Content-Length = " + contentLength);
|
||||
System.out.println("fileName = " + fileName);
|
||||
|
||||
// opens input stream from the HTTP connection
|
||||
inputStream = httpConn.getInputStream();
|
||||
|
||||
} else {
|
||||
throw new IOException(
|
||||
"No file to download. Server replied HTTP code: "
|
||||
+ responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() throws IOException {
|
||||
inputStream.close();
|
||||
httpConn.disconnect();
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return this.fileName;
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
return this.contentLength;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return this.inputStream;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.greinet.tvtotalripper.download;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author agreiner
|
||||
*/
|
||||
public class ConnectionUtil {
|
||||
|
||||
/** The connection to the download URL */
|
||||
private HttpURLConnection httpConn;
|
||||
/** The input stream from the connection */
|
||||
private InputStream inputStream;
|
||||
/** The download size */
|
||||
private int contentLength;
|
||||
|
||||
/**
|
||||
* Prepare the download connection and open the input stream
|
||||
* @param fileUrl the URL to the file
|
||||
* @throws IOException error preparing download
|
||||
*/
|
||||
public void prepare(String fileUrl) throws IOException{
|
||||
URL url = new URL(fileUrl);
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
int responseCode = httpConn.getResponseCode();
|
||||
|
||||
// always check HTTP response code first
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
contentLength = httpConn.getContentLength();
|
||||
// opens input stream from the HTTP connection
|
||||
inputStream = httpConn.getInputStream();
|
||||
|
||||
} else {
|
||||
throw new IOException(
|
||||
"No file to download. Server replied HTTP code: "
|
||||
+ responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the inout stream and disconnect the download
|
||||
* @throws IOException
|
||||
*/
|
||||
public void disconnect() throws IOException {
|
||||
inputStream.close();
|
||||
httpConn.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content length of the download
|
||||
* @return the content length
|
||||
*/
|
||||
public int getContentLength() {
|
||||
return this.contentLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the download related inout stream
|
||||
* @return the input stream
|
||||
*/
|
||||
public InputStream getInputStream() {
|
||||
return this.inputStream;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,95 +1,130 @@
|
||||
package com.greinet.tvtotalripper.download;
|
||||
|
||||
import com.greinet.tvtotalripper.MetaDataWriter;
|
||||
import com.greinet.tvtotalripper.crawler.CrawlerUtil;
|
||||
import com.greinet.tvtotalripper.crawler.EpisodeWrapper;
|
||||
import com.greinet.tvtotalripper.ui.SettingsRipperPanel;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author agreiner
|
||||
*/
|
||||
public class DownloadTask extends SwingWorker<Void, Void> {
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
|
||||
private final EpisodeWrapper episodeWrapper;
|
||||
|
||||
|
||||
public DownloadTask(EpisodeWrapper episodeWrapper) {
|
||||
this.episodeWrapper = episodeWrapper;
|
||||
}
|
||||
|
||||
public EpisodeWrapper getEpisodeWrapper() {
|
||||
return episodeWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return episodeWrapper.getTitle();
|
||||
}
|
||||
|
||||
public long fileSize = 0;
|
||||
|
||||
public long totalBytesRead = 0;
|
||||
|
||||
public int percentCompleted = 0;
|
||||
|
||||
/**
|
||||
* Executed in background thread
|
||||
*/
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
try {
|
||||
String downloadURL = CrawlerUtil.getFetchfileURL(episodeWrapper.getUrl());
|
||||
|
||||
ConnectionUtil util = new ConnectionUtil();
|
||||
util.prepare(downloadURL);
|
||||
|
||||
InputStream inputStream = util.getInputStream();
|
||||
|
||||
String fixedTitle = episodeWrapper.getTitle().replace(":", " ");
|
||||
|
||||
File outputFile = new File(SettingsRipperPanel.DOWNLOADFOLDER, fixedTitle+".mp4");
|
||||
FileOutputStream outputStream = new FileOutputStream(outputFile);
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
totalBytesRead = 0;
|
||||
percentCompleted = 0;
|
||||
int oldPercentCompleted = 0;
|
||||
fileSize = util.getContentLength();
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
totalBytesRead += bytesRead;
|
||||
percentCompleted = (int) (totalBytesRead * 100 / fileSize);
|
||||
firePropertyChange(Long.toString(episodeWrapper.getId()), oldPercentCompleted, percentCompleted);
|
||||
oldPercentCompleted = percentCompleted;
|
||||
//setProgress(percentCompleted);
|
||||
}
|
||||
|
||||
outputStream.close();
|
||||
|
||||
util.disconnect();
|
||||
|
||||
MetaDataWriter mdp = new MetaDataWriter();
|
||||
mdp.writeMetadata(outputFile.getAbsolutePath(), episodeWrapper.getTitle(), episodeWrapper.getShow(), episodeWrapper.getShow(), episodeWrapper.getEpisode());
|
||||
|
||||
} catch (IOException ex) {
|
||||
cancel(true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed in Swing's event dispatching thread
|
||||
*/
|
||||
@Override
|
||||
protected void done() {
|
||||
firePropertyChange(Long.toString(episodeWrapper.getId()), 0, 101);
|
||||
}
|
||||
}
|
||||
package com.greinet.tvtotalripper.download;
|
||||
|
||||
import com.greinet.tvtotalripper.crawler.CrawlerUtil;
|
||||
import com.greinet.tvtotalripper.crawler.EpisodeWrapper;
|
||||
import com.greinet.tvtotalripper.ui.SettingsRipperPanel;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
/**
|
||||
* A task representing a single file download
|
||||
*/
|
||||
public class DownloadTask extends SwingWorker<Void, Void> {
|
||||
|
||||
/** The download buffer size */
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
/** The episode of the downloaded file */
|
||||
private final EpisodeWrapper episodeWrapper;
|
||||
|
||||
/**
|
||||
* Create a download task for a episode
|
||||
* @param episodeWrapper
|
||||
*/
|
||||
public DownloadTask(EpisodeWrapper episodeWrapper) {
|
||||
this.episodeWrapper = episodeWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the episode of the download
|
||||
* @return the episode
|
||||
*/
|
||||
public EpisodeWrapper getEpisodeWrapper() {
|
||||
return episodeWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return episodeWrapper.getTitle();
|
||||
}
|
||||
|
||||
/** The bytes size of the file to download */
|
||||
private long fileSize = 0;
|
||||
|
||||
/** the amount of downloaded bytes */
|
||||
private long totalBytesRead = 0;
|
||||
|
||||
/** The completion percentage */
|
||||
private int percentCompleted = 0;
|
||||
|
||||
/**
|
||||
* Executed in background thread
|
||||
*/
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
try {
|
||||
String downloadURL = CrawlerUtil.getFetchfileURL(episodeWrapper.getUrl());
|
||||
|
||||
ConnectionUtil util = new ConnectionUtil();
|
||||
util.prepare(downloadURL);
|
||||
|
||||
InputStream inputStream = util.getInputStream();
|
||||
|
||||
String fixedTitle = episodeWrapper.getTitle().replace(":", " ");
|
||||
|
||||
File outputFile = new File(SettingsRipperPanel.DOWNLOADFOLDER, fixedTitle+".mp4");
|
||||
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);
|
||||
totalBytesRead += bytesRead;
|
||||
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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the download file size in bytes
|
||||
* @return the file size in bytes
|
||||
*/
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of already downloaded bytes
|
||||
* @return the amount of already downloaded bytes
|
||||
*/
|
||||
public long getTotalBytesRead() {
|
||||
return totalBytesRead;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the completiton percentage
|
||||
* @return the completition percentage
|
||||
*/
|
||||
public int getPercentCompleted() {
|
||||
return percentCompleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a property change event with the progress value of 101 meaning that the task is done
|
||||
*/
|
||||
@Override
|
||||
protected void done() {
|
||||
firePropertyChange(Long.toString(episodeWrapper.getId()), 0, 101);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user