Exemple #1
0
 private void btnList_Click(object sender, EventArgs e)
 {
     using (VideoDownloader videoDownloader = new VideoDownloader())
     {
         Broadcasts broadcasts = videoDownloader.GetBroadcasts(txtUserName.Text);
         grdBroadcast.DataSource = broadcasts.broadcasts;
     }
 }
        public void DownloadMultiple()
        {
            _localFileActions.CreateUserListFile();
            List<string> activeUsers = new List<string>();

            while (true)
            {
                try
                {
                    DateTime whileStart = DateTime.Now;
                    DateTime nextWhile = whileStart.AddMilliseconds(_settings.UserSettings.MultipleUserListCheckPeriod);

                    var users = _localFileActions.GetUsersFromFile();
                    var usersToCheck = users.Except(activeUsers);
                    Console.WriteLine("Total users: " + users.Count);

                    using (VideoDownloader videoDownloader = new VideoDownloader(_settings))
                    {
                        foreach (var userToCheck in usersToCheck)
                        {
                            var liveStreamBroadcasts = videoDownloader.GetBroadcasts(userToCheck).broadcasts.Where(b => b.IsLiveStream);
                            foreach (Broadcast liveStreamBroadcast in liveStreamBroadcasts)
                            {
                                _multipleUsersBroadcasts.Add(new MultipleUserBroadcast { Broadcast = liveStreamBroadcast });
                            }
                        }
                    }

                    var initialBroadcasts = _multipleUsersBroadcasts.Where(ub => ub.BroadcastStatus == EnumBroadcastStatus.Initial);
                    foreach (var initialBroadcast in initialBroadcasts)
                    {
                        initialBroadcast.BroadcastStatus = EnumBroadcastStatus.Downloading;
                        new Thread(() =>
                        {
                            using (VideoDownloader videoDownloader = new VideoDownloader(_settings))
                            {
                                videoDownloader.DownloadVideos(initialBroadcast.Broadcast);
                            }
                            initialBroadcast.BroadcastStatus = EnumBroadcastStatus.Finished;
                        }).Start();
                    }

                    activeUsers =
                     _multipleUsersBroadcasts.Where(ub => ub.BroadcastStatus == EnumBroadcastStatus.Downloading)
                         .Select(ub => ub.Broadcast.username)
                         .ToList();

                    Console.WriteLine("Active users: " + activeUsers.Count);

                    double totalTime = (DateTime.Now - whileStart).TotalMilliseconds;
                    Console.WriteLine("Total time: " + totalTime + " ms");

                    double waitTime = (nextWhile - DateTime.Now).TotalMilliseconds;
                    if (waitTime > 0)
                    {
                        Console.WriteLine("Wait time: " + waitTime + " ms");
                        Thread.Sleep((int)waitTime);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }