示例#1
0
        public async Task HttpDownLoadTest()
        {
            const int roomId  = 23058;
            var       playUrl = BililiveApi.GetPlayUrlAsync(roomId).Result;

            Assert.IsTrue(playUrl.StartsWith(@"https://"));

            const string path = @"D:\Downloads\test.flv";

            var instance = new HttpDownLoad(playUrl, path, true);

#pragma warning disable 4014
            Task.Delay(10000).ContinueWith(task =>
#pragma warning restore 4014
            {
                instance.Dispose();
            });
            await instance.Start().ContinueWith(task =>
            {
                Console.WriteLine(task.Status);
                Console.WriteLine(task.IsCanceled);
                Console.WriteLine(task.IsCompleted);
                Console.WriteLine(task.IsCompletedSuccessfully);
                instance.Dispose();
            });
        }
示例#2
0
        public static async Task HttpDownLoadRecordTask(string url, string path, CancellationTokenSource cts)
        {
            var instance   = new HttpDownLoad(url, path, true);
            var ctsEndTask = new CancellationTokenSource();

            cts.Token.Register(() =>
            {
                if (!ctsEndTask.IsCancellationRequested)
                {
                    ctsEndTask.Cancel();
                }
            });
            ctsEndTask.Token.Register(() => { instance.Stop(); });

            try
            {
                await instance.Start();

                ctsEndTask.Cancel();
            }
            catch
            {
                // ignored
            }
        }
示例#3
0
        public void DownloadFileAndCheckSizeTest()
        {
            const string url  = @"https://raw.githubusercontent.com/HMBSbige/Text_Translation/master/TrayStatus/ZH-CN.lang";
            const string path = @"D:\Downloads\ZH-CN.lang";

            var instance = new HttpDownLoad(url, path, true);

            instance.Start().Wait();

            var size = Util.GetFileSize(path);

            Assert.AreEqual(size, 28064);
        }
示例#4
0
        public void HttpDownLoadTest()
        {
            var recorder = new BilibiliLiveRecorder(3);

            Assert.ThrowsExceptionAsync <ArgumentException>(async() => { await recorder.GetLiveUrl(); }).Wait();

            recorder.Refresh().Wait();

            var urls      = Task.Run(recorder.GetLiveUrl).Result.ToArray();
            var urlNumber = urls.Length;

            Assert.IsTrue(urlNumber > 1);

            var          url      = urls[0];
            const string path     = @"D:\Downloads\test.flv";
            var          instance = new HttpDownLoad(url, path, true);

            Task.Run(async() =>
            {
                await instance.Start();
            });
            Thread.Sleep(10000);
            instance.Stop();
        }
示例#5
0
        public async Task Start()
        {
            while (true)
            {
                if (_disposedValue)
                {
                    return;
                }

                if (_currentRoom.IsRecording == RecordingStatus.Recording)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 已经在录制中了"
                    });
                    return;
                }

                EnsureDirectory();

                if (!_currentRoom.IsLive)
                {
                    return;
                }
                string url;
                try
                {
                    url = await BililiveApi.GetPlayUrlAsync(_currentRoom.RoomId);

                    url = await GetRedirectUrl(url);
                }
                catch (Exception ex)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] {ex.Message}"
                    });
                    await ReCheck();

                    continue;
                }

                if (_response.StatusCode != HttpStatusCode.OK)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"尝试下载直播流时服务器返回了 ({_response.StatusCode}){_response.ReasonPhrase}"
                    });

                    await ReCheck();

                    continue;
                }

                var filename = FileName;
                _downLoadTask = new HttpDownLoad(url, filename, true);
                try
                {
                    _currentRoom.IsRecording = RecordingStatus.Recording;
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 开始录制"
                    });
                    await _downLoadTask.Start(_response)
                    .ContinueWith(task =>
                    {
                        LogEvent?.Invoke(this, new LogEventArgs {
                            Log = $@"[{_currentRoom.RoomId}] 录制结束"
                        });
                        RecordCompletedEvent?.Invoke(this, new LogEventArgs {
                            Log = filename
                        });
                    });
                }
                catch (Exception ex)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 录制出错:{ex}"
                    });
                }
                finally
                {
                    Stop();
                    await ReCheck();
                    await Start();
                }

                break;
            }
            Dispose();
        }