예제 #1
0
        // Tries to match the videoinfo with the corresponding show or movie
        // If it finds a single result, and matches the tipe of videoinfo data
        // modifies the program and returns true. Otherwise returns false.
        // Stores the search for subsequent actions.
        public static bool setOriginalTitle(ref EVideoInfo info)
        {
            IEnumerable <TraktSearchResult> search;

            if (QueryOriginalTitle(info.Title, out search))
            {
                IEnumerator <TraktSearchResult> p = search.GetEnumerator();
                if (p.MoveNext())
                {
                    if ((p.Current.Type.Equals("show")) & (info.Type == VideoType.Series))
                    {
                        //overrideCurrentProgram(p.Current.Show);
                        info.Title = p.Current.Show.Title;
                        info.Year  = p.Current.Show.Year.ToString();
                        return(true);
                    }
                    if ((p.Current.Type.Equals("movie")) & (info.Type == VideoType.Movie))
                    {
                        //overrideCurrentProgram(p.Current.Movie);
                        info.Title = p.Current.Movie.Title;
                        info.Year  = p.Current.Movie.Year.ToString();
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Gets the current program
        /// </summary>
        /// <returns></returns>
        private EVideoInfo GetCurrentProgram()
        {
            EVideoInfo videoInfo = new EVideoInfo();

            // get current program details
            Program program = TVHome.Navigator.Channel.CurrentProgram;

            if (program == null || string.IsNullOrEmpty(program.Title))
            {
                TraktLogger.Info("Unable to get current program from database");
                return(null);
            }
            else
            {
                string title = null;
                string year  = null;
                BasicHandler.GetTitleAndYear(program.Title, out title, out year);

                videoInfo = new EVideoInfo
                {
                    Type       = !string.IsNullOrEmpty(program.EpisodeNum) || !string.IsNullOrEmpty(program.SeriesNum) ? VideoType.Series : VideoType.Movie,
                    Title      = title,
                    Year       = year,
                    SeasonIdx  = program.SeriesNum,
                    EpisodeIdx = program.EpisodeNum,
                    StartTime  = program.StartTime,
                    Runtime    = GetRuntime(program),
                };
                videoInfo.setVariables();
                TraktLogger.Info("Current program details. Title='{0}', Year='{1}', Season='{2}', Episode='{3}', StartTime='{4}', Runtime='{5}', isScrobbling='{6}'", videoInfo.Title, videoInfo.Year.ToLogString(), videoInfo.SeasonIdx.ToLogString(), videoInfo.EpisodeIdx.ToLogString(), videoInfo.StartTime == null ? "<empty>" : videoInfo.StartTime.ToString(), videoInfo.Runtime, videoInfo.IsScrobbling);
            }
            return(videoInfo);
        }
예제 #3
0
 public static void clearCurrentProgram()
 {
     lock (currentProgram)
     {
         currentProgram = null;
     }
 }
예제 #4
0
        public static TraktScrobbleMovie GetOriginalMovieTitle(EVideoInfo info, double playerprogress)
        {
            IEnumerable <TraktSearchResult> movieMatch = TraktAPI.TraktAPI.SearchByName(info.Title, "movie", "Title");
            IEnumerator <TraktSearchResult> p          = movieMatch.GetEnumerator();

            if (!(p.MoveNext()))
            {
#if DEBUG
                TraktLogger.Info("No result found for '{0}'", info.Title);
#endif
                return(null);
            }
#if DEBUG
            TraktLogger.Info("Fetching informations for '{0}'. Matched to '{1}'", info.Title, p.Current.Movie.Title);
#endif
            var scrobbleData = new TraktScrobbleMovie
            {
                Movie = new TraktMovie
                {
                    Ids   = new TraktMovieId(),
                    Title = p.Current.Movie.Title.ToString(),
                    Year  = info.Year.ToNullableInt32()
                },
                Progress   = playerprogress,
                AppDate    = TraktSettings.BuildDate,
                AppVersion = TraktSettings.Version
            };

            return(scrobbleData);
        }
예제 #5
0
 public static void overrideVideoInfoProgram(ref EVideoInfo info, TraktMovieSummary originalMovie)
 {
     {
         info.Title      = originalMovie.Title;
         info.EpisodeIdx = null;
         info.SeasonIdx  = null;
         info.Year       = originalMovie.Year.ToString();
         info.Type       = VideoType.Movie;
     }
 }
예제 #6
0
 public static void overrideVideoInfoProgram(ref EVideoInfo info, TraktShowSummary originalShow, TraktEpisode episode = null)
 {
     info.Title = originalShow.Title;
     info.Year  = originalShow.Year.ToString();
     info.Type  = VideoType.Series;
     if (episode != null)
     {
         info.EpisodeIdx = episode.Number.ToString();
         info.SeasonIdx  = episode.Season.ToString();
     }
 }
예제 #7
0
        /// <summary>
        /// Checks if the current program is considered watched
        /// </summary>
        private bool IsProgramWatched(EVideoInfo program)
        {
            // check if we have watched atleast 80% of the program
            // this wont be an exact calculation +- 5mins due to the scrobble timer
            double durationPlayed = DateTime.Now.Subtract(program.StartTime).TotalMinutes;
            double percentPlayed  = 0.0;

            if (program.Runtime > 0.0)
            {
                percentPlayed = durationPlayed / program.Runtime;
            }

            return(percentPlayed >= 0.8);
        }
예제 #8
0
        internal static TraktScrobbleResponse StartScrobbleMovie(EVideoInfo videoInfo)
        {
            // get scrobble data to send to api
            var scrobbleData = CreateMovieScrobbleData(videoInfo);

            if (scrobbleData == null)
            {
                return(null);
            }

            var response = TraktAPI.TraktAPI.StartMovieScrobble(scrobbleData);

            TraktLogger.LogTraktResponse(response);
            return(response);
        }
예제 #9
0
        internal static TraktScrobbleResponse StartScrobbleEpisode(EVideoInfo videoInfo)
        {
            // get scrobble data to send to api
            var scrobbleData = CreateEpisodeScrobbleData(videoInfo);

            if (scrobbleData == null)
            {
                return(null);
            }
            TraktLogger.Info("Trying to scrobble '{0}' season {1} episode {2}", scrobbleData.Show.Title, scrobbleData.Episode.Season, scrobbleData.Episode.Number);
            var response = TraktAPI.TraktAPI.StartEpisodeScrobble(scrobbleData);

            TraktLogger.LogTraktResponse(response);
            return(response);
        }
예제 #10
0
        internal static TraktScrobbleMovie CreateMovieScrobbleData(EVideoInfo info)
        {
            //create scrobble data
            var scrobbleData = new TraktScrobbleMovie
            {
                Movie = new TraktMovie
                {
                    Ids   = new TraktMovieId(),
                    Title = info.Title,
                    Year  = info.Year.ToNullableInt32()
                },
                Progress   = GetPlayerProgress(info),
                AppDate    = TraktSettings.BuildDate,
                AppVersion = TraktSettings.Version
            };

            return(scrobbleData);
        }
예제 #11
0
        internal static double GetPlayerProgress(EVideoInfo videoInfo)
        {
            //get duration/ position in minutes
            double duration = videoInfo.Runtime > 0.0 ? videoInfo.Runtime : g_Player.Duration / 60;
            double position = g_Player.CurrentPosition / 60;
            double progress = 0.0;

            if (duration > 0.0)
            {
                progress = (position / duration) * 100.0;
            }

            if (progress > 100)
            {
                progress = 100;
            }

            return(Math.Round(progress, 2));
        }
예제 #12
0
        public static void setCurrentProgram(EVideoInfo videoInfoIn)
        {
            if (currentProgram == null)
            {
                currentProgram = videoInfoIn;
            }
            else
            {
                lock (currentProgram)
                {
#if DEBUG
                    TraktLogger.Info("Current Program is a {0}: '{1}' and its scrobbling status is {2}", currentProgram.Type, currentProgram.Title, currentProgram.IsScrobbling);
#endif
                    currentProgram = null;
                    currentProgram = videoInfoIn;
#if DEBUG
                    TraktLogger.Info("New program set is a {0}: '{1}' and its scrobbling status is {2}", currentProgram.Type, currentProgram.Title, currentProgram.IsScrobbling);
#endif
                }
            }
        }
예제 #13
0
        internal static TraktScrobbleEpisode CreateEpisodeScrobbleData(EVideoInfo info)
        {
            // create scrobble data
            var scrobbleData = new TraktScrobbleEpisode
            {
                Show = new TraktShow
                {
                    Ids   = new TraktShowId(),
                    Title = info.Title,
                    Year  = info.Year.ToNullableInt32()
                },
                Episode = new TraktEpisode
                {
                    Ids    = new TraktEpisodeId(),
                    Number = info.EpisodeIdx.ToInt(),
                    Season = info.SeasonIdx.ToInt()
                },
                Progress   = GetPlayerProgress(info),
                AppDate    = TraktSettings.BuildDate,
                AppVersion = TraktSettings.Version
            };

            return(scrobbleData);
        }
예제 #14
0
        public bool Scrobble(string filename)
        {
            StopScrobble();
            istStoppingScrobble = false;

            if (!g_Player.IsTV)
            {
                return(false);
            }

            EbasicHandler.setCurrentProgram(GetCurrentProgram());

            if (EbasicHandler.getCurrentProgram() == null)
            {
                return(false);
            }
            EbasicHandler.SetCurrentProgramIsScrobbling(true);

            TVJustTurnedOn = true;
            if (EbasicHandler.getCurrentProgramType() == VideoType.Series)
            {
                TraktLogger.Info("Detected tv show playing on Live TV. Title = '{0}'", EbasicHandler.getCurrentProgram().Title.ToString());
            }
            else
            {
                TraktLogger.Info("Detected movie playing on Live TV. Title = '{0}'", EbasicHandler.getCurrentProgram().Title.ToString());
            }

            #region Scrobble Timer

            TraktTimer = new Timer(new TimerCallback((stateInfo) =>
            {
                Thread.CurrentThread.Name = "Scrobble";

                // get the current program airing on tv now
                // this may have changed since last status update on trakt
                EVideoInfo videoInfo = GetCurrentProgram();

                //Reinit all variables
                item     = new TraktEPGCacheRecord();
                response = new TraktScrobbleResponse();

                // I have problems with GUI rendering most of the times. If i set the thread to sleep it really helps with GUI rendering.
                // This might also work well to handle zapping correctly.
                Thread.Sleep((TraktSettings.ETVScrobbleDelay) * 1000);

                try
                {
                    if (videoInfo != null)
                    {
                        // if we are watching something different,
                        // check if we should mark previous as watched
                        //if (!videoInfo.Equals(CurrentProgram))

                        if (!videoInfo.Equals(EbasicHandler.getCurrentProgram()))
                        {
                            TraktLogger.Info("Detected new tv program has started. Previous Program = '{0}', New Program = '{1}'", EbasicHandler.getCurrentProgram().ToString(), videoInfo.ToString());
                            //The new program has changed. I should check if the active window is GUIShowSelect and eventually close it.
                            if (GUIWindowManager.ActiveWindow.Equals((int)TraktGUIWindows.EPGShowSelect))
                            {
                                GUIShowSelectGUI.exitGUI();
                            }
                            if (IsProgramWatched(EbasicHandler.getCurrentProgram()) && EbasicHandler.IsCurrentProgramScrobbling())
                            {
                                TraktLogger.Info("Playback of program on Live TV is considered watched. Title = '{0}'", EbasicHandler.getCurrentProgram().ToString());
                                BasicHandler.StopScrobble(EbasicHandler.getCurrentProgram(), true);
                            }
                            //The programs are different so we should start the whole scrobbling process.
                            //For that we set the new videoInfo scrobbling status to true
                            //later we're checking if videoInfo is scrobbling, and it should be false if we don't set it true here.
                            videoInfo.IsScrobbling = true;
                            //EbasicHandler.SetCurrentProgramIsScrobbling(true);
                        }

                        // continue watching new program
                        // dont try to scrobble if previous attempt failed
                        // If the current program is scrobbling, according to the APIARY there's no need to resend every 15 minutes,
                        // it will expire after runtime has elapsed.

                        if ((videoInfo.IsScrobbling) | (TVJustTurnedOn))
                        {
                            TVJustTurnedOn = false;
                            //Starts the scrobble of the new program because it changed
                            //cache should search here because some shows have no name and could be in cache.

                            #region CACHE CHECK and SCROBBLE from CACHE
                            if (EPGCache.searchOnCache(videoInfo.Title, out item))
                            {
                                if (item.Type.Equals("nullRecord"))
                                {
                                    response.Code        = 901;
                                    response.Description = "Manually set don't scrobble";
                                }
                                else if ((item.Type.Equals("movie")) & (videoInfo.Type == VideoType.Movie))
                                {
                                    EbasicHandler.overrideVideoInfoProgram(ref videoInfo, item.Movie);
                                    response = EbasicHandler.StartScrobbleMovie(videoInfo);
                                }
                                else if ((item.Type.Equals("show")) & (videoInfo.Type == VideoType.Series))
                                {
                                    EbasicHandler.overrideVideoInfoProgram(ref videoInfo, item.Show);
                                    response = EbasicHandler.StartScrobbleEpisode(videoInfo);
                                }
                                else
                                //The item type is different from the videoinfo type. Cache is authoritative here so overriding videoInfo with the cache.
                                //It's most likely that a show is detected wrong as a movie, because a movie with episode and season number is totally nonsense, but handles both.
                                {
                                    response.Code = 904; //This code will be used later.
                                    if (item.Type.Equals("show"))
                                    {
                                        EbasicHandler.overrideVideoInfoProgram(ref videoInfo, item.Show);
                                        response.Description = item.Show.Ids.Slug;
                                    }
                                    else
                                    {
                                        EbasicHandler.overrideVideoInfoProgram(ref videoInfo, item.Movie);
                                    }
                                }
                            }// end cache thingy if it was successful it should be scrobbled.
                            #endregion
                            #region CACHE MATCH UNSUCCESSFUL: TRY RETRIEVE ORIGINAL LANGUAGE AND SCROBBLE
                            else
                            {
                                #region FOUND ORIGINAL LANGUAGE TITLE WITH A SINGLE MATCH
                                // Try first to scrobble without changing anything. This is to avoid problems with shows that uses
                                // real non translated shows. In this case the name of the show usually is enough to succesfully scrobble.
                                if (videoInfo.Type == VideoType.Series)
                                {
                                    response = EbasicHandler.StartScrobbleEpisode(videoInfo);
                                }
                                else
                                {
                                    response = EbasicHandler.StartScrobbleMovie(videoInfo);
                                }
                                if (!(response.Code == 0) && EbasicHandler.setOriginalTitle(ref videoInfo))
                                {
                                    if ((videoInfo.Type == VideoType.Series))
                                    {
                                        response = EbasicHandler.StartScrobbleEpisode(videoInfo);
                                    }
                                    else
                                    {
                                        response = EbasicHandler.StartScrobbleMovie(videoInfo);
                                    }
                                }
                                #endregion
                                #region NO CACHE, NO ORIGINAL LANGUAGE SINGLE MATCH FOUND everything is very likely to fail.
                                else if (!response.Code.Equals(0))
                                {
                                    response.Code        = 404;
                                    response.Description = "Not Found";
                                }
                                #endregion
                            }
                            #endregion
                            EbasicHandler.setCurrentProgram(videoInfo);
                            if (response.Code.Equals(901))
                            {
                                TraktLogger.Info("Program {0} skipped because manually marked to skip on cache", videoInfo.Title);
                            }
                            else if (response.Code.Equals(0))
                            {
                                if (TraktSettings.AllowPopUPOnSuccessfulScrobbling)
                                {
                                    GUIDialogNotify notification = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
                                    notification.Reset();
                                    notification.SetHeading(string.Format("{0} scrobbled!", EbasicHandler.getCurrentProgram().Type.ToString()));
                                    notification.SetText(string.Format("Scrobbling '{0}' as '{1}'", EbasicHandler.getCurrentProgram().getOriginalTitle(), EbasicHandler.getCurrentProgram().Title));
                                    notification.SetImage(Path.Combine(Config.GetFolder(Config.Dir.Skin), string.Format(@"{0}\Media\Logos\trakt.png", Config.SkinName)));
                                    notification.DoModal(GUIWindowManager.ActiveWindow);
                                }
                            }
                            else // the response was bad! Everything gone worse than ever, again open the GUI
                            {
                                GUIDialogYesNo askManualSelection = (GUIDialogYesNo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_YES_NO);
                                if (askManualSelection != null)
                                {
                                    askManualSelection.Reset();
                                    askManualSelection.SetHeading("Start manual matching?");
                                    askManualSelection.SetLine(1, string.Format("Cannot find a match for '{0}'", videoInfo.Title));
                                    askManualSelection.SetDefaultToYes(true);
                                    askManualSelection.DoModal(GUIWindowManager.ActiveWindow);
                                    if (askManualSelection.IsConfirmed)
                                    {
                                        if (!istStoppingScrobble) //maybe we can handle this better. If the user stops the program right before this the plugin crashes.
                                        {
                                            EbasicHandler.StartGui(response.Code, item, TraktSettings.GUIAsDialog);
                                        }
                                    }
                                    else
                                    {
                                        // Need to handle cancel button with a null on cache to avoid future scrobbling.
                                        EPGCache.addOnCache(videoInfo.Title);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (NullReferenceException exception)
                {
                    //handle the worst. This usually happens when the user stops the player during a dialog.
                    //We should at least log this in the mediaportal error.
                }
            }), null, 1000, 300000);

            #endregion
            return(true);
        }
예제 #15
0
 public void setCurrentProgram(EVideoInfo program)
 {
     EbasicHandler.setCurrentProgram(program);
 }