mirror of
https://github.com/fergalmoran/podnoms.git
synced 2026-01-03 07:05:32 +00:00
Moving extension to proper boilerplate
This commit is contained in:
@@ -5,6 +5,7 @@ using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
@@ -12,96 +13,82 @@ using PodNoms.Api.Models;
|
||||
using PodNoms.Api.Models.ViewModels;
|
||||
using PodNoms.Api.Persistence;
|
||||
using PodNoms.Api.Services.Downloader;
|
||||
using PodNoms.Api.Services.Storage;
|
||||
using PodNoms.Api.Services.Realtime;
|
||||
using PodNoms.Api.Services.Storage;
|
||||
using PusherServer;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace PodNoms.Api.Services.Processor
|
||||
{
|
||||
internal class UrlProcessService : ProcessService, IUrlProcessService
|
||||
{
|
||||
namespace PodNoms.Api.Services.Processor {
|
||||
internal class UrlProcessService : ProcessService, IUrlProcessService {
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IEntryRepository _repository;
|
||||
|
||||
public ApplicationsSettings _applicationsSettings { get; }
|
||||
|
||||
public UrlProcessService(IEntryRepository repository, IUnitOfWork unitOfWork,
|
||||
IFileUploader fileUploader, IOptions<ApplicationsSettings> applicationsSettings,
|
||||
ILoggerFactory logger, IMapper mapper, IRealTimeUpdater pusher) : base(logger, mapper, pusher)
|
||||
{
|
||||
public UrlProcessService (IEntryRepository repository, IUnitOfWork unitOfWork,
|
||||
IFileUploader fileUploader, IOptions<ApplicationsSettings> applicationsSettings,
|
||||
ILoggerFactory logger, IMapper mapper, IRealTimeUpdater pusher) : base (logger, mapper, pusher) {
|
||||
this._applicationsSettings = applicationsSettings.Value;
|
||||
this._repository = repository;
|
||||
this._unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
private async Task __downloader_progress(string userId, string uid, ProcessProgressEvent e)
|
||||
{
|
||||
await _sendProgressUpdate(
|
||||
private async Task __downloader_progress (string userId, string uid, ProcessProgressEvent e) {
|
||||
await _sendProgressUpdate (
|
||||
userId,
|
||||
uid,
|
||||
e);
|
||||
}
|
||||
public async Task<bool> GetInformation(int entryId)
|
||||
{
|
||||
var entry = await _repository.GetAsync(entryId);
|
||||
if (entry == null || string.IsNullOrEmpty(entry.SourceUrl))
|
||||
{
|
||||
_logger.LogError("Unable to process item");
|
||||
public async Task<bool> GetInformation (int entryId) {
|
||||
var entry = await _repository.GetAsync (entryId);
|
||||
if (entry == null || string.IsNullOrEmpty (entry.SourceUrl)) {
|
||||
_logger.LogError ("Unable to process item");
|
||||
return false;
|
||||
}
|
||||
|
||||
var downloader = new AudioDownloader(entry.SourceUrl, _applicationsSettings.Downloader);
|
||||
downloader.DownloadInfo();
|
||||
if (downloader.Properties != null)
|
||||
{
|
||||
var downloader = new AudioDownloader (entry.SourceUrl, _applicationsSettings.Downloader);
|
||||
downloader.DownloadInfo ();
|
||||
if (downloader.Properties != null) {
|
||||
entry.Title = downloader.Properties?.title;
|
||||
entry.Description = downloader.Properties?.description;
|
||||
entry.ImageUrl = downloader.Properties?.thumbnail;
|
||||
entry.ProcessingStatus = ProcessingStatus.Processing;
|
||||
try
|
||||
{
|
||||
try {
|
||||
entry.Author = downloader.Properties?.uploader;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_logger.LogWarning($"Unable to extract downloader info for: {entry.SourceUrl}");
|
||||
} catch (Exception) {
|
||||
_logger.LogWarning ($"Unable to extract downloader info for: {entry.SourceUrl}");
|
||||
}
|
||||
|
||||
await _unitOfWork.CompleteAsync();
|
||||
await _unitOfWork.CompleteAsync ();
|
||||
|
||||
_logger.LogDebug("***DOWNLOAD INFO RETRIEVED****\n");
|
||||
_logger.LogDebug($"Title: {entry.Title}\nDescription: {entry.Description}\nAuthor: {entry.Author}\n");
|
||||
_logger.LogDebug ("***DOWNLOAD INFO RETRIEVED****\n");
|
||||
_logger.LogDebug ($"Title: {entry.Title}\nDescription: {entry.Description}\nAuthor: {entry.Author}\n");
|
||||
|
||||
var pusherResult = await _sendProcessCompleteMessage(entry);
|
||||
var pusherResult = await _sendProcessCompleteMessage (entry);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public async Task<bool> DownloadAudio(int entryId)
|
||||
{
|
||||
var entry = await _repository.GetAsync(entryId);
|
||||
public async Task<bool> DownloadAudio (int entryId) {
|
||||
var entry = await _repository.GetAsync (entryId);
|
||||
if (entry == null)
|
||||
return false;
|
||||
|
||||
var downloader = new AudioDownloader(entry.SourceUrl, _applicationsSettings.Downloader);
|
||||
var downloader = new AudioDownloader (entry.SourceUrl, _applicationsSettings.Downloader);
|
||||
var outputFile =
|
||||
Path.Combine(System.IO.Path.GetTempPath(), $"{System.Guid.NewGuid().ToString()}.mp3");
|
||||
Path.Combine (System.IO.Path.GetTempPath (), $"{System.Guid.NewGuid().ToString()}.mp3");
|
||||
|
||||
downloader.DownloadProgress += async (s, e) => await __downloader_progress(entry.Podcast.User.GetUserId(), entry.Uid, e);
|
||||
downloader.DownloadProgress += async (s, e) => await __downloader_progress (entry.Podcast.User.GetUserId (), entry.Uid, e);
|
||||
|
||||
downloader.PostProcessing += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
downloader.PostProcessing += (s, e) => {
|
||||
Console.WriteLine (e);
|
||||
};
|
||||
var sourceFile = downloader.DownloadAudio(entry.Uid);
|
||||
if (!string.IsNullOrEmpty(sourceFile))
|
||||
{
|
||||
var sourceFile = downloader.DownloadAudio (entry.Uid);
|
||||
if (!string.IsNullOrEmpty (sourceFile)) {
|
||||
entry.ProcessingStatus = ProcessingStatus.Uploading;
|
||||
entry.AudioUrl = sourceFile;
|
||||
|
||||
await _sendProcessCompleteMessage(entry);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
await _sendProcessCompleteMessage (entry);
|
||||
await _unitOfWork.CompleteAsync ();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user