private int GetNumberOfVideosWithId(string id, StreamHosterRSSFeed response) { int count = 0; for (int i = 0; i < response.Channel.Item.Length; i++) { if (response.Channel.Item[i].StreamMediaId == id.Remove(id.Length - 2)) { count++; } } return(count); }
private List <string> GetVideoIdsWithId(string id, int videoCount, StreamHosterRSSFeed response) { List <string> ids = new List <string>(); for (int i = 0; i < response.Channel.Item.Length; i++) { if (ids.Count == videoCount) { break; } if (response.Channel.Item[i].StreamMediaId == id.Remove(id.Length - 2)) { ids.Add(response.Channel.Item[i].MediaContentId); } } return(ids); }
private async Task ArchiveVideo(StreamHosterRSSFeed response, DataModels.Channel channel) { List <Video> videos = new List <Video>(); streamHandler.TryGetValue(channel.Username, out videos); var ids = GetVideoIdsWithId(channel.ChannelKey, videos.Count, response); videos.Reverse(); //reverse list content for (int i = 0; i < videos.Count; i++) //go through the response as well as the dict and associate each stream to its object { var video = videos[i]; video.StreamID = ids[i]; await Save(video.Id, video); } channel.InitialStreamCount = 0; await Save(channel.Id, channel); streamHandler.TryRemove(channel.Username, out _); }
private async Task PollVideo(DataModels.Channel channel) //This thread handles checking if the stream is still live { var archivedVideo = GetVideo(channel); //Get video information from channel await ClearChannelStreamInfo(channel); //clear all channel info if (streamHandler.ContainsKey(channel.Username)) //check if the concurrent dict has the key with the specified username, if it does this means that a thread has already been spawned looking for this video in the rss feed { List <Video> oldvideos = new List <Video>(); List <Video> videos = new List <Video>(); streamHandler.TryGetValue(channel.Username, out videos); //add the video info to the list of videos in the concurrent dictionary streamHandler.TryGetValue(channel.Username, out oldvideos); videos.Add(archivedVideo); streamHandler.TryUpdate(channel.Username, videos, oldvideos); } else { streamHandler.TryAdd(channel.Username, new List <Video> { archivedVideo }); //create a new list and add the archived video info into the list StreamHosterRSSFeed initialResponse = await CallXML <StreamHosterRSSFeed>("https://c.streamhoster.com/feed/WxsdDM/mAe0epZsixC/iAahx7oSswv?format=mrss"); //feed contains all the archived videos and we use it to get the stream id for each video if (initialResponse != null && initialResponse.Channel.Item != null) { channel.InitialStreamCount = GetNumberOfVideosWithId(channel.ChannelKey, initialResponse); //check for initial count of the rss feed await Save(channel.Id, channel); } while (true) //looking for the initial count of the feed + how many items are in the video list. The video list is retreived by the username of the current user { try { await Task.Delay(30000); StreamHosterRSSFeed response = await CallXML <StreamHosterRSSFeed>("https://c.streamhoster.com/feed/WxsdDM/mAe0epZsixC/iAahx7oSswv?format=mrss"); if (response != null) { List <Video> videos = new List <Video>(); streamHandler.TryGetValue(channel.Username, out videos); Debug d = new Debug(); d.Id = Guid.NewGuid().ToString(); d.Timestamp = DateTime.UtcNow; d.Message = channel.Username + " | " + (GetNumberOfVideosWithId(channel.ChannelKey, response)); await Save(d.Id, d); log.Warning(channel.Username + " is still in the while loop"); log.Warning(channel.Username + " " + GetNumberOfVideosWithId(channel.ChannelKey, response) + " Initial Count: " + channel.InitialStreamCount + " Video Count:" + videos.Count); if (response.Channel.Item != null && (GetNumberOfVideosWithId(channel.ChannelKey, response) >= channel.InitialStreamCount + videos.Count)) // we keep polling until the feed is updated to the number of videos (the initial count & concurrent dict count) { Debug d1 = new Debug(); d1.Id = Guid.NewGuid().ToString(); d.Timestamp = DateTime.UtcNow; d1.Message = channel.Username + " | Videos Found | " + channel.InitialStreamCount + videos.Count; await Save(d1.Id, d1); Console.WriteLine("Videos Found"); await ArchiveVideo(response, channel); break; } } else { log.Warning("response is null in poll for " + channel.Username); Console.WriteLine("response is null in poll for " + channel.Username); } } catch (Exception e) { log.Error(e, "Error in Poll"); break; } } } }