Advertisement
Guest User

Untitled

a guest
May 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.73 KB | None | 0 0
  1. package imdb;
  2. //72169 == actresses -> roles est une date ? apparement oui...
  3. // RE INIT LES DONNEES A CHAQUE TOUR DE BOUCLE !!!
  4. //actors.list : ligne 6956365 , 20595967 -> changer le { en espace car mauvais encodage
  5. //bug episode si le nom est une année ? ligne 131925 de actor.list
  6. //INSERT NULL POUR TOUS LES TRUCS 0
  7.  
  8. /*
  9. export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar
  10. */ //!!!!!!!!!!!!!!!!!
  11. import java.util.*;
  12. import java.util.Date;
  13. import java.io.BufferedReader;
  14. import java.sql.*;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.util.regex.*;
  18.  
  19.  
  20. public class Main{
  21.  
  22.     public static void main(String[] args) {
  23.         final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  24.         final String DB_URL = "jdbc:mysql://localhost/maindb";
  25.         final String USER = "root";
  26.         final String PASS = "";
  27.  
  28.         Connection conn = null;
  29.         PreparedStatement stmt = null;
  30.        
  31.         try{
  32.            
  33.             Class.forName("com.mysql.jdbc.Driver");
  34.                 System.out.println("Connecting to database...");
  35.                 conn = DriverManager.getConnection(DB_URL,USER,PASS);
  36.                 conn.setAutoCommit(false); // !!!
  37.             }
  38.         catch(Exception e){ e.printStackTrace();}
  39.        
  40.        
  41.         getRatings
  42.         /*getMoviesInfo("movies.list",stmt,conn);
  43.         System.out.println("MOVIE DONE");
  44.         getMoviesInfo("language.list",stmt,conn);
  45.         System.out.println("LANGUE DONE");
  46.         getMoviesInfo("genres.list",stmt,conn);
  47.         System.out.println("GENRE DONE");
  48.         getMoviesInfo("countries.list",stmt,conn);
  49.         System.out.println("COUNTRY DONE");*/
  50.  
  51.         //getPersonInfo("actors.list",stmt,conn);
  52.        
  53.        
  54.        
  55.         /*getPersonInfo("actors.list",stmt,conn);
  56.         getPersonInfo("actresses.list",stmt,conn);
  57.         getPersonInfo("writers.list",stmt,conn);
  58.         getPersonInfo("directors.list",stmt,conn);*/
  59.     }
  60.    
  61.  
  62.     static void getMoviesInfo(String file,PreparedStatement stmt,Connection conn){
  63.         /*  récupère toutes les infos concernant une oeuvre (nom de l'oeuvre,nom
  64.          *  et saison/numéro de l'épisode si il y en a, la langue et le pays dans
  65.          *  lequel l'oeuvre a été tournée */
  66.        
  67.         try {
  68.             BufferedReader br = new BufferedReader(new FileReader((file)));
  69.             String info = "", line = "", sql_query="",identifiant="", table="";
  70.             int  beginYear=-1, endYear =-1, workYear=-1,index = 0, i=0,beginLine=0,endLine=0;
  71.             String ep[] = new String[2], name[] = new String[3];
  72.             ResultSet rs =null;
  73.            
  74.             PreparedStatement stmtA =null,stmtB =null,stmtC =null,stmtD =null, stmtE=null, stmtF=null;
  75.             sql_query  = "REPLACE INTO Oeuvre(identifiant,titre,annee,note) VALUES(?,?,?,?)";
  76.             stmtA = conn.prepareStatement(sql_query);
  77.             sql_query  = "REPLACE INTO Episode(identifiant, numeroSaison, numeroEpisode, date, titreEpisode) VALUES(?,?,?,?,?)";
  78.             stmtB = conn.prepareStatement(sql_query);
  79.             sql_query = "REPLACE INTO Serie(identifiant,anneeFin) VALUES(?,?)";
  80.             stmtC = conn.prepareStatement(sql_query);
  81.             sql_query  = "REPLACE INTO Film(identifiant) VALUES(?)";
  82.             stmtD = conn.prepareStatement(sql_query);
  83.             if( file.equals("countries.list") )
  84.                 table="Pays";
  85.             else if(file.equals("language.list"))
  86.                 table="Langue";
  87.             else if(file.equals("genres.list"))
  88.                 table="Genre";
  89.             sql_query  = "REPLACE INTO "+table+"(nom) VALUES(?)";
  90.             stmtE = conn.prepareStatement(sql_query);
  91.             sql_query  = "INSERT INTO Oeuvre"+table+"(identifiant,nom) VALUES(?,?)";
  92.             stmtF = conn.prepareStatement(sql_query);
  93.            
  94.             if(file.equals("genres.list")){
  95.                 beginLine = 383;endLine=2490750;
  96.             }
  97.             else if(file.equals("movies.list")){
  98.                 beginLine = 15;endLine=4270540;
  99.             }
  100.             else{
  101.                 beginLine = 14; endLine = 2018390;
  102.                 if(file.equals("countries.list"))
  103.                     endLine = 2050240;
  104.             }
  105.            
  106.             for(i=0; i<beginLine;++i)
  107.                 br.readLine();     
  108.             while ((line = br.readLine()) != null && i<endLine) {
  109.                 index = line.length()-1;
  110.                 while(line.charAt(index) != ' ' && line.charAt(index) != '\t'){
  111.                     info = line.charAt(index)+info;
  112.                     index--;
  113.                 }
  114.                
  115.                 if(file.equals("movies.list") ){
  116.                     if(isInteger(info.substring(0, 4)))
  117.                         beginYear = Integer.parseInt(info.substring(0, 4));
  118.                     if( info.length() == 9){
  119.                         workYear=beginYear;
  120.                         if(isInteger(info.substring(5,9)))
  121.                             endYear = Integer.parseInt(info.substring(5, 9));
  122.                         else
  123.                             endYear=0;
  124.                     }
  125.                 }
  126.                
  127.                 name = getName(line);                                   //name[0] = nom, name[1] = série ou film
  128.                 if(name[1].equals("0"))                                 // si ="0" c'est une série
  129.                     ep = getEpisode(line, index);
  130.                 else
  131.                     workYear=beginYear;
  132.                
  133.                
  134.                 // name = titre, ep[0] = titre ep, ep[1]= saison ep.
  135.                 if( file.equals("movies.list") && (1999 < workYear) && (workYear < 2011) && beginYear<2011 && beginYear>1999){
  136.                    
  137.                     //c'est une série
  138.                     if(name[1].equals("0") && !(ep[0].equals("SUSPENDED"))){
  139.                         identifiant=name[0]+name[3]+ep[0]+ep[1];
  140.                         stmtA.setString(1, identifiant);
  141.                         stmtA.setString(2, name[0]);
  142.                         stmtA.setInt(3,workYear);
  143.                         stmtA.setObject(4, null);
  144.                         stmtA.addBatch();
  145.                        
  146.                        
  147.                         if(((!ep[0].equals("") || !ep[1].equals(" ")))){
  148.                             stmtB.setString(1,identifiant);
  149.                             if(!ep[1].equals(" ")){
  150.                                 String[] tmp = ep[1].split(" ");
  151.                                 stmtB.setInt(2,Integer.parseInt(tmp[0])); // numéro saison
  152.                                 stmtB.setInt(3,Integer.parseInt(tmp[1])); // numéro episode
  153.                             }
  154.    
  155.                             else{
  156.                                 stmtB.setObject(2,null);
  157.                                 stmtB.setObject(3,null); // numéro episode
  158.                             }
  159.                            
  160.                             if(beginYear!=-1)
  161.                                 stmtB.setInt(4,beginYear); // date de l'épisode
  162.                             else
  163.                                 stmtB.setObject(4,null);
  164.                             if(!ep[0].equals(""))
  165.                                 stmtB.setString(5,ep[0]);
  166.                             else
  167.                                 stmtB.setObject(5, null);
  168.                            
  169.                             stmtB.addBatch();
  170.                         }
  171.                        
  172.                            
  173.                        
  174.                        
  175.                         stmtC.setString(1,identifiant);
  176.                         if(endYear!=-1){
  177.                             stmtC.setInt(2,endYear);
  178.                         }
  179.                         else {
  180.                             stmtC.setObject(2,null);
  181.                         }
  182.                         stmtC.addBatch();
  183.                     }
  184.                     // c'est un film
  185.                     else{
  186.                         identifiant=name[0]+name[3];
  187.                         stmtA.setString(1, identifiant);
  188.                         stmtA.setString(2, name[0]);
  189.                         stmtA.setInt(3,workYear);
  190.                         stmtA.setObject(4, null);
  191.                         stmtA.addBatch();
  192.                        
  193.                         stmtD.setString(1,identifiant);
  194.                         stmtD.addBatch();
  195.                     }
  196.  
  197.                    
  198.                 }
  199.                
  200.                 else if(!file.equals("movies.list") && isInteger(name[2]) && ( Integer.parseInt(name[2])  > 1999 ) && ( Integer.parseInt(name[2])<2011 ) && !(ep[0].equals("SUSPENDED")) ){
  201.                     if(name[1].equals("0"))
  202.                         identifiant=name[0]+name[3]+ep[0]+ep[1];
  203.                     else
  204.                         identifiant=name[0]+name[3]+ep[0]+ep[1];
  205.                     insertCouLangGen(info,name[1],identifiant,stmtE,stmtF);
  206.                 }
  207.                
  208.                 info = "";
  209.                 beginYear=-1;endYear=-1;
  210.                 i++;
  211.                 if(i%25000==0){
  212.                     if(file.equals("movies.list")){
  213.                         stmtA.executeBatch();
  214.                         stmtB.executeBatch();
  215.                         stmtC.executeBatch();
  216.                         stmtD.executeBatch();
  217.                     }
  218.                     else{
  219.                         stmtE.executeBatch();
  220.                         stmtF.executeBatch();
  221.                     }
  222.                        
  223.                     conn.commit( );
  224.                     System.out.println(i);
  225.                 }
  226.             }
  227.             br.close();
  228.             conn.commit( );        
  229.  
  230.         }
  231.         catch (IOException e){e.printStackTrace();}
  232.         catch(Exception e){ e.printStackTrace();}
  233.     }
  234.    
  235.    
  236.     static String[] getName(String line){
  237.         String name ="", num = "", idYear="";
  238.         String isMovie = "1";
  239.         int index=0;
  240.         while(!isInteger(num) && index<line.length()-1 && !num.equals("????")){
  241.             while((line.charAt(index)!= '(' && index<line.length()-1) ||  index==1){
  242.                 name+= line.charAt(index);
  243.                 index++;
  244.             }
  245.             num = line.substring(index+1, index+5);
  246.             index++;
  247.         }
  248.  
  249.         if(line.charAt(index+4)!=')'){
  250.             index+=4;
  251.             while(line.charAt(index)!=')'){
  252.                 num+=line.charAt(index);
  253.                 index++;
  254.             }
  255.         }
  256.  
  257.         idYear=num;
  258.         if(name.charAt(0)== '\"'){
  259.             isMovie = "0";
  260.             name = name.substring(1, name.length()-2);
  261.         }
  262.        
  263.         String res[] = new String[4];
  264.         res[0] = name;
  265.         res[1] = isMovie;
  266.         res[2]=  num;
  267.         res[3]=  idYear;
  268.        
  269.         return res;
  270.     }
  271.    
  272.     static String[] getEpisode(String line, int index){
  273.         String episode="", numEpisode="", saison ="", ep="";
  274.         String res[] = new String[2];
  275.         while(line.charAt(index) != '{' && index>0){
  276.             index--;
  277.         }
  278.        
  279.         //on a un épisode
  280.         if(index != 0){
  281.             index++;
  282.             while(line.charAt(index) != '}'){
  283.                 episode += line.charAt(index);
  284.                 index++;
  285.             }
  286.             //on a un numSaison/NumEpisode
  287.             if(episode.indexOf('#') != -1){
  288.                 Pattern p = Pattern.compile("(^.*)?(?=\\(#)(\\(#([0-9]*).([0-9]*)\\))");
  289.                 Matcher m = p.matcher(episode);
  290.                 if(m.matches()){                                // GROUPE 1 = NOM, groupe 2 = tout le (#...), groupe 3 = saison, groupe 4 = episode;
  291.                     numEpisode = m.group(2);
  292.                     saison = m.group(3);
  293.                     ep = m.group(4);
  294.                 }
  295.                 //on enleve le numSaison/numEp de nom de l'épisode
  296.                 episode = episode.substring(0, episode.length()-numEpisode.length());
  297.                
  298.             }
  299.         }
  300.         res[0] = episode;
  301.         res[1] = saison+ " "+ ep;
  302.         return res;
  303.     }
  304.  
  305.     static void insertCouLangGen(String info, String isMovie, String identifiant, PreparedStatement stmtA, PreparedStatement stmtB){
  306.         try{
  307.             stmtA.setString(1, info);
  308.             stmtA.addBatch();
  309.            
  310.             stmtB.setString(1, identifiant);
  311.             stmtB.setString(2, info);
  312.             stmtB.addBatch();
  313.             stmtB.executeBatch();
  314.            
  315.         }
  316.         catch(SQLException se){se.printStackTrace();}
  317.  
  318.     }
  319.    
  320.    
  321.     static void getPersonInfo(String file,PreparedStatement stmt,Connection conn){
  322.         try {
  323.             String work[]={}, name[]={};
  324.             BufferedReader br = new BufferedReader(new FileReader((file)));
  325.             int beginLine=0, i=0, endLine=0, identifiant=0;
  326.             String line="", info="", sql_query="";
  327.             ResultSet rs = null;
  328.            
  329.             if(file.equals("directors.list")){
  330.                 beginLine = 235;endLine=3280382;
  331.             }
  332.             else if(file.equals("actors.list")){
  333.                 beginLine = 239;endLine=21562311;
  334.             }
  335.             else if(file.equals("actresses.list")){
  336.                 beginLine=13022040; endLine=13022047;
  337.             }
  338.             else if(file.equals("writers.list")){
  339.                 beginLine=303;endLine=5224334;
  340.             }
  341.             for(i=0; i<beginLine;++i)
  342.                 br.readLine(); 
  343.            
  344.             sql_query="INSERT INTO Personne(numero,nom,prenom,genre) VALUES(?,?,?,?)";
  345.             stmt = conn.prepareStatement(sql_query);
  346.            
  347.             PreparedStatement stmtA =null;
  348.             sql_query="INSERT INTO Acteur(numero,nom,prenom) VALUES(?,?,?)";
  349.             stmtA = conn.prepareStatement(sql_query);
  350.            
  351.             PreparedStatement stmtB=null;
  352.             sql_query="SELECT MAX(numero) FROM Personne";
  353.             stmtB = conn.prepareStatement(sql_query);
  354.            
  355.             PreparedStatement stmtC=null;
  356.             sql_query  = "REPLACE INTO OeuvreActeur(identifiant,nom,prenom,numero) SELECT e.identifiant,?,?,? "
  357.                     +"FROM Episode AS e, Oeuvre AS o WHERE e.titreEpisode= ? AND e.date = ?";
  358.             stmtC = conn.prepareStatement(sql_query);
  359.            
  360.            
  361.             // work[0] = nom oeuvre
  362.             // work[1] = nom episode
  363.             // work[2] = saison/num
  364.             // work[3] = année de début
  365.             // work[4] = si c un film/série
  366.             // name[0] = nom
  367.             // name[1] = prénom
  368.            
  369.             Date date = new Date();
  370.             System.out.println(date.toString());
  371.             while ((line = br.readLine()) != null && i<endLine) {
  372.                 //System.out.println(i);
  373.                 if(line != null && !line.isEmpty() && line.charAt(0)!=' ' && line.charAt(0)!= '\t' && line.charAt(0)!= '0' ){
  374.                     name = getPersonName(line);
  375.                     identifiant++;
  376.                     //insert dans Personne
  377.                     stmt.setInt(1, identifiant);
  378.                     stmt.setString(2,name[0]);
  379.                     stmt.setString(3,name[1]);
  380.                     if(file.equals("actors.list"))
  381.                         stmt.setString(4, "men");
  382.                     else
  383.                         stmt.setString(4, "woman");
  384.                    
  385.                     stmt.addBatch();
  386.        
  387.                    
  388.                     // insert dans acteur
  389.                     stmtA.setInt(1,identifiant);
  390.                     stmtA.setString(2, name[0]);
  391.                     stmtA.setString(3, name[1]);
  392.                     stmtA.addBatch();
  393.                 }
  394.                 if(line != null && !line.isEmpty()){
  395.                     work = getWork(line, i);
  396.                     if((file.equals("actors.list") || file.equals("actresses.list")) && isInteger(work[3]) && Integer.parseInt(work[3])>1999&& Integer.parseInt(work[3])<2011){
  397.                         info = getRole(line);
  398.                         //c'est une série
  399.                         if(work[4]=="0"){
  400.                             stmtC.setString(1,name[0]);
  401.                             stmtC.setString(2,name[1]);
  402.                             stmtC.setInt(3,identifiant);
  403.                             stmtC.setString(4,work[1]);
  404.                             if(work[3].equals("????"))
  405.                                 stmtC.setObject(5, null);
  406.                             else
  407.                                 stmtC.setInt(5,Integer.parseInt(work[3]));
  408.                             stmtC.addBatch();
  409.                            
  410.                         }
  411.                                                
  412.                     }
  413.                     // A VERIF POUR PAS RAJOUTER UN WORK AU SUIVANT QUI N'EN AURAIT PAS
  414.                     // + P-E QU'IL FAUT SAVOIR SI C UN FILM/SERIE
  415.                    
  416.                 }
  417.                 i++;
  418.                 System.out.println(i);
  419.                 if(i%50000==0){
  420.                     date = new Date();
  421.                     System.out.println(i+ "  "+date.toString());
  422.                     stmt.executeBatch();
  423.                     stmtA.executeBatch();
  424.                     stmtC.executeBatch();
  425.                     conn.commit();
  426.                    
  427.                 }
  428.             }
  429.             stmt.executeBatch();
  430.             stmtA.executeBatch();
  431.             stmtC.executeBatch();
  432.             conn.commit();
  433.             br.close();
  434.             conn.close();
  435.            
  436.         }
  437.         catch (IOException e) {e.printStackTrace();}
  438.         catch(Exception e){ e.printStackTrace();}
  439.        
  440.     }
  441.    
  442.     static String getRole(String line){
  443.         int index = line.length()-1;
  444.         String role = "";
  445.         while(index>0){
  446.             if(line.charAt(index)==']'){
  447.                 index--;
  448.                 while(line.charAt(index)!='[' && index>0){
  449.                     role = line.charAt(index) + role;
  450.                     index--;
  451.                 }
  452.                 if(index==0)
  453.                     role="";
  454.                 return role;
  455.             }
  456.             index--;
  457.         }
  458.         return role;
  459.     }
  460.    
  461.     static String[] getWork(String line, int i){
  462.         String workEp="",  numEp="";
  463.         if(line.charAt(0) != '\t'){             // on est sur une ligne où il y a le nom/prénom
  464.             int index=0;
  465.             while(line.charAt(index)!='\t')
  466.                 index++;
  467.             line = line.substring(index+1, line.length());
  468.         }
  469.         line = line.replaceAll("^\\s+", "");        // on enlève les tabs de gauche
  470.         String name[] = getName(line);
  471.        
  472.         if(name[1].equals("0")){
  473.             String ep[] = getEpisode(line, line.length()-1);
  474.             workEp = ep[0];
  475.             numEp= ep[1];
  476.            
  477.         }
  478.         else
  479.             name[0]= name[0].substring(0, name[0].length()-1);
  480.        
  481.         String res[] = new String[5];
  482.         res[0]= name[0];    // nom oeuvre
  483.         res[1]= workEp;     // nom episode
  484.         res[2]= numEp;      // saison/num
  485.         res[3]= name[2];    // année de début
  486.         res[4]=name[1];     // si c'est un film/série
  487.         return res;
  488.     }
  489.    
  490.     static String[] getPersonName(String line){
  491.        
  492.         String firstname ="", name="";
  493.         int index =0;
  494.          
  495.         while(true){
  496.             if(line.charAt(index)==',' || line.charAt(index)=='\t')
  497.                 break;
  498.             name+= line.charAt(index);
  499.             index++;
  500.         }
  501.        
  502.         if(line.charAt(index)==','){
  503.             index+=2;
  504.             while(line.charAt(index)!=' ' && line.charAt(index)!= '\t'){
  505.                 firstname+= line.charAt(index);
  506.                 index++;
  507.             }
  508.         }
  509.        
  510.         String res[] = new String[2];
  511.         res[0]= name;
  512.         res[1]= firstname;
  513.         return res;
  514.     }
  515.    
  516.    
  517.     static void getRatingInfo(String file,PreparedStatement stmt,Connection conn){
  518.         try {
  519.             BufferedReader br = new BufferedReader(new FileReader((file)));
  520.             int beginLine = 30, i=0, votes=0, year = 0;
  521.             float score=0.0f;
  522.             String line="", title="", production="";
  523.            
  524.             for(i=0; i<beginLine;++i)
  525.                 br.readLine();     
  526.             while ((line = br.readLine()) != null) {
  527.                 if(line.length()!=0 && line.charAt(0) == ' '){
  528.                     Pattern p = Pattern.compile("\\s*([0-9\\.*]{10})\\s*([0-9]*)(\\s*[0-9]+\\.[0-9]\\s*)([^\\(]*)?(\\(([^\\(]*)\\))?(\\s\\{(.*)\\})?");
  529.                     Matcher m = p.matcher(line);
  530.                     if(m.matches()){                   
  531.                         title = m.group(4);
  532.                         score=Float.parseFloat(m.group(3));
  533.                         votes =Integer.parseInt(m.group(2));
  534.                         production=m.group(1);
  535.                         // fonctionne pas car il y 2016/I... a voir si il faut le prendre
  536.                         //year = Integer.parseInt(m.group(6));
  537.                         System.out.println(i + " || " + score + " || " +title + " || " +votes);
  538.                        
  539.                     }
  540.                 }
  541.                 i++;
  542.             }
  543.  
  544.         }
  545.         catch (IOException e){e.printStackTrace();}
  546.     }
  547.    
  548.    
  549.     static boolean isInteger( String input ){
  550.        try{
  551.           Integer.parseInt( input );
  552.           return true;
  553.        }
  554.        catch( Exception e){
  555.           return false;
  556.        }
  557.     }
  558. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement