public EmbedBuilder GetEmbed(FollowedStream fs, StreamStatus status, ulong guildId) { var embed = new EmbedBuilder().WithTitle(fs.Username) .WithUrl(GetLink(fs)) .AddField(efb => efb.WithName(GetText(fs, "status")) .WithValue(status.IsLive ? "Online" : "Offline") .WithIsInline(true)) .AddField(efb => efb.WithName(GetText(fs, "viewers")) .WithValue(status.IsLive ? status.Views : "-") .WithIsInline(true)) .AddField(efb => efb.WithName(GetText(fs, "platform")) .WithValue(fs.Type.ToString()) .WithIsInline(true)) .WithColor(status.IsLive ? Mitternacht.MitternachtBot.OkColor : Mitternacht.MitternachtBot.ErrorColor); return(embed); }
public async Task <StreamStatus> GetStreamStatus(FollowedStream stream, bool checkCache = true) { string response; StreamStatus result; switch (stream.Type) { case FollowedStream.FollowedStreamType.Smashcast: var hitboxUrl = $"https://api.hitbox.tv/media/status/{stream.Username.ToLowerInvariant()}"; if (checkCache && _cachedStatuses.TryGetValue(hitboxUrl, out result)) { return(result); } using (var http = new HttpClient()) { response = await http.GetStringAsync(hitboxUrl).ConfigureAwait(false); } var hbData = JsonConvert.DeserializeObject <HitboxResponse>(response); if (!hbData.Success) { throw new StreamNotFoundException($"{stream.Username} [{stream.Type}]"); } result = new StreamStatus() { IsLive = hbData.IsLive, ApiLink = hitboxUrl, Views = hbData.Views }; _cachedStatuses.AddOrUpdate(hitboxUrl, result, (key, old) => result); return(result); case FollowedStream.FollowedStreamType.Twitch: var twitchUrl = $"https://api.twitch.tv/kraken/streams/{Uri.EscapeUriString(stream.Username.ToLowerInvariant())}?client_id=67w6z9i09xv2uoojdm9l0wsyph4hxo6"; if (checkCache && _cachedStatuses.TryGetValue(twitchUrl, out result)) { return(result); } using (var http = new HttpClient()) { response = await http.GetStringAsync(twitchUrl).ConfigureAwait(false); } var twData = JsonConvert.DeserializeObject <TwitchResponse>(response); if (twData.Error != null) { throw new StreamNotFoundException($"{stream.Username} [{stream.Type}]"); } result = new StreamStatus() { IsLive = twData.IsLive, ApiLink = twitchUrl, Views = twData.Stream?.Viewers.ToString() ?? "0" }; _cachedStatuses.AddOrUpdate(twitchUrl, result, (key, old) => result); return(result); case FollowedStream.FollowedStreamType.Mixer: var beamUrl = $"https://mixer.com/api/v1/channels/{stream.Username.ToLowerInvariant()}"; if (checkCache && _cachedStatuses.TryGetValue(beamUrl, out result)) { return(result); } using (var http = new HttpClient()) { response = await http.GetStringAsync(beamUrl).ConfigureAwait(false); } var bmData = JsonConvert.DeserializeObject <BeamResponse>(response); if (bmData.Error != null) { throw new StreamNotFoundException($"{stream.Username} [{stream.Type}]"); } result = new StreamStatus() { IsLive = bmData.IsLive, ApiLink = beamUrl, Views = bmData.ViewersCurrent.ToString() }; _cachedStatuses.AddOrUpdate(beamUrl, result, (key, old) => result); return(result); default: break; } return(null); }