public static string GetOcrResultString(IOcrService ocrService, string filePath, FenceConfiguration[] fences = null) { var utcNow = SystemClock.Instance.GetCurrentInstant().InUtc(); var channelTimeZone = DateTimeZoneProviders.Tzdb["Europe/Zurich"]; var result = ocrService.AddRaidAsync(typeof(i18n), utcNow, channelTimeZone, filePath, 4, fences, true).Result; return(result.Message); }
public async Task OcrAsync() { try { var utcNow = SystemClock.Instance.GetCurrentInstant().InUtc(); // Subtract around 30 seconds to account for the delay to take and send a screenshot utcNow = utcNow.Minus(Duration.FromSeconds(30)); using (var httpClient = new HttpClient()) { foreach (var attachment in Context.Message.Attachments) { if (!await IsImageUrlAsync(httpClient, attachment.Url)) { continue; } var tempImageFile = string.Empty; try { tempImageFile = Path.GetTempFileName() + "." + attachment.Url.Split('.').Last(); await DownloadAsync(httpClient, new Uri(attachment.Url), tempImageFile); var response = OcrService.AddRaidAsync(typeof(i18n), utcNow, ChannelTimeZone, tempImageFile, InteractiveReactionLimit, Fences, false); await ReplyWithInteractive(() => response, LocalizationService.Get(typeof(i18n), "Raids_Messages_Ocr_Successful_Title")); } finally { try { if (File.Exists(tempImageFile)) { File.Delete(tempImageFile); } } catch (Exception) { // Ignore } } } } } catch (Exception ex) { var innerstEx = ex.GetInnermostException(); Console.WriteLine(innerstEx.Message); Console.WriteLine(innerstEx.StackTrace); await ReplyFailureAsync(LocalizationService.Get(typeof(i18n), "Raids_Errors_Unexpected", innerstEx.Message)); } }
private async Task HandleOcrAsync(DiscordMessage message) { _logger.Trace($"OcrProcessor::HandleOcrAsync [Message={message.Id}]"); using (var httpClient = new HttpClient()) { foreach (var attachment in message.Attachments) { if (!await httpClient.IsImageUrlAsync(attachment.Url)) { continue; } var tempImageFile = string.Empty; try { tempImageFile = Path.GetTempFileName() + "." + attachment.Url.Split('.').Last(); await httpClient.DownloadAsync(new Uri(attachment.Url), tempImageFile); var response = await _ocrService.AddRaidAsync(tempImageFile, false); if (response == null || !response.IsSuccess)// || !response.IsRaidImage) { _logger.Error($"Failed to parse ocr, invalid raid image."); continue; } if (string.IsNullOrEmpty(response.Gym)) { await message.RespondAsync($"{message.Author.Mention} Failed to parse gym name '{response.Gym}'."); //TODO: Ask for gym name. continue; } if (response.IsRaidBoss) { var pokeId = FindPokemon(response.Pokemon); if (pokeId == 0) { pokeId = await AskForPokemon(message); if (pokeId == 0) { pokeId = _config.DefaultRaidBoss; await message.RespondAsync($"{message.Author.Mention} Failed to parse raid boss name, assuming {Database.Instance.Pokemon[pokeId]}."); } } if (await HandleRaidOcr(response, message, pokeId)) { var pokemonName = Database.Instance.Pokemon[pokeId]; await message.RespondAsync($"{message.Author.Mention} Reported {pokemonName} raid at {response.Gym} that's ending in {response.RaidTimer.ToReadableString()}."); //(Different plus total left = 45) - DateTime.Now //var now = DateTime.Now; //var endTime = now.AddMinutes(response.RaidTimer.Minutes); //var startTime = endTime.Subtract(new TimeSpan(0, 45, 0)); //var spawnTime = endTime.Subtract(new TimeSpan(1, 45, 0)); } } else { var level = response.EggLevel; if (level == 0) { level = await AskForEggLevel(message); if (level == 0) { level = _config.DefaultRaidLevel; await message.RespondAsync($"{message.Author.Mention} Failed to parse egg level, assuming level 5."); } } if (await HandleEggOcr(response, message, level)) { await message.RespondAsync($"{message.Author.Mention} Reported level {response.EggLevel} raid egg at {response.Gym} that's starting in {response.EggTimer.ToReadableString()}."); } } //await message.DeleteAsync(); } catch (Exception ex) { await message.RespondAsync($"{message.Author.Mention} Error: {ex.ToString()}"); var dir = Path.Combine(Directory.GetCurrentDirectory(), Strings.FailedOcrFolder); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Copy(tempImageFile, Path.Combine(dir, Path.GetFileName(tempImageFile))); } } } }