Skip to content

Commit

Permalink
Add thumbnail download
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Jun 20, 2024
1 parent 3b0bb8f commit 0d780be
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
139 changes: 138 additions & 1 deletion downloader/Downloader/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using Downloader.Models;
using ReactiveUI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Downloader.ViewModels;

Expand All @@ -15,6 +23,8 @@ public MainViewModel()
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

_client = new();

if (!Directory.Exists("data"))
{
Directory.CreateDirectory("data");
Expand All @@ -37,54 +47,147 @@ .. _data.Playlists.Keys
];

SongCount = $"{_data.Musics.Count} music found";

DownloadCmd = ReactiveCommand.CreateFromTask(async () =>
{
IsDownloading = true;
try
{
var imagePath = CanInputAlbumUrl ? $"tmpLogo{Path.GetExtension(AlbumUrl)}" : null;
if (imagePath != null)
{
using var file = new FileStream(imagePath, FileMode.Create, FileAccess.Write, FileShare.None);
await foreach (var prog in DownloadAndFollowAsync(AlbumUrl, file, new()))
{
DownloadImage = prog;
}
}
}
finally
{
IsDownloading = false;
}
});
}

private async IAsyncEnumerable<float> DownloadAndFollowAsync(string url, Stream destination, CancellationToken token)
{
using var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
var contentLength = response.Content.Headers.ContentLength;

using var download = await response.Content.ReadAsStreamAsync(token);

if (!contentLength.HasValue)
{
await download.CopyToAsync(destination);
yield return 1f;
}
else
{
var buffer = new byte[8192];
float totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await download.ReadAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, token).ConfigureAwait(false);
totalBytesRead += bytesRead;
yield return totalBytesRead / contentLength.Value;
}

yield return 1f;
}
}

private bool CleanCompare(string a, string b)
{
return a.Trim().ToUpperInvariant() == b.Trim().ToUpperInvariant();
}

private JsonExportData _data;
private JsonSerializerOptions _jsonOptions;
private HttpClient _client;

public ICommand DownloadCmd { get; }

private string _songName;
/// <summary>
/// Name of the song
/// </summary>
public string SongName
{
get => _songName;
set => this.RaiseAndSetIfChanged(ref _songName, value);
}

private string _artist;
/// <summary>
/// Name of the artist
/// </summary>
public string Artist
{
get => _artist;
set => this.RaiseAndSetIfChanged(ref _artist, value);
}

private string _musicUrl;
/// <summary>
/// URL to the song
/// </summary>
public string MusicUrl
{
get => _musicUrl;
set => this.RaiseAndSetIfChanged(ref _musicUrl, value);
}

private string _albumName;
/// <summary>
/// Name of the album
/// </summary>
public string AlbumName
{
get => _albumName;
set => this.RaiseAndSetIfChanged(ref _albumName, value);
set
{
this.RaiseAndSetIfChanged(ref _albumName, value);
if (string.IsNullOrWhiteSpace(value))
{
CanInputAlbumUrl = false;
}
else if (_data.Albums.Any(x => CleanCompare(x.Key, value)))
{
CanInputAlbumUrl = false;
}
else
{
CanInputAlbumUrl = true;
}
}
}

private string _albumUrl;
/// <summary>
/// URL to the album image
/// </summary>
public string AlbumUrl
{
get => _albumUrl;
set => this.RaiseAndSetIfChanged(ref _albumUrl, value);
}

private string _songType;
/// <summary>
/// Type of the song
/// </summary>
public string SongType
{
get => _songType;
set => this.RaiseAndSetIfChanged(ref _songType, value);
}

public string _playlist;
/// <summary>
/// What playlist this song belong to
/// </summary>
public string Playlist
{
get => _playlist;
Expand All @@ -93,9 +196,43 @@ public string Playlist
public string[] PlaylistChoices { private set; get; }

private string _songCount;
/// <summary>
/// Number of songs already downloaded
/// </summary>
public string SongCount
{
get => _songCount;
set => this.RaiseAndSetIfChanged(ref _songCount, value);
}

private bool _canInputAlbumUrl;
/// <summary>
/// Can we type the album URL
/// e.g. did we not already download it for a previous song
/// </summary>
public bool CanInputAlbumUrl
{
get => _canInputAlbumUrl;
set => this.RaiseAndSetIfChanged(ref _canInputAlbumUrl, value);
}

private bool _isDownloading;
/// <summary>
/// Are we currently downloading everything
/// </summary>
public bool IsDownloading
{
get => _isDownloading;
set => this.RaiseAndSetIfChanged(ref _isDownloading, value);
}

private float _downloadImage;
/// <summary>
/// Progress of the download of the image
/// </summary>
public float DownloadImage
{
get => _downloadImage;
set => this.RaiseAndSetIfChanged(ref _downloadImage, value);
}
}
20 changes: 17 additions & 3 deletions downloader/Downloader/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
</Design.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="220" />
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Expand All @@ -31,17 +32,30 @@
HorizontalAlignment="Right"
VerticalAlignment="Top"
Text="{Binding SongCount}" />
<StackPanel Grid.Row="1">
<StackPanel Grid.Row="1" IsEnabled="{Binding !IsDownloading}">
<TextBox Text="{Binding SongName}" Watermark="Name" />
<TextBox Text="{Binding Artist}" Watermark="Artist" />
<TextBox Text="{Binding MusicUrl}" Watermark="YouTube URL" />
<TextBox Text="{Binding AlbumName}" Watermark="Album Name" />
<TextBox
IsEnabled="{Binding AlbumName, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
IsEnabled="{Binding CanInputAlbumUrl}"
Text="{Binding AlbumUrl}"
Watermark="Album URL" />
<TextBox Text="{Binding SongType}" Watermark="Song Type" />
<ComboBox HorizontalAlignment="Stretch" ItemsSource="{Binding PlaylistChoices}" />
</StackPanel>
<StackPanel
Grid.Row="2"
HorizontalAlignment="Right"
IsEnabled="{Binding !IsDownloading}"
Orientation="Horizontal">
<Button Command="{Binding DownloadCmd}">Download</Button>
</StackPanel>
<StackPanel Grid.Row="3">
<ProgressBar
Height="20"
Maximum="1"
Value="{Binding DownloadImage}" />
</StackPanel>
</Grid>
</UserControl>

0 comments on commit 0d780be

Please sign in to comment.