Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package moviemanager.util;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.Locale;
- import java.util.List;
- import java.util.regex.Pattern;
- import javax.imageio.ImageIO;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.eclipse.jface.dialogs.MessageDialog;
- import org.eclipse.swt.graphics.Image;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.w3c.dom.DOMException;
- import org.w3c.dom.Document;
- import org.w3c.dom.NamedNodeMap;
- import org.xml.sax.SAXException;
- import moviemanager.MovieManager;
- import moviemanager.data.Movie;
- import moviemanager.data.Performer;
- import moviemanager.util.MovieManagerUtil.MovieManagerException;;
- public class OMDbAPIRunner {
- // OMDb API Base URL
- private static final String OMDB_BASE = "http://www.omdbapi.com/?";
- // OMDb API request URL parameters
- private static final String omdbTitel = OMDB_BASE + "t=";
- private static final String omdbIMDB_ID = OMDB_BASE + "i=";
- private static final String omdbSearch = OMDB_BASE + "s=";
- // OMDb API xml data format parameter
- private static final String omdbParams = "&plot=full&r=xml";
- public static void main(String[] args) {
- // perform a omdb search for the title 'Star Wars'
- String title = "Star Wars";
- String year_ = "";
- // response holds the response of the API
- NamedNodeMap response = null;
- try {
- String title_ = title.replaceAll(" ", "+");
- // create a URL which is used to perform the API request
- URL omdb = new URL(omdbTitel + title_ + "&y=" + year_ + omdbParams);
- // DocumentBuilder instance handles the XML data returned by the API
- DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- // perform the request to the API
- Document responseDoc = builder.parse(omdb.openStream());
- // check if the response contains movie data
- if (responseDoc.getElementsByTagName("movie") != null && responseDoc.getElementsByTagName("movie").getLength() > 0) {
- // store the movie data as NamedNodeMap in the response
- response = responseDoc.getElementsByTagName("movie").item(0).getAttributes();
- } else {
- throw new MovieManagerException("Could not find the movie in the OMDb.");
- }
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (MovieManagerException e) {
- e.printStackTrace();
- }
- // if the response contains data print out the raw data to the console
- // TODO: Update / Create movie and Performer instances right here with
- // the received data
- if (response != null) {
- for (int i = 0; i < response.getLength(); i++) {
- System.out.println(response.item(i));
- }
- }
- }
- public static void getOMDBMovieData(String imdbId, Movie movie) {
- // response holds the response of the API
- NamedNodeMap response = null;
- try {
- // create a URL which is used to perform the API request
- URL omdb = new URL(omdbIMDB_ID + imdbId + omdbParams);
- // DocumentBuilder instance handles the XML data returned by the API
- DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- // perform the request to the API
- Document responseDoc = builder.parse(omdb.openStream());
- // check if the response contains movie data
- if (responseDoc.getElementsByTagName("movie") != null && responseDoc.getElementsByTagName("movie").getLength() > 0) {
- // store the movie data as NamedNodeMap in the response
- response = responseDoc.getElementsByTagName("movie").item(0).getAttributes();
- } else {
- throw new MovieManagerException("Could not find the movie in the OMDb.");
- }
- // if the response contains data print out the raw data to the
- // console
- if (response != null) {
- // Update movie data
- movie.setTitle(response.getNamedItem("title").getNodeValue());
- movie.setDescription(response.getNamedItem("plot").getNodeValue());
- movie.setCountry(response.getNamedItem("country").getNodeValue());
- movie.setLanguage(response.getNamedItem("language").getNodeValue());
- Float imdbRating = Float.parseFloat(response.getNamedItem("imdbRating").getNodeValue()) * 10;
- movie.setRating(Math.round(imdbRating));
- // Runtime 'calculation' => the 4 last characters have to be
- // chopped
- // and the remaining string is to be casted to an Integer
- String runtime = response.getNamedItem("runtime").getNodeValue();
- runtime = runtime.substring(0, runtime.length() - 4);
- movie.setRuntime(Integer.parseInt(runtime));
- // Date creation, must be surrounded by a catch-block in case
- // the
- // date cannot be parsed correctly
- DateFormat format = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
- Date released = format.parse(response.getNamedItem("released").getNodeValue());
- movie.setReleaseDate(released);
- // Movie picture
- URL posterUrl = new URL(response.getNamedItem("poster").getNodeValue());
- InputStream is = posterUrl.openStream();
- Image poster = new Image(Display.getCurrent(), is);
- movie.setImage(poster);
- // Link actors
- String actorsCommaSeparated = response.getNamedItem("actors").getNodeValue();
- java.util.List<String> actors = Arrays.asList(actorsCommaSeparated.split("\\s*,\\s*"));
- System.out.println(actors.toString());
- for (String performerName : actors) {
- // Try to fetch the performer object
- Performer performer = MovieManager.getInstance().getPerformer(performerName);
- // If there is no performer with the given name, create one
- // and
- // name them
- if (performer == null) {
- performer = new Performer();
- String[] names = performerName.split(" ");
- String lastname = names[names.length-1];
- String firstname = performerName.substring(0, performerName.length()-lastname.length()-1);
- performer.setFirstName(firstname);
- performer.setLastName(lastname);
- }
- // Link performer
- movie.linkPerformer(performer);
- MovieManager.getInstance().addPerformer(performer, true);
- }
- // Refresh viewers
- MovieManager.getInstance().getDialog().refreshViewers();
- }
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (MovieManagerException e) {
- MessageDialog.openError(Display.getDefault().getActiveShell(), "Movie Manager Exception", e.getMessage());
- } catch (ParseException e) {
- MessageDialog.openError(Display.getDefault().getActiveShell(), "Parse Exception", e.getMessage());
- } catch(NumberFormatException e){
- MessageDialog.openError(Display.getDefault().getActiveShell(), "Number Format Exception", e.getMessage());
- } catch(ArrayIndexOutOfBoundsException e){
- MessageDialog.openError(Display.getDefault().getActiveShell(), "Array Index out of Bound", e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement