예제 #1
0
        // Command-line entry point + argument handling
        static void Main(string[] args)
        {
            String sInput = "";       // Input file or dir
              String sOutput = "/scratch/ekomen/out/";      // Output directory, if specified
              String sLanguage = "dut";                     // This is the language abbreviation used in [osrMovie.cs] for sBaseUrl
              String sDict = "";        // Movie dictionary
              bool bIsDebug = false;    // Debugging
              bool bForce = false;      // Force
              bool bOview = false;      // Make overview or not
              bool bSkip = false;       // Skip everything that has *not* been made
              String sAction = "cmdi";  // Type of action to be taken

              try {
            // Check command-line options
            for (int i = 0; i < args.Length; i++) {
              // get this argument
              String sArg = args[i].Trim();
              if (sArg.StartsWith("-")) {
            errHandle.Status("Processing argument ["+sArg+"]");
            // Check out the arguments
            switch (sArg.Substring(1)) {
              case "i": // Input file or directory with .folia.xml files
                sInput = args[++i];
                break;
              case "f": // Force
                bForce = true;
                break;
              case "s": // Skip
                bSkip = true;
                break;
              case "m": // Movie dictionary   -- Tab-separated list from opensubtitles.org
                sDict = args[++i];
                break;
              case "o": // Output directory
                sOutput = args[++i];
                break;
              case "h": // Calculate hashes and add them to existing .cmdi.xml files
                sAction = "hash";
                break;
              case "v": // Make an overview
                bOview = true;
                break;
              case "d": // Debugging
                bIsDebug = true;
                break;
              case "l": // Language (three letter code)
                sLanguage = args[++i];
                break;
            }
              } else if (sArg == "" || sArg == "\r") {
            // Do nothing
              } else {
            // Throw syntax error and leave
            SyntaxError("1 - i=" + i + " args=" + args.Length + " argCurrent=[" + sArg + "]"); return;
              }
            }
            // Check presence of input/output
            if (sInput == "" ) { SyntaxError("2"); return; }

            // Initialize the main entry point for the conversion
            oprConv objConv = new oprConv(errHandle);
            osrMovie objMovie = new osrMovie(errHandle, sLanguage);
            omdbapi objOmdb = new omdbapi(errHandle);

            // Set directory for conversion
            objConv.dirRoot(sOutput);

            // Load the movie dictionary
            if (!objConv.loadMovieDictionary(sDict)) {
              errHandle.DoError("Main", "Could not load movie dictionary from [" + sDict + "]");
              return;
            }

            // Initialise the Treebank Xpath functions, which may make use of tb:matches()
            util.XPathFunctions.conTb.AddNamespace("tb", util.XPathFunctions.TREEBANK_EXTENSIONS);

            // Check if the input is a directory or file
            if (Directory.Exists(sInput)) {
              WalkDirectoryTree(sInput, "*.folia.xml.gz", sInput, bForce, bSkip, bIsDebug, sAction,
            ref objConv, ref objMovie);
            } else {
              // Show we don't have input file
              errHandle.DoError("Main", "Cannot find input file(s) in: " + sInput);
            }
            // Calculate for each file which others are close to it
            // - try to determine the license information for the best matching .cmdi.xml files
            // - add some more meta-information to the .cmdi.xml files
            objConv.findDuplicates(ref lSubInst, 3, ref objOmdb);

            // Create an overview - if required
            if (bOview) {
              String sOview = objConv.getDistanceOview();
              // Save it in a standard file
              String sFileCsv = Path.GetDirectoryName(sInput) + "/oview.csv";
              File.WriteAllText(sFileCsv, sOview);
            }
            // Exit the program
            Console.WriteLine("Ready");
              } catch (Exception ex) {
            errHandle.DoError("Main", ex); // Provide standard error message
            throw;
              }
        }
예제 #2
0
        /// <summary>
        /// WalkDirectoryTree --
        ///     Recursively walk the directory starting with @sStartDir
        ///     Execute conversion on any .gz file encountered using @objConv
        /// </summary>
        /// <param name="sStartDir"></param>
        /// <param name="sFilter"></param>
        /// <param name="sInput"></param>
        /// <param name="bForce"></param>
        /// <param name="bIsDebug"></param>
        /// <param name="sAction">The action to be taken: "cmdi", "hash"</param>
        /// <param name="objConv"></param>
        /// <param name="objMovie"></param>
        /// <param name="objOmdb"></param>
        static void WalkDirectoryTree(String sStartDir, String sFilter, String sInput, bool bForce, 
      bool bSkip, bool bIsDebug, String sAction, ref oprConv objConv, ref osrMovie objMovie)
        {
            String[] arFiles = null;
              String[] arSubDirs = null;

              // Exclude 'raw'
              if (sStartDir.Contains("/raw/") || sStartDir.Contains("\\raw\\")) return;
              // First, process all the files directly under this folder
              try {
            arFiles = Directory.GetFiles(sStartDir, sFilter);
              }
              // This is thrown if even one of the files requires permissions greater
              // than the application provides.
              catch (UnauthorizedAccessException e) {
            // Only give warning
            errHandle.Status(e.Message);
              } catch (System.IO.DirectoryNotFoundException e) {
            errHandle.Status(e.Message);
              }

              // Check if all is valid
              if (arFiles != null) {
            // bSkip = true;
            // Walk all files in this directory
            foreach (String sFile in arFiles) {
              // What we do here depends on the action identified
              switch(sAction) {
            case "cmdi":
              // Parse this input file to the output directory
              if (!objConv.ConvertOneOpsToCmdi(sFile, ref objMovie, bForce, bIsDebug)) {
                errHandle.DoError("Main", "Could not convert file [" + sFile + "]");
                return;
              }
              break;
            case "hash":
              if (bSkip) {
                if (!objConv.HarvestHashFromCmdi(sFile, ref lSubInst)) {
                  errHandle.DoError("Main", "Could not harvest hash for file [" + sFile + "]");
                  return;
                }
              } else {
                // Calculate the HASH of this .folia.xml file, and put it into the existing CMDI
                if (!objConv.CalculateHashToCmdi(sFile, ref lSubInst, bIsDebug)) {
                  errHandle.DoError("Main", "Could not calculate hash for file [" + sFile + "]");
                  return;
                }
              }
              break;
              }
            }

            // Now find all the subdirectories under this directory.
            arSubDirs = Directory.GetDirectories(sStartDir);
            // Walk all directories
            foreach (String sDirName in arSubDirs) {
              // Resursive call for each subdirectory.
              WalkDirectoryTree(sDirName, sFilter, sInput, bForce, bSkip, bIsDebug, sAction, ref objConv, ref objMovie);
            }
              }
        }