GetSearchResults.java Source code

Java tutorial

Introduction

Here is the source code for GetSearchResults.java

Source

/**
 * NZBMatrixLib provides an intuitive java interface library to interact with the API provided by (http://www.nzbmatrix.com)
 *
 * The author(s) of NZBMatrixLib are in NO WAY associated with (http://nzbmatrix.com) or its affiliates
 *
 * If you  would like to contribute to this library, find us at (https://github.com/TheBigS/NZBMatrixLib)
 *
 *   Copyright (C) 2010  Steven King
 *
 *   This program 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.
 *
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.io.IOException;
import java.text.ParseException;

import org.apache.http.client.ClientProtocolException;

import com.github.thebigs.nzbmatrixapi.ApiCredentials;
import com.github.thebigs.nzbmatrixapi.NZBMatrixApi;
import com.github.thebigs.nzbmatrixapi.NZBMatrixApiException;
import com.github.thebigs.nzbmatrixapi.response.SearchResult;
import com.github.thebigs.nzbmatrixapi.response.SearchResultsResponse;

public class GetSearchResults {

    private static String USER_NAME = "<YOUR USER NAME>";
    private static String API_KEY = "<YOUR API KEY>";

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(final String[] args) throws ParseException {

        // you can either set the 2 strings above, or pass them in to this program
        if (args.length == 2) {
            USER_NAME = args[0];
            API_KEY = args[1];
        }

        // make sure the credentials got set
        if (USER_NAME.equals("<YOUR USER NAME>") || API_KEY.equals("<YOUR API KEY>")) {
            System.err.println(
                    "You must either edit this example file and put in your username and apikey OR pass them in as program arguments");
            System.exit(1);
        }

        final ApiCredentials credentials = new ApiCredentials(USER_NAME, API_KEY);
        final NZBMatrixApi api = new NZBMatrixApi(credentials);

        try {
            final String searchTerm = "fedora";
            // makes the search request to the nzbMatrixAPI
            final SearchResultsResponse searchResponse = api.getSearchResults(searchTerm);

            System.out.println("Got first " + searchResponse.getResults().size() + " results for the search term '"
                    + searchTerm + "'\n");

            // print out information about each result (see all of the methods attached to the result object.)
            for (final SearchResult result : searchResponse.getResults()) {
                System.out.println("\nNzb Name : " + result.getNzbName());
                System.out.println("\tNzb ID     : " + result.getNzbID());
                System.out.println("\tNzb Url    : " + result.getDetailsURL());
                System.out.println("\tCategory   : " + result.getCategory());
                System.out.println("\tAge        : " + result.getAgeInDays() + " days");
                System.out.println("\tPost Date  : " + result.getDatePosted());
                System.out.println("\tIndex Date : " + result.getDateIndexed());
                // see the rest of the methods attached to the SearchResult object
            }

        } catch (final ClientProtocolException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final NZBMatrixApiException e) {
            e.printStackTrace();
        }
    }

}