com.shahnami.fetch.Controller.FetchSeries.java Source code

Java tutorial

Introduction

Here is the source code for com.shahnami.fetch.Controller.FetchSeries.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.shahnami.fetch.Controller;

import com.shahnami.fetch.Model.Episode;
import com.shahnami.fetch.Model.Show;
import com.shahnami.fetch.Model.Torrent;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author nami
 */
public class FetchSeries {
    List<Show> _shows;
    List<Episode> _episodes;

    public FetchSeries() {
        this._shows = new ArrayList<>();
        this._episodes = new ArrayList<>();
    }

    public List<Show> getShows(String query) {
        try {
            JSONArray json = JsonReader.readJsonArrayFromUrl("http://eztvapi.re/shows");
            for (int i = 1; i < json.length() + 1; i++) {
                JSONArray shows = JsonReader.readJsonArrayFromUrl(
                        "http://eztvapi.re/shows/" + i + "?keywords=" + URLEncoder.encode(query, "UTF-8"));
                if (shows.length() > 0) {
                    for (int j = 0; j < shows.length(); j++) {
                        String id = shows.getJSONObject(j).getString("_id");
                        String title = shows.getJSONObject(j).getString("title");
                        String year = shows.getJSONObject(j).getString("year");
                        if (year.equalsIgnoreCase("null")) {
                            year = "-----";
                        }
                        int rating = shows.getJSONObject(j).getJSONObject("rating").getInt("percentage");
                        String poster = shows.getJSONObject(j).getJSONObject("images").getString("poster");
                        int num_seasons = shows.getJSONObject(j).getInt("num_seasons");
                        long last_updated = shows.getJSONObject(j).getLong("last_updated");
                        Show s = new Show();
                        s.setId(id);
                        s.setTitle(title);
                        s.setYear(year);
                        s.setPercentage(rating);
                        s.setPoster(poster);
                        s.setNum_seasons(num_seasons);
                        s.setLast_updated(last_updated);
                        _shows.add(s);
                    }
                }
            }

        } catch (IOException | JSONException ex) {
            Logger.getLogger(FetchSeries.class.getName()).log(Level.SEVERE, null, ex);
        }
        return _shows;
    }

    public List<Episode> getEpisodesFromShow(String id, int num_season, Show s) {
        try {
            JSONObject json = JsonReader.readJsonFromUrl("http://eztvapi.re/show/" + id);
            JSONArray arr = json.getJSONArray("episodes");
            for (int i = 0; i < arr.length(); i++) {
                boolean dupe = false;
                int season = arr.getJSONObject(i).getInt("season");
                String title = arr.getJSONObject(i).getString("title");
                for (Episode e : _episodes) {
                    if (e.getTitle().equalsIgnoreCase(title)) {
                        dupe = true;
                    }
                }
                if (num_season == season && !dupe) {
                    int episode = arr.getJSONObject(i).getInt("episode");
                    long first_aired = arr.getJSONObject(i).getLong("first_aired");
                    List<Torrent> torrents = new ArrayList<>();
                    JSONObject torrent = arr.getJSONObject(i).getJSONObject("torrents").getJSONObject("0");
                    Torrent tor = new Torrent();
                    int peers = torrent.getInt("peers");
                    tor.setPeers(peers);
                    String url = torrent.getString("url");
                    tor.setUrl(url);
                    torrents.add(tor);
                    Episode ep = new Episode();
                    ep.setShow(s);
                    ep.setTitle(title);
                    ep.setEpisode(episode);
                    ep.setSeason(season);
                    ep.setFirst_aired(first_aired);
                    ep.setTorrents(torrents);
                    _episodes.add(ep);
                }
            }
        } catch (IOException | JSONException ex) {
            Logger.getLogger(FetchSeries.class.getName()).log(Level.SEVERE, null, ex);
        }
        Collections.sort(_episodes, new Episode());
        return _episodes;
    }
}