Exemple #1
0
        public ActionResult RegenerateAllEpisodeLinks()
        {
            try
            {
                using (var upd = Repo.Instance.CrossRef_AniDB_Provider.BeginBatchUpdate(() => Repo.Instance.CrossRef_AniDB_Provider.GetByType(Shoko.Models.Enums.CrossRefType.TvDB)))
                {
                    foreach (SVR_CrossRef_AniDB_Provider p in upd)
                    {
                        p.EpisodesList.DeleteAllUnverifiedLinks();
                        if (p.EpisodesList.NeedPersitance)
                        {
                            p.EpisodesList.Persist();
                            upd.Update(p);
                        }
                    }
                    upd.Commit();
                }
                Repo.Instance.AnimeSeries.GetAll().ToList().AsParallel().ForAll(animeseries => LinkingHelper.GenerateEpisodeMatches(animeseries.AniDB_ID, Shoko.Models.Enums.CrossRefType.TvDB, true));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(APIStatus.InternalError(e.Message));
            }

            return(APIStatus.OK());
        }
Exemple #2
0
        /// <summary>
        /// Test Database Connection with Current Settings
        /// </summary>
        /// <returns>200 if connection successful, 400 otherwise</returns>
        private object TestDatabaseConnection()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            if (ServerSettings.DatabaseType.Equals(Constants.DatabaseType.MySQL,
                                                   StringComparison.InvariantCultureIgnoreCase) && new MySQL().TestConnection())
            {
                return(APIStatus.OK());
            }

            if (ServerSettings.DatabaseType.Equals(Constants.DatabaseType.SqlServer,
                                                   StringComparison.InvariantCultureIgnoreCase) && new SQLServer().TestConnection())
            {
                return(APIStatus.OK());
            }

            if (ServerSettings.DatabaseType.Equals(Constants.DatabaseType.Sqlite,
                                                   StringComparison.InvariantCultureIgnoreCase))
            {
                return(APIStatus.OK());
            }

            return(APIStatus.BadRequest("Failed to Connect"));
        }
Exemple #3
0
        public ActionResult ImportConfig(CL_ServerSettings settings)
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            string raw_settings = settings.ToJSON();

            if (raw_settings.Length == new CL_ServerSettings().ToJSON().Length)
            {
                return(APIStatus.BadRequest("Empty settings are not allowed"));
            }

            string path = Path.Combine(ServerSettings.ApplicationPath, "temp.json");

            System.IO.File.WriteAllText(path, raw_settings, Encoding.UTF8);
            try
            {
                ServerSettings.LoadSettingsFromFile(path, true);
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError("Error while importing settings"));
            }
        }
Exemple #4
0
        /// <summary>
        /// Upload Watched States to MAL
        /// </summary>
        /// <returns></returns>
        private object UploadToMAL()
        {
            CommandRequest_MALUploadStatusToMAL cmd = new CommandRequest_MALUploadStatusToMAL();

            cmd.Save();
            return(APIStatus.OK());
        }
Exemple #5
0
        public ActionResult TestAniDB()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            ShokoService.AnidbProcessor.ForceLogout();
            ShokoService.AnidbProcessor.CloseConnections();

            Thread.Sleep(1000);

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Instance.Culture);

            ShokoService.AnidbProcessor.Init(ServerSettings.Instance.AniDb.Username, ServerSettings.Instance.AniDb.Password,
                                             ServerSettings.Instance.AniDb.ServerAddress,
                                             ServerSettings.Instance.AniDb.ServerPort, ServerSettings.Instance.AniDb.ClientPort);

            if (!ShokoService.AnidbProcessor.Login())
            {
                return(APIStatus.Unauthorized());
            }
            ShokoService.AnidbProcessor.ForceLogout();

            return(APIStatus.OK());
        }
Exemple #6
0
        /// <summary>
        /// Download Watched States from MAL
        /// </summary>
        /// <returns></returns>
        private object DownloadFromMAL()
        {
            CommandRequest_MALDownloadStatusFromMAL cmd = new CommandRequest_MALDownloadStatusFromMAL();

            cmd.Save();
            return(APIStatus.OK());
        }
Exemple #7
0
        object WebhookPost()
        {
            PlexEvent eventData = JsonConvert.DeserializeObject <PlexEvent>(this.Context.Request.Form.payload,
                                                                            new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            switch (eventData.Event)
            {
            case "media.scrobble":
                Scrobble(eventData);
                break;

            case "media.resume":
            case "media.play":
                TraktScrobble(eventData, ScrobblePlayingStatus.Start);
                break;

            case "media.pause":
                TraktScrobble(eventData, ScrobblePlayingStatus.Pause);
                break;

            case "media.stop":
                TraktScrobble(eventData, ScrobblePlayingStatus.Stop);
                break;
            }

            return(APIStatus.OK());
        }
Exemple #8
0
        public ActionResult ImportConfig(CL_ServerSettings settings)
        {
            return(BadRequest("The model that this method takes is deprecated and will break the settings file. Use APIv3"));

            string raw_settings = settings.ToJSON();

            if (raw_settings.Length == new CL_ServerSettings().ToJSON().Length)
            {
                return(APIStatus.BadRequest("Empty settings are not allowed"));
            }

            string path = Path.Combine(ServerSettings.ApplicationPath, "temp.json");

            System.IO.File.WriteAllText(path, raw_settings, Encoding.UTF8);
            try
            {
                ServerSettings.LoadSettingsFromFile(path, true);
                ServerSettings.Instance.SaveSettings();
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError("Error while importing settings"));
            }
        }
Exemple #9
0
        public ActionResult SyncForUser(int uid)
        {
            JMMUser user = HttpContext.GetUser();

            ShokoServer.Instance.SyncPlex();
            return(APIStatus.OK());
        }
Exemple #10
0
        public ActionResult SetAniDB(Credentials cred)
        {
            var details = new List <(string, string)>();

            if (string.IsNullOrEmpty(cred.login))
            {
                details.Add(("login", "Username missing"));
            }
            if (string.IsNullOrEmpty(cred.password))
            {
                details.Add(("password", "Password missing"));
            }
            if (details.Count > 0)
            {
                return(new APIMessage(400, "Login or Password missing", details));
            }

            ServerSettings.Instance.AniDb.Username = cred.login;
            ServerSettings.Instance.AniDb.Password = cred.password;
            if (cred.port != 0)
            {
                ServerSettings.Instance.AniDb.ClientPort = cred.port;
            }
            if (!string.IsNullOrEmpty(cred.apikey))
            {
                ServerSettings.Instance.AniDb.AVDumpKey = cred.apikey;
            }
            if (cred.apiport != 0)
            {
                ServerSettings.Instance.AniDb.AVDumpClientPort = cred.apiport;
            }

            return(APIStatus.OK());
        }
Exemple #11
0
        public ActionResult Sync()
        {
            Analytics.PostEvent("Plex", "SyncOne");

            new CommandRequest_PlexSyncWatched(HttpContext.GetUser()).Save();
            return(APIStatus.OK());
        }
Exemple #12
0
        /// <summary>
        /// Set AniDB account credentials with a Credentials object
        /// </summary>
        /// <returns></returns>
        private object SetAniDB()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            Credentials cred = this.Bind();

            if (string.IsNullOrEmpty(cred.login) || string.IsNullOrEmpty(cred.password))
            {
                return(new APIMessage(400, "Login and Password missing"));
            }

            ServerSettings.AniDB_Username = cred.login;
            ServerSettings.AniDB_Password = cred.password;
            if (cred.port != 0)
            {
                ServerSettings.AniDB_ClientPort = cred.port.ToString();
            }
            if (!string.IsNullOrEmpty(cred.apikey))
            {
                ServerSettings.AniDB_AVDumpKey = cred.apikey;
            }
            if (cred.apiport != 0)
            {
                ServerSettings.AniDB_AVDumpClientPort = cred.apiport.ToString();
            }

            return(APIStatus.OK());
        }
Exemple #13
0
        public ActionResult WebhookPost([FromForm] PlexEvent eventData)
        {
            /*PlexEvent eventData = JsonConvert.DeserializeObject<PlexEvent>(this.Context.Request.Form.payload,
             *  new JsonSerializerSettings() {ContractResolver = new CamelCasePropertyNamesContractResolver()});*/

            logger.Trace($"{eventData.Event}: {eventData.Metadata.Guid}");
            switch (eventData.Event)
            {
            case "media.scrobble":
                Scrobble(eventData);
                break;

            case "media.resume":
            case "media.play":
                TraktScrobble(eventData, ScrobblePlayingStatus.Start);
                break;

            case "media.pause":
                TraktScrobble(eventData, ScrobblePlayingStatus.Pause);
                break;

            case "media.stop":
                TraktScrobble(eventData, ScrobblePlayingStatus.Stop);
                break;
            }

            return(APIStatus.OK());
        }
Exemple #14
0
        public ActionResult AddFolder(ImportFolder folder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (folder.ImportFolderLocation != string.Empty)
            {
                try
                {
                    // TODO Do this correctly without calling APIv1
                    CL_Response <ImportFolder> response = new ShokoServiceImplementation().SaveImportFolder(folder);

                    if (string.IsNullOrEmpty(response.ErrorMessage))
                    {
                        return(APIStatus.OK());
                    }
                    return(new APIMessage(500, response.ErrorMessage));
                }
                catch
                {
                    return(APIStatus.InternalError());
                }
            }
            return(new APIMessage(400, "Bad Request: The Folder path must not be Empty"));
        }
Exemple #15
0
        public ActionResult SyncForUser(int uid)
        {
            JMMUser user = Repo.Instance.JMMUser.GetByID(uid);

            ShokoServer.Instance.SyncPlex();
            return(APIStatus.OK());
        }
Exemple #16
0
        public ActionResult EditFolder(ImportFolder folder)
        {
            if (!String.IsNullOrEmpty(folder.ImportFolderLocation) && folder.ImportFolderID != 0)
            {
                try
                {
                    // TODO Do this correctly without calling APIv1
                    if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
                    {
                        return(new APIMessage(409, "The Import Folder can't be both Destination and Source"));
                    }

                    if (folder.ImportFolderID == 0)
                    {
                        return(new APIMessage(409, "The Import Folder must have an ID"));
                    }
                    CL_Response <ImportFolder> response =
                        new ShokoServiceImplementation().SaveImportFolder(folder);

                    if (!string.IsNullOrEmpty(response.ErrorMessage))
                    {
                        return(new APIMessage(500, response.ErrorMessage));
                    }

                    return(APIStatus.OK());
                }
                catch
                {
                    return(APIStatus.InternalError());
                }
            }
            return(new APIMessage(400, "ImportFolderLocation and ImportFolderID missing"));
        }
Exemple #17
0
        private object UpdateImages()
        {
            Importer.RunImport_UpdateTvDB(true);
            ShokoServer.Instance.DownloadAllImages();

            return(APIStatus.OK());
        }
Exemple #18
0
        public ActionResult SyncAniDBVotes()
        {
            //TODO APIv2: Command should be split into AniDb/MAL sepereate
            CommandRequest_SyncMyVotes cmdVotes = new CommandRequest_SyncMyVotes();

            cmdVotes.Save();
            return(APIStatus.OK());
        }
Exemple #19
0
        public ActionResult SyncTrakt()
        {
            if (ServerSettings.Instance.TraktTv.Enabled && !string.IsNullOrEmpty(ServerSettings.Instance.TraktTv.AuthToken))
            {
                CommandQueue.Queue.Instance.Add(new CmdTraktSyncCollection(true));
                return(APIStatus.OK());
            }

            return(new APIMessage(204, "Trakt is not enabled or you are missing the authtoken"));
        }
Exemple #20
0
        public ActionResult <APIMessage> TestDatabaseConnection()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            return(new Repositories.Repo().GetProvider().GetContext() != null?APIStatus.OK() : APIStatus.BadRequest("Failed to connect"));

            //return APIStatus.BadRequest("Failed to Connect");
        }
Exemple #21
0
        /// <summary>
        /// Sync Trakt Collection
        /// </summary>
        /// <returns></returns>
        private object SyncTrakt()
        {
            if (ServerSettings.Trakt_IsEnabled && !string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken))
            {
                CommandRequest_TraktSyncCollection cmd = new CommandRequest_TraktSyncCollection(true);
                cmd.Save();
                return(APIStatus.OK());
            }

            return(new APIMessage(204, "Trak is not enabled or you missing authtoken"));
        }
Exemple #22
0
        /// <summary>
        /// Set JMMServer Port
        /// </summary>
        /// <returns></returns>
        private object SetPort()
        {
            Credentials cred = this.Bind();

            if (cred.port != 0)
            {
                ServerSettings.JMMServerPort = cred.port.ToString();
                return(APIStatus.OK());
            }
            return(new APIMessage(400, "Port Missing"));
        }
Exemple #23
0
        /// <summary>
        /// Set Database Settings
        /// </summary>
        /// <returns></returns>
        private object SetDatabaseSettings()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            DatabaseSettings settings = this.Bind();
            string           dbtype   = settings?.db_type?.Trim();

            if (string.IsNullOrEmpty(dbtype))
            {
                return(APIStatus.BadRequest("You must specify database type and use valid xml or json."));
            }
            if (dbtype.Equals(Constants.DatabaseType.MySQL, StringComparison.InvariantCultureIgnoreCase))
            {
                if (string.IsNullOrEmpty(settings.mysql_hostname) || string.IsNullOrEmpty(settings.mysql_password) ||
                    string.IsNullOrEmpty(settings.mysql_schemaname) || string.IsNullOrEmpty(settings.mysql_username))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                ServerSettings.DatabaseType     = Constants.DatabaseType.MySQL;
                ServerSettings.MySQL_Hostname   = settings.mysql_hostname;
                ServerSettings.MySQL_Password   = settings.mysql_password;
                ServerSettings.MySQL_SchemaName = settings.mysql_schemaname;
                ServerSettings.MySQL_Username   = settings.mysql_username;
                return(APIStatus.OK());
            }
            if (dbtype.Equals(Constants.DatabaseType.SqlServer, StringComparison.InvariantCultureIgnoreCase))
            {
                if (string.IsNullOrEmpty(settings.sqlserver_databasename) || string.IsNullOrEmpty(settings.sqlserver_databaseserver) ||
                    string.IsNullOrEmpty(settings.sqlserver_password) || string.IsNullOrEmpty(settings.sqlserver_username))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                ServerSettings.DatabaseType     = Constants.DatabaseType.SqlServer;
                ServerSettings.DatabaseServer   = settings.sqlserver_databaseserver;
                ServerSettings.DatabaseName     = settings.sqlserver_databasename;
                ServerSettings.DatabaseUsername = settings.sqlserver_username;
                ServerSettings.DatabasePassword = settings.sqlserver_password;
                return(APIStatus.OK());
            }
            if (dbtype.Equals(Constants.DatabaseType.Sqlite, StringComparison.InvariantCultureIgnoreCase))
            {
                ServerSettings.DatabaseType = Constants.DatabaseType.Sqlite;
                if (!string.IsNullOrEmpty(settings.sqlite_databasefile))
                {
                    ServerSettings.DatabaseFile = settings.sqlite_databasefile;
                }
                return(APIStatus.OK());
            }
            return(APIStatus.BadRequest("An invalid setting was passed"));
        }
Exemple #24
0
        /// <summary>
        /// Set Trakt PIN
        /// </summary>
        /// <returns></returns>
        private object SetTraktPIN()
        {
            Credentials cred = this.Bind();

            if (!String.IsNullOrEmpty(cred.token) && cred.token != string.Empty)
            {
                ServerSettings.Trakt_PIN = cred.token;
                return(APIStatus.OK());
            }

            return(new APIMessage(400, "Token missing"));
        }
Exemple #25
0
 public ActionResult SetDefaultUserCredentials(Credentials credentials)
 {
     try
     {
         ServerSettings.Instance.Database.DefaultUserUsername = credentials.login;
         ServerSettings.Instance.Database.DefaultUserPassword = credentials.password;
         return(APIStatus.OK());
     }
     catch
     {
         return(APIStatus.InternalError());
     }
 }
Exemple #26
0
 public ActionResult DeleteFolder(int folderId)
 {
     if (folderId != 0)
     {
         string res = Importer.DeleteImportFolder(folderId);
         if (res == string.Empty)
         {
             return(APIStatus.OK());
         }
         return(new APIMessage(500, res));
     }
     return(new APIMessage(400, "ImportFolderID missing"));
 }
Exemple #27
0
        /// <summary>
        /// Authentication module
        /// </summary>
        public Auth() : base("/api/auth")
        {
            // Request Body (safer) { "user":"******", "pass":"******", "device":"device name" }
            // return apikey=yzx
            Post["/", true] = async(x, ct) => await Task.Factory.StartNew(() =>
            {
                //Bind POST body
                AuthUser auth = this.Bind();
                if (string.IsNullOrEmpty(auth.user?.Trim()))
                {
                    return new Response {
                        StatusCode = HttpStatusCode.BadRequest
                    }
                }
                ;

                if (auth.pass == null)
                {
                    auth.pass = string.Empty;
                }
                //if password or device is missing
                if (string.IsNullOrEmpty(auth.device) || auth.pass == null)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.BadRequest
                    }
                }
                ;

                //create and save new token for authenticated user or return known one
                string apiKey = RepoFactory.AuthTokens.ValidateUser(auth.user.Trim(), auth.pass.Trim(), auth.device.Trim());

                if (!string.IsNullOrEmpty(apiKey))
                {
                    return(Response.AsJson(new { apikey = apiKey }));
                }

                return(new Response {
                    StatusCode = HttpStatusCode.Unauthorized
                });
            }, ct);

            //remove apikey from database
            //pass it as ?apikey=xyz
            Delete["/", true] = async(x, ct) => await Task.Factory.StartNew(() =>
            {
                var apiKey = (string)Request.Query.apikey;
                RepoFactory.AuthTokens.DeleteWithToken(apiKey);
                return(APIStatus.OK());
            }, ct);
        }
Exemple #28
0
        /// <summary>
        /// Delete user from his ID
        /// </summary>
        /// <returns></returns>
        private object DeleteUser()
        {
            SVR_JMMUser _user = (SVR_JMMUser)Context.CurrentUser;

            if (_user.IsAdmin == 1)
            {
                SVR_JMMUser user = this.Bind();
                return(new ShokoServiceImplementation().DeleteUser(user.JMMUserID) == string.Empty
                    ? APIStatus.OK()
                    : APIStatus.InternalError());
            }

            return(APIStatus.AdminNeeded());
        }
Exemple #29
0
        /// <summary>
        /// Set MAL account with login, password
        /// </summary>
        /// <returns></returns>
        private object SetMAL()
        {
            Credentials cred = this.Bind();

            if (!String.IsNullOrEmpty(cred.login) && cred.login != string.Empty && !String.IsNullOrEmpty(cred.password) &&
                cred.password != string.Empty)
            {
                ServerSettings.MAL_Username = cred.login;
                ServerSettings.MAL_Password = cred.password;
                return(APIStatus.OK());
            }

            return(new APIMessage(400, "Login and Password missing"));
        }
Exemple #30
0
        public ActionResult SetAniDB(Credentials cred)
        {
            if (!string.IsNullOrEmpty(cred.login) && !string.IsNullOrEmpty(cred.password))
            {
                ServerSettings.Instance.AniDb.Username = cred.login;
                ServerSettings.Instance.AniDb.Password = cred.password;
                if (cred.port != 0)
                {
                    ServerSettings.Instance.AniDb.ClientPort = cred.port;
                }
                return(APIStatus.OK());
            }

            return(new APIMessage(400, "Login and Password missing"));
        }