private void SearchChannel(string channel, VideoType videoType, LoadLimitType loadLimit, DateTime loadFrom, DateTime loadTo, int loadLastVods)
        {
            if (string.IsNullOrWhiteSpace(channel))
            {
                throw new ArgumentNullException(nameof(channel));
            }

            string channelId = GetChannelIdByName(channel);

            ObservableCollection <TwitchVideo> videos = new ObservableCollection <TwitchVideo>();

            string broadcastTypeParam = null;

            if (videoType == VideoType.Broadcast)
            {
                broadcastTypeParam = "archive";
            }
            else if (videoType == VideoType.Highlight)
            {
                broadcastTypeParam = "highlight";
            }
            else if (videoType == VideoType.Upload)
            {
                broadcastTypeParam = "upload";
            }
            else
            {
                throw new ApplicationException("Unsupported video type '" + videoType.ToString() + "'");
            }

            string channelVideosUrl = string.Format(CHANNEL_VIDEOS_URL, channelId);

            DateTime fromDate = DateTime.Now;
            DateTime toDate   = DateTime.Now;

            if (loadLimit == LoadLimitType.Timespan)
            {
                fromDate = loadFrom;
                toDate   = loadTo;
            }

            int offset = 0;
            int total  = 0;
            int sum    = 0;

            bool stop = false;

            do
            {
                using (WebClient webClient = CreateTwitchWebClient())
                {
                    webClient.QueryString.Add("broadcast_type", broadcastTypeParam);
                    webClient.QueryString.Add("limit", TWITCH_MAX_LOAD_LIMIT.ToString());
                    webClient.QueryString.Add("offset", offset.ToString());

                    string result = webClient.DownloadString(channelVideosUrl);

                    JObject videosResponseJson = JObject.Parse(result);

                    if (videosResponseJson != null)
                    {
                        if (total == 0)
                        {
                            total = videosResponseJson.Value <int>("_total");
                        }

                        foreach (JObject videoJson in videosResponseJson.Value <JArray>("videos"))
                        {
                            sum++;

                            if (videoJson.Value <string>("_id").StartsWith("v"))
                            {
                                TwitchVideo video = ParseVideo(videoJson);

                                if (loadLimit == LoadLimitType.LastVods)
                                {
                                    videos.Add(video);

                                    if (sum >= loadLastVods)
                                    {
                                        stop = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    DateTime recordedDate = video.RecordedDate;

                                    if (recordedDate.Date >= fromDate.Date && recordedDate.Date <= toDate.Date)
                                    {
                                        videos.Add(video);
                                    }

                                    if (recordedDate.Date < fromDate.Date)
                                    {
                                        stop = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                offset += TWITCH_MAX_LOAD_LIMIT;
            } while (!stop && sum < total);

            Videos = videos;
        }
Exemple #2
0
        private ObservableCollection <TwitchVideo> SearchChannel(string channel, VideoType videoType, LoadLimitType loadLimit, DateTime loadFrom, DateTime loadTo, int loadLastVods)
        {
            if (string.IsNullOrWhiteSpace(channel))
            {
                throw new ArgumentNullException(nameof(channel));
            }

            string channelId = GetChannelIdByName(channel);

            ObservableCollection <TwitchVideo> videos = new ObservableCollection <TwitchVideo>();

            string broadcastTypeParam;

            if (videoType == VideoType.Broadcast)
            {
                broadcastTypeParam = "archive";
            }
            else if (videoType == VideoType.Highlight)
            {
                broadcastTypeParam = "highlight";
            }
            else if (videoType == VideoType.Upload)
            {
                broadcastTypeParam = "upload";
            }
            else
            {
                throw new ApplicationException("Unsupported video type '" + videoType.ToString() + "'");
            }

            DateTime fromDate = DateTime.Now;
            DateTime toDate   = DateTime.Now;

            if (loadLimit == LoadLimitType.Timespan)
            {
                fromDate = loadFrom;
                toDate   = loadTo;
            }

            int    sum    = 0;
            bool   stop   = false;
            string cursor = null;

            do
            {
                using (WebClient webClient = CreateApiWebClient())
                {
                    webClient.QueryString.Add("user_id", channelId);
                    webClient.QueryString.Add("type", broadcastTypeParam);
                    webClient.QueryString.Add("first", TWITCH_MAX_LOAD_LIMIT.ToString());

                    if (!string.IsNullOrWhiteSpace(cursor))
                    {
                        webClient.QueryString.Add("after", cursor);
                    }

                    WaitForNextRequest();

                    string result = webClient.DownloadString(VIDEOS_URL);

                    SetNextRequestTime(webClient);

                    JObject videosResponseJson = JObject.Parse(result);

                    if (videosResponseJson != null)
                    {
                        JArray videosJson = videosResponseJson.Value <JArray>("data");

                        if (videosJson.Count == 0)
                        {
                            stop = true;
                        }
                        else
                        {
                            JObject paginationJson = videosResponseJson.Value <JObject>("pagination");

                            if (paginationJson.ContainsKey("cursor"))
                            {
                                cursor = paginationJson.Value <string>("cursor");
                            }

                            foreach (JObject videoJson in videosJson)
                            {
                                sum++;

                                TwitchVideo video = ParseVideo(videoJson);

                                if (loadLimit == LoadLimitType.LastVods)
                                {
                                    videos.Add(video);

                                    if (sum >= loadLastVods)
                                    {
                                        stop = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    DateTime recordedDate = video.RecordedDate;

                                    if (recordedDate.Date >= fromDate.Date && recordedDate.Date <= toDate.Date)
                                    {
                                        videos.Add(video);
                                    }

                                    if (recordedDate.Date < fromDate.Date)
                                    {
                                        stop = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            } while (!stop);

            return(videos);
        }