private static bool Add(Dictionary <string, WatchedChannel> dict, ulong discordChannelId, string youtubeChannelId, string channelName, WatchType wType) { WatchedChannel wc; if (dict.ContainsKey(youtubeChannelId)) { if (dict[youtubeChannelId].ChannelsThatAreSubbed.Contains(discordChannelId)) { return(false); } dict[youtubeChannelId].ChannelsThatAreSubbed.Add(discordChannelId); return(true); } dict.Add(youtubeChannelId, wc = new WatchedChannel { ChannelId = youtubeChannelId, ChannelName = channelName, ChannelsThatAreSubbed = new List <ulong> { discordChannelId }, WatchedType = wType }); AddToQueue(wc); return(true); }
private static void AddToQueue(WatchedChannel wc) { _queue.Enqueue(wc); if (_timer != null) { _timer.Change(120000 / _queue.Count, 120000 / _queue.Count); } else { _timer = new Timer(RunSomethingAsync, null, 120000 / _queue.Count, 120000 / _queue.Count); } }
private static void RemoveFromQueue(WatchedChannel wc) { _queue = new Queue <WatchedChannel>(_queue.Where(s => s != wc)); if (_queue.Count > 0) { _timer.Change(120000 / _queue.Count, 120000 / _queue.Count); } else { _timer.Dispose(); _timer = null; } }
private static async Task CheckForNewContentAsync(WatchedChannel watched) { SearchResource.ListRequest listRequest = YoutubeS.Search.List("snippet"); listRequest.ChannelId = watched.ChannelId; listRequest.Type = "video"; listRequest.Order = SearchResource.ListRequest.OrderEnum.Date; listRequest.MaxResults = 1; if (watched.WatchedType == WatchType.Livestream) { listRequest.EventType = SearchResource.ListRequest.EventTypeEnum.Live; } SearchListResponse searchResponse; try { searchResponse = await listRequest.ExecuteAsync(); } catch (Exception ex) { Log.Error(ex, "YoutubeService threw an error"); return; } SearchResult data = searchResponse.Items.FirstOrDefault(v => v.Id.Kind == "youtube#video"); if (data != null && (watched.LastVideoId != null || watched.WatchedType == WatchType.Livestream) && data.Id.VideoId != watched.LastVideoId) { foreach (var channelId in watched.ChannelsThatAreSubbed) { var socketTextChannel = _client.GetChannel(channelId) as SocketTextChannel; if (socketTextChannel != null) { var eb = new EmbedBuilder() .WithTitle(data.Snippet.Title) .WithDescription($"{data.Snippet.ChannelTitle} {(watched.WatchedType == WatchType.Livestream ? "just went live!" : "just uploaded a new video!" )}") .WithUrl($"https://www.youtube.com/watch?v=" + $"{data.Id.VideoId}") .WithImageUrl(data.Snippet.Thumbnails.High.Url) .WithColor(new Color(0xb31217)); await socketTextChannel.SendMessageAsync("", embed : eb); } else //means we're no longer in the guild that contains this channel, or the channel was deleted { TryRemove(channelId, watched.ChannelId, watched.WatchedType); } } } watched.LastVideoId = data?.Id.VideoId; }