def test_probe_and_download_success_new_stream_video_by_empty_expired_stream_video( self, mock_RecorderFactory, mock_os_makedirs): from StreamRecorder.tasks import probe_and_download st1 = StreamerTask(id=1, streamer_name='test_streamer1', platform='douyu', room_id='000001', room_url='https://www.douyu.com/000001', record_enabled=True, record_running=False, record_dir_path='/dummy/dir', record_chunk_size_limit='1G', upload_bilibili_enabled=True, upload_bilibili_video_name='', upload_bilibili_info='{}', upload_onedrive_enabled=False) st1.save() sv1 = StreamVideo( id=1, streamer_id=st1, start_time=(timezone.now() - timedelta(hours=6)), ) sv1.save() mock_recorder = MagicMock() mock_recorder.probe.return_value = True mock_RecorderFactory.return_value = mock_recorder probe_and_download(1) self.assertEqual(StreamerTask.objects.get(id=1).record_running, False) self.assertEqual(len(StreamVideo.objects.all()), 2) self.assertEqual(len(VideoChunk.objects.all()), 1) self.assertEqual(mock_recorder.probe.call_count, 1) self.assertEqual(mock_recorder.download.call_count, 1) self.assertEqual(mock_os_makedirs.call_count, 1)
def probe_and_download(streamer_task_id): try: st = StreamerTask.objects.get(id=streamer_task_id) except: return "error when finding StreamerTask in db" recorder = RecorderFactory(platform_name=st.platform) if recorder is None: return "platform error" try: if recorder.probe(st.room_id): record_time = timezone.now() file_name = "{}_{}.flv".format(st.room_id, str(int(record_time.timestamp()))) full_path = os.path.join(st.record_dir_path, file_name) sv = StreamVideo.objects.filter(streamer_id=st).order_by('-start_time').first() if not sv: sv = StreamVideo( streamer_id=st, start_time=record_time, ) sv.save() else: last_chunk = VideoChunk.objects.filter(stream_video_id=sv).order_by('-start_time').first() if not last_chunk: last_chunk_time = sv.start_time else: last_chunk_time = last_chunk.start_time if last_chunk_time.timestamp() < (record_time - timedelta(hours=5)).timestamp(): sv = StreamVideo( streamer_id=st, start_time=record_time, ) sv.save() new_chunk = VideoChunk( stream_video_id=sv, file_name=file_name, full_path=full_path, fs_exist=True, start_time=record_time ) new_chunk.save() os.makedirs(os.path.dirname(full_path), exist_ok=True) recorder.download(st.room_id, full_path, parse_file_size(st.record_chunk_size_limit)) finally: st.record_running = False st.save()
def test_period_upload(self, mock_upload_bilibili): from StreamRecorder.tasks import periodic_upload st1 = StreamerTask(id=1, streamer_name='test_streamer1', platform='douyu', room_id='000001', room_url='https://www.douyu.com/000001', record_enabled=True, record_running=False, record_dir_path='/dummy/dir', record_chunk_size_limit='1G', upload_bilibili_enabled=True, upload_bilibili_video_name='', upload_bilibili_info='{}', upload_onedrive_enabled=False) st1.save() st2 = StreamerTask(id=2, streamer_name='test_streamer2', platform='douyu', room_id='000002', room_url='https://www.douyu.com/000002', record_enabled=False, record_running=False, record_dir_path='/dummy/dir', record_chunk_size_limit='1G', upload_bilibili_enabled=True, upload_bilibili_video_name='', upload_bilibili_info='{}', upload_onedrive_enabled=False) st2.save() sv1 = StreamVideo( id=1, streamer_id=st1, start_time=(timezone.now() - timedelta(hours=6)), ) sv1.save() vc1 = VideoChunk(stream_video_id=sv1, file_name='vc1.flv', full_path='/dummy/dir/vc1.flv', fs_exist=True, start_time=(timezone.now() - timedelta(hours=6))) vc1.save() sv2 = StreamVideo( id=2, streamer_id=st2, start_time=timezone.now(), ) sv2.save() vc2 = VideoChunk(stream_video_id=sv2, file_name='vc2.flv', full_path='/dummy/dir/vc2.flv', fs_exist=True, start_time=timezone.now()) vc2.save() periodic_upload() self.assertEqual( StreamVideo.objects.get(id=1).bilibili_status, "started") self.assertEqual(StreamVideo.objects.get(id=2).bilibili_status, None) self.assertEqual(mock_upload_bilibili.delay.call_count, 1)
def test_periodic_delete(self, mock_os_remove, mock_os_path_exists): from StreamRecorder.tasks import periodic_delete st1 = StreamerTask(id=1, streamer_name='test_streamer1', platform='douyu', room_id='000001', room_url='https://www.douyu.com/000001', record_enabled=True, record_running=False, record_dir_path='/dummy/dir', record_chunk_size_limit='1G', upload_bilibili_enabled=True, upload_bilibili_video_name='', upload_bilibili_info='{ \ "title": "{date}棋客老师第一视角", \ "tid": 171, \ "tag": ["电子竞技", "星际争霸2", "神族", "棋客"], \ "description": "{date}棋客老师第一视角", \ "source": "https://www.douyu.com/120219", \ "no_reprint": true, \ "open_elec": true \ }', upload_onedrive_enabled=False) st1.save() sv1 = StreamVideo( id=1, streamer_id=st1, start_time=timezone.now().replace(2020, 2, 1, 19, 30, 00), ) sv1.save() vc1 = VideoChunk( id=1, stream_video_id=sv1, file_name="1.flv", full_path="/data/video/dummystreamername/1.flv", fs_exist=True, start_time=timezone.now() - timedelta(days=14), ) vc1.save() vc2 = VideoChunk( id=2, stream_video_id=sv1, file_name="2.flv", full_path="/data/video/dummystreamername/2.flv", fs_exist=True, start_time=timezone.now() - timedelta(days=6), ) vc2.save() vc3 = VideoChunk( id=3, stream_video_id=sv1, file_name="3.flv", full_path="/data/video/dummystreamername/3.flv", fs_exist=True, start_time=timezone.now() - timedelta(days=7), ) vc3.save() mock_os_path_exists.return_value = True periodic_delete() vc1 = VideoChunk.objects.all().filter(id=1).first() vc2 = VideoChunk.objects.all().filter(id=2).first() vc3 = VideoChunk.objects.all().filter(id=3).first() self.assertEqual(vc1.fs_exist, False) self.assertEqual(vc2.fs_exist, True) self.assertEqual(vc3.fs_exist, False) self.assertEqual(mock_os_path_exists.call_count, 2) self.assertEqual(mock_os_remove.call_count, 2)
def test_upload_bilibili(self, mock_lib_bilibiliuploader, mock_isfile, mock_upload_bilibili): from StreamRecorder.tasks import upload_bilibili st1 = StreamerTask(id=1, streamer_name='test_streamer1', platform='douyu', room_id='000001', room_url='https://www.douyu.com/000001', record_enabled=True, record_running=False, record_dir_path='/dummy/dir', record_chunk_size_limit='1G', upload_bilibili_enabled=True, upload_bilibili_video_name='', upload_bilibili_info='{ \ "title": "{date}棋客老师第一视角", \ "tid": 171, \ "tag": ["电子竞技", "星际争霸2", "神族", "棋客"], \ "description": "{date}棋客老师第一视角", \ "source": "https://www.douyu.com/120219", \ "no_reprint": true, \ "open_elec": true \ }', upload_onedrive_enabled=False) st1.save() sv1 = StreamVideo( id=1, streamer_id=st1, start_time=timezone.now().replace(2020, 2, 1, 19, 30, 00), ) sv1.save() vc1 = VideoChunk( id=1, stream_video_id=sv1, file_name='vc1.flv', full_path='/dummy/dir/vc1.flv', fs_exist=True, start_time=timezone.now().replace(2020, 2, 1, 19, 30, 00), ) vc1.save() vc2 = VideoChunk( id=2, stream_video_id=sv1, file_name='vc2.flv', full_path='/dummy/dir/vc2.flv', fs_exist=True, start_time=timezone.now().replace(2020, 2, 1, 19, 40, 00), ) vc2.save() mock_isfile.return_value = True upload_bilibili(1) self.assertEqual(mock_isfile.call_count, 2) self.assertEqual(mock_lib_bilibiliuploader.VideoPart.call_count, 2) self.assertEqual(mock_upload_bilibili.upload.call_count, 1) upload_args = mock_upload_bilibili.upload.call_args_list[0][1] self.assertEqual(upload_args['title'], '2020年2月1日棋客老师第一视角') self.assertEqual( StreamVideo.objects.get(id=1).bilibili_status, "finished")