-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
CustomFormatCalculationService.cs
190 lines (161 loc) · 6.5 KB
/
CustomFormatCalculationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Blocklisting;
using NzbDrone.Core.History;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Music;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.CustomFormats
{
public interface ICustomFormatCalculationService
{
List<CustomFormat> ParseCustomFormat(RemoteAlbum remoteAlbum, long size);
List<CustomFormat> ParseCustomFormat(TrackFile trackFile, Artist artist);
List<CustomFormat> ParseCustomFormat(TrackFile trackFile);
List<CustomFormat> ParseCustomFormat(Blocklist blocklist, Artist artist);
List<CustomFormat> ParseCustomFormat(EntityHistory history, Artist artist);
List<CustomFormat> ParseCustomFormat(LocalTrack localTrack);
}
public class CustomFormatCalculationService : ICustomFormatCalculationService
{
private readonly ICustomFormatService _formatService;
private readonly Logger _logger;
public CustomFormatCalculationService(ICustomFormatService formatService, Logger logger)
{
_formatService = formatService;
_logger = logger;
}
public List<CustomFormat> ParseCustomFormat(RemoteAlbum remoteAlbum, long size)
{
var input = new CustomFormatInput
{
AlbumInfo = remoteAlbum.ParsedAlbumInfo,
Artist = remoteAlbum.Artist,
Size = size
};
return ParseCustomFormat(input);
}
public List<CustomFormat> ParseCustomFormat(TrackFile trackFile, Artist artist)
{
return ParseCustomFormat(trackFile, artist, _formatService.All());
}
public List<CustomFormat> ParseCustomFormat(TrackFile trackFile)
{
return ParseCustomFormat(trackFile, trackFile.Artist.Value, _formatService.All());
}
public List<CustomFormat> ParseCustomFormat(Blocklist blocklist, Artist artist)
{
var parsed = Parser.Parser.ParseAlbumTitle(blocklist.SourceTitle);
var episodeInfo = new ParsedAlbumInfo
{
ArtistName = artist.Name,
ReleaseTitle = parsed?.ReleaseTitle ?? blocklist.SourceTitle,
Quality = blocklist.Quality,
ReleaseGroup = parsed?.ReleaseGroup
};
var input = new CustomFormatInput
{
AlbumInfo = episodeInfo,
Artist = artist,
Size = blocklist.Size ?? 0
};
return ParseCustomFormat(input);
}
public List<CustomFormat> ParseCustomFormat(EntityHistory history, Artist artist)
{
var parsed = Parser.Parser.ParseAlbumTitle(history.SourceTitle);
long.TryParse(history.Data.GetValueOrDefault("size"), out var size);
var albumInfo = new ParsedAlbumInfo
{
ArtistName = artist.Name,
ReleaseTitle = parsed?.ReleaseTitle ?? history.SourceTitle,
Quality = history.Quality,
ReleaseGroup = parsed?.ReleaseGroup,
};
var input = new CustomFormatInput
{
AlbumInfo = albumInfo,
Artist = artist,
Size = size
};
return ParseCustomFormat(input);
}
public List<CustomFormat> ParseCustomFormat(LocalTrack localTrack)
{
var albumInfo = new ParsedAlbumInfo
{
ArtistName = localTrack.Artist.Name,
ReleaseTitle = localTrack.SceneName,
Quality = localTrack.Quality,
ReleaseGroup = localTrack.ReleaseGroup
};
var input = new CustomFormatInput
{
AlbumInfo = albumInfo,
Artist = localTrack.Artist,
Size = localTrack.Size
};
return ParseCustomFormat(input);
}
private List<CustomFormat> ParseCustomFormat(CustomFormatInput input)
{
return ParseCustomFormat(input, _formatService.All());
}
private static List<CustomFormat> ParseCustomFormat(CustomFormatInput input, List<CustomFormat> allCustomFormats)
{
var matches = new List<CustomFormat>();
foreach (var customFormat in allCustomFormats)
{
var specificationMatches = customFormat.Specifications
.GroupBy(t => t.GetType())
.Select(g => new SpecificationMatchesGroup
{
Matches = g.ToDictionary(t => t, t => t.IsSatisfiedBy(input))
})
.ToList();
if (specificationMatches.All(x => x.DidMatch))
{
matches.Add(customFormat);
}
}
return matches.OrderBy(x => x.Name).ToList();
}
private List<CustomFormat> ParseCustomFormat(TrackFile trackFile, Artist artist, List<CustomFormat> allCustomFormats)
{
var releaseTitle = string.Empty;
if (trackFile.SceneName.IsNotNullOrWhiteSpace())
{
_logger.Trace("Using scene name for release title: {0}", trackFile.SceneName);
releaseTitle = trackFile.SceneName;
}
else if (trackFile.OriginalFilePath.IsNotNullOrWhiteSpace())
{
_logger.Trace("Using original file path for release title: {0}", Path.GetFileName(trackFile.OriginalFilePath));
releaseTitle = trackFile.OriginalFilePath;
}
else if (trackFile.Path.IsNotNullOrWhiteSpace())
{
_logger.Trace("Using path for release title: {0}", Path.GetFileName(trackFile.Path));
releaseTitle = Path.GetFileName(trackFile.Path);
}
var episodeInfo = new ParsedAlbumInfo
{
ArtistName = artist.Name,
ReleaseTitle = releaseTitle,
Quality = trackFile.Quality,
ReleaseGroup = trackFile.ReleaseGroup
};
var input = new CustomFormatInput
{
AlbumInfo = episodeInfo,
Artist = artist,
Size = trackFile.Size,
Filename = Path.GetFileName(trackFile.Path)
};
return ParseCustomFormat(input, allCustomFormats);
}
}
}