public XkcdComicVM ComicObtenerPorId(int idComic) { _jsS = new JavaScriptSerializer(); var url = $"https://xkcd.com/" + idComic + "/info.0.json"; var comic = new XkcdComic(); var jsonresponse = string.Empty; using (_client = new HttpClient()) { var httpResp = _client.GetAsync(url).Result; if (httpResp.IsSuccessStatusCode) { comic = _jsS.Deserialize <XkcdComic>(httpResp.Content.ReadAsStringAsync().Result); } else { // I'm creating an empty object so it will be redirected to the home controller if you // type Comic/404, comic/0, or any other invalid number directly into de address bar, since we know not // which direction you're moving towards. var comicDelDia = this.ComicObtener(); return(comicDelDia); } } var viewModel = new XkcdComicVM() { Comic = comic, IdComic = comic.Num, NextId = VerifyNext(comic.Num, "next"), PrevId = VerifyNext(comic.Num, "previous") }; return(viewModel); }
public XkcdComicVM MyFavourite() { //Reminds me of Éricka =') _jsS = new JavaScriptSerializer(); var comic = new XkcdComic(); var url = $"https://xkcd.com/162/info.0.json"; var jsonResponse = string.Empty; using (_client = new HttpClient()) { jsonResponse = _client.GetStringAsync(url).Result; comic = _jsS.Deserialize <XkcdComic>(jsonResponse); } var viewModel = new XkcdComicVM() { Comic = comic, IdComic = comic.Num, NextId = VerifyNext(comic.Num, "next"), PrevId = VerifyNext(comic.Num, "previous") }; return(viewModel); }
public XkcdComicVM ComicObtener() { _jsS = new JavaScriptSerializer(); var comic = new XkcdComic(); var jsonresponse = string.Empty; using (_client = new HttpClient()) { var httpResp = _client.GetAsync(_urlHome).Result; if (httpResp.IsSuccessStatusCode) { comic = _jsS.Deserialize <XkcdComic>(httpResp.Content.ReadAsStringAsync().Result); } } SetLastComicId(comic.Num); var viewModel = new XkcdComicVM() { Comic = comic, IdComic = comic.Num, NextId = VerifyNext(comic.Num, "next"), PrevId = VerifyNext(comic.Num, "previous") }; return(viewModel); }
public async Task RandomAsync(CommandContext ctx) { XkcdComic comic = await XkcdService.GetRandomComicAsync(); if (comic is null) { throw new CommandFailedException("Failed to retrieve comic from xkcd."); } await ctx.RespondAsync(embed : comic.ToDiscordEmbedBuilder(this.ModuleColor)); }
public async Task ByIdAsync(CommandContext ctx, [Description("Comic ID.")] int?id = null) { XkcdComic comic = await XkcdService.GetComicAsync(id); if (comic is null) { throw new CommandFailedException("Failed to retrieve comic from xkcd."); } await ctx.RespondAsync(embed : comic.ToDiscordEmbedBuilder(this.ModuleColor)); }
private Task PrintComicAsync(CommandContext ctx, XkcdComic comic) { return(ctx.RespondWithLocalizedEmbedAsync(emb => { emb.WithTitle($"xkcd #{comic.Id} - {comic.Title}"); emb.WithImageUrl(comic.ImageUrl); string?url = XkcdService.CreateUrlForComic(comic.Id); if (url is { }) { emb.WithUrl(url); } emb.WithColor(this.ModuleColor); emb.WithLocalizedFooter("fmt-xkcd", null, $"{comic.Month}/{comic.Year}"); }));
public static DiscordEmbedBuilder ToDiscordEmbedBuilder(this XkcdComic comic, DiscordColor?color = null) { var emb = new DiscordEmbedBuilder { Title = $"xkcd #{comic.Id} : {comic.Title}", ImageUrl = comic.ImageUrl, Url = XkcdService.CreateUrlForComic(comic.Id) }; if (!(color is null)) { emb.WithColor(color.Value); } emb.WithFooter($"Publish date: {comic.Month}/{comic.Year}"); return(emb); }
private static void OfflineSetup() { var token = System.IO.File.ReadAllText("token.txt"); Bot = new DiscordClient(new DiscordConfiguration { Token = token, TokenType = TokenType.Bot }); Http = new HttpClient(); Random = new Random(); Bot.MessageCreated += async args => { if (!args.MentionedUsers.Contains(Bot.CurrentUser)) { return; } UIMessage message = null; Console.WriteLine(args.Message.Content); string command = args.Message.Content.Replace(MentionSelf, "").ToLower(); Console.WriteLine($"Received command: {command}"); if (command.Contains("test")) { message = new TestBook(); } if (command.Contains("xkcd")) { message = new XkcdComic(); } if (message != null) { await Task.Run(() => CreateUIMessage(message, args)); } }; // TODO bot reacts to mentions and not this crap }
private async void ResumeComic() { if (Comic == null) { var comicUri = ComicSettings.XkcdSettings.LastViewedUri; XkcdComic xkcd = null; if (comicUri == null) { xkcd = (XkcdComic)await ComicProvider.LoadLatestComicAsync(); } else { xkcd = (XkcdComic)await ComicProvider.LoadComicAsync(comicUri); } Comic = xkcd; Description = xkcd.Description; } }
/// <summary> /// Method to verify if there is a previous or following comic /// </summary> /// <param name="currentId">The id of the current Comic</param> /// <param name="direction"> The direction of the clicked arrow: Next or Previous</param> /// <returns></returns> public int VerifyNext(int currentId, string direction) { var url = string.Empty; var exists = false; var comic = new XkcdComic(); var returnId = -1; var httpResponse = new HttpResponseMessage(); //we have to actually jump twice, if we dont get a successfull response on the second jump, then // it means we've reached the last comic at either direction. // this is coded for the 404 use case. for (int i = 0; i < 2; i++) { if (direction.Equals("next", System.StringComparison.InvariantCultureIgnoreCase)) { currentId++; } else { currentId--; } using (_client = new HttpClient()) { url = $"https://xkcd.com/" + currentId + "/info.0.json"; httpResponse = _client.GetAsync(url).Result; exists = httpResponse.IsSuccessStatusCode; } // if it does exist, then we break the for loop. The next/previous button will show on the view. if (exists) { returnId = _jsS.Deserialize <XkcdComic>(httpResponse.Content.ReadAsStringAsync().Result).Num; break; } } return(returnId); }