Friday, February 25, 2011

Using IMDb api to get details of movie from Java application

The Internet Movie Database (IMDb) is an online database of information related to movies, television shows, actors, production crew personnel, video games and fictional characters featured in visual entertainment media by Amazon.com. IMDbdoes not provide any api but some websites on the internet provides api which can be used to access IMDb database. One of the such website is http://www.deanclatworthy.com/imdb/.

Here a simple Java application shows how to access these apis. You can visit http://www.deanclatworthy.com/imdb/ for more information about api.


/*********************************************************************************
  Program to demonstrate how to use IMDB api to get details about any movie
  Developed by : Malhar Vora
  Developed on : 25-02-2011
  Development Status : Completed and tested
  Email              : vbmade2000@gmail.com
  WebSite            : http://malhar2010.blogspot.com
**********************************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;

public class IMDBDemo{
 
 public static void main(String []str){
   
  URL url = null;
  Scanner sc = null;
  String apiurl="http://www.deanclatworthy.com/imdb/";
  String moviename=null;  
  String dataurl=null;
  String retdata=null;
  InputStream is = null;
  DataInputStream dis = null;
  
  

  try{
    
   //Getting movie name from user
   sc = new Scanner(System.in);
   moviename=sc.nextLine();
   
   //Check if user has inputted nothing or blank
   if(moviename==null || moviename.equals("")){
    System.out.println("No movie found");
    System.exit(1);
   }
   
   //Remove unwanted space from moviename yb trimming it
   moviename=moviename.trim();
   
   //Replacing white spaces with + sign as white spaces are not allowed in IMDB api
   moviename=moviename.replace(" ","+");
   
   //Forming a complete url ready to send (type parameter can be JSON also)
   dataurl=apiurl+"?q="+moviename + "&type=text";
      
   System.out.println("Getting data from service");
   System.out.println("########################################");
   
   url = new URL(dataurl);   
   
   is = url.openStream();
   dis  = new DataInputStream(is);
   
   String details[];
   //Reading data from url
   while((retdata = dis.readLine())!=null){
    //Indicates that movie does not exist in IMDB databse
    if(retdata.equals("error|Film not found")){
     System.out.println("No such movie found");
     break;
    }
    
    //Replacing | character with # character for spliting
    retdata=retdata.replace("|","#");
    
    //Splitting up string by # character and storing output in details array
    details=retdata.split("#");
    
    //details[0] contains name of detail. e.g title,genre etc
    System.out.print(details[0].toUpperCase() + " -> ");
    
    //details[1] contains value of detail. e.g The Cave
    System.out.print(details[1]);
    System.out.println();
       
   }  
      
  }  
  catch(Exception e){
   System.out.println(e);
  }
  finally{
   try{
   
    if(dis!=null){
     dis.close();
    }
    
    if(is!=null){
     is.close();
    }
    
    if(sc!=null){
     sc.close();
    }
   }
   catch(Exception e2){
    ;
   }
  }
   
     
 }

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.