public void ParseCatch() { string mode = "osu!catch"; var result = ModeExtensions.Parse(mode); Assert.Equal(Mode.Catch, result); mode = "cAtcH"; result = ModeExtensions.Parse(mode); Assert.Equal(Mode.Catch, result); mode = "╫ск╝╧Ш"; result = ModeExtensions.Parse(mode); Assert.Equal(Mode.Catch, result); }
public async Task ProcessAsync(MessageContext context, HttpApiClient api) { int uid; Mode mode; try { mode = ModeExtensions.Parse(ModeString); } catch { mode = default; } if (string.IsNullOrEmpty(Name)) { uid = await EnsureGetBindingIdAsync(context.UserId).ConfigureAwait(false); } else { var user = await EnsureGetUserInfo(Name, Mode.Standard).ConfigureAwait(false); uid = user.Id; } string url = $"https://info.osustuff.ri.mk/cn/users/{uid}/{s_modes.GetValueOrDefault(mode)}"; using var page = await Chrome.OpenNewPageAsync().ConfigureAwait(false); await page.SetViewportAsync(new ViewPortOptions { DeviceScaleFactor = Math.Sqrt(0.5), Width = 1058, Height = 80, }).ConfigureAwait(false); _ = await page.GoToAsync(url, WaitUntilNavigation.Networkidle0).ConfigureAwait(false); await page.WaitForTimeoutAsync(500).ConfigureAwait(false); var data = await page.ScreenshotDataAsync(new ScreenshotOptions { FullPage = true, Type = ScreenshotType.Jpeg, Quality = 100, }).ConfigureAwait(false); _ = await api.SendMessageAsync(context.Endpoint, Message.ByteArrayImage(data)).ConfigureAwait(false); }
public async Task ProcessAsync(MessageContext context, HttpApiClient api) { Mode?mode = default(Mode); try { if (!string.IsNullOrWhiteSpace(ModeString)) { mode = ModeExtensions.Parse(ModeString); } } catch { // Ignore when the mode string is invalid. return; } Message message; if (QQId != default) { var bindingInfo = await NewbieContext.Bindings.Where(b => b.UserId == QQId).FirstOrDefaultAsync().ConfigureAwait(false); if (bindingInfo is null) { await api.SendMessageAsync(context.Endpoint, "未绑定 osu! 账号。").ConfigureAwait(false); return; } var osuId = bindingInfo.OsuId; message = await QueryHelper.QueryByUserId(osuId, mode).ConfigureAwait(false); } else if (!string.IsNullOrEmpty(Name)) { message = await QueryHelper.QueryByUserName(Name, mode).ConfigureAwait(false); } else { return; } object sendResponse = null; if (!_memoryCache.TryGetValue(SendingFailureCacheKey, out int t) || t < Threshold) { sendResponse = await api.SendMessageAsync(context.Endpoint, message).ConfigureAwait(false); } if (sendResponse is null) { // 可能会假失败,即消息发出去了,但检测到失败。 //await api.SendMessageAsync(context.Endpoint, $"检测到发送失败,消息长度为{message.Raw.Length},[调试]将转换成 base64 发送。").ConfigureAwait(false); //await api.SendMessageAsync(context.Endpoint, Convert.ToBase64String(Encoding.UTF8.GetBytes(message.Raw))).ConfigureAwait(false); // 记录发送失败次数。72小时内失败2次即转图。 // 不是完全线程安全。 t = _memoryCache.Set(SendingFailureCacheKey, t + 1, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(3), Priority = CacheItemPriority.NeverRemove, SlidingExpiration = TimeSpan.FromDays(1), }); var text = message.Sections[0].Data["text"]; using var sr = new System.IO.StringReader(text); var lines = new List <string>(); while (sr.ReadLine() is string line) { // 分行发送发送失败的消息 //await api.SendMessageAsync(context.Endpoint, line).ConfigureAwait(false); lines.Add(line); } //lines.Add(string.Empty); //lines.Add("// 有会 html/css 的帮我弄好看点吗? 斜杠可怜"); using var page = await Chrome.OpenNewPageAsync().ConfigureAwait(false); await page.SetViewportAsync(new ViewPortOptions { DeviceScaleFactor = 3, Width = 360, Height = 202, }).ConfigureAwait(false); var style = "font-family: 'Source Han Sans CN', sans-serif;"; await page.SetContentAsync($@"<div lang=""zh-CN"" style=""{style}"">{string.Join("<br>", lines.Select(HttpUtility.HtmlEncode))}</div>").ConfigureAwait(false); var data = await page.ScreenshotDataAsync(new ScreenshotOptions { FullPage = true, }).ConfigureAwait(false); await api.SendMessageAsync(context.Endpoint, Message.ByteArrayImage(data)).ConfigureAwait(false); } }
public void Parse(string modeString, Mode expected) { var mode = ModeExtensions.Parse(modeString); Assert.Equal(expected, mode); }