Я работаю над программой, которая захватывает Iframe по ссылке, а затем приступает к загрузке эпизода. По какой-то причине, когда я вызываю метод для фактической загрузки файла, который находится в другом классе, он вызывает бесконечное l oop и производит больше экземпляров класса, что заставляет программу постоянно создавать новые JFrames. Что может быть причиной этого? Разве я не создаю экземпляр класса должным образом?
В строке 21 он вызывает другой класс, вызывая l oop.
Class 1:
package com.trentmenard;
import org.jsoup.*;
import org.jsoup.nodes.*;
import java.io.IOException;
class WebsiteScraper {
private String URL;
private Document websiteConnection;
private String episodeName;
private String iFrameLink;
private String URLDownloadLink;
//FOR DEBUG PURPOSES:
private String directDownloadLink = "https://st1x.cdnfile.info/user1342/1f1b04321e51fac1c1d1c23a1de4f7f0/" +
"EP.1.mp4?token=qV-3haWjcomPzXJVAeBtdg&expires=1579170124&id=113345&title=(orginalP - mp4) Sword+Art+" +
"Online%3A+Alicization+%28Dub%29+Episode+1";
WebsiteScraper(String URL) {
this.URL = URL;
//connectToURL(URL);
new DownloadEpisode(directDownloadLink, episodeName); //LOOP STARTS HERE
}
private void connectToURL(String URL){
if(URL.startsWith("https://st1x.cdnfile.info/")){
getDirectDownloadLink();
}
else{
try {
websiteConnection = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established to: " + URL);
if(URL.equalsIgnoreCase("https://swordartonlineepisode.com/sword-art-online-season-3-episode" +
"-1-english-dubbed-watch-online/")){
episodeName = "Sword Art Online Season 3 Episode 1 English Dubbed";
getIFrameLink();
}
else if((URL).equalsIgnoreCase(iFrameLink)) {
getDownloadLink();
}
else if(URL.equalsIgnoreCase(URLDownloadLink)){
getDirectDownloadLink();
}
}
catch (IOException e){
e.printStackTrace();
System.out.println("Failed to Establish Connection to:" + URL);
}
}
}
String getNextEpisodeURL(){
String nextEpisodeURL = websiteConnection.getElementsByClass("button SAO").get(1).attr("href");
System.out.println("Found Next Episode URL: " + nextEpisodeURL);
return nextEpisodeURL;
}
String getNextEpisodeName(){
Element h2 = websiteConnection.selectFirst("h2");
String nextEpisodeName = h2.text();
System.out.println("Found Next Episode URL: " + nextEpisodeName);
//getIFrameLink();
return nextEpisodeName;
}
private void getIFrameLink(){
Element iFrame = websiteConnection.selectFirst("iframe");
iFrameLink = iFrame.attr("src");
System.out.println("Found iFrame Link: " + iFrameLink + " for: " + episodeName);
connectToURL(iFrameLink);
}
private void getDownloadLink() {
Element hiddenID = websiteConnection.getElementById("id");
String hiddenIDValue = hiddenID.attr("value");
URLDownloadLink = "https://www.vidstreaming.io/download?id=" + hiddenIDValue;
System.out.println("Found Download Link Using ID Value (" + hiddenIDValue + "): " + URLDownloadLink);
connectToURL(URLDownloadLink);
}
private void getDirectDownloadLink(){
if(URL.equalsIgnoreCase(URLDownloadLink)) {
new DownloadEpisode(URL, episodeName);
}
else if(URL.startsWith("https://st1x.cdnfile.info/")){
new DownloadEpisode(URL, episodeName);
}
else{
Element downloadClass = websiteConnection.getElementsContainingOwnText("Download (orginalP - mp4)").first();
directDownloadLink = downloadClass.attr("href");
System.out.println("Found Direct Download Link: " + directDownloadLink);
new DownloadEpisode(directDownloadLink, episodeName);
}
}
}
Класс 2:
package com.trentmenard;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
class DownloadEpisode{
private String URL;
private static DecimalFormat decimalFormat = new DecimalFormat("#.##");
public DownloadEpisode(String URL, String episodeName) {
System.out.println("[Debug:] Instance called!");
this.URL = URL;
float Percent = 0;
String downloadProgress = "0.00";
JFrame progressFrame = new JFrame();
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setSize(100, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressFrame.setTitle("Downloading: " + episodeName + " - " + Percent + "%");
progressFrame.add(progressBar);
progressFrame.setVisible(true);
progressFrame.setLayout(new FlowLayout());
progressFrame.setSize(575, 100);
progressFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel percentComplete = new JLabel(downloadProgress + "% complete.");
progressFrame.add(percentComplete);
WebsiteScraper websiteScraper = new WebsiteScraper(URL);
String nextEpisodeURl = websiteScraper.getNextEpisodeURL();
String nextEpisodeName = websiteScraper.getNextEpisodeName();
File createFile = new File(episodeName + ".mp4");
if(createFile.exists() && !createFile.isDirectory()) {
System.out.println("File Already Exists! Moving Onto Next URL.");
new DownloadEpisode(nextEpisodeURl, nextEpisodeName);
}
else{
try {
java.net.URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("Connection Successfully Established!");
System.out.println("Downloading File: " + episodeName);
int filesize = connection.getContentLength();
float totalDataRead = 0;
byte[] data = new byte[1024];
int i = 0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(episodeName + ".mp4");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
Percent = (totalDataRead * 100) / filesize;
decimalFormat.setRoundingMode(RoundingMode.CEILING);
downloadProgress = decimalFormat.format(Percent);
progressFrame.setTitle("Downloading: " + episodeName);
progressBar.setValue((int) Percent);
percentComplete.setText(downloadProgress);
}
bout.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Connection Failed!");
}
}
}
}
https://i.imgur.com/dl95D3d.png