Exemplo n.º 1
0
        public void Finds_Steins_Gate()
        {
            var tvdb = new TVDBRequest();
            var results = tvdb.Search("Steins;Gate");

            Assert.NotEmpty(results);
        }
        public static TVDBSearchResult Search(string name)
        {
            SyncInfo info;
            lock(searching) {
                if(searching.TryGetValue(name, out info)) {
                    lock(info.Lock) {
                        return info.Result;
                    }
                } else {
                    searching.Add(name, info = new SyncInfo {
                        Lock = new object()
                    });
                }
            }

            lock(info.Lock) {
                var results = new TVDBRequest().Search(name);
                info.Result = results.FirstOrDefault();
                return info.Result;
            }
        }
        void CheckUnmonitoredFile(string fileName)
        {
            Logger.Debug("File is not monitored: " + fileName);
            var mon = new MonitoredFile {
                FileName = fileName,
                Start = DateTime.Now
            };

            using(var info = new MediaInfo(fileName)) {
                mon.Length = info.Length;
            }

            var name = Path.GetFileName(fileName);
            var match = new TvMatcher().Match(name);
            if(match != null) {
                Logger.Debug("Found episode info: " + match.ToString());

                mon.TvMatch = match;

                // Try and look it up
                // TODO: movies
                Series series;
                using(var db = new EpisodeTrackerDBContext()) {
                    series = db.Series.SingleOrDefault(s => s.Name == match.Name || s.Aliases.Any(a => a.Name == match.Name));

                    int? tvdbid = null;
                    if(series == null) {
                        var results = new TVDBRequest().Search(match.Name);
                        var first = results.FirstOrDefault();
                        if(first != null) {
                            Logger.Debug("Found TVDB result: " + first.Name);
                            series = db.Series.SingleOrDefault(s => s.TVDBID == first.ID || s.Name == first.Name || s.Aliases.Any(a => a.Name == first.Name));
                            tvdbid = first.ID;
                        }
                    }else {
                        tvdbid = series.TVDBID;
                    }

                    if(tvdbid.HasValue) {
                        if(series == null || series.Updated <= DateTime.Now.AddDays(-7)) {
                            var syncer = new TVDBSeriesSyncer {
                                TVDBID = tvdbid.Value,
                                Name = match.Name,
                                DownloadBannersAsync = true
                            };
                            syncer.Sync();
                        }

                        // Pull out series again as it might have been updated
                        series = db.Series
                            .Include(s => s.Episodes)
                            .Single(s => s.TVDBID == tvdbid.Value);

                        mon.Series = series;

                        if(match.Season.HasValue) {
                            var eps = series.Episodes.Where(ep => ep.Season == match.Season.Value);
                            if(match.ToEpisode.HasValue) {
                                mon.Episodes = eps.Where(ep => ep.Number >= match.Episode && ep.Number <= match.ToEpisode.Value);
                            } else {
                                mon.Episodes = eps.Where(ep => ep.Number == match.Episode);
                            }
                        } else {
                            mon.Episodes = series.Episodes.Where(ep => ep.AbsoluteNumber == match.Episode);
                        }

                        if(mon.Episodes != null) {
                            Logger.Debug("Found TVDB episodes: " + String.Join(" + ", mon.Episodes.Select(e => e.Name)));
                        }
                    }
                }
            }

            monitored.Add(mon);
        }
Exemplo n.º 4
0
        private void Sync(SyncInfo syncInfo)
        {
            if(syncInfo.Complete) return;

            var tvdbSeries = new TVDBRequest().Series(TVDBID, true);
            Series series = null;

            using(var db = new EpisodeTrackerDBContext()) {
                var seriesQuery = db.Series
                    .Include(s => s.Episodes)
                    .Include(s => s.Aliases);

                series = seriesQuery.SingleOrDefault(s => s.TVDBID == tvdbSeries.ID);

                if(series == null) {
                    series = seriesQuery
                        .SingleOrDefault(s =>
                            s.Name == tvdbSeries.Name
                            || s.Name == Name
                            || s.Aliases.Any(a => a.Name == Name)
                        );
                }

                if(series == null) {
                    series = new Series {
                        Added = DateTime.Now
                    };
                    db.Series.Add(series);
                }

                series.TVDBID = tvdbSeries.ID;
                series.Name = tvdbSeries.Name;
                series.AirsDay = tvdbSeries.AirsDay;
                series.AirsTime = tvdbSeries.AirsTime;
                series.Status = tvdbSeries.Status;
                series.Overview = tvdbSeries.Overview;
                series.LengthMinutes = tvdbSeries.LengthMinutes;
                series.Rating = tvdbSeries.Rating;

                if(Name != null && !Name.Equals(series.Name, StringComparison.OrdinalIgnoreCase) && !db.SeriesAliases.Any(a => a.Name == Name)) {
                    series.Aliases.Add(new SeriesAlias { Name = Name });
                }

                series.Genres.Clear();
                GenresSync(series, tvdbSeries.Genres);

                series.Updated = DateTime.Now;

                foreach(var ep in tvdbSeries.Episodes) {
                    SyncEpisode(ep, series);
                }

                db.SaveChanges();
            }

            if(DownloadBanners || DownloadBannersAsync) {
                // Do this after saving so we can use the ID
                var syncBanners = SyncBannersAsync(series, tvdbSeries);
                if(!DownloadBannersAsync) syncBanners.Wait();
            }

            lock(syncing) {
                syncing.Remove(TVDBID);
            }

            syncInfo.Complete = true;
        }