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

Java tutorial

Introduction

Here is the source code for com.shahnami.fetch.Controller.FetchMovies.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.Movie;
import com.shahnami.fetch.Model.Torrent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author nami
 */
public class FetchMovies {
    List<Movie> _movies;

    public FetchMovies() {
        this._movies = new ArrayList<>();
    }

    public List<Movie> getBollyMovies(String query) {
        try {
            Document doc;
            Elements searchDetails;
            String link;
            String title;
            String image = null;
            Elements linkAndTitles;
            Document movieDetails;
            double rating = 0;
            String magnetLink;
            String torrentFile;
            NumberFormat formatter;
            String output;
            if (query.equalsIgnoreCase("")) {
                //
            } else {
                doc = Jsoup.connect("https://1337x.to/search/" + URLEncoder.encode(query, "UTF-8") + "+hindi/1/")
                        .userAgent("Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803").get();
                searchDetails = doc.getElementsByClass("coll-1");
                for (Element e : searchDetails) {
                    linkAndTitles = e.getElementsByTag("strong");
                    for (Element e1 : linkAndTitles) {
                        link = "https://1337x.to" + e1.getElementsByTag("a").first().attr("href");
                        title = e1.getElementsByTag("a").first().html();
                        if (!link.contains("/mirror")) {
                            Movie m = new Movie();
                            m.setTitle(title.replace("<b>", "").replace("</b>", "").trim()); //.substring(0, 47)+ "..."
                            m.setLanguage("Hindi");
                            m.setUrl(link);
                            Pattern pattern = Pattern.compile(".*([\\s(]+[0-9]{4}[\\s)]+).*");
                            Matcher matcher = pattern.matcher(title);
                            while (matcher.find()) {
                                m.setYear(Integer
                                        .parseInt(matcher.group(1).replace("(", "").replace(")", "").trim()));
                            }
                            movieDetails = Jsoup.connect(link).get();
                            try {
                                image = movieDetails.getElementsByClass("moive-box").first().getElementsByTag("img")
                                        .first().attr("src");
                                rating = Float.parseFloat(
                                        movieDetails.getElementsByClass("rateing").first().getElementsByTag("i")
                                                .attr("style").split(":")[1].replace("%;", "").trim());
                            } catch (Exception ex) {
                                //
                            }
                            magnetLink = movieDetails.getElementsByClass("magnet").first().attr("href");
                            torrentFile = movieDetails.getElementsByClass("torrent").first().attr("href");
                            formatter = NumberFormat.getNumberInstance();
                            formatter.setMinimumFractionDigits(2);
                            formatter.setMaximumFractionDigits(2);
                            output = formatter.format(rating / 10);
                            rating = Double.parseDouble(output);
                            if (rating < 1) {
                                rating = 0;
                            }
                            m.setRating(rating);
                            m.setSmall_cover_image(image);
                            List<Torrent> torrents = new ArrayList<>();
                            Torrent t = new Torrent();
                            t.setUrl(magnetLink);
                            Torrent t2 = new Torrent();
                            t2.setUrl(torrentFile);
                            torrents.add(t);
                            torrents.add(t2);
                            m.setTorrents(torrents);
                            m.getTorrents().get(0).setSeeds(
                                    Integer.valueOf(movieDetails.getElementsByClass("green").first().text()));
                            m.getTorrents().get(0).setPeers(
                                    Integer.valueOf(movieDetails.getElementsByClass("red").first().text()));
                            m.setIsBollywood(true);
                            m.setSize(movieDetails.getElementsByClass("list").first().getElementsByTag("li").get(3)
                                    .text().substring(10).trim());
                            _movies.add(m);
                        }
                    }
                    //String link = linkAndTitle.getElementsByAttribute("href").first().text();
                    //System.out.println(link);
                    //String title = linkAndTitle.getElementsByTag("b").text();
                    //System.out.println(title);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(FetchMovies.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FetchMovies.class.getName()).log(Level.SEVERE, null, ex);
        }
        return _movies;
    }

    public List<Movie> getHollyMovies(String query) {
        try {

            JSONObject json = JsonReader.readJsonFromUrl(
                    "https://yts.bypassed.me/api/v2/list_movies.json?sort_by=seeds&limit=50&query_term="
                            + URLEncoder.encode(query, "UTF-8"));
            if (json.getJSONObject("data").getInt("movie_count") > 0) {
                JSONArray arr = json.getJSONObject("data").getJSONArray("movies");

                for (int i = 0; i < arr.length(); i++) {
                    int id = arr.getJSONObject(i).getInt("id");
                    String url = arr.getJSONObject(i).getString("url");
                    String imdb_code = arr.getJSONObject(i).getString("imdb_code");
                    String title = arr.getJSONObject(i).getString("title");
                    int year = arr.getJSONObject(i).getInt("year");
                    double rating = arr.getJSONObject(i).getDouble("rating");
                    int runtime = arr.getJSONObject(i).getInt("runtime");
                    String language = arr.getJSONObject(i).getString("language");
                    String summary = arr.getJSONObject(i).getString("summary");
                    String mpa_rating = arr.getJSONObject(i).getString("mpa_rating");
                    String background_image = arr.getJSONObject(i).getString("background_image");
                    String small_cover_image = arr.getJSONObject(i).getString("small_cover_image");
                    String medium_cover_image = arr.getJSONObject(i).getString("medium_cover_image");
                    String state = arr.getJSONObject(i).getString("state");
                    String date_uploaded = arr.getJSONObject(i).getString("date_uploaded");
                    List<String> genres = new ArrayList<>();
                    JSONArray genre = arr.getJSONObject(i).getJSONArray("genres");
                    for (int j = 0; j < genre.length(); j++) {
                        genres.add(genre.getString(j));
                    }

                    List<Torrent> torrents = new ArrayList<>();
                    JSONArray torrent = arr.getJSONObject(i).getJSONArray("torrents");
                    for (int k = 0; k < torrent.length(); k++) {
                        Torrent t = new Torrent();
                        t.setUrl(torrent.getJSONObject(k).getString("url"));
                        t.setHash(torrent.getJSONObject(k).getString("hash"));
                        t.setPeers(torrent.getJSONObject(k).getInt("peers"));
                        t.setSeeds(torrent.getJSONObject(k).getInt("seeds"));
                        t.setSize(torrent.getJSONObject(k).getString("size"));
                        t.setQuality(torrent.getJSONObject(k).getString("quality"));
                        t.setUploadDate(torrent.getJSONObject(k).getString("date_uploaded"));
                        torrents.add(t);
                    }
                    Movie m = new Movie();
                    m.setGenres(genres);
                    m.setBackground_image(background_image);
                    m.setDate_uploaded(date_uploaded);
                    m.setId(id);
                    m.setImdb_code(imdb_code);
                    m.setLanguage(language);
                    m.setMedium_cover_image(medium_cover_image);
                    m.setMpa_rating(mpa_rating);
                    m.setRating(rating);
                    m.setRuntime(runtime);
                    m.setSmall_cover_image(small_cover_image);
                    m.setState(state);
                    m.setTitle(title);
                    m.setTorrents(torrents);
                    m.setUrl(url);
                    m.setYear(year);
                    m.setDescription(summary);
                    m.setIsBollywood(false);
                    m.setSize(m.getTorrents().get(0).getSize());
                    this._movies.add(m);
                }
            }
        } catch (JSONException | IOException ex) {
            //Logger.getLogger(FetchMovies.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this._movies;
    }

}