public async Task Run()
    {
        try
        {
            var Status = await GetLivestreamStatusFromChannelId(ChannelId);

            if (!Status.IsLivestreaming)
            {
                return;
            }

            var ytExplode = new YoutubeClient();
            var metadata  = await ytExplode.Videos.GetAsync(Status.videoId);

            var UploadInformation = new YoutubeUpload()
            {
                Title          = metadata.Title,
                Description    = metadata.Description,
                ThumbnailPath  = FilePaths.GetThumbnailPath(FileName.Purify($"{metadata.Title} [{DateTime.Now.Ticks.GetHashCode()}].jpeg")),
                LivestreamPath = FilePaths.GetLivestreamsPath(FileName.Purify($"{metadata.Title} [{DateTime.Now.Ticks.GetHashCode()}].mp4"))
            };

            CMessage.LivestreamFound(metadata.Title, Platform.YouTube);


            try
            { // Try in a try, because sometimes thumbnail downloads fails
              // if not this try block it will not download the livestream and will not upload
                new WebClient().DownloadFile(metadata.Thumbnails.MaxResUrl, UploadInformation.ThumbnailPath);
            }
            catch (Exception) { }

            await Streamlink.Download(UploadInformation.LivestreamPath, metadata.Url);

            var Upload = new Youtube(FilePaths.SecretsFile);
            await Upload.Init();

            _ = Upload.UploadWithRetry(UploadInformation, TimeSpan.FromHours(3));
        }
        catch (Exception x)
        {
            CError.ErrorInRunBlock(API_NAME, x.Message);
        }
    }
Esempio n. 2
0
    public async Task UploadWithRetry(YoutubeUpload info, TimeSpan RetryTimeout)
    {
        try
        {
            var video = new Video();
            video.Snippet             = new VideoSnippet();
            video.Snippet.Title       = info.Title;
            video.Snippet.Description = info.Description;
            video.Snippet.Tags        = new string[] { "" };
            video.Snippet.CategoryId  = "22";
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "private";
            var filePath = info.LivestreamPath;

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var videosInsertRequest = service.Videos.Insert(video, "snippet,status", fileStream, "video/*");

                videosInsertRequest.ResponseReceived += async(Video) =>
                {
                    if (info.ThumbnailPath != null && File.Exists(info.ThumbnailPath))
                    {
                        await SetThumbnail(Video.Id, info.ThumbnailPath);

                        Console.WriteLine($"UPLOADED VIDEO WITH ID {Video.Id} TITLE: {info.Title} PATH: {info.LivestreamPath}");
                    }
                };
                await videosInsertRequest.UploadAsync();

                fileStream.Dispose();
            }
        }
        catch (FileNotFoundException)
        {
            CError.TryUploadFileNonExistFile();
        }
        catch (Exception x)
        {
            CError.YouTubeAPIDailyQuotaLimitReached(x);
            await Task.Delay(RetryTimeout);

            _ = UploadWithRetry(info, RetryTimeout);
        }
    }