private async Task <Tuple <bool, string> > GetStreamStatus(StreamNotificationConfig stream) { bool isLive; string response; JObject data; switch (stream.Type) { case StreamNotificationConfig.StreamType.Hitbox: response = await SearchHelper.GetResponseStringAsync($"https://api.hitbox.tv/media/status/{stream.Username}"); data = JObject.Parse(response); isLive = data["media_is_live"].ToString() == "1"; return(new Tuple <bool, string>(isLive, data["media_views"].ToString())); case StreamNotificationConfig.StreamType.Twitch: response = await SearchHelper.GetResponseStringAsync($"https://api.twitch.tv/kraken/streams/{Uri.EscapeUriString(stream.Username)}"); data = JObject.Parse(response); isLive = !string.IsNullOrWhiteSpace(data["stream"].ToString()); return(new Tuple <bool, string>(isLive, isLive ? data["stream"]["viewers"].ToString() : "0")); default: break; } return(new Tuple <bool, string>(false, "0")); }
private async Task <Tuple <bool, string> > GetStreamStatus(StreamNotificationConfig stream) { bool isLive; string response; JObject data; Tuple <bool, string> result; switch (stream.Type) { case StreamNotificationConfig.StreamType.Hitbox: var hitboxUrl = $"https://api.hitbox.tv/media/status/{stream.Username}"; if (cachedStatuses.TryGetValue(hitboxUrl, out result)) { return(result); } response = await SearchHelper.GetResponseStringAsync(hitboxUrl).ConfigureAwait(false); data = JObject.Parse(response); isLive = data["media_is_live"].ToString() == "1"; result = new Tuple <bool, string>(isLive, data["media_views"].ToString()); cachedStatuses.TryAdd(hitboxUrl, result); return(result); case StreamNotificationConfig.StreamType.Twitch: var twitchUrl = $"https://api.twitch.tv/kraken/streams/{Uri.EscapeUriString(stream.Username)}"; if (cachedStatuses.TryGetValue(twitchUrl, out result)) { return(result); } response = await SearchHelper.GetResponseStringAsync(twitchUrl).ConfigureAwait(false); data = JObject.Parse(response); isLive = !string.IsNullOrWhiteSpace(data["stream"].ToString()); result = new Tuple <bool, string>(isLive, isLive ? data["stream"]["viewers"].ToString() : "0"); cachedStatuses.TryAdd(twitchUrl, result); return(result); case StreamNotificationConfig.StreamType.Beam: var beamUrl = $"https://beam.pro/api/v1/channels/{stream.Username}"; if (cachedStatuses.TryGetValue(beamUrl, out result)) { return(result); } response = await SearchHelper.GetResponseStringAsync(beamUrl).ConfigureAwait(false); data = JObject.Parse(response); isLive = data["online"].ToObject <bool>() == true; result = new Tuple <bool, string>(isLive, data["viewersCurrent"].ToString()); cachedStatuses.TryAdd(beamUrl, result); return(result); default: break; } return(new Tuple <bool, string>(false, "0")); }
private Func <CommandEventArgs, Task> TrackStream(StreamNotificationConfig.StreamType type) => async e => { var username = e.GetArg("username"); if (string.IsNullOrWhiteSpace(username)) { return; } var config = SpecificConfigurations.Default.Of(e.Server.Id); var stream = new StreamNotificationConfig { ServerId = e.Server.Id, ChannelId = e.Channel.Id, Username = username, Type = type, }; var exists = config.ObservingStreams.Contains(stream); if (exists) { await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel."); } Tuple <bool, string> data; try { data = await GetStreamStatus(stream); } catch { await e.Channel.SendMessage(":anger: Stream probably doesn't exist."); return; } var msg = $"Stream is currently **{(data.Item1 ? "ONLINE" : "OFFLINE")}** with **{data.Item2}** viewers"; if (data.Item1) { if (type == StreamNotificationConfig.StreamType.Hitbox) { msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; } else if (type == StreamNotificationConfig.StreamType.Twitch) { msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; } else if (type == StreamNotificationConfig.StreamType.YoutubeGaming) { msg += $"\n`Here is the Link:` not implemented yet - {stream.Username}"; } } stream.LastStatus = data.Item1; if (!exists) { msg = $":ok: I will notify this channel when status changes.\n{msg}"; } await e.Channel.SendMessage(msg); config.ObservingStreams.Add(stream); };
private async Task<Tuple<bool, string>> GetStreamStatus(StreamNotificationConfig stream) { bool isLive; string response; JObject data; Tuple<bool, string> result; switch (stream.Type) { case StreamNotificationConfig.StreamType.Hitbox: var hitboxUrl = $"https://api.hitbox.tv/media/status/{stream.Username}"; if (cachedStatuses.TryGetValue(hitboxUrl, out result)) return result; response = await SearchHelper.GetResponseStringAsync(hitboxUrl); data = JObject.Parse(response); isLive = data["media_is_live"].ToString() == "1"; result = new Tuple<bool, string>(isLive, data["media_views"].ToString()); cachedStatuses.TryAdd(hitboxUrl, result); return result; case StreamNotificationConfig.StreamType.Twitch: var twitchUrl = $"https://api.twitch.tv/kraken/streams/{Uri.EscapeUriString(stream.Username)}"; if (cachedStatuses.TryGetValue(twitchUrl, out result)) return result; response = await SearchHelper.GetResponseStringAsync(twitchUrl); data = JObject.Parse(response); isLive = !string.IsNullOrWhiteSpace(data["stream"].ToString()); result = new Tuple<bool, string>(isLive, isLive ? data["stream"]["viewers"].ToString() : "0"); cachedStatuses.TryAdd(twitchUrl, result); return result; case StreamNotificationConfig.StreamType.Beam: var beamUrl = $"https://beam.pro/api/v1/channels/{stream.Username}"; if (cachedStatuses.TryGetValue(beamUrl, out result)) return result; response = await SearchHelper.GetResponseStringAsync(beamUrl); data = JObject.Parse(response); isLive = data["online"].ToObject<bool>() == true; result = new Tuple<bool, string>(isLive, data["viewersCurrent"].ToString()); cachedStatuses.TryAdd(beamUrl, result); return result; default: break; } return new Tuple<bool, string>(false, "0"); }
private Func<CommandEventArgs, Task> TrackStream(StreamNotificationConfig.StreamType type) => async e => { var username = e.GetArg("username")?.ToLowerInvariant(); if (string.IsNullOrWhiteSpace(username)) return; var config = SpecificConfigurations.Default.Of(e.Server.Id); var stream = new StreamNotificationConfig { ServerId = e.Server.Id, ChannelId = e.Channel.Id, Username = username, Type = type, }; var exists = config.ObservingStreams.Contains(stream); if (exists) { await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel.").ConfigureAwait(false); return; } Tuple<bool, string> data; try { data = await GetStreamStatus(stream).ConfigureAwait(false); } catch { await e.Channel.SendMessage(":anger: Stream probably doesn't exist.").ConfigureAwait(false); return; } var msg = $"Stream is currently **{(data.Item1 ? "ONLINE" : "OFFLINE")}** with **{data.Item2}** viewers"; if (data.Item1) if (type == StreamNotificationConfig.StreamType.Hitbox) msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; else if (type == StreamNotificationConfig.StreamType.Twitch) msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; else if (type == StreamNotificationConfig.StreamType.Beam) msg += $"\n`Here is the Link:`【 https://beam.pro/{stream.Username}/ 】"; else if (type == StreamNotificationConfig.StreamType.YoutubeGaming) msg += $"\n`Here is the Link:` not implemented yet - {stream.Username}"; stream.LastStatus = data.Item1; if (!exists) msg = $":ok: I will notify this channel when status changes.\n{msg}"; await e.Channel.SendMessage(msg).ConfigureAwait(false); config.ObservingStreams.Add(stream); };
private Func <CommandEventArgs, Task> TrackStream(StreamNotificationConfig.StreamType type) => async e => { var username = e.GetArg("username")?.ToLowerInvariant(); if (string.IsNullOrWhiteSpace(username)) { return; } var config = SpecificConfigurations.Default.Of(e.Server.Id); var stream = new StreamNotificationConfig { ServerId = e.Server.Id, ChannelId = e.Channel.Id, Username = username, Type = type, }; var exists = config.ObservingStreams.Contains(stream); if (exists) { await e.Channel.SendMessage(":anger: Ich benachrichtige diesen Channel schon über diesen Stream.").ConfigureAwait(false); } Tuple <bool, string> data; try { data = await GetStreamStatus(stream).ConfigureAwait(false); } catch { await e.Channel.SendMessage(":anger: Stream existiert wahrscheinlich nicht.").ConfigureAwait(false); return; } var msg = $"Stream ist derzeit **{(data.Item1 ? "ONLINE" : "OFFLINE")}** mit **{data.Item2}** Zuschauern"; if (data.Item1) { if (type == StreamNotificationConfig.StreamType.Hitbox) { msg += $"\n`Hier ist der Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; } else if (type == StreamNotificationConfig.StreamType.Twitch) { msg += $"\n`Hier ist der Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; } else if (type == StreamNotificationConfig.StreamType.Beam) { msg += $"\n`Here is the Link:`【 https://beam.pro/{stream.Username}/ 】"; } else if (type == StreamNotificationConfig.StreamType.YoutubeGaming) { msg += $"\n`Hier ist der Link:`【 Derzeit nicht implementiert - {stream.Username} 】"; } } stream.LastStatus = data.Item1; if (!exists) { msg = $":ok: Ich werde diesen Channel benachrichtigen wenn sich der Status ändert.\n{msg}"; } await e.Channel.SendMessage(msg).ConfigureAwait(false); config.ObservingStreams.Add(stream); };