HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java batch movie downloader

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
downloaderbatchjavamovie

Problem

The idea is to batch download a list of movies (torrents) off a torrent site and add them to your server.

I have a little bit of Java experience (sophomore in college), so I'm looking for things that I could improve on.

Main.java

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

public class Main {

    public static void main(String[] args) throws IOException, JSchException, InterruptedException, SftpException {
        //execution of program
        ConfigReader config = new ConfigReader();
        List  info = new ArrayList  ();
        info = config.grabUserInfo();
        Login login = new Login(info.get(0), info.get(1));
        Scanner File1 = new Scanner(new File("C:\\IPDownloader\\movies.txt"));
        String token = "";
        List  temps = new ArrayList  ();
        while (File1.hasNext()) {
            token = File1.nextLine();
            temps.add(token);
        }
        File1.close();
        RemoteConnect connect = new RemoteConnect(info.get(2), info.get(3), info.get(4));
        for (int i = 0; i <= temps.size() - 1; i++) {
            String movie = temps.get(i);
            Search search = new Search(login.grabLogin(), movie);
            TorrentStarter tor = new TorrentStarter(connect.getAuth(), search.searchMovie(), info.get(5), i);
            tor.downloadTorrent();
            if (i % 5 == 0 && i != 0) {
                System.out.println("Waiting 5 seconds");
                Thread.sleep(5000);
            }
        }
        System.exit(0);
    }
}


Login.java

```
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public class Login {
private String username;
private String password;
public Login(String username, String password) {
this.username = usern

Solution

Some things I noticed while skimming over main:

Encapsulate your configuration

Instead of accessing your configuration through magic indices, you should introduce properly named getters for the properties.

Login login = new Login(info.get(0), info.get(1));


becomes something much more manageable like:

Login login = new Login(config.getUsername(), config.getPassword());


This similarly applies for the construction of RemoteConnect.

Use of intermediary List and old File API

You're using an intermediary List (which uses memory) and the old File API (which is ... mediocre). Instead of

Scanner File1 = new Scanner(new File("C:\\IPDownloader\\movies.txt"));
    String token = "";
    List  temps = new ArrayList  ();
    while (File1.hasNext()) {
        token = File1.nextLine();
        temps.add(token);
    }
    File1.close();


you should be able to process the whole "movies.txt" in a Stream:

try (Stream movies = Files.lines(Paths.get(MOVIES_TXT))) {
    movies.flatMap(line -> Arrays.stream(line.split("[\\s]+")))
        .forEach(movie -> {
             //.. the body of your for-loop, best encapsuled in a method
         });
} catch (IOException e) {
     // handle properly :D
}


Nitpicks

  • The initial comment //execution of program is needlessness incarnate.



  • File1 should have a camelCased variable name and probably not one with a number in it :D



  • System.exit(0); is superfluous

Code Snippets

Login login = new Login(info.get(0), info.get(1));
Login login = new Login(config.getUsername(), config.getPassword());
Scanner File1 = new Scanner(new File("C:\\IPDownloader\\movies.txt"));
    String token = "";
    List < String > temps = new ArrayList < String > ();
    while (File1.hasNext()) {
        token = File1.nextLine();
        temps.add(token);
    }
    File1.close();
try (Stream<String> movies = Files.lines(Paths.get(MOVIES_TXT))) {
    movies.flatMap(line -> Arrays.stream(line.split("[\\s]+")))
        .forEach(movie -> {
             //.. the body of your for-loop, best encapsuled in a method
         });
} catch (IOException e) {
     // handle properly :D
}

Context

StackExchange Code Review Q#114010, answer score: 2

Revisions (0)

No revisions yet.