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

Java tutorial

Introduction

Here is the source code for com.deltachi.videotex.grabers.IMDBGraber.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.Genre;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author dimitrios1988
 */
public class IMDBGraber extends VideoGraber {

    @Override
    public Video getVideo(String imdbCode) throws JSONException, IOException {
        Video video = null;
        JSONReader jsonReader = new JSONReader();
        JSONObject jSONObject = null;
        jSONObject = jsonReader.readJsonFromUrl("http://www.omdbapi.com/?i=" + imdbCode);
        video = new Video();
        video.setTitle(jSONObject.get("Title").toString());
        video.setYear(Integer.parseInt(jSONObject.get("Year").toString()));
        video.setRated(jSONObject.get("Rated").toString());
        String releasedString = jSONObject.get("Released").toString();
        SimpleDateFormat releasedFormat = new SimpleDateFormat("dd MMM yyyy", Locale.US);
        Date released = null;
        try {
            released = releasedFormat.parse(releasedString);
        } catch (ParseException ex) {
            Logger.getLogger(IMDBGraber.class.getName()).log(Level.SEVERE, null, ex);
        }
        video.setReleased(released);
        video.setRuntime(jSONObject.get("Runtime").toString());
        String[] genres = jSONObject.get("Genre").toString().split(", ");
        Collection<Genre> genreCollection = new ArrayList<>();
        for (String genres1 : genres) {
            Genre genre = new Genre();
            genre.setName(genres1);
            genreCollection.add(genre);
        }
        video.setGenreCollection(genreCollection);
        video.setDirector(jSONObject.get("Director").toString());
        video.setWriter("Writer");
        video.setActors(jSONObject.get("Actors").toString());
        video.setPlot(jSONObject.get("Plot").toString());
        video.setLanguage(jSONObject.get("Language").toString());
        video.setCountry(jSONObject.get("Country").toString());
        video.setAwards(jSONObject.get("Awards").toString());
        URL posterUrl = null;
        try {
            posterUrl = new URL(jSONObject.get("Poster").toString());
        } catch (MalformedURLException ex) {
            Logger.getLogger(IMDBGraber.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] posterFile = null;
        try {
            posterFile = Downloader.getAsByteArray(posterUrl);
        } catch (IOException ex) {
            Logger.getLogger(IMDBGraber.class.getName()).log(Level.SEVERE, null, ex);
        }
        video.setPoster(posterFile);
        video.setRating(Float.parseFloat(jSONObject.get("imdbRating").toString()));
        video.setImdbCode(imdbCode);
        return video;
    }
}