Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Fix issues with downloading playlists.
Browse files Browse the repository at this point in the history
  • Loading branch information
bozali committed Feb 11, 2024
1 parent 8a2c1a2 commit 7d25437
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Bal.Converter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Native", "Native", "{5E777A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bal.Converter.YouTubeDl", "sources\Bal.Converter.YouTubeDl\Bal.Converter.YouTubeDl.csproj", "{3218552D-170F-4EBA-A5AE-A3101B4A0F29}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bal.Converter.UpdateManager", "sources\Bal.Converter.UpdateManager\Bal.Converter.UpdateManager.csproj", "{2FAD8EFC-AC22-4E6B-AE87-4A34ADBA061D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bal.Converter.UpdateManager", "sources\Bal.Converter.UpdateManager\Bal.Converter.UpdateManager.csproj", "{2FAD8EFC-AC22-4E6B-AE87-4A34ADBA061D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
10 changes: 5 additions & 5 deletions sources/Bal.Converter.CLI/Executors/ConvertExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Bal.Converter.CLI.Executors;

public class ConvertExecutor
{
private readonly ITransformationProvider transformationProvider;
private readonly ITransformationService transformationService;

public ConvertExecutor(ITransformationProvider transformationProvider)
public ConvertExecutor(ITransformationService transformationService)
{
this.transformationProvider = transformationProvider;
this.transformationService = transformationService;
}

public async Task Execute(ConvertVerb verb)
Expand All @@ -21,7 +21,7 @@ public async Task Execute(ConvertVerb verb)
Console.WriteLine(verb.Path);
Console.WriteLine(verb.Destination);

string[] supported = this.transformationProvider.GetSupportedFormats(verb.Path);
string[] supported = this.transformationService.GetSupportedFormats(verb.Path);

string? targetExtension = Path.GetExtension(verb.Destination)?.Replace(".", string.Empty);

Expand All @@ -31,7 +31,7 @@ public async Task Execute(ConvertVerb verb)
return;
}

var conversion = this.transformationProvider.Provide(targetExtension);
var conversion = this.transformationService.Provide(targetExtension);

if (conversion.Topology.HasFlag(TransformationTopology.Audio))
{
Expand Down
2 changes: 1 addition & 1 deletion sources/Bal.Converter.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void ConfigureServices(HostBuilderContext context, IServiceCollect
collection.ConfigureTransformation();

collection
.AddSingleton<ITransformationProvider, TransformationProvider>()
.AddSingleton<ITransformationService, TransformationService>()
.AddSingleton<IFFmpeg, FFmpeg.FFmpeg>(provider => new FFmpeg.FFmpeg(@"Tools\ffmpeg.exe"))
.AddTransient<ConvertExecutor>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Bal.Converter.Common.Transformation;

public interface ITransformationProvider
public interface ITransformationService
{
string[] GetSupportedFormats(string path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Bal.Converter.Common.Transformation;

public class TransformationProvider : ITransformationProvider
public class TransformationService : ITransformationService
{
private readonly IServiceProvider serviceProvider;

public TransformationProvider(IServiceProvider serviceProvider)
public TransformationService(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
Expand Down
3 changes: 2 additions & 1 deletion sources/Bal.Converter/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Windows.System;
using Bal.Converter.Activation;
using Bal.Converter.Common.Services;
using Bal.Converter.Common.Transformation;
Expand Down Expand Up @@ -124,7 +125,7 @@ private void ConfigureServices(HostBuilderContext context, IServiceCollection co
collection.AddSingleton<IFileDownloaderService, FileDownloaderService>();
collection.AddSingleton<IDownloadsRegistryService, DownloadsRegistryService>();
collection.AddSingleton<IMediaTagService, MediaTagService>();
collection.AddSingleton<ITransformationProvider, TransformationProvider>();
collection.AddSingleton<ITransformationService, TransformationService>();
collection.AddSingleton<IDialogPickerService, DialogPickerService>();
collection.AddSingleton<MainWindow>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public partial class ConversionSelectionViewModel : ObservableObject

private readonly INavigationService navigationService;
private readonly IDialogPickerService dialog;
private readonly ITransformationProvider transformationProvider;
private readonly ITransformationService transformationService;

public ConversionSelectionViewModel(INavigationService navigationService, IDialogPickerService dialog, ITransformationProvider transformationProvider)
public ConversionSelectionViewModel(INavigationService navigationService, IDialogPickerService dialog, ITransformationService transformationService)
{
this.navigationService = navigationService;
this.dialog = dialog;
this.transformationProvider = transformationProvider;
this.transformationService = transformationService;
}

public bool IsFileSelected
Expand All @@ -44,7 +44,7 @@ public bool IsFileSelected
public void HandleDrop(string path)
{
this.Path = path;
this.SupportedFormats = new ObservableCollection<string>(this.transformationProvider.GetSupportedFormats(path));
this.SupportedFormats = new ObservableCollection<string>(this.transformationService.GetSupportedFormats(path));
this.SelectedFormat = this.SupportedFormats.FirstOrDefault();
}

Expand All @@ -60,6 +60,8 @@ private async Task OpenFile()
if (result != null)
{
this.Path = result.Path;

this.HandleDrop(this.Path);
}
}

Expand All @@ -71,7 +73,7 @@ private void Continue()
return;
}

var conversion = this.transformationProvider.Provide(this.SelectedFormat);
var conversion = this.transformationService.Provide(this.SelectedFormat);
var parameters = new Dictionary<string, object>
{
{ "Conversion", conversion },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System.Collections.ObjectModel;

using Windows.System;
using Bal.Converter.Messages;
using Bal.Converter.Services;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using DispatcherQueuePriority = Microsoft.UI.Dispatching.DispatcherQueuePriority;

namespace Bal.Converter.Modules.Downloads.ViewModels;

public partial class DownloadsViewModel : ObservableObject
{
public static readonly DispatcherQueue DispatcherQueue = DispatcherQueue.GetForCurrentThread();

private readonly IDownloadsRegistryService downloadsRegistry;

public DownloadsViewModel(IDownloadsRegistryService downloadsRegistry)
Expand Down Expand Up @@ -57,9 +60,13 @@ private void OnDownloadRemoved(object recipient, DownloadRemovedMessage message)
this.DownloadJobs.Remove(item);
}
}


private void OnDownloadAdded(object recipient, DownloadAddedMessage message)
{
this.DownloadJobs.Add(new DownloadJobViewModel(this.downloadsRegistry, message.Value));
App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
this.DownloadJobs.Add(new DownloadJobViewModel(this.downloadsRegistry, message.Value));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,23 @@
<winui:SettingsExpander.Items>
<winui:SettingsCard x:Uid="VideoQuality" HeaderIcon="{ui:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xe7f4;}">
<StackPanel Orientation="Horizontal" Spacing="6">
<Button Command="{x:Bind ViewModel.SelectVideoFormatCommand}" Style="{StaticResource SubtleButtonStyle}" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xe8da;" />
<!--<Button Command="{x:Bind ViewModel.SelectVideoFormatCommand}" Style="{StaticResource SubtleButtonStyle}" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xe8da;" />-->

<ComboBox MinWidth="{StaticResource SettingActionControlMinWidth}" SelectedValue="Best" SelectedValuePath="Tag">
<ComboBoxItem x:Uid="QualityBest" Tag="Best" />
<ComboBoxItem x:Uid="QualityWorst" Tag="Worst" />
<ComboBoxItem x:Uid="QualityCustom" Tag="Custom" IsEnabled="False" />
<!--<ComboBoxItem x:Uid="QualityCustom" Tag="Custom" IsEnabled="False" />-->
</ComboBox>
</StackPanel>
</winui:SettingsCard>

<winui:SettingsCard x:Uid="AudioQuality" HeaderIcon="{ui:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xe767;}">
<StackPanel Orientation="Horizontal" Spacing="6">
<Button Style="{StaticResource SubtleButtonStyle}" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xe8da;" />
<!--<Button Style="{StaticResource SubtleButtonStyle}" FontFamily="{ThemeResource SymbolThemeFontFamily}" Content="&#xe8da;" />-->

<ComboBox MinWidth="{StaticResource SettingActionControlMinWidth}" SelectedValue="Best" SelectedValuePath="Tag">
<ComboBoxItem x:Uid="QualityBest" Tag="Best" />
<ComboBoxItem x:Uid="QualityWorst" Tag="Worst" />
<ComboBoxItem x:Uid="QualityCustom" Tag="Custom" IsEnabled="False" />
</ComboBox>
</StackPanel>
</winui:SettingsCard>
Expand Down
17 changes: 15 additions & 2 deletions sources/Bal.Converter/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Bal.Converter.Messages;
using Windows.ApplicationModel.Core;
using Windows.System;
using Bal.Converter.Messages;
using Bal.Converter.Modules.Downloads;
using Bal.Converter.Modules.Settings.Views;
using Bal.Converter.Services;
Expand All @@ -8,11 +10,14 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Navigation;
using Bal.Converter.Modules.Downloads.ViewModels;

namespace Bal.Converter.ViewModels;

public partial class ShellViewModel : ObservableRecipient
{
private static readonly DispatcherQueue DispatcherQueue = DispatcherQueue.GetForCurrentThread();

private readonly ILocalSettingsService localSettingsService;

[ObservableProperty] private bool isInteractionEnabled;
Expand All @@ -35,7 +40,7 @@ public ShellViewModel(INavigationService navigationService,
this.DownloadCount = downloadsRegistry.AllJobs.Count(x => x.State != DownloadState.Cancelled || x.State != DownloadState.Cancelled);

WeakReferenceMessenger.Default.Register<DisableInteractionChangeMessage>(this, (recipient, message) => this.IsInteractionEnabled = !message.Value);
WeakReferenceMessenger.Default.Register<DownloadAddedMessage>(this, (recipient, message) => this.DownloadCount++);
WeakReferenceMessenger.Default.Register<DownloadAddedMessage>(this, this.OnDownloadAdded);
WeakReferenceMessenger.Default.Register<DownloadRemovedMessage>(this, this.OnDownloadRemoved);
WeakReferenceMessenger.Default.Register<DownloadStateMessage>(this, this.OnDownloadStateChanged);
}
Expand Down Expand Up @@ -86,6 +91,14 @@ private void OnDownloadStateChanged(object recipient, DownloadStateMessage messa
}
}

private void OnDownloadAdded(object recipient, DownloadAddedMessage message)
{
App.MainWindow.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
this.DownloadCount++;
});
}

private void OnNavigated(object sender, NavigationEventArgs e)
{
this.IsBackEnabled = this.NavigationService.CanGoBack;
Expand Down
71 changes: 45 additions & 26 deletions sources/Bal.Converter/Workers/PlaylistFetchBackgroundWorker.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
using AutoMapper;
using Windows.ApplicationModel.Core;
using Windows.System;
using Windows.UI.Core;
using AutoMapper;

using Bal.Converter.Common.Media;
using Bal.Converter.Common.Web;
using Bal.Converter.Messages;
using Bal.Converter.Modules.Downloads;
using Bal.Converter.Services;
using Bal.Converter.YouTubeDl;

using CommunityToolkit.Mvvm.Messaging;

namespace Bal.Converter.Workers;

public class PlaylistFetchBackgroundWorker
{
private static readonly DispatcherQueue MyDispatcher = DispatcherQueue.GetForCurrentThread();

private readonly IDownloadsRegistryService downloadsRegistry;
private readonly IFileDownloaderService fileDownloaderService;
private readonly IYouTubeDl youtubeDl;
private readonly IMapper mapper;

public PlaylistFetchBackgroundWorker(IDownloadsRegistryService downloadsRegistry, IYouTubeDl youtubeDl, IMapper mapper)
public PlaylistFetchBackgroundWorker(IDownloadsRegistryService downloadsRegistry, IFileDownloaderService fileDownloaderService, IYouTubeDl youtubeDl, IMapper mapper)
{
this.downloadsRegistry = downloadsRegistry;
this.fileDownloaderService = fileDownloaderService;
this.youtubeDl = youtubeDl;
this.mapper = mapper;
}
Expand All @@ -27,34 +34,46 @@ public async Task Process(CancellationToken ct = default)
{
while (!ct.IsCancellationRequested)
{
var job = await this.downloadsRegistry.NextPlaylistFetchJob().ConfigureAwait(false);

if (job == null)
try
{
continue;
}
var job = await this.downloadsRegistry.NextPlaylistFetchJob().ConfigureAwait(false);

var playlist = await this.youtubeDl.GetPlaylist(job.Url).ConfigureAwait(false);

foreach (var video in playlist.Videos)
{
var tags = new MediaTags
if (job == null)
{
Title = video.Title,
Artist = video.Channel,
Year = video.UploadDate.Year
};
continue;
}

var playlist = await this.youtubeDl.GetPlaylist(job.Url).ConfigureAwait(false);

var download = new DownloadJob(video.Url)
foreach (var video in playlist.Videos)
{
AutomaticQualityOption = job.AutomaticQualityOption,
TargetFormat = job.TargetFormat,
State = DownloadState.Pending,
Tags = tags
};

this.downloadsRegistry.EnqueueDownload(download);
WeakReferenceMessenger.Default.Send(new DownloadAddedMessage(download));
var tags = new MediaTags
{
Title = video.Title,
Artist = video.Channel,
Year = video.UploadDate.Year
};

var downloadResponse = await this.fileDownloaderService.DownloadImageAsync(video.ThumbnailUrl, Path.Combine(ILocalSettingsService.TempPath, "Thumbnails", Guid.NewGuid() + ".jpg"));

var download = new DownloadJob(video.Url)
{
AutomaticQualityOption = job.AutomaticQualityOption,
ThumbnailPath = downloadResponse.DownloadPath,
TargetFormat = job.TargetFormat,
State = DownloadState.Pending,
Tags = tags,
};

// var dispatcher = Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread);

this.downloadsRegistry.EnqueueDownload(download);

WeakReferenceMessenger.Default.Send(new DownloadAddedMessage(download));
}
}
catch (Exception e)
{
}
}
}
Expand Down

0 comments on commit 7d25437

Please sign in to comment.