Added base functionality, working prototype
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user