Skip to content

Commit

Permalink
Make code not awful
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Jul 21, 2024
1 parent 51a78c7 commit 817d326
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
7 changes: 1 addition & 6 deletions TwitchDownloaderCLI/Modes/DownloadVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ private static VideoDownloadOptions GetDownloadOptions(VideoDownloadArgs inputOp

if (!Path.HasExtension(inputOptions.OutputFile) && inputOptions.Quality is { Length: > 0 })
{
if (inputOptions.Quality.Contains("audio", StringComparison.OrdinalIgnoreCase))
inputOptions.OutputFile += ".m4a";
else if (char.IsDigit(inputOptions.Quality[0])
|| inputOptions.Quality.Contains("source", StringComparison.OrdinalIgnoreCase)
|| inputOptions.Quality.Contains("chunked", StringComparison.OrdinalIgnoreCase))
inputOptions.OutputFile += ".mp4";
inputOptions.OutputFile += FilenameService.GuessVodFileExtension(inputOptions.Quality);
}

VideoDownloadOptions downloadOptions = new()
Expand Down
23 changes: 23 additions & 0 deletions TwitchDownloaderCore/Tools/FilenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ public static string ReplaceInvalidFilenameChars([AllowNull] string filename)
return newName.ReplaceAny(FilenameInvalidChars, '_');
}

[return: MaybeNull]
public static string GuessVodFileExtension([AllowNull] string qualityString)
{
if (string.IsNullOrWhiteSpace(qualityString))
{
return ".mp4";
}

if (qualityString.Contains("audio", StringComparison.OrdinalIgnoreCase))
{
return ".m4a";
}

if (char.IsDigit(qualityString[0])
|| qualityString.Contains("source", StringComparison.OrdinalIgnoreCase)
|| qualityString.Contains("chunked", StringComparison.OrdinalIgnoreCase))
{
return ".mp4";
}

return null;
}

public static FileInfo GetNonCollidingName(FileInfo fileInfo)
{
fileInfo.Refresh();
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderWPF/PageVodDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public VideoDownloadOptions GetOptions(string filename, string folder)
Filename = filename ?? Path.Combine(folder, FilenameService.GetFilename(Settings.Default.TemplateVod, textTitle.Text, currentVideoId.ToString(), currentVideoTime, textStreamer.Text,
checkStart.IsChecked == true ? new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value) : TimeSpan.Zero,
checkEnd.IsChecked == true ? new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value) : vodLength,
viewCount, game) + (comboQuality.Text.Contains("Audio", StringComparison.OrdinalIgnoreCase) ? ".m4a" : ".mp4")),
viewCount, game) + FilenameService.GuessVodFileExtension(comboQuality.Text)),
Oauth = TextOauth.Text,
Quality = GetQualityWithoutSize(comboQuality.Text),
Id = currentVideoId,
Expand Down Expand Up @@ -425,7 +425,7 @@ private async void SplitBtnDownloader_Click(object sender, RoutedEventArgs e)
FileName = FilenameService.GetFilename(Settings.Default.TemplateVod, textTitle.Text, currentVideoId.ToString(), currentVideoTime, textStreamer.Text,
checkStart.IsChecked == true ? new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value) : TimeSpan.Zero,
checkEnd.IsChecked == true ? new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value) : vodLength,
viewCount, game) + (comboQuality.Text.Contains("Audio", StringComparison.OrdinalIgnoreCase) ? ".m4a" : ".mp4")
viewCount, game) + FilenameService.GuessVodFileExtension(comboQuality.Text)
};
if (saveFileDialog.ShowDialog() == false)
{
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/WindowQueueOptions.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private void EnqueueDataList()
downloadOptions.Filename = Path.Combine(folderPath, FilenameService.GetFilename(Settings.Default.TemplateVod, taskData.Title, taskData.Id, taskData.Time, taskData.Streamer,
downloadOptions.TrimBeginning ? downloadOptions.TrimBeginningTime : TimeSpan.Zero,
downloadOptions.TrimEnding ? downloadOptions.TrimEndingTime : TimeSpan.FromSeconds(taskData.Length),
taskData.Views, taskData.Game) + (downloadOptions.Quality != null && downloadOptions.Quality.Contains("audio", StringComparison.OrdinalIgnoreCase) ? ".m4a" : ".mp4")); // This is awful
taskData.Views, taskData.Game) + FilenameService.GuessVodFileExtension(downloadOptions.Quality));

VodDownloadTask downloadTask = new VodDownloadTask
{
Expand Down

0 comments on commit 817d326

Please sign in to comment.