private void taskListTimerCallback(object state)
 {
     try {
         FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
         bgProc.Action = FitActions.ProcessTaskList;
         MovingPicturesCore.ProcessManager.StartProcess(bgProc);
     }
     catch (Exception ex) {
         logger.ErrorException("", ex);
         _follwitAPI = null;
         MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
         return;
     }
 }
        private bool UploadMovieInfo(List<DBMovieInfo> movies, ProgressDelegate progress)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not upload movie info to follw.it because service is offline");
                return false;
            }

            try {

                List<FitMovie> fitMovies = new List<FitMovie>();

                int count = 0;
                foreach (var movie in movies) {
                    count++;
                    FitMovie fitMovie = FollwitConnector.MovieToFitMovie(movie);
                    if (fitMovie.Resources.Length > 1) {
                        logger.Debug("Adding '{0}' to list of movies to be synced.", movie.Title);
                        fitMovies.Add(fitMovie);
                    }
                    else {
                        logger.Debug("Skipping '{0}' because it doesn't have source information.", movie.Title);
                    }

                    if (progress != null)
                        progress("Syncing All Movies to follw.it", (int)(count * 100 / movies.Count));

                    if (fitMovies.Count >= MovingPicturesCore.Settings.FollwitBatchSize || count == movies.Count) {
                        logger.Debug("Sending batch of {0} movies", fitMovies.Count);
                        MovingPicturesCore.Follwit.FollwitApi.AddMoviesToCollection(ref fitMovies);

                        // update fitId on the DBMovieInfo object
                        foreach (FitMovie fitMovieDTO in fitMovies) {
                            DBMovieInfo m = DBMovieInfo.Get(fitMovieDTO.InternalId);
                            if (m != null) {
                                m.FitId = fitMovieDTO.MovieId;
                                if (fitMovieDTO.UserRating > 0)
                                    m.ActiveUserSettings.UserRating = fitMovieDTO.UserRating;
                                if (fitMovieDTO.Watched && m.ActiveUserSettings.WatchedCount == 0)
                                    m.ActiveUserSettings.WatchedCount = 1;

                                m.Commit();
                            }
                        }

                        fitMovies.Clear();
                    }
                }
            }
            catch (WebException ex) {
                logger.Error("There was a problem connecting to the follw.it Server! " + ex.Message);
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.CONNECTION_ERROR;
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error uploading movie information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }
        private void movieDeletedListener(DatabaseTable obj)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                // if this is not a movie object, break
                if (obj.GetType() != typeof(DBMovieInfo))
                    return;

                if (!IsOnline) {
                    logger.Warn("Can not remove movie from follw.it collection because service is offline");
                    return;
                }

                DBMovieInfo movie = (DBMovieInfo)obj;

                List<DBMovieInfo> allMovies = DBMovieInfo.GetAll();

                int fitIdMovieCount = (from m in allMovies
                                       where m.FitId == movie.FitId
                                       select m).Count();

                if (fitIdMovieCount == 0)
                {
                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.RemoveMovieFromCollection;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }

            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error removing an object from your follw.it collection!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        private bool RemoveFilteredMovies(List<DBMovieInfo> movies, ProgressDelegate progress)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not remove movies from follw.it collection because service is offline");
                return false;
            }

            try {
                int count = 0;
                foreach (var movie in movies) {
                    count++;
                    if (movie.FitId != null && movie.FitId != 0) {
                        logger.Debug("Removing '{0}' from follw.it because it has been excluded by a filter.", movie.Title);
                        FollwitApi.RemoveMovieFromCollection((int)movie.FitId);
                        movie.FitId = null;
                    }

                    if (progress != null)
                        progress("Removing excluded movies from follw.it", (int)(count * 100 / movies.Count));
                }
            }
            catch (WebException ex) {
                logger.Error("There was a problem connecting to the follw.it Server! " + ex.Message);
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.CONNECTION_ERROR;
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error removing movies from your follw.it collection!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }
        private void DatabaseManager_ObjectUpdated(DatabaseTable obj, TableUpdateInfo updateInfo)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                // we're looking for user rating changes
                if (obj.GetType() != typeof(DBUserMovieSettings))
                    return;

                DBUserMovieSettings settings = (DBUserMovieSettings)obj;
                if (updateInfo.RatingChanged()) {
                    if (!IsOnline) {
                        logger.Warn("Can not send rating info to follw.it because service is offline");
                        return;
                    }

                    DBMovieInfo movie = settings.AttachedMovies[0];

                    if (currentlySyncingMovies.Contains(movie))
                        return;

                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.UpdateUserRating;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }

            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending rating information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        private void Init()
        {
            _follwitAPI = null;
            lastConnectAttempt = new DateTime(1900, 1, 1);

            if (MovingPicturesCore.Settings.FollwitEnabled) {
                if (!receivingEvents) {
                    MovingPicturesCore.DatabaseManager.ObjectDeleted += new DatabaseManager.ObjectAffectedDelegate(movieDeletedListener);
                    MovingPicturesCore.DatabaseManager.ObjectUpdatedEx += new DatabaseManager.ObjectUpdatedDelegate(DatabaseManager_ObjectUpdated);
                    MovingPicturesCore.DatabaseManager.ObjectInserted += new DatabaseManager.ObjectAffectedDelegate(DatabaseManager_ObjectInserted);
                    receivingEvents = true;
                }
                if (MovingPicturesCore.Settings.FollwitTaskListTimer > 0) {
                    taskListTimer = new Timer(taskListTimerCallback, null, 0, MovingPicturesCore.Settings.FollwitTaskListTimer * 60000);
                }
            }
            else {
                if (taskListTimer != null) taskListTimer.Dispose();
                MovingPicturesCore.DatabaseManager.ObjectDeleted -= new DatabaseManager.ObjectAffectedDelegate(movieDeletedListener);
                MovingPicturesCore.DatabaseManager.ObjectUpdatedEx -= new DatabaseManager.ObjectUpdatedDelegate(DatabaseManager_ObjectUpdated);
                MovingPicturesCore.DatabaseManager.ObjectInserted -= new DatabaseManager.ObjectAffectedDelegate(DatabaseManager_ObjectInserted);
                receivingEvents = false;
            }
        }
        private void DatabaseManager_ObjectInserted(DatabaseTable obj)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                if (obj.GetType() == typeof(DBWatchedHistory)) {
                    DBWatchedHistory wh = (DBWatchedHistory)obj;
                    DBMovieInfo movie = wh.Movie;

                    WatchMovie(movie, true);
                }
                else if (obj.GetType() == typeof(DBMovieInfo)) {
                    DBMovieInfo movie = (DBMovieInfo)obj;
                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.AddMoviesToCollection;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error connecting to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        public bool WatchMovie(DBMovieInfo movie, bool includeInStream)
        {
            if (currentlySyncingMovies.Contains(movie))
                return true;

            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not send movie watched count to follw.it because service is offline");
                return false;
            }

            try {
                FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                bgProc.Action = includeInStream ? FitActions.WatchMovie : FitActions.WatchMovieIgnoreStream;
                bgProc.Movies.Add(movie);
                MovingPicturesCore.ProcessManager.StartProcess(bgProc);
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending 'movie watched' information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }
        public void Synchronize(ProgressDelegate progress)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            if (!IsOnline) {
                logger.Warn("Can not synchonize to follw.it because service is offline");
                return;
            }

            try {
                logger.Info("Synchronizing with follw.it.");
                List<DBMovieInfo> moviesToSynch;
                List<DBMovieInfo> moviesToExclude;

                if (MovingPicturesCore.Settings.RestrictSynchronizedMovies) {
                    moviesToSynch = new List<DBMovieInfo>();
                    moviesToExclude = new List<DBMovieInfo>();

                    moviesToSynch.AddRange(MovingPicturesCore.Settings.FollwitSyncFilter.Filter(DBMovieInfo.GetAll()));
                    moviesToExclude.AddRange(DBMovieInfo.GetAll().Except(moviesToSynch));

                    logger.Debug("Using synchronization filter. Syncing {0} movies. Excluding {1} movies.", moviesToSynch.Count, moviesToExclude.Count);
                }
                else {
                    moviesToSynch = DBMovieInfo.GetAll();
                    moviesToExclude = new List<DBMovieInfo>();
                    logger.Debug("Syncing {0} movies.", moviesToSynch.Count);
                }

                moviesToSynch = moviesToSynch.OrderBy(m => m.DateAdded).ToList();

                UploadMovieInfo(moviesToSynch, progress);
                RemoveFilteredMovies(moviesToExclude, progress);
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error synchronizing with follw.it!", ex);
                _follwitAPI = null;
                Status = StatusEnum.INTERNAL_ERROR;
                return;
            }

            return;
        }
        public bool CurrentlyWatching(DBMovieInfo movie, bool isWatching)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not send movie watched count to follw.it because service is offline");
                return false;
            }

            try {
                if (MovingPicturesCore.Settings.RestrictSynchronizedMovies) {
                    var filtered = MovingPicturesCore.Settings.FollwitSyncFilter.Filter(new List<DBMovieInfo>() {movie});
                    if (filtered.Count == 0)
                        return false;
                }

                FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                bgProc.Action = isWatching ? FitActions.BeginWatching : FitActions.EndWatching;
                bgProc.Movies.Add(movie);
                MovingPicturesCore.ProcessManager.StartProcess(bgProc);
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending 'now watching' information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }
示例#11
0
        /// <summary>
        /// Attempts to login with the given username and password.
        /// </summary>
        /// <returns>A FollwitAPI object if login is successful.</returns>
        public static FollwitApi Login(string username, string hashedPassword, string apiUrl)
        {
            if (apiUrl == null) apiUrl = DefaultUrl;

            IFollwitProxy proxy = CreateProxy(username, hashedPassword, apiUrl);
            try { object result = proxy.CheckAuthentication(); }
            catch (XmlRpcServerException ex) {
                if (ex.Message == "Unauthorized") return null;
                else throw;
            }

            FollwitApi api = new FollwitApi(username, hashedPassword, apiUrl, proxy);
            api.GetUserData();

            proxy.RequestEvent += new XmlRpcRequestEventHandler(api.proxy_RequestEvent);
            proxy.ResponseEvent += new XmlRpcResponseEventHandler(api.proxy_ResponseEvent);

            return api;
        }