Advertisement
Guest User

Untitled

a guest
Oct 14th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8.  
  9. /**
  10.  * A simple parser for the TheMovieDB Database
  11.  * This class receives as input a file with a list of movie IDs from TheMovieDB and creates a graph in GML describing
  12.  * the connections among actors in the casts.
  13.  *
  14.  * It works by creating a clique for each movie. When a actor participates in more than one movie he connects both cliques.
  15.  *
  16.  * @author Alexandre Duarte - http://alexandre.ci.ufpb.br - alexandre@ci.ufpb.br
  17.  *
  18.  */
  19. public class TheMovieDBParser {
  20.  
  21.    
  22.     private String movieID;
  23.     private String title;
  24.  
  25.     private Vector <String>genres;
  26.     private Vector<String> cast;
  27.  
  28.     public TheMovieDBParser(String movieID) {
  29.        
  30.         this.movieID = movieID;
  31.         genres = new  Vector<String>();
  32.         cast = new Vector <String>();
  33.    
  34.     }
  35.    
  36.     public void extractData() throws IOException, InterruptedException {
  37.                
  38.      URL url = new URL( "http://api.themoviedb.org/3/movie/" + movieID + "?api_key=YOUR API KEY");
  39.      
  40.      BufferedReader br = new BufferedReader( new InputStreamReader(url.openStream()));
  41.    
  42.      String line = br.readLine();
  43.      
  44.      int s = line.indexOf( "original_title") + 17;
  45.      int e = line.indexOf("overview") - 3;
  46.      
  47.      this.setTitle(line.substring(s,e));
  48.  
  49.      s = line.indexOf("genres") + 8;
  50.      e = line.indexOf("homepage") - 2;
  51.      
  52.      StringTokenizer st = new StringTokenizer(line.substring(s,e),",");
  53.      
  54.      int i = 0;
  55.      while(st.hasMoreTokens()) {
  56.          
  57.          String t = st.nextToken();
  58.          if( i % 2 == 1) {
  59.              t = t.replaceAll("\"name\":\"", "").replaceAll("\"}", "").replaceAll("]", "");
  60.              
  61.             genres.add(t);
  62.              
  63.          }
  64.          i++;
  65.          
  66.      }
  67.      
  68.     }
  69.    
  70.    
  71.     public void extractCast() throws IOException, InterruptedException {
  72.        
  73.          URL url = new URL( "http://api.themoviedb.org/3/movie/" + movieID + "/casts?api_key=YOUR API KEY");
  74.          
  75.          BufferedReader br = new BufferedReader( new InputStreamReader(url.openStream()));
  76.        
  77.          String line = br.readLine();
  78.          line = line.substring(0, line.indexOf("]"));
  79.          
  80.          StringTokenizer st = new StringTokenizer(line, "{");
  81.          
  82.          st.nextToken();
  83.          while( st.hasMoreTokens()) {
  84.              String a = st.nextToken();          
  85.              cast.add (a.substring(a.indexOf("name") + 7, a.indexOf("\",\"")));
  86.          }
  87.          
  88.     }
  89.    
  90.  
  91.  
  92.     public String getTitle() {
  93.         return title;
  94.     }
  95.  
  96.     public void setTitle(String title) {
  97.         this.title = title;
  98.     }
  99.    
  100.     private Vector <String> getCast() {
  101.         return cast;
  102.     }
  103.    
  104.     public static void main( String args[] ) throws IOException, InterruptedException {
  105.        
  106.        
  107.         BufferedReader r = new BufferedReader( new FileReader (args[0]));
  108.         String movie;
  109.        
  110.        
  111.         System.out.println( "graph");
  112.         System.out.println( "[");
  113.         System.out.println("\tdirected 0");
  114.        
  115.        
  116.         while( (movie = r.readLine())!=null) {
  117.            
  118.             TheMovieDBParser m = new TheMovieDBParser(movie);
  119.             m.extractData();
  120.             m.extractCast();
  121.        
  122.             Thread.sleep(1000);
  123.            
  124.             Vector <String>v = m.getCast();
  125.             int edgeID= 0;
  126.             for( int i = 0; i < v.size(); i++ )
  127.                 for( int j = i + 1; j < v.size(); j++) {
  128.                    
  129.                     System.out.println( "\tedge");
  130.                     System.out.println( "\t[");
  131.                     System.out.println( "\t\t id " + edgeID++ );
  132.                     System.out.println( "\t\t label \"" + m.getTitle() + "\"");
  133.                     System.out.println( "\t\t source \"" + v.elementAt(i) + "\"");
  134.                     System.out.println( "\t\t target \"" + v.elementAt(j) + "\"");
  135.                     System.out.println( "\t\t value 1");
  136.                     System.out.println( "\t]");
  137.                    
  138.                 }
  139.         }
  140.        
  141.         System.out.println ( "]");
  142.     }
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement