Example #1
0
        private static void Main(string[] args)
        {
            // Configure the console
            Console.Title = "PlexBird";

            // Configure the logger
            var xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(Resources.log4net);

            XmlConfigurator.Configure(xmlDocument.DocumentElement);

            Logger.Info("Launching PlexBird..");

            // Check config
            var configPath = Path.Combine(Environment.CurrentDirectory, "config.json");

            if (!File.Exists(configPath))
            {
                var defaultConfiguration = new Configuration
                {
                    Username     = string.Empty,
                    AuthToken    = string.Empty,
                    PlexUrl      = "http://localhost:32400/",
                    AnimeLibrary = "Anime TV Series",
                    SyncTime     = 60
                };

                File.WriteAllText(configPath, JsonConvert.SerializeObject(defaultConfiguration, Formatting.Indented));

                Logger.Error("Configuration file not found!");
                Logger.Warn("A configuration file has been generated, please edit config.json!");

                Console.ReadKey();
                return;
            }

            Configuration = JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(configPath));

            if (Configuration.Username.Equals(string.Empty) || Configuration.AuthToken.Equals(string.Empty))
            {
                Logger.Info("No AuthToken was found, you have to sign in to connect with HummingBird.");

                while (Configuration.AuthToken.Equals(string.Empty))
                {
                    Console.Write("HummingBird username: "******"HummingBird password: "******"Wrong username/password combination, try again.");
                    }
                }
            }

            HummingBird.SetAuthenticationToken(Configuration.AuthToken);
            User = HummingBird.GetUser(Configuration.Username);

            Logger.Info($"Connected to HummingBird as the user {User.Name}, you've spent {User.LifeSpentOnAnime} minutes watching anime.");

            // Check database
            var databasePath = Path.Combine(Environment.CurrentDirectory, "database.json");

            if (!File.Exists(databasePath))
            {
                Database = new Dictionary <string, DatabaseEntry>();
                SaveDatabase();
            }
            else
            {
                Database = JsonConvert.DeserializeObject <Dictionary <string, DatabaseEntry> >(File.ReadAllText(databasePath));
            }

            // Setup plex
            var         plex        = new Plex(Configuration.PlexUrl);
            PlexLibrary plexLibrary = null;

            // Search plexLibrary
            foreach (var plexLib in plex.GetLibraries())
            {
                if (!plexLib.Title.Equals(Configuration.AnimeLibrary))
                {
                    continue;
                }

                if (plexLibrary != null)
                {
                    throw new Exception($"The plex installation '{plex.Information.FriendlyName}' seems to have more than one '{plexLib.Title}' library.");
                }

                plexLibrary = plexLib;
            }

            if (plexLibrary == null)
            {
                throw new Exception($"The plex installation '{plex.Information.FriendlyName}' seems to not have '{Configuration.AnimeLibrary}' library.");
            }

            // Connected
            new Thread(SynchronizeLibrary).Start(new ThreadData
            {
                Plex        = plex,
                PlexLibrary = plexLibrary
            });

            // End
            SaveDatabase();
        }