예제 #1
0
        public MovieCollection ImportMoviesFromDumpFiles(string[] filesToRead, MovieCollection movieCollection)
        {
            var movieList = new List <Movie>();

            foreach (var txtFile in filesToRead)
            {
                if (File.Exists(txtFile) && txtFile.Contains(".txt"))
                {
                    var fileName = Path.GetFileNameWithoutExtension(txtFile);
                    var Encoding = EncodingDetector.DetectTextFileEncoding(txtFile);

                    var myFile       = new StreamReader(txtFile, Encoding);
                    var moviesString = myFile.ReadToEnd();
                    myFile.Close();

                    moviesString = moviesString.Replace("last-modified   |ext |size|name|location", string.Empty);
                    var moviesLines = moviesString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    OnProgressUpdate?.Invoke(ImportProgressType.NewFile, fileName, moviesLines.Count());
                    foreach (var line in moviesLines)
                    {
                        if (line != "done" && !string.IsNullOrWhiteSpace(line))
                        {
                            var movie = FileHelper.GetMovieFromLine(line, fileName);
                            if (movie != null)
                            {
                                movieCollection.AddMovie(movie);
                            }
                        }
                        OnProgressUpdate?.Invoke(ImportProgressType.NewLine, string.Empty, 0);
                    }
                }
            }
            return(movieCollection);
        }
예제 #2
0
        // Import data from PS HH
        protected override void DoImport()
        {
            importingResetEvent.Reset();

            var isActualTaskRunning    = false;
            var isNotActualTaskRunning = false;

            while (!cancellationTokenSource.IsCancellationRequested)
            {
                try
                {
                    var settings     = ServiceLocator.Current.GetInstance <ISettingsService>().GetSettings();
                    var siteSettings = settings.SiteSettings.SitesModelList?.FirstOrDefault(x => x.PokerSite == Site);

                    if (siteSettings != null && !siteSettings.Enabled)
                    {
                        Stop();
                        continue;
                    }

                    var handHistoryFolders = GetHandHistoryFolders(siteSettings);

                    IsAdvancedLogEnabled = settings.GeneralSettings.IsAdvancedLoggingEnabled;

                    // detect all *.txt files in directories
                    var handHistoryFiles = handHistoryFolders
                                           .Where(x => x.Exists)
                                           .SelectMany(x => x.GetFiles(HandHistoryFilter, SearchOption.AllDirectories))
                                           .DistinctBy(x => x.FullName.ToLower())
                                           .ToArray();

                    var capturedFiles = actualCapturedFiles.Select(x => x.Value).Concat(notActualCapturedFiles.Select(x => x.Value)).ToArray();

                    var newlyDetectedHandHistories = (from handHistoryFile in handHistoryFiles
                                                      join capturedFile in capturedFiles on handHistoryFile.FullName equals capturedFile.ImportedFile.FileName into capturedFileGrouped
                                                      from item in capturedFileGrouped.DefaultIfEmpty()
                                                      where item == null && !filesToSkip.Contains(handHistoryFile.FullName)
                                                      select handHistoryFile).ToArray();

                    // check if files were processed
                    if (newlyDetectedHandHistories.Length > 0)
                    {
                        var newlyDetectedHandHistoriesNames = newlyDetectedHandHistories.Select(x => x.FullName).ToArray();

                        var processedFiles = dataService.GetImportedFiles(newlyDetectedHandHistoriesNames);

                        var processedFilesToSkip = (from processedFile in processedFiles
                                                    join newlyDetectedHandHistory in newlyDetectedHandHistories on
                                                    new
                        {
                            FileName = processedFile.FileName,
                            FileSize = processedFile.FileSize,
                            LastWrite = DateTime.SpecifyKind(processedFile.LastWriteTime, DateTimeKind.Utc)
                        } equals
                                                    new
                        {
                            FileName = newlyDetectedHandHistory.FullName,
                            FileSize = newlyDetectedHandHistory.Length,
                            LastWrite = new DateTime(newlyDetectedHandHistory.LastWriteTimeUtc.Year,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Month,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Day,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Hour,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Minute,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Second,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Kind)
                        }
                                                    select newlyDetectedHandHistory).ToArray();

                        if (processedFilesToSkip.Length > 0)
                        {
                            newlyDetectedHandHistories = newlyDetectedHandHistories
                                                         .Except(processedFilesToSkip, new LambdaComparer <FileInfo>((x, y) => x.FullName == y.FullName))
                                                         .ToArray();
                        }
                    }

                    // add new files and lock them
                    foreach (var hh in newlyDetectedHandHistories)
                    {
                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        if (!actualCapturedFiles.ContainsKey(hh.FullName) &&
                            !notActualCapturedFiles.ContainsKey(hh.FullName))
                        {
                            var fs = File.Open(hh.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                            fs.Seek(0, SeekOrigin.Begin);

                            Encoding encoding;

                            try
                            {
                                encoding = EncodingDetector.DetectTextFileEncoding(fs, 0x10000) ?? Encoding.UTF8;
                            }
                            catch (Exception ex)
                            {
                                LogProvider.Log.Error(this, $"Couldn't detect encoding of {hh.FullName}. UTF8 will be used. [{SiteString}]", ex);
                                encoding = Encoding.UTF8;
                            }

                            fs.Seek(0, SeekOrigin.Begin);

                            var capturedFile = new CapturedFile
                            {
                                FileStream   = fs,
                                Session      = GetSessionForFile(hh.FullName),
                                Encoding     = encoding,
                                ImportedFile = new ImportedFile
                                {
                                    FileName = hh.FullName
                                }
                            };

                            var lastWriteTime = File.GetLastWriteTimeUtc(hh.FullName);

                            if (lastWriteTime > DateTime.UtcNow.AddSeconds(-ActualFileLifeTime))
                            {
                                actualCapturedFiles.TryAdd(hh.FullName, capturedFile);
                            }
                            else
                            {
                                notActualCapturedFiles.TryAdd(hh.FullName, capturedFile);
                            }
                        }
                    }

                    if (!isActualTaskRunning)
                    {
                        ImportCapturedFiles(actualCapturedFiles, true);
                        isActualTaskRunning = true;
                    }

                    if (!isNotActualTaskRunning)
                    {
                        ImportCapturedFiles(notActualCapturedFiles, false);
                        isNotActualTaskRunning = true;
                    }

                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            Task.Delay(ReadingTimeout).Wait(cancellationTokenSource.Token);
                        }
                        catch (OperationCanceledException)
                        {
                        }
                    }
                }
                catch (Exception e)
                {
                    LogProvider.Log.Error(this, string.Format("{0} auto-import failed", SiteString), e);
                }
            }

            Clean();

            RaiseProcessStopped();
        }