public async Task SankakuSearchAsync([Remainder] string tags) { try { if (Global.SankakuClient.LoggedIn) { if (!Context.Channel.IsNsfw && (tags.IndexOf("rating:explicit") > -1 || tags.IndexOf("rating:questionable") > -1)) { await Context.Channel.SendMessageAsync("Please go to an NSFW channel!"); return; } if (!Context.Channel.IsNsfw && tags.IndexOf("rating:safe") == -1) { tags += " rating:safe"; } List <SankakuPost> TempPosts = Global.SankakuClient.SearchSankaku(tags.Trim()); SankakuPost TempPost = TempPosts[new Random().Next(TempPosts.Count - 1)]; using (System.Drawing.Bitmap TempImg = new System.Drawing.Bitmap(Global.SankakuClient.GetImage(TempPost))) { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Images\")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"Images\"); } Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory + @"Images\" + TempPost.ImageFileName); TempImg.Save(AppDomain.CurrentDomain.BaseDirectory + @"Images\" + TempPost.ImageFileName); await Context.Channel.SendFileAsync(AppDomain.CurrentDomain.BaseDirectory + @"\Images\" + TempPost.ImageFileName); } } } catch { await Context.Channel.SendMessageAsync("Could not get an image!"); } }
public async Task <Post> LoadPostAsync(int postId) { var pageHtml = await _booruLoader.LoadPageAsync(SANKAKU_POST_URL + postId); var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(pageHtml); var post = new SankakuPost(postId, htmlDoc.DocumentNode); return(post); }
public void WriteToLog( string msg, bool registerTime = false, string filename = "", string exMessage = "", SankakuPost postInfo = null, string[] fndPosts = null, bool wasSkipped = false) { // Dispatcher needs to be called when interacting with the GUI - otherwise an error can be thrown Dispatcher.Invoke(() => { var date = DateTime.Now; if (registerTime) { RegisteredTimestamps.Add(date); } Logs.Add(new Log($"[{date.ToString("HH:mm:ss")}] " + msg, date, filename, exMessage, postInfo, fndPosts, wasSkipped)); // <--- adding the log to my log collection listBox.ItemsSource = Logs; // <-- displaying my log collection listBox.Items.Refresh(); // <-- refreshing the view listBox.ScrollIntoView(listBox.Items[listBox.Items.Count - 1]); // <-- scrolling to the end, so the viewer sees the latest log }); }
private static async Task ScrapeNewest() { DiscordContext context = new DiscordContext(); HttpWebRequest request = Helpers.CreateRequestWithHeaders(_baseUrl + "/post/index.content?tags=feet+order%3Arandom+-contentious_content+-furry+-rating%3As&page=3"); if (request.CookieContainer == null) { request.CookieContainer = new CookieContainer(); } request.CookieContainer.Add(new Cookie("login", Config.SankakuLogin, "/", _domain)); request.CookieContainer.Add(new Cookie("pass_hash", Config.SankakuPassword, "/", _domain)); var data = (await request.GetResponseAsync()).GetResponseStream(); var doc = new HtmlDocument(); doc.Load(data); var postNodes = doc.DocumentNode.Descendants("span"); ConcurrentBag <SankakuImage> sankakuImages = new ConcurrentBag <SankakuImage>(); Parallel.ForEach(postNodes, async postNode => { var localContext = new DiscordContext(); var cPost = new SankakuPost(postNode); cPost.GetScoreCount(); if ((await localContext.SankakuImage.FindAsync(cPost.Id)) != null) { return; } var sankakuImage = new SankakuImage(); sankakuImage.Id = cPost.Id; sankakuImage.Score = cPost.Score; sankakuImage.Rating = (int)cPost.imgRating; sankakuImage.RatingCount = cPost.ScoreCount != null ? cPost.ScoreCount.Value : 0; foreach (var tag in cPost.Tags) { var dbTag = localContext.SankakuTag.FirstOrDefault(sTag => sTag.Name == tag); var imgTag = new SankakuImageTag(); if (dbTag == null) { dbTag = new SankakuTag() { Name = tag }; imgTag.Tag = dbTag; } else { imgTag.TagId = dbTag.Id; } sankakuImage.SankakuImageTag.Add(imgTag); } sankakuImages.Add(sankakuImage); }); await context.SankakuImage.AddRangeAsync(sankakuImages); await context.SaveChangesAsync(); }