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

Java tutorial

Introduction

Here is the source code for com.deltachi.videotex.grabers.YIFYGraber.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.entities.Screenshot;
import com.deltachi.videotex.entities.Torrent;
import com.deltachi.videotex.entities.Video;
import com.deltachi.videotex.utils.Downloader;
import com.deltachi.videotex.utils.JSONReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author dimitrios1988
 */
public class YIFYGraber extends TorrentGraber {

    @Override
    public Object[][] getLatestTorrentWithIMDBCode(int index)
            throws JSONException, IOException, NoSuchAlgorithmException {
        Object[][] o = null;
        o = getLatestTorrentsWithDetails(index, 1);
        return o;
    }

    private Object[][] getLatestTorrentsWithDetails(int set, int limit)
            throws JSONException, IOException, MalformedURLException, NoSuchAlgorithmException {
        Object[][] o = new Object[limit][2];
        JSONReader jsonReader = new JSONReader();
        JSONObject jSONObject = null;
        jSONObject = jsonReader.readJsonFromUrl(
                "http://ytsre.eu/api/list.json?set=" + Integer.toString(set) + "&limit=" + Integer.toString(limit));
        if (jSONObject.has("status")) {
            if (jSONObject.get("status").equals("fail")) {
                o = null;
            }
        } else {
            JSONArray jSONArray = (JSONArray) jSONObject.get("MovieList");
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jsono = (JSONObject) jSONArray.get(i);
                int torrentID = Integer.parseInt(jsono.get("MovieID").toString());
                String imdbCode = jsono.get("ImdbCode").toString();
                o[i][0] = getTorrentDetails(torrentID);
                o[i][1] = imdbCode;
            }
        }
        return o;
    }

    private Object[][] getVideosAndTorrentsFromLatestTorrentsWithDetails(int set, int limit)
            throws ParseException, IOException, JSONException, MalformedURLException, NoSuchAlgorithmException {
        Object[][] o = new Object[limit][2];
        Collection<Torrent> torrentCollection = null;
        JSONReader jsonReader = new JSONReader();
        JSONObject jSONObject = null;
        jSONObject = jsonReader.readJsonFromUrl(
                "http://ytsre.eu/api/list.json?set=" + Integer.toString(set) + "&limit=" + Integer.toString(limit));
        JSONArray jSONArray = (JSONArray) jSONObject.get("MovieList");
        for (int i = 0; i < jSONArray.length(); i++) {
            JSONObject jsono = (JSONObject) jSONArray.get(i);
            IMDBGraber imdbg = new IMDBGraber();
            String imdbCode = jsono.get("ImdbCode").toString();
            torrentCollection = getAllTorrentsFromVideo(imdbCode);
            Video video = imdbg.getVideo(imdbCode);
            o[i][0] = video;
            o[i][1] = torrentCollection;
        }
        return o;
    }

    private Torrent getTorrentDetails(int id)
            throws NoSuchAlgorithmException, MalformedURLException, JSONException, IOException {
        Torrent torrent;
        JSONReader jsonReader = new JSONReader();
        JSONObject jSONObject = null;
        jSONObject = jsonReader.readJsonFromUrl("http://ytsre.eu/api/movie.json?id=" + Integer.toString(id));
        torrent = new Torrent();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            torrent.setDateUploaded(dateFormat.parse(jSONObject.get("DateUploaded").toString()));
        } catch (ParseException ex) {
            Logger.getLogger(YIFYGraber.class.getName()).log(Level.SEVERE, null, ex);
        }
        torrent.setQuality(jSONObject.get("Quality").toString());
        torrent.setSize(jSONObject.get("Size").toString());
        torrent.setTorrentSeeds(Integer.parseInt(jSONObject.get("TorrentSeeds").toString()));
        torrent.setTorrentPeers(Integer.parseInt(jSONObject.get("TorrentPeers").toString()));
        torrent.setMagnetURL(jSONObject.get("TorrentMagnetUrl").toString());
        URL torrentUrl = null;
        torrentUrl = new URL(jSONObject.get("TorrentUrl").toString());
        byte[] torrentFile = null;
        torrentFile = Downloader.getAsByteArray(torrentUrl);
        torrent.setTorrentFile(torrentFile);
        String checksum = "";
        checksum = Downloader.SHAsum(torrentFile);
        Collection<Screenshot> screenshotCollection = new ArrayList<>();
        int k = 1;
        while (true) {
            URL screenshotURL = null;
            if (jSONObject.has("MediumScreenshot" + Integer.toString(k))) {
                screenshotURL = new URL(jSONObject.get("MediumScreenshot" + Integer.toString(k)).toString());
            } else {
                break;
            }
            byte[] screenshotFile = null;
            screenshotFile = Downloader.getAsByteArray(screenshotURL);
            Screenshot screenshot = new Screenshot();
            screenshot.setTorrentID(torrent);
            screenshot.setPicture(screenshotFile);
            screenshotCollection.add(screenshot);
            k++;
        }
        torrent.setScreenshotCollection(screenshotCollection);
        torrent.setChecksum(checksum);
        torrent.setDistributor("yify");
        return torrent;
    }

    private Collection<Torrent> getAllTorrentsFromVideo(String imdbCode)
            throws JSONException, IOException, MalformedURLException, NoSuchAlgorithmException {
        Collection<Torrent> torrentCollection = null;
        JSONReader jsonReader = new JSONReader();
        JSONObject jSONObject = null;
        jSONObject = jsonReader.readJsonFromUrl("http://ytsre.eu/api/listimdb.json?imdb_id=" + imdbCode);

        torrentCollection = new ArrayList<>();
        JSONArray jSONArray = (JSONArray) jSONObject.get("MovieList");
        for (int i = 0; i < jSONArray.length(); i++) {
            JSONObject json = (JSONObject) jSONArray.get(i);
            torrentCollection.add(getTorrentDetails(Integer.parseInt(json.get("MovieID").toString())));
        }
        return torrentCollection;
    }

    @Override
    public String getGraberType() {
        String graberType = "movie";
        return graberType;
    }

}