Esempio n. 1
0
        /// <summary>
        /// Identifies the name of the show.
        /// </summary>
        /// <param name="name">The name of the show.</param>
        /// <param name="ep">The episode.</param>
        /// <param name="askRemote">if set to <c>true</c> lab.rolisoft.net's API will be asked to identify a show after the local database failed.</param>
        /// <returns>
        /// A tuple containing the show's and episode's title and airdate.
        /// </returns>
        private static Tuple<string, string, DateTime, TVShow, Episode> IdentifyShow(string name, ShowEpisode ep, bool askRemote = false)
        {
            var title = string.Empty;
            var date  = DateTime.MinValue;
            var match = false;
            var ltvsh = default(TVShow);
            var lepis = default(Episode);

            // try to find show in local database

            foreach (var show in Database.TVShows.Values.ToList())
            {
                var titleMatch   = ShowNames.Parser.GenerateTitleRegex(show.Name).Match(name);
                var releaseMatch = !string.IsNullOrWhiteSpace(show.Release) ? Regex.Match(name, show.Release) : null;

                if ((titleMatch.Success && titleMatch.Value == name) || (releaseMatch != null && releaseMatch.Success && releaseMatch.Value == name))
                {
                    if (ep == null)
                    {
                        match = true;
                        ltvsh = show;
                        name  = show.Name;

                        break;
                    }
                    else if (ep.AirDate != null)
                    {
                        var episode = show.Episodes.Where(x => x.Airdate.ToOriginalTimeZone(x.Show.TimeZone).Date == ep.AirDate.Value.Date).ToList();
                        if (episode.Count != 0)
                        {
                            match = true;
                            ltvsh = show;
                            name  = show.Name;
                            lepis = episode[0];
                            title = episode[0].Name;
                            date  = episode[0].Airdate;

                            ep.Season  = episode[0].Season;
                            ep.Episode = episode[0].Number;

                            break;
                        }
                    }
                    else
                    {
                        Episode episode;
                        if (show.EpisodeByID.TryGetValue(ep.Season * 1000 + ep.Episode, out episode))
                        {
                            match = true;
                            ltvsh = show;
                            name  = show.Name;
                            lepis = episode;
                            title = episode.Name;
                            date  = episode.Airdate;

                            break;
                        }
                    }
                }
            }

            // try to find show in the local cache of the list over at lab.rolisoft.net

            if (!match)
            {
                if (AllKnownTVShows.Count == 0)
                {
                    var path = Path.Combine(Signature.InstallPath, @"misc\tvshows");

                    if (File.Exists(path) && new FileInfo(path).Length != 0)
                    {
                        using (var fs = File.OpenRead(path))
                        using (var br = new BinaryReader(fs))
                        {
                            var ver = br.ReadByte();
                            var upd = br.ReadUInt32();
                            var cnt = br.ReadUInt32();

                            AllKnownTVShows = new List<KnownTVShow>();

                            for (var i = 0; i < cnt; i++)
                            {
                                var show = new KnownTVShow();

                                show.Title      = br.ReadString();
                                show.Slug       = br.ReadString();
                                show.Database   = br.ReadString();
                                show.DatabaseID = br.ReadString();

                                AllKnownTVShows.Add(show);
                            }
                        }
                    }
                    else
                    {
                        try { GetAllKnownTVShows(); } catch { }
                    }
                }

                var slug    = Utils.CreateSlug(name);
                var matches = new List<KnownTVShow>();

                foreach (var show in AllKnownTVShows)
                {
                    if (show.Slug == slug)
                    {
                        matches.Add(show);
                    }
                }

                if (matches.Count != 0 && ep == null)
                {
                    match = true;
                    name  = matches[0].Title;
                }
                else if (matches.Count != 0 && ep != null)
                {
                    TVShow local = null;

                    foreach (var mtch in matches)
                    {
                        foreach (var show in Database.TVShows.Values)
                        {
                            if (show.Source == mtch.Database && show.SourceID == mtch.DatabaseID)
                            {
                                local = show;
                                break;
                            }
                        }
                    }

                    if (local != null)
                    {
                        match = true;
                        name  = local.Name;

                        if (ep.AirDate != null)
                        {
                            var eps = local.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                ltvsh = eps[0].Show;
                                title = eps[0].Name;
                                lepis = eps[0];
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = local.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                ltvsh = eps[0].Show;
                                title = eps[0].Name;
                                lepis = eps[0];
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else if (ShowIDCache.ContainsKey(name) && TVShowCache.ContainsKey(name))
                    {
                        match = true;
                        name  = ShowIDCache[name].Title;

                        if (ep.AirDate != null)
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else if (askRemote)
                    {
                        var guide = Updater.CreateGuide(matches[0].Database);
                        var data  = guide.GetData(matches[0].DatabaseID);

                        ShowIDCache[name] = new ShowID { Title = data.Title };

                        match = true;
                        name  = data.Title;

                        TVShowCache[name] = data;

                        if (ep.AirDate != null)
                        {
                            var eps = data.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = data.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                }
            }

            // try to find show in cache

            if (!match && ShowIDCache.ContainsKey(name))
            {
                match = true;
                name  = ShowIDCache[name].Title;

                if (ep != null)
                {
                    if (TVShowCache.ContainsKey(name))
                    {
                        if (ep.AirDate != null)
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else
                    {
                        match = false;
                    }
                }
            }

            // try to identify show using lab.rolisoft.net's API

            if (!match && askRemote)
            {
                var req = Remote.API.GetShowInfo(name, new[] { "Title", "Source", "SourceID" });

                if (req.Success)
                {
                    if (ep == null)
                    {
                        ShowIDCache[name] = new ShowID { Title = req.Title };

                        match = true;
                        name  = req.Title;
                    }
                    else
                    {
                        var guide = Updater.CreateGuide(req.Source);
                        var data  = guide.GetData(req.SourceID);

                        ShowIDCache[name] = new ShowID { Title = data.Title };

                        match = true;
                        name  = data.Title;

                        TVShowCache[name] = data;

                        if (ep.AirDate != null)
                        {
                            var eps = data.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = data.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                }
            }

            // return

            return match
                   ? new Tuple<string, string, DateTime, TVShow, Episode>(name, title, date, ltvsh, lepis)
                   : null;
        }
Esempio n. 2
0
        /// <summary>
        /// Identifies the name of the show.
        /// </summary>
        /// <param name="name">The name of the show.</param>
        /// <param name="ep">The episode.</param>
        /// <param name="askRemote">if set to <c>true</c> lab.rolisoft.net's API will be asked to identify a show after the local database failed.</param>
        /// <returns>
        /// A tuple containing the show's and episode's title and airdate.
        /// </returns>
        private static Tuple <string, string, DateTime, TVShow, Episode> IdentifyShow(string name, ShowEpisode ep, bool askRemote = false)
        {
            var title = string.Empty;
            var date  = DateTime.MinValue;
            var match = false;
            var ltvsh = default(TVShow);
            var lepis = default(Episode);

            // try to find show in local database

            foreach (var show in Database.TVShows.Values.ToList())
            {
                var titleMatch   = ShowNames.Parser.GenerateTitleRegex(show.Title, show).Match(name);
                var releaseRegex = string.Empty;
                var releaseMatch = show.Data.TryGetValue("regex", out releaseRegex) && !string.IsNullOrWhiteSpace(releaseRegex) ? Regex.Match(name, releaseRegex) : null;

                if ((titleMatch.Success && titleMatch.Value == name) || (releaseMatch != null && releaseMatch.Success && releaseMatch.Value == name))
                {
                    if (ep == null)
                    {
                        match = true;
                        ltvsh = show;
                        name  = show.Title;

                        break;
                    }
                    else if (ep.AirDate != null)
                    {
                        var episode = show.Episodes.Where(x => x.Airdate.ToOriginalTimeZone(x.Show.TimeZone).Date == ep.AirDate.Value.Date).ToList();
                        if (episode.Count != 0)
                        {
                            match = true;
                            ltvsh = show;
                            name  = show.Title;
                            lepis = episode[0];
                            title = episode[0].Title;
                            date  = episode[0].Airdate;

                            ep.Season  = episode[0].Season;
                            ep.Episode = episode[0].Number;

                            break;
                        }
                    }
                    else
                    {
                        Episode episode;
                        if (show.EpisodeByID.TryGetValue(ep.Season * 1000 + ep.Episode, out episode))
                        {
                            match = true;
                            ltvsh = show;
                            name  = show.Title;
                            lepis = episode;
                            title = episode.Title;
                            date  = episode.Airdate;

                            break;
                        }
                    }
                }
            }

            // try to find show in the local cache of the list over at lab.rolisoft.net

            if (!match)
            {
                if (AllKnownTVShows.Count == 0)
                {
                    var path = Path.Combine(Signature.InstallPath, @"misc\tvshows");

                    if (File.Exists(path) && new FileInfo(path).Length != 0)
                    {
                        using (var fs = File.OpenRead(path))
                            using (var br = new BinaryReader(fs))
                            {
                                var ver = br.ReadByte();
                                var upd = br.ReadUInt32();
                                var cnt = br.ReadUInt32();

                                AllKnownTVShows = new List <KnownTVShow>();

                                for (var i = 0; i < cnt; i++)
                                {
                                    var show = new KnownTVShow();

                                    show.Title      = br.ReadString();
                                    show.Slug       = br.ReadString();
                                    show.Database   = br.ReadString();
                                    show.DatabaseID = br.ReadString();

                                    AllKnownTVShows.Add(show);
                                }
                            }
                    }
                    else
                    {
                        try { GetAllKnownTVShows(); } catch { }
                    }
                }

                var slug    = Utils.CreateSlug(name);
                var matches = new List <KnownTVShow>();

                foreach (var show in AllKnownTVShows)
                {
                    if (show.Slug == slug)
                    {
                        matches.Add(show);
                    }
                }

                if (matches.Count != 0 && ep == null)
                {
                    match = true;
                    name  = matches[0].Title;
                }
                else if (matches.Count != 0 && ep != null)
                {
                    TVShow local = null;

                    foreach (var mtch in matches)
                    {
                        foreach (var show in Database.TVShows.Values)
                        {
                            if (show.Source == mtch.Database && show.SourceID == mtch.DatabaseID)
                            {
                                local = show;
                                break;
                            }
                        }
                    }

                    if (local != null)
                    {
                        match = true;
                        name  = local.Title;

                        if (ep.AirDate != null)
                        {
                            var eps = local.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                ltvsh = eps[0].Show;
                                title = eps[0].Title;
                                lepis = eps[0];
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = local.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                ltvsh = eps[0].Show;
                                title = eps[0].Title;
                                lepis = eps[0];
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else if (ShowIDCache.ContainsKey(name) && TVShowCache.ContainsKey(name))
                    {
                        match = true;
                        name  = ShowIDCache[name].Title;

                        if (ep.AirDate != null)
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else if (askRemote)
                    {
                        var guide = Updater.CreateGuide(matches[0].Database);
                        var data  = guide.GetData(matches[0].DatabaseID);

                        ShowIDCache[name] = new ShowID {
                            Title = data.Title
                        };

                        match = true;
                        name  = data.Title;

                        TVShowCache[name] = data;

                        if (ep.AirDate != null)
                        {
                            var eps = data.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = data.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                }
            }

            // try to find show in cache

            if (!match && ShowIDCache.ContainsKey(name))
            {
                match = true;
                name  = ShowIDCache[name].Title;

                if (ep != null)
                {
                    if (TVShowCache.ContainsKey(name))
                    {
                        if (ep.AirDate != null)
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = TVShowCache[name].Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                    else
                    {
                        match = false;
                    }
                }
            }

            // try to identify show using lab.rolisoft.net's API

            if (!match && askRemote)
            {
                var req = Remote.API.GetShowInfo(name, new[] { "Title", "Source", "SourceID" });

                if (req.Success)
                {
                    if (ep == null)
                    {
                        ShowIDCache[name] = new ShowID {
                            Title = req.Title
                        };

                        match = true;
                        name  = req.Title;
                    }
                    else
                    {
                        var guide = Updater.CreateGuide(req.Source);
                        var data  = guide.GetData(req.SourceID);

                        ShowIDCache[name] = new ShowID {
                            Title = data.Title
                        };

                        match = true;
                        name  = data.Title;

                        TVShowCache[name] = data;

                        if (ep.AirDate != null)
                        {
                            var eps = data.Episodes.Where(ch => ch.Airdate.Date == ep.AirDate.Value.Date).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;

                                ep.Season  = eps[0].Season;
                                ep.Episode = eps[0].Number;
                            }
                        }
                        else
                        {
                            var eps = data.Episodes.Where(ch => ch.Season == ep.Season && ch.Number == ep.Episode).ToList();
                            if (eps.Count() != 0)
                            {
                                title = eps[0].Title;
                                date  = eps[0].Airdate;
                            }
                        }
                    }
                }
            }

            // return

            return(match
                   ? new Tuple <string, string, DateTime, TVShow, Episode>(name, title, date, ltvsh, lepis)
                   : null);
        }