com.deltachi.videotex.grabers.Updater.java Source code

Java tutorial

Introduction

Here is the source code for com.deltachi.videotex.grabers.Updater.java

Source

/*
 * 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.deltachi.videotex.grabers;

import com.deltachi.videotex.daos.UpdaterForMovies;
import com.deltachi.videotex.daos.UpdaterForSubtitles;
import com.deltachi.videotex.entities.Torrent;
import com.deltachi.videotex.grabers.factories.SubtitleGraberFactory;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;

/**
 *
 * @author dimitrios1988
 */
public class Updater extends Thread {

    private final TorrentGraber graber;
    private volatile boolean allowedToRun;
    private volatile int counter;
    private volatile boolean isRunning;
    private volatile boolean isUpdateComplete;
    private final String graberType;
    private int failedAttempts;

    public Updater(TorrentGraber graber) {
        this.failedAttempts = 0;
        this.graber = graber;
        this.allowedToRun = true;
        this.counter = 0;
        this.isRunning = false;
        this.graberType = graber.getGraberType();
        this.isUpdateComplete = false;
    }

    public void stopThread() {
        this.allowedToRun = false;
    }

    @Override
    public void run() {
        isRunning = true;
        while (allowedToRun) {
            //Comment the following line if you want to harvest all the available torrents
            stopThread();
            counter++;
            Object[][] o;
            try {
                o = graber.getLatestTorrentWithIMDBCode(counter);
                if (o != null) {
                    if (graberType.toLowerCase().equals("movie")) {
                        UpdaterForMovies ufm = new UpdaterForMovies((Torrent) o[0][0], (String) o[0][1]);
                        ufm.start();
                        try {
                            ufm.join(50);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        if (ufm.isCompletedSuccessfuly() == false) {
                            failedAttempts++;
                            counter--;
                        } else {
                            failedAttempts = 0;
                            /*
                            Code for subtitles
                             */
                            UpdaterForSubtitles updaterForSubtitles = new UpdaterForSubtitles((String) o[0][1],
                                    "opensubtitles");
                            updaterForSubtitles.start();
                        }
                    }
                    if (graberType.toLowerCase().equals("serie")) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                } else {
                    this.isUpdateComplete = true;
                    stopThread();
                    break;
                }
            } catch (JSONException ex) {
                Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
                failedAttempts++;
                counter--;
            } catch (IOException ex) {
                Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
                failedAttempts++;
                counter--;
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
                failedAttempts++;
                counter--;
            }
            if (failedAttempts == 5) {
                stopThread();
                System.err.println("Update failed due to continious failing attempts");
                System.err.println("Counter = " + counter);
            }
        }
        isRunning = false;
    }

    public boolean isRunning() {
        return this.isRunning;
    }

    public boolean isUpdateComplete() {
        return this.isUpdateComplete;
    }

    public TorrentGraber getGraber() {
        return this.graber;
    }

    public int getCounter() {
        return this.counter;
    }
}