Esempio n. 1
0
        public StreamsStructure GetStreamsLogic(int?id, int?streamId, int?streamerId)
        {
            StreamsStructure streams = new StreamsStructure();

            streams.data = new List <Stream>();
            using (var context = new MainDataContext()) {
                if (id != null || streamId != null || streamerId != null)
                {
                    Stream stream = new Stream();
                    if (id != null)
                    {
                        stream = context.Streams.FirstOrDefault(item => item.id == id);
                        streams.data.Add(stream);
                    }
                    else if (streamId != null)
                    {
                        stream = context.Streams.FirstOrDefault(item => item.streamId == streamId);
                        streams.data.Add(stream);
                    } // else if (streamerId != null) {

                    var streamList = context.Streams.ToList();
                    for (var x = 0; x < streamList.Count; x++)
                    {
                        if (streamList[x].streamerId == streamerId)
                        {
                            streams.data.Add(streamList[x]);
                        }
                    }
                }
                else
                {
                    streams.data = context.Streams.ToList();
                }
            }

            return(streams);
        }
        public bool PrepareDownload(StreamExtended stream)
        {
            string streamUrl;

            streamUrl = "https://www.twitch.tv/videos/" + stream.streamId;

            YoutubeDlVideoJson.YoutubeDlVideoInfo youtubeDlVideoInfo =
                StreamHelpers.GetDownloadQualityUrl(streamUrl, stream.streamerId);

            string streamDirectory = $"{GlobalConfig.GetGlobalConfig("contentRootPath")}streamers/{stream.streamerId}/vods/{stream.streamId}";


            Directory.CreateDirectory(streamDirectory);

            if (!string.IsNullOrEmpty(stream.thumbnailLocation))
            {
                //todo handle missing thumbnail, maybe use youtubedl generated thumbnail instead
                DownloadHelpers.DownloadFile(
                    stream.thumbnailLocation.Replace("%{width}", "320").Replace("%{height}", "180"),
                    $"{streamDirectory}/thumbnail.jpg");
            }

            string title      = String.IsNullOrEmpty(stream.title) ? "vod" : stream.title;
            string outputPath = $"{streamDirectory}/{title}.{stream.streamId}";

            string dbOutputPath = $"streamers/{stream.streamerId}/vods/{stream.streamId}/{title}.{stream.streamId}.mp4";

            //TODO more should be queued, not done immediately


            IJobDetail job;
            string     triggerIdentity;

            job = JobBuilder.Create <DownloadStreamJob>()
                  .WithIdentity("StreamDownload" + stream.streamId)
                  .UsingJobData("title", title)
                  .UsingJobData("streamDirectory", streamDirectory)
                  .UsingJobData("formatId", youtubeDlVideoInfo.formatId)
                  .UsingJobData("url", streamUrl)
                  .UsingJobData("isLive", false)
                  .UsingJobData("youtubeDlVideoInfoDuration", youtubeDlVideoInfo.duration)
                  .UsingJobData("retry", true)
                  .RequestRecovery()
                  .Build();

            job.JobDataMap.Put("stream", stream);
            triggerIdentity = $"StreamDownload{stream.streamId}";


            /*string jobId = BackgroundJob.Enqueue(() =>
             *  DownloadStream(stream, title, streamDirectory, youtubeDlVideoInfo.url, CancellationToken.None,
             *      isLive, youtubeDlVideoInfo.duration));*/

            Stream?    dbStream;
            bool       downloadChat    = false;
            IJobDetail chatDownloadJob = new JobDetailImpl();

            using (var context = new MainDataContext()) {
                dbStream = context.Streams.FirstOrDefault(item => item.streamId == stream.streamId);

                if (dbStream != null)
                {
                    dbStream.streamId = stream.streamId;

                    dbStream.streamerId    = stream.streamerId;
                    dbStream.quality       = youtubeDlVideoInfo.quality;
                    dbStream.url           = youtubeDlVideoInfo.url;
                    dbStream.title         = stream.title;
                    dbStream.createdAt     = stream.createdAt;
                    dbStream.location      = $"streamers/{stream.streamerId}/vods/{stream.streamId}/";
                    dbStream.fileName      = $"{title}.{stream.streamId}.mp4";
                    dbStream.duration      = youtubeDlVideoInfo.duration;
                    dbStream.downloading   = true;
                    dbStream.downloadJobId = job.Key.ToString();
                }
                else
                {
                    downloadChat    = true;
                    chatDownloadJob = JobBuilder.Create <ChatDownloadJob>()
                                      .WithIdentity("DownloadChat" + stream.streamId)
                                      .UsingJobData("streamId", stream.streamId)
                                      .UsingJobData("retry", true)
                                      .RequestRecovery()
                                      .Build();

                    dbStream = new Stream {
                        streamId          = stream.streamId,
                        streamerId        = stream.streamerId,
                        quality           = youtubeDlVideoInfo.quality,
                        title             = stream.title,
                        url               = youtubeDlVideoInfo.url,
                        createdAt         = stream.createdAt,
                        location          = $"streamers/{stream.streamerId}/vods/{stream.streamId}/",
                        fileName          = $"{title}.{stream.streamId}.mp4",
                        duration          = youtubeDlVideoInfo.duration,
                        downloading       = true,
                        chatDownloading   = true,
                        downloadJobId     = job.Key.ToString(),
                        chatDownloadJobId = chatDownloadJob.Key.ToString()
                    };
                    // only download chat if this is a new vod


                    context.Add(dbStream);
                }

                context.SaveChanges();
            }

            //var chatSchedulerFactory = new StdSchedulerFactory(QuartzSchedulers.SingleThreadScheduler());
            var vodSchedulerFactory = new StdSchedulerFactory(QuartzSchedulers.PrimaryScheduler());
            //IScheduler chatScheduler = chatSchedulerFactory.GetScheduler().Result;
            IScheduler vodScheduler = vodSchedulerFactory.GetScheduler().Result;

            //chatScheduler.Start();
            vodScheduler.Start();

            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
                                     .WithIdentity(triggerIdentity)
                                     .StartNow()
                                     .Build();

            vodScheduler.ScheduleJob(job, trigger);

            if (downloadChat)
            {
                ISimpleTrigger chatDownloadTrigger = (ISimpleTrigger)TriggerBuilder.Create()
                                                     .WithIdentity("DownloadChat" + stream.streamId)
                                                     .StartNow()
                                                     .Build();

                vodScheduler.ScheduleJob(chatDownloadJob, chatDownloadTrigger);
            }

            //_hubContext.Clients.All.SendAsync("ReceiveMessage", CheckForDownloadingStreams());

            return(true);
        }