/// <summary> /// Sets the settings needed for the bot and returns a settings class /// </summary> /// <param name="settingsPath"></param> /// <returns></returns> private BotSettings Setup(string settingsPath) { try { // Attempt to load the settings var settingsManager = new InstaFamousSettings(settingsPath); var botSettings = settingsManager.LoadSettings(); // Set the botname BotName = botSettings.Subreddit + " | " + botSettings.InstagramUsername; InstaFamousLogger.LogMessage($"Set up bot for ${BotName}", InstaFamousLogger.LogLevel.INFO, BotName); return(botSettings); } catch (Exception ex) { InstaFamousLogger.LogMessage($"Unable to setup bot for: ${BotName}" + $" {Environment.NewLine}" + $" {ex.Message}", InstaFamousLogger.LogLevel.WARNING, BotName); return(null); } }
/// <summary> /// Attempts to change the picture format of a PNG file /// </summary> /// <param name="fileClient"></param> /// <param name="file"></param> private void ChangePictureFormat(FileManager fileClient, string file) { try { fileClient.ChangePictureFormat(file); File.Delete(file); } catch (Exception ex) { InstaFamousLogger.LogMessage($"Unable to change the picture format of {file} " + $"{Environment.NewLine}" + $" {ex.Message}", InstaFamousLogger.LogLevel.WARNING, BotName); } }
/// <summary> /// This function is used to start the bot /// </summary> /// <param name="settingsPath">Path to the settings.json</param> public void Start(string settingsPath) { var settings = Setup(settingsPath); if (settings != null) { Run(settings); } else { InstaFamousLogger.LogMessage($"Unable to setup bot for: {settingsPath}", InstaFamousLogger.LogLevel.WARNING, BotName); } }
/// <summary> /// Attempts to download the reddit post /// </summary> /// <param name="redditClient"></param> /// <param name="redditPost"></param> /// <param name="downloadDirectory"></param> private void DownloadFile(RedditClient redditClient, Post redditPost, string downloadDirectory) { try { // Attempt to download the message redditClient.DownloadPost(redditPost, downloadDirectory); } catch (Exception ex) { InstaFamousLogger.LogMessage($"Unable to download {redditPost.Url} " + $"{Environment.NewLine}" + $" {ex.Message}", InstaFamousLogger.LogLevel.WARNING, BotName); } }
/// <summary> /// This is the main bot loop /// </summary> /// <param name="settings"></param> private void Run(BotSettings settings) { ImagesDownloaded = 0; ImagesUploaded = 0; var botSettings = settings; var directoryName = settings.Subreddit; var fileClient = new FileManager(directoryName); if (fileClient.Setup()) { // Main bot loop while (true) { var redditClient = GetRedditClient(botSettings); var instagramClient = GetInstagramClient(botSettings); var redditPosts = redditClient.GetPosts(); redditPosts.ForEach(post => { DownloadFile(redditClient, post, directoryName); }); var pngFilePaths = fileClient.GetPngImages(); pngFilePaths.ForEach(file => { ChangePictureFormat(fileClient, file); }); var filePaths = fileClient.GetImageList(); filePaths.ForEach(file => { PrepareImage(fileClient, file); }); var instagramFiles = fileClient.GetImageList(); foreach (var filePath in instagramFiles) { // Login to instagram if (LoginInstagram(instagramClient)) { // Attempt to upload the image to instagram. try { instagramClient.PostImage(filePath); InstaFamousLogger.LogMessage($"Succesfully uploaded {filePath}", InstaFamousLogger.LogLevel.INFO, BotName); } catch (Exception ex) { InstaFamousLogger.LogMessage($"Unable to upload {filePath}" + $" {Environment.NewLine}" + $" {ex.Message}", InstaFamousLogger.LogLevel.INFO, BotName); } } // Attempt to logout try { instagramClient.Logout(); } catch (Exception ex) { Console.WriteLine($"Unable to logout to instagram account {botSettings.InstagramUsername}" + $" {Environment.NewLine}" + $" {ex.Message}"); } // Sleep 6 hours System.Threading.Thread.Sleep(216 * 100000); } // Clean up the directory of images EmptyDirectory(directoryName); } } }