Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. package moviemanager.util;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.text.DateFormat;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Arrays;
  12. import java.util.Date;
  13. import java.util.Locale;
  14. import java.util.List;
  15. import java.util.regex.Pattern;
  16.  
  17. import javax.imageio.ImageIO;
  18. import javax.xml.parsers.DocumentBuilder;
  19. import javax.xml.parsers.DocumentBuilderFactory;
  20. import javax.xml.parsers.ParserConfigurationException;
  21.  
  22. import org.eclipse.jface.dialogs.MessageDialog;
  23. import org.eclipse.swt.graphics.Image;
  24. import org.eclipse.swt.widgets.Display;
  25. import org.eclipse.swt.widgets.Shell;
  26. import org.w3c.dom.DOMException;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.NamedNodeMap;
  29. import org.xml.sax.SAXException;
  30.  
  31. import moviemanager.MovieManager;
  32. import moviemanager.data.Movie;
  33. import moviemanager.data.Performer;
  34. import moviemanager.util.MovieManagerUtil.MovieManagerException;;
  35.  
  36. public class OMDbAPIRunner {
  37. // OMDb API Base URL
  38. private static final String OMDB_BASE = "http://www.omdbapi.com/?";
  39.  
  40. // OMDb API request URL parameters
  41. private static final String omdbTitel = OMDB_BASE + "t=";
  42. private static final String omdbIMDB_ID = OMDB_BASE + "i=";
  43. private static final String omdbSearch = OMDB_BASE + "s=";
  44.  
  45. // OMDb API xml data format parameter
  46. private static final String omdbParams = "&plot=full&r=xml";
  47.  
  48. public static void main(String[] args) {
  49. // perform a omdb search for the title 'Star Wars'
  50. String title = "Star Wars";
  51. String year_ = "";
  52. // response holds the response of the API
  53. NamedNodeMap response = null;
  54. try {
  55. String title_ = title.replaceAll(" ", "+");
  56. // create a URL which is used to perform the API request
  57. URL omdb = new URL(omdbTitel + title_ + "&y=" + year_ + omdbParams);
  58. // DocumentBuilder instance handles the XML data returned by the API
  59. DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  60. // perform the request to the API
  61. Document responseDoc = builder.parse(omdb.openStream());
  62. // check if the response contains movie data
  63. if (responseDoc.getElementsByTagName("movie") != null && responseDoc.getElementsByTagName("movie").getLength() > 0) {
  64. // store the movie data as NamedNodeMap in the response
  65. response = responseDoc.getElementsByTagName("movie").item(0).getAttributes();
  66. } else {
  67. throw new MovieManagerException("Could not find the movie in the OMDb.");
  68. }
  69. } catch (SAXException e) {
  70. e.printStackTrace();
  71. } catch (MalformedURLException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. } catch (ParserConfigurationException e) {
  76. e.printStackTrace();
  77. } catch (MovieManagerException e) {
  78. e.printStackTrace();
  79. }
  80. // if the response contains data print out the raw data to the console
  81. // TODO: Update / Create movie and Performer instances right here with
  82. // the received data
  83. if (response != null) {
  84.  
  85. for (int i = 0; i < response.getLength(); i++) {
  86. System.out.println(response.item(i));
  87. }
  88. }
  89. }
  90.  
  91. public static void getOMDBMovieData(String imdbId, Movie movie) {
  92.  
  93. // response holds the response of the API
  94. NamedNodeMap response = null;
  95. try {
  96.  
  97. // create a URL which is used to perform the API request
  98. URL omdb = new URL(omdbIMDB_ID + imdbId + omdbParams);
  99.  
  100. // DocumentBuilder instance handles the XML data returned by the API
  101. DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  102.  
  103. // perform the request to the API
  104. Document responseDoc = builder.parse(omdb.openStream());
  105.  
  106. // check if the response contains movie data
  107. if (responseDoc.getElementsByTagName("movie") != null && responseDoc.getElementsByTagName("movie").getLength() > 0) {
  108.  
  109. // store the movie data as NamedNodeMap in the response
  110. response = responseDoc.getElementsByTagName("movie").item(0).getAttributes();
  111. } else {
  112. throw new MovieManagerException("Could not find the movie in the OMDb.");
  113. }
  114.  
  115. // if the response contains data print out the raw data to the
  116. // console
  117. if (response != null) {
  118.  
  119. // Update movie data
  120. movie.setTitle(response.getNamedItem("title").getNodeValue());
  121. movie.setDescription(response.getNamedItem("plot").getNodeValue());
  122. movie.setCountry(response.getNamedItem("country").getNodeValue());
  123. movie.setLanguage(response.getNamedItem("language").getNodeValue());
  124. Float imdbRating = Float.parseFloat(response.getNamedItem("imdbRating").getNodeValue()) * 10;
  125. movie.setRating(Math.round(imdbRating));
  126.  
  127. // Runtime 'calculation' => the 4 last characters have to be
  128. // chopped
  129. // and the remaining string is to be casted to an Integer
  130. String runtime = response.getNamedItem("runtime").getNodeValue();
  131. runtime = runtime.substring(0, runtime.length() - 4);
  132. movie.setRuntime(Integer.parseInt(runtime));
  133.  
  134. // Date creation, must be surrounded by a catch-block in case
  135. // the
  136. // date cannot be parsed correctly
  137. DateFormat format = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
  138. Date released = format.parse(response.getNamedItem("released").getNodeValue());
  139. movie.setReleaseDate(released);
  140.  
  141. // Movie picture
  142. URL posterUrl = new URL(response.getNamedItem("poster").getNodeValue());
  143. InputStream is = posterUrl.openStream();
  144. Image poster = new Image(Display.getCurrent(), is);
  145. movie.setImage(poster);
  146.  
  147. // Link actors
  148. String actorsCommaSeparated = response.getNamedItem("actors").getNodeValue();
  149. java.util.List<String> actors = Arrays.asList(actorsCommaSeparated.split("\\s*,\\s*"));
  150. System.out.println(actors.toString());
  151.  
  152. for (String performerName : actors) {
  153.  
  154. // Try to fetch the performer object
  155. Performer performer = MovieManager.getInstance().getPerformer(performerName);
  156.  
  157. // If there is no performer with the given name, create one
  158. // and
  159. // name them
  160. if (performer == null) {
  161. performer = new Performer();
  162. String[] names = performerName.split(" ");
  163. String lastname = names[names.length-1];
  164. String firstname = performerName.substring(0, performerName.length()-lastname.length()-1);
  165. performer.setFirstName(firstname);
  166. performer.setLastName(lastname);
  167. }
  168.  
  169. // Link performer
  170. movie.linkPerformer(performer);
  171. MovieManager.getInstance().addPerformer(performer, true);
  172. }
  173.  
  174. // Refresh viewers
  175. MovieManager.getInstance().getDialog().refreshViewers();
  176. }
  177. } catch (SAXException e) {
  178. e.printStackTrace();
  179. } catch (MalformedURLException e) {
  180. e.printStackTrace();
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. } catch (ParserConfigurationException e) {
  184. e.printStackTrace();
  185. } catch (MovieManagerException e) {
  186. MessageDialog.openError(Display.getDefault().getActiveShell(), "Movie Manager Exception", e.getMessage());
  187. } catch (ParseException e) {
  188. MessageDialog.openError(Display.getDefault().getActiveShell(), "Parse Exception", e.getMessage());
  189. } catch(NumberFormatException e){
  190. MessageDialog.openError(Display.getDefault().getActiveShell(), "Number Format Exception", e.getMessage());
  191. } catch(ArrayIndexOutOfBoundsException e){
  192. MessageDialog.openError(Display.getDefault().getActiveShell(), "Array Index out of Bound", e.getMessage());
  193. }
  194.  
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement