Skip to content

Commit

Permalink
Remove illegal chars before applying custom save expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenmoulder committed Oct 18, 2022
1 parent 537fece commit 169f6d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
20 changes: 1 addition & 19 deletions Services/DownloaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public async Task CheckExecutableExists()

public async Task DownloadClips(List<TwitchClipModel> clips)
{
await CreateAllDirectoriesRequired(clips);

var root = Directory.GetCurrentDirectory();
var username = clips.First().BroadcasterName;

Expand All @@ -112,30 +110,14 @@ await ParallelExtensions.ParallelForEachAsync(clips, async clip =>
if (!File.Exists(path))
{
counter++;
await ProcessEx.RunAsync(await _hostService.GetDownloaderExecutablePath(), $"{clip.Url} -o \"{path}\"");
await ProcessEx.RunAsync(await _hostService.GetDownloaderExecutablePath(), $"{clip.Url} --restrict-filenames --windows-filenames -o \"{path}\"");
await LogHelper.Log($"Downloading clip {counter}/{nonExistingClips.Count} using {await _twitchConfigurationService.GetDownloadThreads()} download threads", asyncLock);
}
}, await _twitchConfigurationService.GetDownloadThreads());

LogHelper.Index += 1;
}

private async Task CreateAllDirectoriesRequired(List<TwitchClipModel> clips)
{
await LogHelper.Log("Creating directories. This might take a while.");

foreach (var clip in clips)
{
var savePath = await _hostService.ConvertCustomPathExpressionToSavePath(clip);
var path = savePath.Replace(Path.GetFileName(savePath), "");
path = path.TrimEnd('\\').TrimEnd('/');

path = Path.Combine(Directory.GetCurrentDirectory().TrimEnd('\\').TrimEnd('/'), "clips", path.TrimStart('\\').TrimStart('/'));

await _hostService.CreateDirectoryIfNotExists(path);
}
}

private async Task<string> GetPath(TwitchClipModel clip)
{
var path = await _hostService.ConvertCustomPathExpressionToSavePath(clip);
Expand Down
38 changes: 19 additions & 19 deletions Services/HostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,6 @@ public async Task<string> ConvertCustomPathExpressionToSavePath(TwitchClipModel
await ErrorHelper.LogAndExit("Your custom path does not end with .mp4. Check https://github.com/mortenmoulder/TwitchClipper/wiki/Custom-save-expressions#requirements");
}

var replace = path
.Replace("{id", "{0")
.Replace("{broadcaster_name", "{1")
.Replace("{broadcaster_id", "{2")
.Replace("{game_id", "{3")
.Replace("{title", "{4")
.Replace("{yyyy", "{5:yyyy").Replace("{yyy", "{5:yyy").Replace("{yy", "{5:yy").Replace("{y", "{5:%y")
.Replace("{MMMM", "{5:MMMM").Replace("{MMM", "{5:MMM").Replace("{MM", "{5:MM").Replace("{M", "{5:%M")
.Replace("{dddd", "{5:dddd").Replace("{ddd", "{5:ddd").Replace("{dd", "{5:dd").Replace("{d", "{5:%d")
.Replace("{HH", "{5:HH").Replace("{H", "{5:%H").Replace("{hh", "{5:hh")
.Replace("{mm", "{5:mm").Replace("{m", "{5:%m")
.Replace("{ss", "{5:ss").Replace("{s", "{5:%s")
.Replace("{tt", "{5:tt").Replace("{t", "{5:t")
.Replace("{viewcount", "{6")
.Replace("{uploader", "{7")
;

path = string.Format(culture, replace, model.Id, model.BroadcasterName, model.BroadcasterId, model.GameId, model.Title, model.CreatedAt, model.ViewCount, model.CreatorName);

if (await GetOSPlatform() == OSPlatform.Windows)
{
illegalCharacters = new List<string>()
Expand All @@ -136,6 +117,25 @@ public async Task<string> ConvertCustomPathExpressionToSavePath(TwitchClipModel
path = path.Replace(@"\", "/");
}

var replace = path
.Replace("{id", "{0")
.Replace("{broadcaster_name", "{1")
.Replace("{broadcaster_id", "{2")
.Replace("{game_id", "{3")
.Replace("{title", "{4")
.Replace("{yyyy", "{5:yyyy").Replace("{yyy", "{5:yyy").Replace("{yy", "{5:yy").Replace("{y", "{5:%y")
.Replace("{MMMM", "{5:MMMM").Replace("{MMM", "{5:MMM").Replace("{MM", "{5:MM").Replace("{M", "{5:%M")
.Replace("{dddd", "{5:dddd").Replace("{ddd", "{5:ddd").Replace("{dd", "{5:dd").Replace("{d", "{5:%d")
.Replace("{HH", "{5:HH").Replace("{H", "{5:%H").Replace("{hh", "{5:hh")
.Replace("{mm", "{5:mm").Replace("{m", "{5:%m")
.Replace("{ss", "{5:ss").Replace("{s", "{5:%s")
.Replace("{tt", "{5:tt").Replace("{t", "{5:t")
.Replace("{viewcount", "{6")
.Replace("{uploader", "{7")
;

path = string.Format(culture, replace, model.Id, model.BroadcasterName, model.BroadcasterId, model.GameId, model.Title, model.CreatedAt, model.ViewCount, model.CreatorName);

foreach (var character in illegalCharacters)
{
path = path.Replace(character, "");
Expand Down
2 changes: 1 addition & 1 deletion Services/TwitchAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ await ParallelExtensions.ParallelForEachAsync(dates, async date =>

LogHelper.Index += 1;

return clips;
return clips.Where(x => x is not null).ToList();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion TwitchClipper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Authors>mortenmoulder</Authors>
<PackageProjectUrl>https://github.com/mortenmoulder/TwitchClipper</PackageProjectUrl>
<RepositoryUrl>https://github.com/mortenmoulder/TwitchClipper</RepositoryUrl>
<Version>1.0.10</Version>
<Version>1.0.12</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 169f6d0

Please sign in to comment.