是什么导致我的代码循环?错误地实例化了一个类?

问题描述 投票:0回答:1

我正在开发一个程序,该程序从链接中获取iframe,然后继续下载该剧集。由于某种原因,当我调用该方法以实际下载位于另一个类中的文件时,它将导致无限循环并产生该类的更多实例,从而导致程序不断创建新的JFrame。是什么原因造成的?我不能正确实例化该类吗?

第21行是它调用另一个类的地方,从而导致循环。

第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

java jsoup
1个回答
0
投票

您的WebsiteScraper正在创建一个DownloadEpisode,该C0正在正在调用WebsiteScraper的构造函数中创建一个DownloadEpisode,依此类推。

如果您在创建类时遇到订购问题,请查看工厂模式。

© www.soinside.com 2019 - 2024. All rights reserved.