コード例 #1
0
        private static BooruPost GetPost(Stream str, ulong ID)
        {
            BooruPost post = null;

            Request(str, RequestCode.Get_Post, (rw) => rw.Write(ID), (rw) => { post = BooruPost.FromReader(rw); });
            Request(str, RequestCode.Get_PostTags, (rw) => rw.Write(ID), (rw) => { post.Tags = BooruTagList.FromReader(rw); });
            return(post);
        }
コード例 #2
0
        public bool NotificatePostAdded(ulong ID, BooruPost Post, BooruImage Thumb)
        {
            StringBuilder mailBody = new StringBuilder();

            mailBody.AppendLine("User " + Post.User + " added post " + ID);
            mailBody.AppendLine("https://eggy.hopto.org/booru/post.php?id=" + ID);
            using (var stream = new MemoryStream(Thumb.Bytes))
                using (var att = new Attachment(stream, "thumb.jpg", "image/jpeg"))
                    return(SendMail("Post " + ID + " added", mailBody.ToString(), att));
        }
コード例 #3
0
        private static ulong AddPost(Stream str, BooruPost Post, BooruTagList Tags, BooruImage Image)
        {
            ulong postID = 0;

            Request(str, RequestCode.Add_Post, (rw) =>
            {
                Post.ToWriter(rw);
                Tags.ToWriter(rw);
                Image.ToWriter(rw);
            }, (rw) => { postID = rw.ReadULong(); });
            return(postID);
        }
コード例 #4
0
        public object Clone()
        {
            BooruPost post = MemberwiseClone() as BooruPost;

            if (Image != null)
            {
                post.Image = Image.Clone() as BooruImage;
            }
            if (Thumbnail != null)
            {
                post.Thumbnail = Thumbnail.Clone() as BooruImage;
            }
            post.Tags = Tags.Clone() as BooruTagList;
            return(post);
        }
コード例 #5
0
        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);
        }