/// <summary>
        /// Reports to trakt.tv that an episode is being watched. Or that Episode(s) have been watched.
        /// </summary>
        /// <param name="episode">The episode being watched</param>
        /// <param name="status">Enum indicating whether an episode is being watched or scrobbled</param>
        /// <param name="traktUser">The user that's watching the episode</param>
        /// <param name="progressPercent"></param>
        /// <returns>A List of standard TraktResponse Data Contracts</returns>
        public async Task<List<TraktScrobbleResponse>> SendEpisodeStatusUpdateAsync(Episode episode, MediaStatus status, TraktUser traktUser, float progressPercent)
        {
            var episodeDatas = new List<TraktScrobbleEpisode>();

            if ((episode.IndexNumberEnd == null || episode.IndexNumberEnd == episode.IndexNumber) &&
                !string.IsNullOrEmpty(episode.GetProviderId(MetadataProviders.Tvdb)))
            {
                episodeDatas.Add(new TraktScrobbleEpisode
                {
                    AppDate = DateTime.Today.ToString("yyyy-MM-dd"),
                    AppVersion = _appHost.ApplicationVersion.ToString(),
                    Progress = progressPercent,
                    Episode = new TraktEpisode
                    {
                        Ids = new TraktEpisodeId
                        {
                            Tvdb = episode.GetProviderId(MetadataProviders.Tvdb).ConvertToInt()
                        },
                    }
                });
            }
            // It's a multi-episode file. Add all episodes
            else if (episode.IndexNumber.HasValue)
            {
                episodeDatas.AddRange(Enumerable.Range(episode.IndexNumber.Value,
                    ((episode.IndexNumberEnd ?? episode.IndexNumber).Value -
                     episode.IndexNumber.Value) + 1)
                    .Select(number => new TraktScrobbleEpisode
                    {
                        AppDate = DateTime.Today.ToString("yyyy-MM-dd"),
                        AppVersion = _appHost.ApplicationVersion.ToString(),
                        Progress = progressPercent,
                        Episode = new TraktEpisode
                        {
                            Season = episode.GetSeasonNumber(),
                            Number = number
                        },
                        Show = new TraktShow
                        {
                            Title = episode.Series.Name,
                            Year = episode.Series.ProductionYear,
                            Ids = new TraktShowId
                            {
                                Tvdb = episode.Series.GetProviderId(MetadataProviders.Tvdb).ConvertToInt(),
                                Imdb = episode.Series.GetProviderId(MetadataProviders.Imdb),
                                TvRage = episode.Series.GetProviderId(MetadataProviders.TvRage).ConvertToInt()
                            }
                        }
                    }).ToList());
            }

            string url;
            switch (status)
            {
                case MediaStatus.Watching:
                    url = TraktUris.ScrobbleStart;
                    break;
                case MediaStatus.Paused:
                    url = TraktUris.ScrobblePause;
                    break;
                default:
                    url = TraktUris.ScrobbleStop;
                    break;
            }
            var responses = new List<TraktScrobbleResponse>();
            foreach (var traktScrobbleEpisode in episodeDatas)
            {
                var response = await PostToTrakt(url, traktScrobbleEpisode, CancellationToken.None, traktUser);
                responses.Add(_jsonSerializer.DeserializeFromStream<TraktScrobbleResponse>(response));
            }
            return responses;
        }
Beispiel #2
0
        /// <summary>
        /// Reports to trakt.tv that an episode is being watched. Or that Episode(s) have been watched.
        /// </summary>
        /// <param name="episode">The episode being watched</param>
        /// <param name="status">Enum indicating whether an episode is being watched or scrobbled</param>
        /// <param name="traktUser">The user that's watching the episode</param>
        /// <param name="progressPercent"></param>
        /// <returns>A List of standard TraktResponse Data Contracts</returns>
        public async Task<List<TraktScrobbleResponse>> SendEpisodeStatusUpdateAsync(Episode episode, MediaStatus status, TraktUser traktUser, float progressPercent)
        {
            var episodeDatas = new List<TraktScrobbleEpisode>();
            var tvDbId = episode.GetProviderId(MetadataProviders.Tvdb);

            if (!string.IsNullOrEmpty(tvDbId) && (!episode.IndexNumber.HasValue || !episode.IndexNumberEnd.HasValue || episode.IndexNumberEnd <= episode.IndexNumber))
            {
                episodeDatas.Add(new TraktScrobbleEpisode
                {
                    AppDate = DateTime.Today.ToString("yyyy-MM-dd"),
                    AppVersion = _appHost.ApplicationVersion.ToString(),
                    Progress = progressPercent,
                    Episode = new TraktEpisode
                    {
                        Ids = new TraktEpisodeId
                        {
                            Tvdb = tvDbId.ConvertToInt()
                        },
                    }
                });
            }
            else if (episode.IndexNumber.HasValue)
            {
                var indexNumber = episode.IndexNumber.Value;
                var finalNumber = (episode.IndexNumberEnd ?? episode.IndexNumber).Value;

                for (var number = indexNumber; number <= finalNumber; number++)
                {
                    episodeDatas.Add(new TraktScrobbleEpisode
                    {
                        AppDate = DateTime.Today.ToString("yyyy-MM-dd"),
                        AppVersion = _appHost.ApplicationVersion.ToString(),
                        Progress = progressPercent,
                        Episode = new TraktEpisode
                        {
                            Season = episode.GetSeasonNumber(),
                            Number = number
                        },
                        Show = new TraktShow
                        {
                            Title = episode.Series.Name,
                            Year = episode.Series.ProductionYear,
                            Ids = new TraktShowId
                            {
                                Tvdb = episode.Series.GetProviderId(MetadataProviders.Tvdb).ConvertToInt(),
                                Imdb = episode.Series.GetProviderId(MetadataProviders.Imdb),
                                TvRage = episode.Series.GetProviderId(MetadataProviders.TvRage).ConvertToInt()
                            }
                        }
                    });
                }
            }

            string url;
            switch (status)
            {
                case MediaStatus.Watching:
                    url = TraktUris.ScrobbleStart;
                    break;
                case MediaStatus.Paused:
                    url = TraktUris.ScrobblePause;
                    break;
                default:
                    url = TraktUris.ScrobbleStop;
                    break;
            }
            var responses = new List<TraktScrobbleResponse>();
            foreach (var traktScrobbleEpisode in episodeDatas)
            {
                using (var response = await PostToTrakt(url, traktScrobbleEpisode, CancellationToken.None, traktUser))
                {
                    responses.Add(_jsonSerializer.DeserializeFromStream<TraktScrobbleResponse>(response));
                }
            }
            return responses;
        }