com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2011.
 *
 * This file is part of TVMan.
 *
 * TVMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TVMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.zergiu.tvman.shows.tvrage;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.xml.transform.stream.StreamSource;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.oxm.Unmarshaller;

import com.zergiu.tvman.ApplicationContextProvider;
import com.zergiu.tvman.entities.TVShow;
import com.zergiu.tvman.entities.TVShowEpisode;
import com.zergiu.tvman.entities.TVShowSeason;
import com.zergiu.tvman.entities.TVShowStatus;
import com.zergiu.tvman.shows.search.AddShowFlowService;
import com.zergiu.tvman.shows.tvrage.entities.Genres;
import com.zergiu.tvman.shows.tvrage.entities.Network;
import com.zergiu.tvman.shows.tvrage.entities.TVRageEpisode;
import com.zergiu.tvman.shows.tvrage.entities.TVRageSeason;
import com.zergiu.tvman.shows.tvrage.entities.TVRageShow;

/**
 * @author sergiu
 *
 */
public class TVRageShowLoaderJob implements Job {
    private static Logger log = LoggerFactory.getLogger(TVRageShowLoaderJob.class);

    /* (non-Javadoc)
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.debug("Starting the InitialTVLoader Job");
        log.debug("Job : " + context.getJobDetail().getJobBuilder().toString());
        TVShow show = getShow(context);
        Unmarshaller unmarshaler = getUnmarshaller();

        //http://services.tvrage.com/feeds/full_show_info.php?sid=<series id>
        try (InputStream in = getShowDataStream(
                "http://services.tvrage.com/feeds/full_show_info.php?sid=" + show.getProviderSeriesId())) {
            TVRageShow tvRageShow = (TVRageShow) unmarshaler.unmarshal(new StreamSource(in));
            merge(show, tvRageShow);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ex.printStackTrace();
        }

        log.debug("got show:" + show);
    }

    /**
     * @param show
     * @param tvRageShow
     */
    private void merge(TVShow show, TVRageShow tvRageShow) {
        show.setAirsDayOfWeek(tvRageShow.getAirday());
        show.setAirsTime(tvRageShow.getAirtime());
        show.setFirstAirDate(tvRageShow.getStarted());
        show.setGenre(getFirstElement(tvRageShow.getGenres()));
        show.setLastUpdateDate(new Date());
        show.setNetwork(getNetwork(tvRageShow.getNetworks()));
        show.setStatus(tvRageShow.getEnded() == null ? TVShowStatus.Continuing : TVShowStatus.Ended);
        show.setTimezone(tvRageShow.getTimezone());
        for (TVRageSeason season : tvRageShow.getEpisodeList().getSeasons()) {
            show.addSeason(getSeason(season));
        }
    }

    /**
     * @param season
     * @return
     */
    private TVShowSeason getSeason(TVRageSeason tvRageSeason) {
        TVShowSeason season = new TVShowSeason();
        season.setName(String.valueOf(tvRageSeason.getNo()));
        season.setNumber(tvRageSeason.getNo());
        for (TVRageEpisode episode : tvRageSeason.getEpisodes()) {
            season.addEpisode(getEpisode(episode));
        }
        return season;
    }

    /**
     * @param episode
     * @return
     */
    private TVShowEpisode getEpisode(TVRageEpisode tvRageEpisode) {
        TVShowEpisode episode = new TVShowEpisode();
        episode.setAirDate(tvRageEpisode.getAirdate());
        episode.setBanner(tvRageEpisode.getLink());
        episode.setEpisode(tvRageEpisode.getEpnum());
        episode.setHasNfo(false);
        episode.setHasTbn(false);
        episode.setLastUpdated(new Date());
        episode.setName(tvRageEpisode.getTitle());
        return episode;
    }

    /**
     * @param networks
     * @return
     */
    private String getNetwork(List<Network> networks) {
        if (networks == null || networks.size() <= 0) {
            return null;
        }
        String network = getNetwork(networks, Locale.getDefault().getCountry());
        if (network == null) {
            //we didnt find one in the user's country, let's try US
            network = getNetwork(networks, "US");
        }
        return network;
    }

    protected String getNetwork(List<Network> networks, String country) {
        for (Network network : networks) {
            if (country.equalsIgnoreCase(network.getCountry())) {
                return network.getNetwork();
            }
        }
        return null;
    }

    /**
     * @param genres
     * @return
     */
    private String getFirstElement(Genres genres) {
        if (genres == null || genres.getGenre() == null) {
            return null;
        }
        if (genres.getGenre().size() > 0) {
            return genres.getGenre().get(0);
        }
        return null;
    }

    /**
     * @return
     */
    protected Unmarshaller getUnmarshaller() {
        return ApplicationContextProvider.getApplicationContext().getBean("tvrageUnmarshaller", Unmarshaller.class);
    }

    protected InputStream getShowDataStream(String urlString) throws IOException {
        URL url = new URL(urlString);
        return url.openStream();
    }

    protected TVShow getShow(JobExecutionContext context) {
        return (TVShow) context.getMergedJobDataMap().get(AddShowFlowService.SHOW_KEY);
    }

}