static void Main(string[] args) { CommandLineArgs commandLine = new CommandLineArgs(args); if (commandLine.ShowHelp) { commandLine.DisplayHelp(Console.Out); return; } using (AnimeRecsClient client = new AnimeRecsClient(commandLine.PortNumber)) { if (commandLine.Operation.Equals("raw", StringComparison.OrdinalIgnoreCase)) { using (TcpClient rawClient = new TcpClient("localhost", commandLine.PortNumber)) { byte[] jsonBytes = Encoding.UTF8.GetBytes(commandLine.RawJson); rawClient.Client.Send(jsonBytes); using (NetworkStream socketStream = rawClient.GetStream()) { rawClient.Client.Shutdown(SocketShutdown.Send); byte[] responseJsonBytes = StreamUtil.ReadFully(socketStream); string responseJsonString = Encoding.UTF8.GetString(responseJsonBytes); dynamic responseJson = JsonConvert.DeserializeObject<dynamic>(responseJsonString); string prettyResponse = JsonConvert.SerializeObject(responseJson, Formatting.Indented); Console.WriteLine(prettyResponse); } } } else if (commandLine.Operation.Equals(OpNames.Ping, StringComparison.OrdinalIgnoreCase)) { string pingResponse = client.Ping(commandLine.PingMessage); Console.WriteLine("The service replied: {0}", pingResponse); } else if (commandLine.Operation.Equals(OpNames.ReloadTrainingData, StringComparison.OrdinalIgnoreCase)) { client.ReloadTrainingData(commandLine.ReloadMode, commandLine.Finalize); Console.WriteLine("Training data reloaded."); } else if (commandLine.Operation.Equals(OpNames.FinalizeRecSources, StringComparison.OrdinalIgnoreCase)) { client.FinalizeRecSources(); Console.WriteLine("Rec sources finalized."); } else if (commandLine.Operation.Equals(OpNames.LoadRecSource, StringComparison.OrdinalIgnoreCase)) { if (commandLine.RecSourceType.Equals(RecSourceTypes.AverageScore, StringComparison.OrdinalIgnoreCase)) { client.LoadRecSource(commandLine.RecSourceName, commandLine.ReplaceExistingRecSource, new AverageScoreRecSourceParams( minEpisodesToCountIncomplete: commandLine.MinEpisodesToCountIncomplete, minUsersToCountAnime: commandLine.MinUsersToCountAnime, useDropped: commandLine.UseDropped ) ); } else if (commandLine.RecSourceType.Equals(RecSourceTypes.MostPopular, StringComparison.OrdinalIgnoreCase)) { client.LoadRecSource(commandLine.RecSourceName, commandLine.ReplaceExistingRecSource, new MostPopularRecSourceParams( minEpisodesToCountIncomplete: commandLine.MinEpisodesToCountIncomplete, useDropped: commandLine.UseDropped ) ); } else if (commandLine.RecSourceType.Equals(RecSourceTypes.AnimeRecs, StringComparison.OrdinalIgnoreCase)) { client.LoadRecSource(commandLine.RecSourceName, commandLine.ReplaceExistingRecSource, new AnimeRecsRecSourceParams( numRecommendersToUse: commandLine.NumRecommendersToUse, fractionConsideredRecommended: commandLine.FractionRecommended, minEpisodesToClassifyIncomplete: commandLine.MinEpisodesToCountIncomplete ) ); } else if (commandLine.RecSourceType.Equals(RecSourceTypes.BiasedMatrixFactorization, StringComparison.OrdinalIgnoreCase)) { client.LoadRecSource(commandLine.RecSourceName, commandLine.ReplaceExistingRecSource, commandLine.BiasedMatrixFactorizationParams); } else { throw new Exception("Oops! Missed a rec source type!"); } Console.WriteLine("Load complete."); } else if (commandLine.Operation.Equals(OpNames.UnloadRecSource, StringComparison.OrdinalIgnoreCase)) { client.UnloadRecSource(commandLine.RecSourceName); Console.WriteLine("Unload complete."); } else if (commandLine.Operation.Equals(OpNames.GetRecSourceType, StringComparison.OrdinalIgnoreCase)) { string recSourceType = client.GetRecSourceType(commandLine.RecSourceName); Console.WriteLine("Type of rec source {0} is {1}.", commandLine.RecSourceName, recSourceType); } else if (commandLine.Operation.Equals(OpNames.GetMalRecs, StringComparison.OrdinalIgnoreCase)) { MalUserLookupResults lookup; using (IMyAnimeListApi malApi = GetMalApi()) { lookup = malApi.GetAnimeListForUser(commandLine.MalUsername); } Dictionary<int, RecEngine.MAL.MalListEntry> animeListEntries = new Dictionary<int, RecEngine.MAL.MalListEntry>(); foreach (MyAnimeListEntry entry in lookup.AnimeList) { animeListEntries[entry.AnimeInfo.AnimeId] = new RecEngine.MAL.MalListEntry((byte?)entry.Score, entry.Status, (short)entry.NumEpisodesWatched); } MalRecResults<IEnumerable<RecEngine.IRecommendation>> recs = client.GetMalRecommendations( animeList: animeListEntries, recSourceName: commandLine.RecSourceName, numRecsDesired: commandLine.NumRecs, targetScore: commandLine.TargetScore); PrintRecs(recs, animeListEntries, commandLine.TargetScore); } else { throw new Exception(string.Format("Oops, missed an operation: {0}", commandLine.Operation)); } } }
static void Main(string[] args) { System.Threading.Thread.CurrentThread.Name = "Main"; Logging.SetUpLogging(); try { CommandLineArgs commandLine = new CommandLineArgs(args); if (commandLine.ShowHelp) { commandLine.DisplayHelp(Console.Out); return; } ConfigRoot config = null; if (commandLine.ConfigFile != null) { config = ConfigRoot.LoadFromFile(commandLine.ConfigFile); } string connectionString = ConfigurationManager.ConnectionStrings["Postgres"].ToString(); PgMalTrainingDataLoaderFactory trainingDataLoaderFactory = new PgMalTrainingDataLoaderFactory(connectionString); using (TcpRecService recService = new TcpRecService(trainingDataLoaderFactory, commandLine.PortNumber)) { recService.Start(); if (config != null) { using (AnimeRecsClient client = new AnimeRecsClient(commandLine.PortNumber)) { foreach (DTO.LoadRecSourceRequest recSourceToLoad in config.RecSources) { client.LoadRecSource(recSourceToLoad); } if (config.FinalizeAfterLoad) { client.FinalizeRecSources(); } } } #if MONO Logging.Log.InfoFormat("Started listening on port {0}. Press ctrl+c to stop.", commandLine.PortNumber); WaitForUnixStopSignal(); #else Logging.Log.InfoFormat("Started listening on port {0}. Press any key to stop.", commandLine.PortNumber); Console.ReadKey(); #endif Logging.Log.InfoFormat("Got stop signal."); } Logging.Log.InfoFormat("Shutdown complete."); } catch (Exception ex) { Logging.Log.FatalFormat("Fatal error: {0}", ex, ex.Message); Environment.ExitCode = 1; } }