public static int Main(string[] args) { var pResult = Parser.Default.ParseArguments(args, new Type[] { typeof(AddOptions), typeof(AddUrlOptions), typeof(DelOptions), typeof(EditOptions), typeof(GetImgOptions), typeof(SetImgOptions), typeof(FindDupeOptions) /* * typeof(GetOptions), * typeof(EditImgOptions), */ }); if (!pResult.Errors.Any()) { try { var commonOptions = (Options)pResult.Value; Config config = Config.TryLoad(); WebProxy proxy = null; if (config != null) { if (commonOptions.API_URL == null) { commonOptions.API_URL = config.URL; } if (commonOptions.Username == null) { commonOptions.Username = config.Username; } if (commonOptions.Password == null) { commonOptions.Password = config.Password; } if (commonOptions.ProxyEnable && config.Proxy != null) { proxy = config.Proxy; } } else if (commonOptions.API_URL == null || commonOptions.Username == null || commonOptions.Password == null) { Console.WriteLine("Config file not found, you must provide --api-url, --username and --password"); } else { Console.WriteLine("Tip: You can create a config file for faster client usage"); } if (commonOptions.ProxyEnable && proxy == null) { Console.WriteLine("Proxy is enabled but not configured"); } Booru booru = new Booru(commonOptions.API_URL, commonOptions.Username, commonOptions.Password, proxy); Type oType = commonOptions.GetType(); if (oType == typeof(AddOptions)) { var options = (AddOptions)commonOptions; byte[] image = DownLoadImage(options.ImagePathOrURL, proxy); var tags = options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries); byte rating = (byte)options.Rating; Console.Write("Uploading post... "); uint id = booru.Upload(image, options.Private, options.Source, options.Info, rating, tags, options.Force); Console.WriteLine(id); return(0); } else if (oType == typeof(AddUrlOptions)) { var options = (AddUrlOptions)commonOptions; if (config != null) { if (options.BooruAPI_Username == null) { options.BooruAPI_Username = config.BooruAPI_Username; } if (options.BooruAPI_Password == null) { options.BooruAPI_Password = config.BooruAPI_Password; } } var apiPost = BooruAPI.GetPost(options.URL, proxy, options.BooruAPI_Username, options.BooruAPI_Password); byte[] image = DownLoadImage(options.CustomImagePathOrURL ?? apiPost.ImageURL, proxy); string info = null; if (options.Info != null) { info = options.Info; } byte rating = (byte)options.Rating; var tags = new List <string>(); if (!options.AllTags) { Console.Write("Checking tag existence... "); foreach (string tag in apiPost.Tags) { if (booru.TagExists(tag)) { tags.Add(tag); } } Console.WriteLine("OK"); } else { tags.AddRange(apiPost.Tags); } if (options.Tags != null) { options.Tags = options.Tags.ToLower(); if (options.TagsNoDelta) { string[] parts = options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries); tags = parts.ToList(); } else { TagDelta(ref tags, options.Tags); } } Console.Write("Uploading post... "); ulong id = booru.Upload(image, options.Private, apiPost.Source, info, rating, tags.ToArray(), options.Force); Console.WriteLine(id); return(0); } else if (oType == typeof(DelOptions)) { var options = (DelOptions)commonOptions; booru.Delete(options.ID); return(0); } else if (oType == typeof(EditOptions)) { var options = (EditOptions)commonOptions; StringBuilder sb = new StringBuilder(); using (XMLFactory factory = booru.CreateXMLFactory(sb)) { factory.WriteEditHeader(options.ID); if (options.Info != null) { factory.WriteEditInfo(options.Info); } if (options.Private.HasValue) { factory.WriteEditPrivate(options.Private.Value); } if (!(options.Rating < 0)) { if (options.Rating < byte.MaxValue) { factory.WriteEditRating((byte)options.Rating); } else { throw new ArgumentException("Rating value too big"); } } if (options.Source != null) { factory.WriteEditSource(options.Source); } if (options.Tags != null) { options.Tags = options.Tags.ToLower(); string[] parts = options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (!options.TagsNoDelta) { List <string> addTags = new List <string>(); List <string> removeTags = new List <string>(); foreach (string part in parts) { if (part.StartsWith("!") || part.StartsWith("_")) { removeTags.Add(part.Substring(1)); } else { addTags.Add(part); } } factory.WriteEditTags(true, addTags.ToArray()); factory.WriteEditTags(false, removeTags.ToArray()); } else { factory.WriteEditTags(null, parts); } } factory.WriteEditFooter(); } booru.Request(sb.ToString()); return(0); } else if (oType == typeof(GetImgOptions)) { GetImgOptions options = (GetImgOptions)commonOptions; Console.Write("Downloading image... "); byte[] imageData; string mimeType; booru.GetImage(options.ID, out imageData, out mimeType); using (FileStream fs = new FileStream(options.Path, FileMode.Create, FileAccess.Write, FileShare.Read)) fs.Write(imageData, 0, imageData.Length); Console.WriteLine("OK"); return(0); } else if (oType == typeof(SetImgOptions)) { SetImgOptions options = (SetImgOptions)commonOptions; byte[] image = DownLoadImage(options.ImagePathOrURL, proxy); Console.Write("Uploading image... "); booru.SetImage(options.ID, image); Console.WriteLine("OK"); return(0); } else if (oType == typeof(FindDupeOptions)) { FindDupeOptions options = (FindDupeOptions)commonOptions; byte[] image = null; using (MemoryStream ms = new MemoryStream(DownLoadImage(options.ImagePathOrURL, proxy))) using (Bitmap bitmap = new Bitmap(ms)) { Console.Write("Resizing image... "); double num = Math.Min(640f / bitmap.Width, 640f / bitmap.Height); Size resultSize = new Size((int)(bitmap.Width * num), (int)(bitmap.Height * num)); using (Bitmap newBitmap = new Bitmap(resultSize.Width, resultSize.Height)) { using (Graphics g = Graphics.FromImage(newBitmap)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(bitmap, 0f, 0f, resultSize.Width, resultSize.Height); } using (MemoryStream ms2 = new MemoryStream()) { newBitmap.Save(ms2, ImageFormat.Jpeg); image = ms2.ToArray(); } } } Console.WriteLine("OK"); Console.Write("Looking for duplicates... "); var sha256 = new SHA256CryptoServiceProvider(); byte[] hashBytes = sha256.ComputeHash(image); string hash = string.Empty; for (byte i = 0; i < 10; i++) { hash += string.Format("{0:x2}", hashBytes[i]); } uint[] dupeIds = booru.FindDuplicates(hash, image); if (dupeIds.Length > 0) { Console.WriteLine("done"); Console.WriteLine("Duplicate IDs: " + string.Join(", ", dupeIds)); } else { Console.WriteLine("no duplicates found"); } return(0); } else { return(1); } #region Other methods /* * else if (oType == typeof(GetOptions)) * { * var options = (GetOptions)commonOptions; * using (var post = GetPost(ns, options.ID)) * { * Console.WriteLine("User " + post.User); * Console.WriteLine("Private " + (post.Private ? "yes" : "no")); * Console.WriteLine("Source " + post.Source ?? string.Empty); * Console.WriteLine("Description " + post.Description ?? string.Empty); * Console.WriteLine("Rating " + post.Rating); * Console.WriteLine("Size {0}x{1}", post.Width, post.Height); * Console.WriteLine("Date {0}", post.CreationDate); * Console.WriteLine("ViewCount " + post.ViewCount); * Console.WriteLine("EditCount " + post.EditCount); * Console.WriteLine("Score " + post.Score); * Console.WriteLine(); * Console.WriteLine(BooruTagListToString(post.Tags)); * } * return 0; * } * else if (oType == typeof(EditImgOptions)) * { * EditImgOptions options = (EditImgOptions)commonOptions; * BooruImage img = null; * string path = options.Path; * try * { * Request(ns, RequestCode.Get_Image, (rw) => rw.Write(options.ID), (rw) => { img = BooruImage.FromReader(rw); }); * img.Save(ref path, true); * } * finally { img.Dispose(); } * if (options.Tool != null) * { * var psi = new ProcessStartInfo(options.Tool, path); * Process tool = new Process() { StartInfo = psi }; * tool.Start(); * Console.Write("Waiting for image editor to exit..."); * tool.WaitForExit(); * } * else * { * Console.Write("Edit the image and press any key to save it... "); * Console.ReadKey(true); * Console.WriteLine(); * } * using (BooruImage eImg = BooruImage.FromFile(path)) * Request(ns, RequestCode.Edit_Image, (rw) => * { * rw.Write(options.ID); * eImg.ToWriter(rw); * }, (rw) => { }); * File.Delete(options.Path); * return 0; * } */ #endregion } catch (RemoteBooruException ex) { Console.Error.WriteLine("RemoteBooruException: " + ex.Message); return(1); } catch (Exception ex) { Console.WriteLine(ex.GetType().FullName + ": " + ex.Message); Console.WriteLine(ex.StackTrace); return(1); } } else { return(1); } }
private static void Process(Stream str, Options commonOptions) { if (!string.IsNullOrWhiteSpace(commonOptions.Username)) { Request(str, RequestCode.Login, (rw) => { rw.Write(commonOptions.Username, true); rw.Write(commonOptions.Password ?? string.Empty, true); }, (rw) => { }); } Type oType = commonOptions.GetType(); if (oType == typeof(AddOptions)) { var options = (AddOptions)commonOptions; Console.Write("Loading image... "); using (var post = new BooruPost()) using (var image = BooruImage.FromFile(options.ImagePath)) { Console.WriteLine("OK"); if (options.Tags != null) { foreach (var tag in options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) { post.Tags.Add(new BooruTag(tag)); } } if (options.Source != null) { post.Source = options.Source; } if (options.Description != null) { post.Description = options.Description; } post.Rating = (byte)options.Rating; if (options.Private.HasValue) { post.Private = options.Private.Value; } Console.Write("Adding post... "); ulong id = AddPost(str, post, post.Tags, image); Console.WriteLine(id); } } else if (oType == typeof(AddUrlOptions)) { var options = (AddUrlOptions)commonOptions; var apiPosts = BooruAPI.SearchPostsPerURL(options.URL); if (apiPosts.Count < 1) { throw new Exception("No post to import detected"); } else if (apiPosts.Count > 1) { Console.WriteLine("Multiple posts found, importing only the first one"); for (int i = 1; i < apiPosts.Count; i++) { apiPosts[i].Dispose(); } } var apiPost = apiPosts[0]; if (options.CustomImagePath == null) { Console.Write("Downloading image... "); apiPost.DownloadImage(); } else { Console.Write("Loading image... "); apiPost.Image = BooruImage.FromFile(options.CustomImagePath); } Console.WriteLine("OK"); if (!options.AllTags) { string[] allTags = null; Request(str, RequestCode.Get_AllTags, (rw) => { }, (rw) => { uint count = rw.ReadUInt(); allTags = new string[count]; for (int a = 0; a < count; a++) { allTags[a] = rw.ReadString(); } }); for (int a = apiPost.Tags.Count - 1; !(a < 0); a--) { if (!allTags.Contains(apiPost.Tags[a].Tag)) { apiPost.Tags.RemoveAt(a); } } } if (options.Tags != null) { options.Tags = options.Tags.ToLower(); if (options.TagsNoDelta) { string[] parts = options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries); apiPost.Tags.Clear(); foreach (string part in parts) { apiPost.Tags.Add(new BooruTag(part)); } } else { TagDelta(ref apiPost.Tags, options.Tags); } } //apiPost.Description = "Imported from " + apiPost.APIName; if (options.Description != null) { apiPost.Description = options.Description; } else { apiPost.Description = string.Empty; //needed? } apiPost.Rating = (byte)options.Rating; if (options.Private.HasValue) { apiPost.Private = options.Private.Value; } Console.Write("Importing post... "); ulong id = AddPost(str, apiPost, apiPost.Tags, apiPost.Image); Console.WriteLine(id); } else if (oType == typeof(DelOptions)) { var options = (DelOptions)commonOptions; Request(str, RequestCode.Delete_Post, (rw) => rw.Write(options.ID), (rw) => { }); } else if (oType == typeof(GetOptions)) { var options = (GetOptions)commonOptions; using (var post = GetPost(str, options.ID)) { Console.WriteLine("User " + post.User); Console.WriteLine("Private " + (post.Private ? "yes" : "no")); Console.WriteLine("Source " + post.Source ?? string.Empty); Console.WriteLine("Description " + post.Description ?? string.Empty); Console.WriteLine("Rating " + post.Rating); Console.WriteLine("Size {0}x{1}", post.Width, post.Height); Console.WriteLine("Date {0}", post.CreationDate); Console.WriteLine("ViewCount " + post.ViewCount); Console.WriteLine("EditCount " + post.EditCount); Console.WriteLine("Score " + post.Score); Console.WriteLine(); Console.WriteLine(BooruTagListToString(post.Tags, !options.NoColor)); } } else if (oType == typeof(EditOptions)) { var options = (EditOptions)commonOptions; using (var post = GetPost(str, options.ID)) { post.EditCount += 1; if (options.Description != null) { post.Description = options.Description; } if (options.Private.HasValue) { post.Private = options.Private.Value; } if (!(options.Rating < 0) && options.Rating < byte.MaxValue) { post.Rating = (byte)options.Rating; } if (options.Source != null) { post.Source = options.Source; } if (options.Tags != null) { options.Tags = options.Tags.ToLower(); if (options.TagsNoDelta) { string[] parts = options.Tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries); post.Tags.Clear(); foreach (string part in parts) { post.Tags.Add(new BooruTag(part)); } } else { TagDelta(ref post.Tags, options.Tags); } } Request(str, RequestCode.Edit_Post, (rw) => { post.ToWriter(rw); post.Tags.ToWriter(rw); }, (rw) => { }); } } else if (oType == typeof(EditImgOptions)) { EditImgOptions options = (EditImgOptions)commonOptions; string path = Helper.GetTempFile(); BooruImage img = null; try { Request(str, RequestCode.Get_Image, (rw) => rw.Write(options.ID), (rw) => { img = BooruImage.FromReader(rw); }); img.Save(ref path, true); } finally { img.Dispose(); } var psi = new ProcessStartInfo(options.Editor, path); Process tool = new Process() { StartInfo = psi }; tool.Start(); Console.Write("Waiting for image editor to exit..."); tool.WaitForExit(); using (BooruImage eImg = BooruImage.FromFile(path)) Request(str, RequestCode.Edit_Image, (rw) => { rw.Write(options.ID); eImg.ToWriter(rw); }, (rw) => { }); } else if (oType == typeof(GetImgOptions)) { GetImgOptions options = (GetImgOptions)commonOptions; BooruImage img = null; try { Request(str, RequestCode.Get_Image, (rw) => rw.Write(options.ID), (rw) => { img = BooruImage.FromReader(rw); }); string path = options.Path; img.Save(ref path, true); } finally { img.Dispose(); } } else if (oType == typeof(SetImgOptions)) { SetImgOptions options = (SetImgOptions)commonOptions; using (BooruImage img = BooruImage.FromFile(options.Path)) Request(str, RequestCode.Edit_Image, (rw) => { rw.Write(options.ID); img.ToWriter(rw); }, (rw) => { }); } else if (oType == typeof(SetImgUrlOptions)) { SetImgUrlOptions options = (SetImgUrlOptions)commonOptions; Console.Write("Downloading image... "); using (BooruImage img = BooruImage.FromURL(options.URL)) { Console.WriteLine("OK"); Request(str, RequestCode.Edit_Image, (rw) => { rw.Write(options.ID); img.ToWriter(rw); }, (rw) => { }); } } else if (oType == typeof(GCOptions)) { Request(str, RequestCode.Start_GC, (rw) => { }, (rw) => { }); } else { throw new Exception("Unknown options type"); } //Logout str.WriteByte(0xFF); str.WriteByte(0xFF); }