Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CCVTAC.Main/CCVTAC.Main.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CCFSharpUtils" Version="0.3.5.1" />
<PackageReference Include="CCFSharpUtils" Version="0.4.0" />
<PackageReference Include="CodeConscious.Startwatch" Version="1.0.0" />
<PackageReference Include="FSharpPlus" Version="1.9.1" />
<PackageReference Include="FsToolkit.ErrorHandling" Version="5.2.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/CCVTAC.Main/Commands.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CCVTAC.Main

open CCFSharpUtils
open CCFSharpUtils.Text
open System

module Commands =
Expand Down
2 changes: 1 addition & 1 deletion src/CCVTAC.Main/Downloading/Downloading.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CCVTAC.Main.Downloading

open CCFSharpUtils
open CCFSharpUtils.Text

module Downloading =

Expand Down
2 changes: 1 addition & 1 deletion src/CCVTAC.Main/Downloading/Updater.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace CCVTAC.Main.Downloading
open CCVTAC.Main
open CCVTAC.Main.ExternalTools
open CCVTAC.Main.Settings.Settings
open CCFSharpUtils
open CCFSharpUtils.Text

module Updater =

Expand Down
3 changes: 2 additions & 1 deletion src/CCVTAC.Main/ExternalTools/Runner.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace CCVTAC.Main.ExternalTools

open CCVTAC.Main
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open Startwatch.Library
open System
open System.Diagnostics
Expand Down
11 changes: 6 additions & 5 deletions src/CCVTAC.Main/History.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace CCVTAC.Main

open CCVTAC.Main.IoUtilities.Files
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open System
open System.IO
open System.Text.Json
Expand All @@ -11,7 +12,7 @@ type History(filePath: string, displayCount: int) =

let separator = ';'

member private _.FilePath = filePath
member private _.FileInfo = FileInfo filePath
member private _.DisplayCount = displayCount

/// Write a URL and its related data to the history file.
Expand All @@ -20,16 +21,16 @@ type History(filePath: string, displayCount: int) =
let serializedTime = JsonSerializer.Serialize(entryTime).Replace("\"", "")
let text = serializedTime + string separator + url + String.newLine

match appendToFile this.FilePath text with
match appendToFile this.FileInfo text with
| Ok _ -> printer.Debug $"Added \"%s{url}\" to the history log."
| Error err -> printer.Error $"Failed to write \"%s{url}\" to the history log at \"{this.FilePath}\": {err}"
| Error err -> printer.Error $"Failed to write \"%s{url}\" to the history log at \"{this.FileInfo}\": {err}"
with exn ->
printer.Error $"Could not append URL(s) to history log: {exn.Message}"

member this.ShowRecent(printer: Printer) : unit =
try
let lines =
File.ReadAllLines this.FilePath
File.ReadAllLines this.FileInfo.FullName
|> Seq.takeLast this.DisplayCount
|> Seq.toList

Expand Down
6 changes: 2 additions & 4 deletions src/CCVTAC.Main/InputHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ module InputHelper =
inputs
|> List.map (fun input ->
{ Text = input
Category = if input[0] = Commands.prefix
then InputCategory.Command
else InputCategory.Url })
Category = if input[0] = Commands.prefix then Command else Url })

let countCategories (inputs: CategorizedInput list) : CategoryCounts =
inputs
|> List.groupBy _.Category
|> List.map (fun (k, grp) -> k, grp |> Seq.length)
|> List.map (fun (category, items) -> category, Seq.length items)
|> Map.ofSeq
|> CategoryCounts
3 changes: 2 additions & 1 deletion src/CCVTAC.Main/IoUtilities/Directories.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace CCVTAC.Main.IoUtilities

open CCVTAC.Main
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open System.IO
open System.Text

Expand Down Expand Up @@ -96,4 +98,3 @@ module Directories =
|> Ok
with exn ->
Error $"Error accessing or creating directory \"%s{dirName}\": %s{exn.Message}"

5 changes: 3 additions & 2 deletions src/CCVTAC.Main/IoUtilities/Files.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CCVTAC.Main.IoUtilities

open CCFSharpUtils
open CCFSharpUtils.Text
open System.IO

module Files =
Expand All @@ -18,5 +19,5 @@ module Files =
let readAllText (filePath: string) : Result<string, string> =
ofTry (fun _ -> File.ReadAllText filePath)

let appendToFile (filePath: string) (text: string) : Result<unit, string> =
ofTry (fun _ -> File.AppendAllText(filePath, text))
let appendToFile (file: FileInfo) (text: string) : Result<unit, string> =
ofTry (fun _ -> File.AppendAllText(file.FullName, text))
3 changes: 2 additions & 1 deletion src/CCVTAC.Main/Orchestrator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ open CCVTAC.Main.PostProcessing
open CCVTAC.Main.Settings
open CCVTAC.Main.Settings.Settings
open CCVTAC.Main.Settings.Settings.LiveUpdating
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open Startwatch.Library
open System

Expand Down
2 changes: 1 addition & 1 deletion src/CCVTAC.Main/PostProcessing/Deleter.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CCVTAC.Main.PostProcessing

open CCVTAC.Main
open CCFSharpUtils
open CCFSharpUtils.Text
open System.IO

module Deleter =
Expand Down
3 changes: 1 addition & 2 deletions src/CCVTAC.Main/PostProcessing/MetadataUtilities.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace CCVTAC.Main.PostProcessing

open CCVTAC.Main
open CCFSharpUtils
open CCFSharpUtils.Text
open System
open System.Text

Expand Down
2 changes: 2 additions & 0 deletions src/CCVTAC.Main/PostProcessing/Mover.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ open CCVTAC.Main.PostProcessing
open CCVTAC.Main.PostProcessing.Tagging
open CCVTAC.Main.Settings.Settings
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open TaggingSet
open System
open System.IO
Expand Down
3 changes: 2 additions & 1 deletion src/CCVTAC.Main/PostProcessing/PostProcessing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ open CCVTAC.Main.IoUtilities
open CCVTAC.Main.PostProcessing.Tagging
open CCVTAC.Main.PostProcessing.Tagging.TaggingSet
open CCVTAC.Main.Settings.Settings
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open Startwatch.Library
open System.IO
open System.Linq
Expand Down
5 changes: 3 additions & 2 deletions src/CCVTAC.Main/PostProcessing/Renamer.fs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
namespace CCVTAC.Main.PostProcessing

open CCVTAC.Main
open CCVTAC.Main.IoUtilities
open CCVTAC.Main.Settings.Settings
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open System
open System.IO
open System.Text
open System.Text.RegularExpressions
open Startwatch.Library

module Renamer =
open CCVTAC.Main.IoUtilities

let private toNormalizationForm (form: string) =
match form.Trim().ToUpperInvariant() with
Expand Down
4 changes: 2 additions & 2 deletions src/CCVTAC.Main/PostProcessing/Tagging/Tagger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ open CCVTAC.Main.Settings.Settings
open CCVTAC.Main.PostProcessing
open CCVTAC.Main.PostProcessing.Tagging
open CCVTAC.Main.Downloading.Downloading
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open Startwatch.Library
open TaggingSet
open MetadataUtilities
open System
open System.IO
Expand Down
68 changes: 35 additions & 33 deletions src/CCVTAC.Main/PostProcessing/Tagging/TaggingSet.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace CCVTAC.Main.PostProcessing.Tagging

open CCVTAC.Main.IoUtilities
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Operators
open CCFSharpUtils.Text
open FsToolkit.ErrorHandling
open System.IO
open System.Text.RegularExpressions
Expand Down Expand Up @@ -32,17 +33,6 @@ module TaggingSet =
ImageFile = i }

let private createValidated (videoId, fileNames) : Result<TaggingSet, string list> =
let ensureNotEmpty xs errorMsg : Validation<'a list, string> =
if List.isNotEmpty xs
then Ok xs
else Error [errorMsg]

let ensureExactlyOne xs emptyErrorMsg multipleErrorMsg : Validation<'a, string> =
match xs with
| [] -> Error [emptyErrorMsg]
| [x] -> Ok x
| _ -> Error [multipleErrorMsg]

let hasSupportedAudioExt (fileName: string) =
match Path.GetExtension fileName with
| Null -> false
Expand All @@ -55,39 +45,51 @@ module TaggingSet =
Files.imageFileExts
|> List.collect (fun ext -> fileNames |> Files.filterByExt ext)

Validation.map3
(fun a j i -> create videoId a j i)
(ensureNotEmpty audioFiles
$"No supported audio files found for video ID {videoId}.")
(ensureExactlyOne jsonFiles
$"No JSON file found for video ID {videoId}."
$"Multiple JSON files found for video ID {videoId}.")
(ensureExactlyOne imageFiles
$"No image file found for video ID {videoId}."
$"Multiple image files found for video ID {videoId}.")
// Validation.map3
// (fun a j i -> create videoId a j i)
// (List.ensureNotEmptyV audioFiles
// $"No supported audio files found for video ID {videoId}.")
// (List.ensureOneV jsonFiles
// $"No JSON file found for video ID {videoId}."
// $"Multiple JSON files found for video ID {videoId}.")
// (List.ensureOneV imageFiles
// $"No image file found for video ID {videoId}."
// $"Multiple image files found for video ID {videoId}.")

/// Creates a collection of TaggingSets from a collection of file paths related to several video IDs.
/// Any extra, unnecessary files will be ignored.
/// Any validation errors will be accumulated and return in an Error.
validation {
let! a = List.ensureNotEmptyV audioFiles
$"No supported audio files found for video ID {videoId}."
and! j = List.ensureOneV jsonFiles
$"No JSON file found for video ID {videoId}."
$"Multiple JSON files found for video ID {videoId}."
and! i = List.ensureOneV imageFiles
$"No image file found for video ID {videoId}."
$"Multiple image files found for video ID {videoId}."
return create videoId a j i
}

/// Creates a collection of TaggingSets from a collection of file paths related to video IDs.
/// Irrelevant files are ignored. Validation errors are accumulated and returned in an Error.
let createSets filePaths : Result<TaggingSet list, string list> =
if Seq.isEmpty filePaths then
Error ["No file paths to create a tagging set were provided."]
else
let isRelevantFile fileName : Match option =
let relevantFileInfo fileName =
// Regex group 0 is the full filename, and group 1 contains the video ID.
let fileNamesHavingVideoIdsRgx =
Regex(@".+\[([\w_\-]{11})\](?:.*)?\.(\w+)", RegexOptions.Compiled)

fileName |> Rgx.trySuccessMatch fileNamesHavingVideoIdsRgx

let fileName (m: Match) = m.Groups[0].Value
let videoId (m: Match) = m.Groups[1].Value
fileName
|> Rgx.trySuccessMatch fileNamesHavingVideoIdsRgx
|> Option.map (fun m ->
{| FileName = m.Groups[0].Value
VideoId = m.Groups[1].Value |} )

filePaths
|> List.ofSeq
|> List.choose isRelevantFile
|> List.groupBy videoId
|> List.mapSnd fileName
|> List.choose relevantFileInfo
|> List.groupBy _.VideoId
|> List.mapSnd _.FileName
|> List.map createValidated
|> List.sequenceResultA
|!! List.collect id
1 change: 1 addition & 0 deletions src/CCVTAC.Main/Printer.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CCVTAC.Main

open CCFSharpUtils
open CCFSharpUtils.Text
open System
open System.Collections.Generic
open System.Linq
Expand Down
3 changes: 2 additions & 1 deletion src/CCVTAC.Main/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ open CCVTAC.Main.IoUtilities
open CCVTAC.Main.Settings
open CCVTAC.Main.Settings.Settings
open Settings.IO
open CCFSharpUtils
open CCFSharpUtils.Collections
open CCFSharpUtils.Text
open Spectre.Console
open System
open System.IO
Expand Down
1 change: 1 addition & 0 deletions src/CCVTAC.Main/ResultTracker.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CCVTAC.Main

open CCFSharpUtils
open CCFSharpUtils.Text
open System
open System.Collections.Generic

Expand Down
5 changes: 3 additions & 2 deletions src/CCVTAC.Main/Settings/Settings.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace CCVTAC.Main.Settings

open CCVTAC.Main
open CCFSharpUtils
open CCFSharpUtils.Text
open Spectre.Console
open FSharpPlus
open System
open System.Text.Json.Serialization

Expand Down Expand Up @@ -185,7 +186,7 @@ module Settings =
fileInfo.FullName
|> File.ReadAllText
|> deserialize<UserSettings>
|> Result.bind validate
>>= validate
with
| :? FileNotFoundException -> Error $"File \"{fileInfo.FullName}\" was not found."
| :? JsonException as e -> Error $"Parse error in \"{fileInfo.FullName}\": {e.Message}"
Expand Down
4 changes: 2 additions & 2 deletions src/CCVTAC.Tests/CCVTAC.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="DownloadEntityTests.fs" />
<Compile Include="TagDetectionTests.fs" />
<Compile Include="RenamerTests.fs" />
<Compile Include="PostProcessing\Tagging\TagDetectionTests.fs" />
<Compile Include="PostProcessing\Tagging\TaggingSetTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CCFSharpUtils" Version="0.3.5.1" />
<PackageReference Include="CCFSharpUtils" Version="0.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module TagDetectionTests
open CCVTAC.Main.PostProcessing.Tagging
open CCVTAC.Main.PostProcessing
open CCVTAC.Main.Settings.Settings
open CCFSharpUtils
open CCFSharpUtils.Text
open System
open Xunit

Expand Down
1 change: 1 addition & 0 deletions src/CCVTAC.Tests/PostProcessing/Tagging/TaggingSetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module TaggingSetInstantiationTests =

Assert.Equal(expected, actual)


[<Fact>]
let ``parses multiple files from playlist`` () =
let dir = Path.Combine("user", "Downloads", "Audio", "tmp")
Expand Down