/// <summary> /// Event gets triggered on playback events in OnlineVideos /// The TrackVideoPlayback event gets fired on Playback Start, Playback Ended /// and Playback Stopped (if percentage watched is greater than 0.8). /// </summary> private void TrackVideoPlayback(ITrackingInfo info, double percentPlayed) { if (info.VideoKind == VideoKind.Movie || info.VideoKind == VideoKind.TvSeries) { // Started Playback // Bug in OnlineVideos 0.31 reports incorrect percentage if (percentPlayed > 1.0) { percentPlayed = 1 / percentPlayed; } if (percentPlayed < 0.8) { currentVideo = info; return; } // Show Rating Dialog ShowRateDialog(info); // Playback Ended or Stopped and Considered Watched // TrackVideoPlayback event only gets fired on Stopped if > 80% watched TraktLogger.Info("Playback of '{0}' is considered watched at {1:0.00}%", info.Title, (percentPlayed * 100).ToString()); Thread scrobbleThread = new Thread(delegate(object o) { ITrackingInfo videoInfo = o as ITrackingInfo; // duration in minutes double duration = g_Player.Duration / 60; double progress = 100.0; TraktEpisodeScrobble scrobbleEpisodeData = null; TraktMovieScrobble scrobbleMovieData = null; TraktResponse response = null; if (videoInfo.VideoKind == VideoKind.TvSeries) { scrobbleEpisodeData = CreateEpisodeScrobbleData(videoInfo); if (scrobbleEpisodeData == null) { return; } scrobbleEpisodeData.Duration = Convert.ToInt32(duration).ToString(); scrobbleEpisodeData.Progress = Convert.ToInt32(progress).ToString(); response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleEpisodeData, TraktScrobbleStates.scrobble); } else { scrobbleMovieData = CreateMovieScrobbleData(videoInfo); if (scrobbleMovieData == null) { return; } scrobbleMovieData.Duration = Convert.ToInt32(duration).ToString(); scrobbleMovieData.Progress = Convert.ToInt32(progress).ToString(); response = TraktAPI.TraktAPI.ScrobbleMovieState(scrobbleMovieData, TraktScrobbleStates.scrobble); } TraktAPI.TraktAPI.LogTraktResponse(response); }) { IsBackground = true, Name = "Scrobble" }; scrobbleThread.Start(info); } }
public void StopScrobble() { if (TraktTimer != null) { TraktTimer.Dispose(); } if (CurrentEpisode == null) { return; } #region Scrobble Thread scrobbleEpisode = new Thread(delegate(object o) { FileLocal episode = o as FileLocal; if (episode == null) { return; } TraktLogger.Info("My Anime episode considered watched '{0}'", episode.ToString()); // get scrobble data to send to api TraktEpisodeScrobble scrobbleData = CreateScrobbleData(episode); if (scrobbleData == null) { return; } // set duration/progress in scrobble data scrobbleData.Duration = Convert.ToInt32(g_Player.Duration / 60).ToString(); scrobbleData.Progress = "100"; TraktResponse response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleData, TraktScrobbleStates.scrobble); TraktAPI.TraktAPI.LogTraktResponse(response); }) { IsBackground = true, Name = "Scrobble" }; #endregion // if episode is atleast 90% complete, consider watched if ((g_Player.CurrentPosition / g_Player.Duration) >= 0.9) { ShowRateDialog(CurrentEpisode); scrobbleEpisode.Start(CurrentEpisode); } else { #region Cancel Watching TraktLogger.Info("Stopped My Anime episode playback '{0}'", CurrentEpisode.ToString()); // stop scrobbling Thread cancelWatching = new Thread(delegate() { TraktEpisodeScrobble scrobbleData = new TraktEpisodeScrobble { UserName = TraktSettings.Username, Password = TraktSettings.Password }; TraktResponse response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleData, TraktScrobbleStates.cancelwatching); TraktAPI.TraktAPI.LogTraktResponse(response); }) { IsBackground = true, Name = "CancelWatching" }; #endregion cancelWatching.Start(); } CurrentEpisode = null; }
public bool Scrobble(string filename) { StopScrobble(); // stop check if not valid player type for plugin handler if (g_Player.IsTV || g_Player.IsTVRecording) { return(false); } // lookup episode by filename List <FileLocal> files = FileLocal.GetAll(); FileLocal file = files.FirstOrDefault(f => f.FileNameFull == filename); if (file == null) { return(false); } CurrentEpisode = file; TraktLogger.Info("Detected episode playing in My Anime: '{0}'", CurrentEpisode.ToString()); // create 15 minute timer to send watching status #region scrobble timer TraktTimer = new Timer(new TimerCallback((stateInfo) => { Thread.CurrentThread.Name = "Scrobble"; FileLocal episode = stateInfo as FileLocal; if (episode == null) { return; } // duration in minutes double duration = g_Player.Duration / 60; double progress = 0.0; // get current progress of player (in seconds) to work out percent complete if (g_Player.Duration > 0.0) { progress = (g_Player.CurrentPosition / g_Player.Duration) * 100.0; } TraktEpisodeScrobble scrobbleData = CreateScrobbleData(CurrentEpisode); if (scrobbleData == null) { return; } // set duration/progress in scrobble data scrobbleData.Duration = Convert.ToInt32(duration).ToString(); scrobbleData.Progress = Convert.ToInt32(progress).ToString(); // set watching status on trakt TraktResponse response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleData, TraktScrobbleStates.watching); TraktAPI.TraktAPI.LogTraktResponse(response); }), CurrentEpisode, 3000, 900000); #endregion return(true); }
public void StopScrobble() { if (TraktTimer != null) { TraktTimer.Dispose(); } if (CurrentRecording == null) { return; } // get current progress of player double progress = 0.0; if (g_Player.Duration > 0.0) { progress = (g_Player.CurrentPosition / g_Player.Duration) * 100.0; } TraktLogger.Debug("Current Position: {0}, Duration: {1}", g_Player.CurrentPosition.ToString(), g_Player.Duration.ToString()); TraktLogger.Debug(string.Format("Percentage of '{0}' watched is {1}%", CurrentRecording.Title, progress > 100.0 ? "100" : progress.ToString("N2"))); // if recording is at least 80% complete, consider watched // consider watched with invalid progress as well, we should never be exactly 0.0 if ((progress == 0.0 || progress >= 80.0) && CurrentRecording.IsScrobbling) { // Show rate dialog ShowRateDialog(CurrentRecording); #region scrobble Thread scrobbleRecording = new Thread(delegate(object obj) { VideoInfo videoInfo = obj as VideoInfo; if (videoInfo == null) { return; } TraktLogger.Info("Playback of '{0}' in Argus tv-recording is considered watched.", videoInfo.ToString()); if (videoInfo.Type == VideoType.Series) { BasicHandler.ScrobbleEpisode(videoInfo, TraktScrobbleStates.scrobble); } else { BasicHandler.ScrobbleMovie(videoInfo, TraktScrobbleStates.scrobble); } }) { IsBackground = true, Name = "Scrobble" }; scrobbleRecording.Start(CurrentRecording); #endregion } else { #region cancel watching TraktLogger.Info("Stopped playback of Argus tv-recording '{0}'", CurrentRecording.ToString()); Thread cancelWatching = new Thread(delegate(object obj) { VideoInfo videoInfo = obj as VideoInfo; if (videoInfo == null) { return; } if (videoInfo.Type == VideoType.Series) { TraktEpisodeScrobble scrobbleData = new TraktEpisodeScrobble { UserName = TraktSettings.Username, Password = TraktSettings.Password }; TraktResponse response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleData, TraktScrobbleStates.cancelwatching); TraktLogger.LogTraktResponse(response); } else { TraktMovieScrobble scrobbleData = new TraktMovieScrobble { UserName = TraktSettings.Username, Password = TraktSettings.Password }; TraktResponse response = TraktAPI.TraktAPI.ScrobbleMovieState(scrobbleData, TraktScrobbleStates.cancelwatching); TraktLogger.LogTraktResponse(response); } }) { IsBackground = true, Name = "CancelWatching" }; cancelWatching.Start(CurrentRecording); #endregion } CurrentRecording = null; }
/// <summary> /// Creates Scrobble data based on a DBMovieInfo object /// </summary> /// <param name="psc"></param> /// <param name="pc">PlayerContext</param> /// <param name="starting"></param> /// <param name="scrobbleData"></param> /// <param name="state"></param> /// <returns>The Trakt scrobble data to send</returns> private bool TryCreateScrobbleData(IPlayerSlotController psc, IPlayerContext pc, bool starting, out AbstractScrobble scrobbleData, out TraktScrobbleStates state) { scrobbleData = null; state = starting ? TraktScrobbleStates.watching : TraktScrobbleStates.scrobble; if (_settings.Settings.Authentication == null) { return(false); } string username = _settings.Settings.Authentication.Username; string password = _settings.Settings.Authentication.Password; if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return(false); } // For canceling the watching, it is to have no TraktMovieScrobble. if (pc.CurrentMediaItem == null) { if (starting) { return(false); } state = TraktScrobbleStates.cancelwatching; return(true); } bool isMovie = pc.CurrentMediaItem.Aspects.ContainsKey(MovieAspect.ASPECT_ID); bool isSeries = pc.CurrentMediaItem.Aspects.ContainsKey(SeriesAspect.ASPECT_ID); if (!isMovie && !isSeries) { return(false); } string title = pc.CurrentPlayer != null ? pc.CurrentPlayer.MediaItemTitle : null; IMediaPlaybackControl pmc = pc.CurrentPlayer as IMediaPlaybackControl; TimeSpan currentPosition; if (pmc != null) { _progressUpdateWorks[psc].Duration = pmc.Duration; currentPosition = pmc.CurrentTime; } else { // Player is already removed on stopping, so take the resume position if available currentPosition = _progressUpdateWorks[psc].ResumePosition; } int progress = currentPosition == TimeSpan.Zero ? (starting ? 0 : 100) : Math.Min((int)(currentPosition.TotalSeconds * 100 / _progressUpdateWorks[psc].Duration.TotalSeconds), 100); string value; int iValue; DateTime dtValue; long lValue; if (isMovie) { TraktMovieScrobble movie = new TraktMovieScrobble(); if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, MovieAspect.ATTR_IMDB_ID, out value) && !string.IsNullOrWhiteSpace(value)) { movie.IMDBID = value; } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, MovieAspect.ATTR_TMDB_ID, out iValue) && iValue > 0) { movie.TMDBID = iValue.ToString(); } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, MediaAspect.ATTR_RECORDINGTIME, out dtValue)) { movie.Year = dtValue.Year.ToString(); } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, MovieAspect.ATTR_RUNTIME_M, out iValue) && iValue > 0) { movie.Duration = iValue.ToString(); } scrobbleData = movie; } if (isSeries) { TraktEpisodeScrobble series = new TraktEpisodeScrobble(); if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_IMDB_ID, out value) && !string.IsNullOrWhiteSpace(value)) { series.IMDBID = value; } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_TVDB_ID, out iValue)) { series.TVDBID = iValue.ToString(); } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_SERIESNAME, out value) && !string.IsNullOrWhiteSpace(value)) { series.Title = value; } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_FIRSTAIRED, out dtValue)) { series.Year = dtValue.Year.ToString(); } if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_SEASON, out iValue)) { series.Season = iValue.ToString(); } List <int> intList; if (MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, SeriesAspect.ATTR_EPISODE, out intList) && intList.Any()) { series.Episode = intList.First().ToString(); // TODO: multi episode files?! } scrobbleData = series; } // Fallback duration info if (string.IsNullOrWhiteSpace(scrobbleData.Duration) && MediaItemAspect.TryGetAttribute(pc.CurrentMediaItem.Aspects, VideoAspect.ATTR_DURATION, out lValue) && lValue > 0) { scrobbleData.Duration = (lValue / 60).ToString(); } if (string.IsNullOrWhiteSpace(scrobbleData.Title)) { scrobbleData.Title = title; } scrobbleData.Progress = progress.ToString(); if (!starting && progress < WATCHED_PERCENT) { state = TraktScrobbleStates.cancelwatching; } scrobbleData.PluginVersion = TraktSettings.Version; scrobbleData.MediaCenter = "MediaPortal 2"; scrobbleData.MediaCenterVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); scrobbleData.MediaCenterBuildDate = String.Empty; scrobbleData.Username = username; scrobbleData.Password = password; return(true); }
public bool Scrobble(string filename) { if (currentVideo == null) { return(false); } if (currentVideo.VideoKind == VideoKind.TvSeries) { TraktLogger.Info("Detected tv series '{0} - {1}x{2}' playing in OnlineVideos", currentVideo.Title, currentVideo.Season.ToString(), currentVideo.Episode.ToString()); } else { TraktLogger.Info("Detected movie '{0}' playing in OnlineVideos", currentVideo.Title); } #region scrobble timer TraktTimer = new Timer(new TimerCallback((stateInfo) => { Thread.CurrentThread.Name = "Scrobble"; ITrackingInfo videoInfo = stateInfo as ITrackingInfo; // get duration in minutes double duration = g_Player.Duration / 60; double progress = 0.0; // get current progress of player if (g_Player.Duration > 0.0) { progress = (g_Player.CurrentPosition / g_Player.Duration) * 100.0; } TraktEpisodeScrobble scrobbleEpisodeData = null; TraktMovieScrobble scrobbleMovieData = null; TraktResponse response = null; if (videoInfo.VideoKind == VideoKind.TvSeries) { scrobbleEpisodeData = CreateEpisodeScrobbleData(videoInfo); if (scrobbleEpisodeData == null) { return; } scrobbleEpisodeData.Duration = Convert.ToInt32(duration).ToString(); scrobbleEpisodeData.Progress = Convert.ToInt32(progress).ToString(); response = TraktAPI.TraktAPI.ScrobbleEpisodeState(scrobbleEpisodeData, TraktScrobbleStates.watching); } else { scrobbleMovieData = CreateMovieScrobbleData(videoInfo); if (scrobbleMovieData == null) { return; } scrobbleMovieData.Duration = Convert.ToInt32(duration).ToString(); scrobbleMovieData.Progress = Convert.ToInt32(progress).ToString(); response = TraktAPI.TraktAPI.ScrobbleMovieState(scrobbleMovieData, TraktScrobbleStates.watching); } TraktLogger.LogTraktResponse(response); }), currentVideo, 3000, 900000); #endregion return(true); }