Esempio n. 1
0
        private async Task <TvSeries> GetSeriesInfo(string seriesUrl, WmcItem episode, string searchName, ILog logger)
        {
            TvSeries tvSeries = null;

            string    url  = string.Format(seriesUrl, WebUtility.UrlEncode(searchName));
            XDocument xdoc = await GetXmlDoc(url, logger);

            IEnumerable <TvSeries> series = (xdoc.Descendants("Series").Select
                                                 (items => new TvSeries
            {
                WmcName = episode.Series.WmcName,
                TvDbId = items.Element("id")?.Value,
                TvDbName = items.Element("SeriesName")?.Value
            }));

            IList <TvSeries> tvs = series as IList <TvSeries> ?? series.ToList();

            if (tvs.Count == 1 && episode.Series.WmcName == tvs[0].TvDbName)
            {
                tvSeries = tvs[0];
            }
            else if (tvs.Count >= 1)
            {
                tvSeries = UserSelectSeries(episode, tvs);
            }

            return(tvSeries);
        }
Esempio n. 2
0
        private TvSeries UserSelectSeries(WmcItem episode, IList <TvSeries> tvSeries)
        {
            Console.WriteLine("Multiple series matches for {0}", episode.Series.WmcName);
            TvSeries tvs = null;

            for (int i = 0; i < tvSeries.Count; i++)
            {
                Console.WriteLine("{0}: {1}", i + 1, tvSeries[i].TvDbName);
            }

            while (tvs == null)
            {
                Console.Write("Select Series (0 to skip): ");
                string s = Console.ReadLine();
                if (int.TryParse(s, out int x))
                {
                    x--;
                    if (x == -1)
                    {
                        break;
                    }

                    if (x >= 0 && x < tvSeries.Count)
                    {
                        tvs = tvSeries[x];
                    }
                }
            }

            return(tvs);
        }
Esempio n. 3
0
        private async Task <Tuple <bool, TvSeries> > UserSearchSeries(WmcItem wmcItem, string seriesUrl, ILog logger)
        {
            bool cont = true;

            Console.WriteLine("No series matches for {0}", wmcItem.Series.WmcName);

            Console.Write("Search for series (0 to skip): ");
            string   s      = Console.ReadLine();
            TvSeries series = null;

            if (int.TryParse(s, out int x) && x == 0)
            {
                cont = false;
            }
            else
            {
                series = await GetSeriesInfo(seriesUrl, wmcItem, s, logger);

                if (series != null)
                {
                    cont = false;
                }
            }

            return(new Tuple <bool, TvSeries>(cont, series));
        }
Esempio n. 4
0
        private void RenameTvFile(string destinationFolder, WmcItem episode, ILog logger)
        {
            string path = Path.Combine(destinationFolder, episode.Series.FolderName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(path, "Season " + episode.TvDbEpisode.SeasonNumber.ToString().PadLeft(2, '0'));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string fileName = string.Format(FileNameFormat, episode.Series.FolderName,
                                            episode.TvDbEpisode.SeasonNumber.ToString().PadLeft(2, '0'),
                                            episode.TvDbEpisode.EpisodeNumber.ToString().PadLeft(2, '0'),
                                            CleanFileName(episode.TvDbEpisode.Name));


            path = Path.Combine(path, fileName);

            MoveFile(episode.File.FullName, path, logger);
        }
Esempio n. 5
0
        private TvDbEpisode UserSelectEpisode(WmcItem episode, IList <TvDbEpisode> tvDbEpisodes)
        {
            Console.WriteLine("Multiple episode match for {0}: {1} {2}", episode.Series.TvDbName, episode.Title, episode.BroadcastDate);
            int         x   = 0;
            TvDbEpisode tvs = null;

            for (int i = 0; i < tvDbEpisodes.Count; i++)
            {
                Console.WriteLine("{0}: s{1} e{2} {3} {4}", i + 1, tvDbEpisodes[i].SeasonNumber, tvDbEpisodes[i].EpisodeNumber, tvDbEpisodes[i].Name, tvDbEpisodes[i].FirstAired);
            }

            while (tvs == null)
            {
                Console.Write("Select Episode (0 to skip): ");
                string s = Console.ReadLine();
                if (int.TryParse(s, out x))
                {
                    x--;
                    if (x == -1)
                    {
                        break;
                    }
                    else if (x >= 0 && x < tvDbEpisodes.Count)
                    {
                        tvs = tvDbEpisodes[x];
                    }
                }
            }

            return(tvs);
        }
Esempio n. 6
0
        private TvSeries FindKnownSeries(WmcItem episode, IEnumerable <TvSeries> knownSeries)
        {
            IEnumerable <TvSeries> ts       = (from s in knownSeries where s.WmcName == episode.Series.WmcName select s);
            IList <TvSeries>       tvSeries = ts as IList <TvSeries> ?? ts.ToList();

            if (tvSeries.Any())
            {
                return(tvSeries.First());
            }

            return(null);
        }
Esempio n. 7
0
        private TvSeries FindKnownSeries(WmcItem episode)
        {
            foreach (var series in _settings.TvSeries)
            {
                if (series.WmcName == episode.Series.WmcName)
                {
                    return(series);
                }
            }

            return(null);
        }
Esempio n. 8
0
        private bool IgnoreWmcItem(WmcItem wmcItem, List <string> ignoreItems)
        {
            bool ignore = false;

            if (wmcItem.Type == ItemType.Tv)
            {
                ignore = ignoreItems.Contains(wmcItem.Series.WmcName);
            }
            else if (wmcItem.Type == ItemType.Movie)
            {
                ignore = ignoreItems.Contains(wmcItem.Title);
            }
            return(ignore);
        }
Esempio n. 9
0
        private bool IgnoreWmcItem(WmcItem wmcItem)
        {
            var ignore = false;

            if (wmcItem.Type == ItemType.Tv)
            {
                ignore = _settings.IgnoreItems.Contains(wmcItem.Series.WmcName);
            }
            else if (wmcItem.Type == ItemType.Movie)
            {
                ignore = _settings.IgnoreItems.Contains(wmcItem.Title);
            }

            return(ignore);
        }
Esempio n. 10
0
        private bool UserIgnore(WmcItem wmcItem)
        {
            if (wmcItem.Type == ItemType.Tv)
            {
                Console.Write("Ignore TV series {0} (y/n)? ", wmcItem.Series.WmcName);
            }
            else if (wmcItem.Type == ItemType.Movie)
            {
                Console.Write("Ignore Movie {0} ({1}) (y/n)? ", wmcItem.Title, wmcItem.ReleaseYear);
            }

            bool?ignore = null;

            while (!ignore.HasValue)
            {
                var s = Console.ReadLine();
                if (s != null)
                {
                    if (s.ToUpper() == "Y")
                    {
                        ignore = true;
                    }
                    else if (s.ToUpper() == "N")
                    {
                        ignore = false;
                    }
                }
            }

            if (ignore.Value)
            {
                if (wmcItem.Type == ItemType.Tv)
                {
                    _settings.IgnoreItems.Add(wmcItem.Series.WmcName);
                }
                else if (wmcItem.Type == ItemType.Movie)
                {
                    _settings.IgnoreItems.Add(wmcItem.Title);
                }
            }

            return(ignore.Value);
        }
Esempio n. 11
0
        private async Task <TvSeries> GetSeriesInfo(WmcItem episode, string searchName)
        {
            TvSeries tvSeries = null;

            var doc = await _tvDbClient.SearchSeries(searchName);



            //IList<TvSeries> tvs = series as IList<TvSeries> ?? series.ToList();
            //if (tvs.Count == 1 && episode.Series.WmcName == tvs[0].TvDbName)
            //{
            //    tvSeries = tvs[0];
            //}
            //else if (tvs.Count >= 1)
            //{
            //    tvSeries = UserSelectSeries(episode, tvs);
            //}

            return(tvSeries);
        }
Esempio n. 12
0
        private async Task GetEpisodeData(string episodeUrl, WmcItem episode, DirectoryInfo workingDirectory)
        {
            string url = string.Format(episodeUrl, episode.Series.TvDbId);

            episode.Series.FolderName = CleanFolderName(episode.Series.TvDbName);
            string path = Path.Combine(workingDirectory.FullName, episode.Series.FolderName);

            DirectoryInfo di = new DirectoryInfo(path);

            if (!di.Exists)
            {
                di.Create();
            }

            FileInfo file = await DownloadEpisodeZip(url, di);

            if (file != null)
            {
                episode.Series.EpisodeDataFile = file.FullName;
            }
        }
Esempio n. 13
0
        private void RenameMovieFile(string destinationMovieFolder, WmcItem wmcItem, ILog logger)
        {
            string path = destinationMovieFolder;

            if (!Directory.Exists(destinationMovieFolder))
            {
                Directory.CreateDirectory(destinationMovieFolder);
            }

            string fileName = wmcItem.File.Name;
            int    index    = wmcItem.File.Name.IndexOf('_');

            if (index > 0)
            {
                fileName = fileName.Substring(0, index);
            }

            fileName = fileName + " (" + wmcItem.ReleaseYear + ")" + wmcItem.File.Extension;

            path = Path.Combine(path, fileName);

            MoveFile(wmcItem.File.FullName, path, logger);
        }
Esempio n. 14
0
        private IEnumerable <WmcItem> GetEpisodes(DirectoryInfo tvDirectory)
        {
            var folder = GetShell32NameSpaceFolder(tvDirectory.FullName);

            GetAttributeIndexes(folder, _attributeIndexes);

            var wmcItems = new List <WmcItem>();

            foreach (FolderItem2 item in folder.Items())
            {
                var attrValue = folder.GetDetailsOf(item, _attributeIndexes["File extension"]);
                if (attrValue.ToLower() == ".wtv")
                {
                    //for (int i = 0; i < 500; i++)
                    //{
                    //    string name = folder.GetDetailsOf(null, i);
                    //    string value = folder.GetDetailsOf(item, i);
                    //    if (name.Length > 0 || value.Length > 0)
                    //    {
                    //        _logger.Debug(name + ": " + value);
                    //    }
                    //}

                    var wmcItem = new WmcItem();

                    attrValue = folder.GetDetailsOf(item, _attributeIndexes["Path"]);
                    if (!string.IsNullOrWhiteSpace(attrValue) && File.Exists(attrValue))
                    {
                        wmcItem.File = new FileInfo(attrValue);
                    }

                    var title = folder.GetDetailsOf(item, _attributeIndexes["Title"]);

                    var epName = folder.GetDetailsOf(item, _attributeIndexes["Subtitle"]);

                    if (string.IsNullOrWhiteSpace(epName))
                    {
                        epName = folder.GetDetailsOf(item, _attributeIndexes["Episode name"]);
                        if (string.IsNullOrWhiteSpace(wmcItem.Title))
                        {
                            epName = string.Empty;
                        }
                    }

                    attrValue = folder.GetDetailsOf(item, _attributeIndexes["Broadcast date"]);
                    attrValue = CleanString(attrValue, (char)8206);
                    attrValue = CleanString(attrValue, (char)8207);
                    if (!string.IsNullOrWhiteSpace(attrValue))
                    {
                        if (DateTime.TryParse(attrValue, Program.EnUsCulture, DateTimeStyles.AdjustToUniversal, out var dt))
                        {
                            wmcItem.BroadcastDate = dt;
                        }
                    }

                    wmcItem.Protected = folder.GetDetailsOf(item, _attributeIndexes["Protected"]).ToLower() == "yes";

                    var genre   = folder.GetDetailsOf(item, _attributeIndexes["Genre"]);
                    var isMovie = genre.ToLower().Contains("movie");
                    attrValue = folder.GetDetailsOf(item, _attributeIndexes["Date released"]);
                    if (!int.TryParse(attrValue, out var year))
                    {
                        year = int.MinValue;
                    }

                    if (isMovie || epName == string.Empty && year > 1900)
                    {
                        wmcItem.ReleaseYear = year > 0 ? year : wmcItem.BroadcastDate.Year;

                        wmcItem.Type  = ItemType.Movie;
                        wmcItem.Title = title;
                    }
                    else
                    {
                        wmcItem.Type   = ItemType.Tv;
                        wmcItem.Series = new TvSeries {
                            WmcName = title
                        };
                        wmcItem.Title       = epName;
                        wmcItem.Description = folder.GetDetailsOf(item, _attributeIndexes["Program description"]);
                        wmcItem.IsReRun     = folder.GetDetailsOf(item, _attributeIndexes["Rerun"]).ToLower() == "yes";
                    }

                    wmcItems.Add(wmcItem);
                }
            }

            return(wmcItems);
        }
Esempio n. 15
0
        private void GetEpisodeDetails(WmcItem episode)
        {
            if (episode.Series.EpisodeDataFile != null)
            {
                XDocument xdoc = XDocument.Load(episode.Series.EpisodeDataFile);
                XElement  data = xdoc.Element("Data");

                int      epNum, seaNum;
                DateTime fa;

                IEnumerable <TvDbEpisode> tvEps = from eps in data.Elements("Episode")
                                                  where eps.Element("EpisodeName").Value == episode.Title
                                                  select new TvDbEpisode
                {
                    Name          = eps.Element("EpisodeName").Value,
                    EpisodeNumber = (int.TryParse(eps.Element("EpisodeNumber").Value, out epNum) ? epNum : 0),
                    SeasonNumber  = (int.TryParse(eps.Element("SeasonNumber").Value, out seaNum) ? seaNum : 0),
                    FirstAired    = (DateTime.TryParse(eps.Element("FirstAired").Value, Config.EnUkCulture, DateTimeStyles.AdjustToUniversal, out fa) ? fa : DateTime.MinValue)
                };



                if (!tvEps.Any())
                {
                    tvEps = from eps in data.Elements("Episode")
                            where eps.Element("Overview").Value == episode.Description
                            select new TvDbEpisode
                    {
                        Name          = eps.Element("EpisodeName").Value,
                        EpisodeNumber = (int.TryParse(eps.Element("EpisodeNumber").Value, out epNum) ? epNum : 0),
                        SeasonNumber  = (int.TryParse(eps.Element("SeasonNumber").Value, out seaNum) ? seaNum : 0),
                        FirstAired    = (DateTime.TryParse(eps.Element("FirstAired").Value, Config.EnUkCulture, DateTimeStyles.AdjustToUniversal, out fa) ? fa : DateTime.MinValue)
                    };
                }

                if (!tvEps.Any())
                {
                    tvEps = from eps in data.Elements("Episode")
                            where (DateTime.TryParse(eps.Element("FirstAired").Value, Config.EnUkCulture, DateTimeStyles.AdjustToUniversal, out fa) ? fa : DateTime.MinValue) == episode.BroadcastDate
                            select new TvDbEpisode
                    {
                        Name          = eps.Element("EpisodeName").Value,
                        EpisodeNumber = (int.TryParse(eps.Element("EpisodeNumber").Value, out epNum) ? epNum : 0),
                        SeasonNumber  = (int.TryParse(eps.Element("SeasonNumber").Value, out seaNum) ? seaNum : 0),
                        FirstAired    = (DateTime.TryParse(eps.Element("FirstAired").Value, Config.EnUkCulture, DateTimeStyles.AdjustToUniversal, out fa) ? fa : DateTime.MinValue)
                    };
                }

                IList <TvDbEpisode> tvDbEpisodes = tvEps as IList <TvDbEpisode> ?? tvEps.ToList();
                if (tvDbEpisodes.Count() == 1)
                {
                    episode.TvDbEpisode = tvDbEpisodes.First();
                }
                else if (tvDbEpisodes.Count() > 1)
                {
                    IEnumerable <TvDbEpisode> dateMatch = tvDbEpisodes.Where(ep => ep.FirstAired == episode.BroadcastDate);
                    if (dateMatch.Count() == 1)
                    {
                        episode.TvDbEpisode = dateMatch.First();
                    }
                    else
                    {
                        episode.TvDbEpisode = UserSelectEpisode(episode, tvDbEpisodes);
                    }
                }
            }
        }
Esempio n. 16
0
        public async Task ProcessEpisodes(List <WmcItem> wmcItems)
        {
            HashSet <string> updatedSeries = new HashSet <string>();

            _settings.TvDbLastUpdate = await GetLastDbUpdate(_updateUrl, _settings.TvDbLastUpdate, updatedSeries, _logger);

            await _settings.Save(_logger);

            foreach (WmcItem wmcItem in wmcItems)
            {
                if (!IgnoreWmcItem(wmcItem, _settings.IgnoreItems))
                {
                    if (wmcItem.Type == ItemType.Tv && !string.IsNullOrEmpty(wmcItem.Series.WmcName))
                    {
                        TvSeries series = FindKnownSeries(wmcItem, _settings.TvSeries);

                        if (series == null)
                        {
                            if (!UserIgnore(wmcItem, _settings.IgnoreItems))
                            {
                                series = await GetSeriesInfo(_seriesUrl, wmcItem, wmcItem.Series.WmcName, _logger);

                                if (series == null)
                                {
                                    Tuple <bool, TvSeries> cont = await UserSearchSeries(wmcItem, _seriesUrl, _logger);

                                    while (cont.Item1)
                                    {
                                        cont = await UserSearchSeries(wmcItem, _seriesUrl, _logger);
                                    }

                                    series = cont.Item2;
                                }

                                if (series != null)
                                {
                                    _settings.TvSeries.Add(series);
                                }
                            }

                            if (series != null)
                            {
                                wmcItem.Series = series;
                            }
                        }
                        else
                        {
                            wmcItem.Series = series;
                        }

                        if (wmcItem.Series.TvDbName != null)
                        {
                            wmcItem.Series.FolderName = CleanFolderName(wmcItem.Series.TvDbName);

                            Match match = _title.Match(wmcItem.Title);
                            if (match.Success)
                            {
                                wmcItem.TvDbEpisode = new TvDbEpisode
                                {
                                    SeasonNumber  = Convert.ToInt32(match.Groups[1].Value),
                                    EpisodeNumber = Convert.ToInt32(match.Groups[2].Value),
                                    Name          = match.Groups[3].Value
                                };
                            }
                        }
                    }
                    else if (wmcItem.Type == ItemType.Movie)
                    {
                        UserIgnore(wmcItem, _settings.IgnoreItems);
                    }
                }
            }

            // find the ones where we could not extract the season and episode from the title
            IEnumerable <IGrouping <string, WmcItem> > distinctSeries = wmcItems.Where(file => file.Type == ItemType.Tv && file.TvDbEpisode == null).GroupBy(file => file.Series.TvDbId);

            await distinctSeries.ForEachAsync(5, async (item) =>
            {
                WmcItem episode = item.FirstOrDefault();

                if (episode != null && episode.Series.TvDbName != null)
                {
                    if (!IsKnownSeries(episode.Series.TvDbId, _settings.TvSeries) || updatedSeries.Contains(episode.Series.TvDbId))
                    {
                        await GetEpisodeData(_episodeUrl, episode, _settings.WorkingDirectory);
                    }
                }
            });

            await _settings.Save(_logger);
        }
Esempio n. 17
0
        public async Task ProcessEpisodes(IEnumerable <WmcItem> wmcItems)
        {
            var items = wmcItems as WmcItem[] ?? wmcItems.ToArray();

            foreach (var wmcItem in items)
            {
                if (!IgnoreWmcItem(wmcItem))
                {
                    if (wmcItem.Type == ItemType.Tv && !string.IsNullOrEmpty(wmcItem.Series.WmcName))
                    {
                        var series = FindKnownSeries(wmcItem);

                        if (series == null)
                        {
                            if (!UserIgnore(wmcItem))
                            {
                                series = await GetSeriesInfo(wmcItem, wmcItem.Series.WmcName);

                                if (series == null)
                                {
                                    //var cont = await UserSearchSeries(wmcItem, _seriesUrl);
                                    //while (cont.Item1)
                                    //{
                                    //    cont = await UserSearchSeries(wmcItem, _seriesUrl);
                                    //}

                                    //series = cont.Item2;
                                }

                                if (series != null)
                                {
                                    _settings.TvSeries.Add(series);
                                }
                            }

                            if (series != null)
                            {
                                wmcItem.Series = series;
                            }
                        }
                        else
                        {
                            wmcItem.Series = series;
                        }

                        if (wmcItem.Series.TvDbName != null)
                        {
                            var match = _seasonEpisode.Match(wmcItem.Title);
                            if (match.Success)
                            {
                                wmcItem.TvDbEpisode = new TvDbEpisode
                                {
                                    SeasonNumber  = Convert.ToInt32(match.Groups[1].Value),
                                    EpisodeNumber = Convert.ToInt32(match.Groups[2].Value),
                                    Name          = match.Groups[3].Value
                                };
                            }
                        }
                    }
                    else if (wmcItem.Type == ItemType.Movie)
                    {
                        UserIgnore(wmcItem);
                    }
                }
            }

            // find the ones where we could not extract the season and episode from the title
            var distinctSeries = items.Where(file => file.Type == ItemType.Tv && file.TvDbEpisode == null)
                                 .GroupBy(file => file.Series.TvDbId);

            foreach (var series in distinctSeries)
            {
                WmcItem episode = series.FirstOrDefault();

                if (episode?.Series.TvDbName != null)
                {
                    if (!IsKnownSeries(episode.Series.TvDbId))
                    {
                        //await GetEpisodeData(episode, _settings.WorkingDirectory);
                    }
                }
            }


            await _settings.Save();
        }