예제 #1
0
 public PlaylistItemsComparer(UserOptions.Playlists config)
 {
     Config = config;
 }
예제 #2
0
        private void CreatePlaylistFiles(Dictionary <string, List <PlaylistItem> > itemsDictionary, UserOptions.Playlists config)
        {
            try
            {
                if (itemsDictionary.Count != 0)
                {
                    string _playlistBaseFolder = config.RelPath;
                    if (!string.IsNullOrEmpty(_playlistBaseFolder))
                    {
                        try
                        {
                            Directory.CreateDirectory(_playlistBaseFolder);
                        }
                        catch (Exception ex)
                        {
                            Loggy.Logger.DebugException("Create playlists folder", ex);
                            return;
                        }

                        if (config.CleanFolder && (config.FileType == UserOptions.PlaylistFileType.M3U))
                        {
                            foreach (string file in Directory.GetFiles(_playlistBaseFolder, "*.m3u"))
                            {
                                try
                                {
                                    FileInfo fi = new FileInfo(file);
                                    File.SetAttributes(file, FileAttributes.Normal);
                                    File.Delete(file);
                                }
                                catch { }
                            }

                            try
                            {
                                Directory.Delete(_playlistBaseFolder);
                            }
                            catch { }

                            Directory.CreateDirectory(_playlistBaseFolder);
                        }


                        //Parallel.ForEach(ItemsDictionary, new Action<KeyValuePair<string,List<PlaylistItem>>>(delegate
                        foreach (KeyValuePair <string, List <PlaylistItem> > _pair in itemsDictionary)
                        {
                            try
                            {
                                // sort the list
                                _pair.Value.Sort(new PlaylistItemsComparer(config));

                                // m3u format
                                if (config.FileType == UserOptions.PlaylistFileType.M3U)
                                {
                                    CreateM3UPlaylist(_pair.Key, _pair.Value, _playlistBaseFolder, config);
                                }

                                //Helpers.DoEvents();
                            }
                            catch (Exception ex)
                            {
                                Loggy.Logger.DebugException("Finalize playlists", ex);
                            }
                        }
                    }
                }
            }
            finally
            {
                itemsDictionary.Clear();
            }
        }
예제 #3
0
        private void CreateM3UPlaylist(string filename, List <PlaylistItem> items, string baseFolder, UserOptions.Playlists config)
        {
            // if there are items in the criteria
            if (items != null && items.Count > 0)
            {
                FileInfo _t = new FileInfo(Path.Combine(baseFolder, filename) + "." + config.FileType.ToString().ToLowerInvariant());
                using (StreamWriter _text = new StreamWriter(_t.FullName))
                {
                    _text.WriteLine("#EXTM3U");
                    foreach (PlaylistItem _item in items)
                    {
                        _text.WriteLine(string.Format("#EXTINF:{0},{1}", _item.Duration, _item.DisplayTitle));

                        string _pathToMakeRelative = null;
                        if (config.UseFolderInsteadOfMovie)
                        {
                            // check if we are inside DVD/Bluray folder structure
                            if (Helpers.IsDVDPath(_item.Filepath))
                            {
                                _pathToMakeRelative = Helpers.GetDVDRootDirectory(_item.Filepath);
                            }
                            else
                            {
                                if (Helpers.IsBlurayPath(_item.Filepath))
                                {
                                    _pathToMakeRelative = Helpers.GetBlurayRootDirectory(_item.Filepath);
                                }
                                else
                                {
                                    // clasic structure, use Folder path
                                    _pathToMakeRelative = Path.GetDirectoryName(_item.Filepath);
                                }
                            }
                        }
                        else
                        {
                            // use movie path
                            _pathToMakeRelative = _item.Filepath;
                        }

                        // make the filepath relative to the path to playlist
                        Uri _plist = new Uri(_t.FullName, UriKind.Absolute);
                        Uri _cfile = new Uri(_pathToMakeRelative, UriKind.Absolute);
                        Uri _final = _plist.MakeRelativeUri(_cfile);

                        //_text.WriteLine(Uri.UnescapeDataString(_final.OriginalString));
                        string _s = Uri.UnescapeDataString(_final.OriginalString);
                        if (_s[_s.Length - 1] != '\\' && _s[_s.Length - 1] != '/' && config.UseFolderInsteadOfMovie)
                        {
                            _s = _s + @"/";
                        }
                        _text.WriteLine(_s);
                        _text.WriteLine();
                    }
                }
            }
        }
예제 #4
0
        private void CreatePlaylists(IList <FileInfo> movieFiles, UserOptions.Playlists config)
        {
            /*
             * - take each file and try to open .nfo/.tgmd
             * - from .nfo extract list of genres and put the to the dictionary
             * - process each item in dictionary
             *
             */

            Dictionary <string, List <PlaylistItem> > _ItemsDictionary = new Dictionary <string, List <PlaylistItem> >();

            if (config != null && movieFiles != null && movieFiles.Count != 0)
            {
                bool _useSingleFile = config.Criteria == PlaylistManager.NOSPLIT_CRITERIA;
                bool _useUnassigned = config.UseUnassignedPlaylist && !_useSingleFile;

                string criteria = config.Criteria;
                if (!string.IsNullOrEmpty(criteria))
                {
                    if (_useUnassigned)
                    {
                        // add the _unassigned as key for the items missing the criteria
                        _ItemsDictionary.Add(UNASSIGNED_PLAYLIST_FILENAME, new List <PlaylistItem>());
                    }

                    if (_useSingleFile)
                    {
                        // add the PlaylistManager.NOSPLIT_PLAYLIST_FILENAME as key for all items
                        _ItemsDictionary.Add(config.SingleFilename, new List <PlaylistItem>());
                    }

                    try
                    {
                        bool _addToUnassigned = false;
                        foreach (FileInfo _file in movieFiles)
                        {
                            _addToUnassigned = false;
                            try
                            {
                                MovieItem _movieItem = FileManager.GetMovieByFilePath(_file.FullName);
                                //FileManager.SetMovieItemStatus(_movieItem, MovieItemStatus.Querying);

                                // extract movieinfo from metadata/nfo files
                                MovieInfo _info = ExtractMovieInfo(_file.FullName);

                                if (_info != null)
                                {
                                    List <string> _criteriaItems = null;

                                    // if more playlists will be generated then extract criteria items (else all items will be added to the PlaylistManager.NOSPLIT_PLAYLIST_FILENAME file
                                    if (!_useSingleFile)
                                    {
                                        _criteriaItems = ExtractCriteriaItems(criteria, _info);
                                    }

                                    if (_criteriaItems != null)
                                    {
                                        // will not enter here if single file mode
                                        // for each criteria item, if not in dictionary add it, if already there add movie to the list
                                        foreach (string _citem in _criteriaItems)
                                        {
                                            string _name = _citem;
                                            if (config.ForceEnglishResults)
                                            {
                                                _name = m_translatorManager.Translate(_name);
                                            }

                                            string _key = string.IsNullOrEmpty(_name) ? null : _name.ToLowerInvariant().Replace(Path.DirectorySeparatorChar, '_').Replace(Path.AltDirectorySeparatorChar, '_');
                                            if (!string.IsNullOrEmpty(_key))
                                            {
                                                if (!_ItemsDictionary.ContainsKey(_key))
                                                {
                                                    _ItemsDictionary.Add(_key, new List <PlaylistItem>());
                                                }
                                                List <PlaylistItem> _list = _ItemsDictionary[_key];

                                                _list.Add(new PlaylistItem(_file.FullName, _info));
                                            }
                                            else
                                            {
                                                _addToUnassigned = true;
                                            }
                                        }

                                        //FileManager.SetMovieItemStatus(_movieItem, MovieItemStatus.Done);
                                    }
                                    else
                                    {
                                        if (_useSingleFile)
                                        {
                                            List <PlaylistItem> _list = _ItemsDictionary[config.SingleFilename];
                                            _list.Add(new PlaylistItem(_file.FullName, _info));
                                            //FileManager.SetMovieItemStatus(_movieItem, MovieItemStatus.Done);
                                        }
                                        else
                                        {
                                            //FileManager.SetMovieItemStatus(_movieItem, MovieItemStatus.NotFound);
                                            _addToUnassigned = true;
                                        }
                                    }
                                }
                                else
                                {
                                    //FileManager.SetMovieItemStatus(_movieItem, MovieItemStatus.MetadataMissing);
                                    _addToUnassigned = true;
                                }

                                if (_useUnassigned && _addToUnassigned)
                                {
                                    _ItemsDictionary[UNASSIGNED_PLAYLIST_FILENAME].Add(new PlaylistItem(_file.FullName, new MovieInfo()));
                                }

                                //Helpers.DoEvents();
                            }
                            catch (Exception ex)
                            {
                                Loggy.Logger.DebugException("Processing playlist:", ex);
                            }
                        } // for
                    }
                    finally
                    {
                        //_translatorManager.ClearCache();
                        //_translatorManager = null;
                    }
                }

                // ItemsDictionary has all required infos, can create playlist files
                CreatePlaylistFiles(_ItemsDictionary, config);
            }
        }