/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package neembuu.uploader.uploaders;
import shashaank.smallmodule.SmallModule;
import neembuu.uploader.interfaces.Uploader;
import neembuu.uploader.interfaces.Account;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import neembuu.uploader.accounts.YaVideoAccount;
import neembuu.uploader.exceptions.NUException;
import neembuu.uploader.exceptions.uploaders.NUFileExtensionException;
import neembuu.uploader.exceptions.uploaders.NUMaxFileSizeException;
import neembuu.uploader.httpclient.NUHttpClient;
import neembuu.uploader.httpclient.httprequest.NUHttpPost;
import neembuu.uploader.interfaces.UploadStatus;
import neembuu.uploader.interfaces.UploaderAccountNecessary;
import neembuu.uploader.interfaces.abstractimpl.AbstractUploader;
import neembuu.uploader.uploaders.common.FileUtils;
import neembuu.uploader.utils.NUHttpClientUtils;
import neembuu.uploader.utils.NULogger;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.util.Random;
/**
*
* @author Paralytic
*/
@SmallModule(
exports={YaVideo.class,YaVideoAccount.class},
interfaces={Uploader.class,Account.class},
name="YaVideo.tv"
)
public class YaVideo extends AbstractUploader implements UploaderAccountNecessary{
YaVideoAccount yaVideoAccount = (YaVideoAccount) getAccountsProvider().getAccount("YaVideo.tv");
private final HttpClient httpclient = NUHttpClient.getHttpClient();
private HttpContext httpContext = new BasicHttpContext();
private HttpResponse httpResponse;
private NUHttpPost httpPost;
private String responseString;
private Document doc;
private String uploadURL;
private String sessionID = "";
private String srv_id = "";
private String disk_id = "";
private String uploadid_s = "";
private String upload_fn = "";
private String downloadlink = "";
private String deletelink = "";
private final ArrayList<String> allowedVideoExtensions = new ArrayList<String>();
public YaVideo() {
downURL = UploadStatus.PLEASEWAIT.getLocaleSpecificString();
delURL = UploadStatus.PLEASEWAIT.getLocaleSpecificString();
host = "YaVideo.tv";
if (yaVideoAccount.loginsuccessful) {
host = yaVideoAccount.username + " | YaVideo.tv";
}
maxFileSizeLimit = 4718592000L; // 4,500 MB (default)
}
private void initialize() throws Exception {
responseString = NUHttpClientUtils.getData("http://yavideo.tv/?op=upload", httpContext);
doc = Jsoup.parse(responseString);
uploadURL = doc.select("form[name=file]").attr("action");
sessionID = doc.select("form[name=file]").select("input[name=sess_id]").attr("value");
srv_id = doc.select("form[name=file]").select("input[name=srv_id]").attr("value");
disk_id = doc.select("form[name=file]").select("input[name=disk_id]").attr("value");
}
@Override
public void run() {
try {
if (yaVideoAccount.loginsuccessful) {
httpContext = yaVideoAccount.getHttpContext();
maxFileSizeLimit = 4718592000L; // 4,500 MB
}
else {
host = "YaVideo.tv";
uploadInvalid();
return;
}
addExtensions();
//Check extension
if(!FileUtils.checkFileExtension(allowedVideoExtensions, file)){
throw new NUFileExtensionException(file.getName(), host);
}
if (file.length() > maxFileSizeLimit) {
throw new NUMaxFileSizeException(maxFileSizeLimit, file.getName(), host);
}
uploadInitialising();
initialize();
long uploadID;
Random random = new Random();
uploadID = Math.round(random.nextFloat() * Math.pow(10,12));
uploadid_s = String.valueOf(uploadID);
uploadURL = uploadURL.replaceAll("upload_id", "X-Progress-ID");
uploadURL += uploadid_s + "&disk_id=" + disk_id;
// http://195.154.204.100/upload/02?upload_id=
// http://195.154.204.100/upload/02?X-Progress-ID=414909415262&disk_id=02
httpPost = new NUHttpPost(uploadURL);
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.addPart("utype", new StringBody("reg"));
mpEntity.addPart("sess_id", new StringBody(sessionID));
mpEntity.addPart("srv_id", new StringBody(srv_id));
mpEntity.addPart("disk_id", new StringBody(disk_id));
mpEntity.addPart("file", createMonitoredFileBody());
mpEntity.addPart("file_title", new StringBody(removeExtension(file.getName())));
mpEntity.addPart("file_descr", new StringBody("Uploaded via Neembuu Uploader!"));
mpEntity.addPart("file_public", new StringBody("1"));
mpEntity.addPart("file_adult", new StringBody("0"));
mpEntity.addPart("tos", new StringBody("1"));
mpEntity.addPart("submit_btn", new StringBody("Upload!"));
httpPost.setEntity(mpEntity);
NULogger.getLogger().log(Level.INFO, "executing request {0}", httpPost.getRequestLine());
NULogger.getLogger().info("Now uploading your file into YaVideo.tv");
uploading();
httpResponse = httpclient.execute(httpPost, httpContext);
responseString = EntityUtils.toString(httpResponse.getEntity());
doc = Jsoup.parse(responseString);
//Read the links
gettingLink();
upload_fn = doc.select("textarea[name=fn]").val();
if (upload_fn != null) {
httpPost = new NUHttpPost("http://yavideo.tv/");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("fn", upload_fn));
formparams.add(new BasicNameValuePair("op", "upload_result"));
formparams.add(new BasicNameValuePair("st", "OK"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(entity);
httpResponse = httpclient.execute(httpPost, httpContext);
responseString = EntityUtils.toString(httpResponse.getEntity());
doc = Jsoup.parse(responseString);
downloadlink = doc.select("textarea").first().val();
deletelink = UploadStatus.NA.getLocaleSpecificString();
NULogger.getLogger().log(Level.INFO, "Delete link : {0}", deletelink);
NULogger.getLogger().log(Level.INFO, "Download link : {0}", downloadlink);
downURL = downloadlink;
delURL = deletelink;
uploadFinished();
}
} catch(NUException ex){
ex.printError();
uploadInvalid();
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
uploadFailed();
}
}
public static String removeExtension(String filePath) {
final int lastPeriodPos = filePath.lastIndexOf('.');
if (lastPeriodPos <= 0) {
return filePath;
}
else {
filePath = filePath.substring(0, lastPeriodPos);
return filePath;
}
}
private void addExtensions(){
// var ext_allowed="avi|mkv|mpg|mpeg|vob|wmv|flv|mp4|mov|m2v|divx|xvid|3gp|webm|ogv|ogg";
allowedVideoExtensions.add("avi");
allowedVideoExtensions.add("mkv");
allowedVideoExtensions.add("mpg");
allowedVideoExtensions.add("mpeg");
allowedVideoExtensions.add("vob");
allowedVideoExtensions.add("wmv");
allowedVideoExtensions.add("flv");
allowedVideoExtensions.add("mp4");
allowedVideoExtensions.add("mov");
allowedVideoExtensions.add("m2v");
allowedVideoExtensions.add("divx");
allowedVideoExtensions.add("xvid");
allowedVideoExtensions.add("3gp");
allowedVideoExtensions.add("webm");
allowedVideoExtensions.add("ogv");
allowedVideoExtensions.add("ogg");
}
}