Пример #1
0
        // [MaximumConcurrentExecutions(1)]
        // [DisableConcurrentExecution(timeoutInSeconds: 60 * 60 * 2)]
        public async Task <bool> Execute(ParsedItemResult item, Guid playlistId, PerformContext context)
        {
            _setContext(context);
            if (item is null || string.IsNullOrEmpty(item.VideoType))
            {
                return(false);
            }

            Log($"Starting process item:\n\t{item.Id}\n\t{item.Title}\n\thttps://www.youtube.com/watch?v={item.Id}");

            var playlist = await _playlistRepository.GetAsync(playlistId);

            var url = item.VideoType.ToLower().Equals("youtube") ? $"https://www.youtube.com/watch?v={item.Id}" :
                      item.VideoType.Equals("mixcloud") ? $"https://mixcloud.com/{item.Id}" :
                      string.Empty;

            if (string.IsNullOrEmpty(url))
            {
                LogError($"Unknown video type for ParsedItem: {item.Id} - {playlist.Id}");
            }
            else
            {
                Log($"Getting info");
                var info = await _audioDownloader.GetInfo(url, playlist.Podcast.AppUserId);

                if (info != RemoteUrlType.Invalid)
                {
                    Log($"URL is valid");

                    var podcast = await _podcastRepository.GetAsync(playlist.PodcastId);

                    var uid = Guid.NewGuid();
                    Log($"Downloading audio");
                    var localFile = Path.Combine(Path.GetTempPath(), $"{System.Guid.NewGuid()}.mp3");
                    try {
                        var entry = new PodcastEntry {
                            SourceUrl        = url,
                            ProcessingStatus = ProcessingStatus.Uploading,
                            Playlist         = playlist,
                            Podcast          = podcast
                        };
                        await _processor.GetInformation(entry, podcast.AppUserId);

                        podcast.PodcastEntries.Add(entry);
                        await _unitOfWork.CompleteAsync();

                        var result = await _preProcessor.PreProcessEntry(podcast.AppUser, entry);

                        return(result == EntryProcessResult.Succeeded);
                    } catch (AudioDownloadException e) {
                        //TODO: we should mark this as failed
                        //so we don't continuously process it
                        LogError(e.Message);
                    }
                }
                else
                {
                    LogError($"Processing playlist item {item.Id} failed");
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        public async Task <ActionResult <PodcastEntryViewModel> > Post([FromBody] PodcastEntryViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid podcast entry posted"));
            }
            PodcastEntry entry;

            if (item.Id != null)
            {
                entry = await _repository.GetAsync(item.Id);

                _mapper.Map(item, entry);
            }
            else
            {
                entry = _mapper.Map <PodcastEntryViewModel, PodcastEntry>(item);
            }

            if (entry is null)
            {
                return(BadRequest());
            }

            if (entry.ProcessingStatus == ProcessingStatus.Uploading ||
                entry.ProcessingStatus == ProcessingStatus.Processed)
            {
                // we're editing an existing entry
                await _repository.AddOrUpdateWithTags(entry);

                await _unitOfWork.CompleteAsync();

                var result = _mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry);
                return(Ok(result));
            }

            //we're adding a new entry
            //TODO: This should return the properties bundle
            //with the status as a member
            var parser  = new UrlTypeParser();
            var urlType = await parser.GetUrlType(entry.SourceUrl);

            if (!urlType.Equals(UrlType.Invalid) &&
                !urlType.Equals(UrlType.Playlist) &&
                !urlType.Equals(UrlType.Channel))
            {
                // check user quota
                var result = await _preProcessor.PreProcessEntry(
                    _applicationUser,
                    entry);

                switch (result)
                {
                case EntryProcessResult.QuotaExceeded:
                    return(StatusCode(402));

                case EntryProcessResult.GeneralFailure:
                    return(BadRequest());
                }

                _repository.GetContext()
                .Entry(entry)
                .State = EntityState.Detached;

                entry = await _repository.GetAsync(entry.Id);

                return(_mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry));
            }

            if (urlType.Equals(UrlType.Playlist))
            {
                entry.ProcessingStatus = ProcessingStatus.Deferred;
                var result = _mapper.Map <PodcastEntry, PodcastEntryViewModel>(entry);
                return(Accepted(result));
            }

            return(BadRequest("Failed to create podcast entry"));
        }