-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrmWriter.cs
More file actions
408 lines (350 loc) · 16.5 KB
/
Copy pathStrmWriter.cs
File metadata and controls
408 lines (350 loc) · 16.5 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Jellyfin.Plugin.StashSync.Configuration;
using Jellyfin.Plugin.StashSync.GraphQL;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.StashSync.Tasks;
/// <summary>
/// Writes .strm + .nfo sidecar pairs for each Stash Group.
///
/// Output layout:
/// {StrmOutputPath}/
/// {GroupName} (StashGroup-{id})/
/// {GroupName}.strm ← streams the FIRST scene (Jellyfin's anchor file)
/// {GroupName}.nfo ← movie metadata for Jellyfin
/// chapter-metadata.xml ← chapter list used by the metadata provider
/// poster.jpg ← cover art (TMDB preferred, Stash fallback)
/// </summary>
public class StrmWriter
{
private readonly ILogger<StrmWriter> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private const string TmdbImageBase = "https://image.tmdb.org/t/p/original";
private const string TmdbApiBase = "https://api.themoviedb.org/3";
public StrmWriter(ILogger<StrmWriter> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}
/// <summary>
/// Creates/updates the folder and files for a single Group.
/// Returns true if anything was written.
/// </summary>
public async Task<bool> WriteGroupAsync(
StashGroup group,
PluginConfiguration config,
CancellationToken ct)
{
if (group.Scenes.Count < config.MinSceneCount)
{
_logger.LogDebug("[StashSync] Skipping group '{Name}' – only {Count} scene(s)", group.Name, group.Scenes.Count);
return false;
}
if (!config.IncludeGroupsWithoutCover && string.IsNullOrEmpty(group.FrontImagePath))
{
_logger.LogDebug("[StashSync] Skipping group '{Name}' – no cover image", group.Name);
return false;
}
// Sanitise folder name
var safeName = SanitiseFileName(group.Name);
var folderName = $"{safeName} (StashGroup-{group.Id})";
var folderPath = Path.Combine(config.StrmOutputPath, folderName);
Directory.CreateDirectory(folderPath);
// Validate all scenes have resolvable stream targets before writing anything
var streamTargets = group.Scenes
.Select(s => (Scene: s, Url: ResolveStreamTarget(s, config)))
.ToList();
if (streamTargets.Any(t => string.IsNullOrEmpty(t.Url)))
{
_logger.LogWarning("[StashSync] Group '{Name}' has scenes with no resolvable stream — skipping", group.Name);
return false;
}
// 1. Write .strm — points to StashProxy if configured, otherwise first scene direct URL
var strmPath = Path.Combine(folderPath, $"{safeName}.strm");
string strmTarget;
if (!string.IsNullOrWhiteSpace(config.ProxyUrl))
{
// StashProxy streams all scenes as continuous MPEG-TS via FFmpeg
strmTarget = $"{config.ProxyUrl.TrimEnd('/')}/group/{group.Id}/stream";
}
else
{
// Fallback: direct first-scene stream URL (single scene only)
strmTarget = streamTargets[0].Url;
}
await File.WriteAllTextAsync(strmPath, strmTarget, new System.Text.UTF8Encoding(false), ct).ConfigureAwait(false);
// 3. Fetch TMDB images if we have a TMDB ID and API key
var tmdbId = group.Urls.Select(ExtractTmdbId).FirstOrDefault(id => id != null);
TmdbImages? tmdbImages = null;
if (tmdbId != null && !string.IsNullOrWhiteSpace(config.TmdbApiKey))
{
tmdbImages = await FetchTmdbImagesAsync(tmdbId, config.TmdbApiKey, ct).ConfigureAwait(false);
}
// 4. Write the NFO sidecar (Kodi/Jellyfin movie format)
var nfoPath = Path.Combine(folderPath, $"{safeName}.nfo");
WriteNfo(group, tmdbId, tmdbImages, nfoPath);
// 5. Write chapter-metadata.xml (custom, read by our metadata provider)
var chaptersPath = Path.Combine(folderPath, "chapter-metadata.xml");
WriteChapterMetadata(group, config, chaptersPath);
// 6. Download poster — TMDB preferred, Stash cover as fallback
var posterPath = Path.Combine(folderPath, "poster.jpg");
if (tmdbImages?.PosterPath != null)
{
var posterUrl = $"{TmdbImageBase}{tmdbImages.PosterPath}";
await DownloadImageAsync(posterUrl, null, posterPath, ct).ConfigureAwait(false);
}
else if (!string.IsNullOrEmpty(group.FrontImagePath))
{
await DownloadImageAsync(group.FrontImagePath, config, posterPath, ct).ConfigureAwait(false);
}
// 7. Download backdrop if available from TMDB
if (tmdbImages?.BackdropPath != null)
{
var backdropPath = Path.Combine(folderPath, "backdrop.jpg");
var backdropUrl = $"{TmdbImageBase}{tmdbImages.BackdropPath}";
await DownloadImageAsync(backdropUrl, null, backdropPath, ct).ConfigureAwait(false);
}
_logger.LogInformation("[StashSync] Written group '{Name}' → {Folder}", group.Name, folderPath);
return true;
}
/// <summary>
/// Removes folders for groups that no longer exist in Stash.
/// </summary>
public void CleanupOrphanedGroups(IEnumerable<StashGroup> currentGroups, PluginConfiguration config)
{
if (!Directory.Exists(config.StrmOutputPath))
return;
var validFolderSuffixes = new HashSet<string>(
currentGroups.Select(g => $"(StashGroup-{g.Id})"),
StringComparer.OrdinalIgnoreCase);
foreach (var dir in Directory.GetDirectories(config.StrmOutputPath))
{
var dirName = Path.GetFileName(dir);
bool isStashFolder = dirName != null &&
validFolderSuffixes.Any(suffix => dirName.EndsWith(suffix, StringComparison.OrdinalIgnoreCase));
if (!isStashFolder && dirName != null && dirName.Contains("(StashGroup-", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation("[StashSync] Removing orphaned group folder: {Dir}", dir);
Directory.Delete(dir, recursive: true);
}
}
}
// ─── Private helpers ─────────────────────────────────────────────────────
private static string ResolveStreamTarget(StashScene scene, PluginConfiguration config)
{
if (config.UseStreamUrls)
return $"{config.StashUrl.TrimEnd('/')}/scene/{scene.Id}/stream";
return scene.Files.FirstOrDefault()?.Path ?? string.Empty;
}
/// <summary>
/// Writes an M3U8 playlist file listing all scene stream URLs in order.
/// Jellyfin reads this and plays each scene sequentially.
/// </summary>
private static void WriteM3U8Playlist(
List<(StashScene Scene, string Url)> scenes,
string playlistPath)
{
var sb = new StringBuilder();
sb.AppendLine("#EXTM3U");
foreach (var (scene, url) in scenes)
{
var title = string.IsNullOrWhiteSpace(scene.Title)
? $"Scene {scene.OrderIndex + 1}"
: scene.Title;
var duration = (int)(scene.Files.FirstOrDefault()?.Duration ?? -1);
// #EXTINF:<duration>,<title>
sb.AppendLine($"#EXTINF:{duration},{title}");
sb.AppendLine(url);
}
File.WriteAllText(playlistPath, sb.ToString(), Encoding.UTF8);
}
private void WriteNfo(StashGroup group, string? tmdbId, TmdbImages? tmdbImages, string nfoPath)
{
double cumulativeSeconds = 0;
var chapterElements = new List<XElement>();
foreach (var scene in group.Scenes)
{
var duration = scene.Files.FirstOrDefault()?.Duration ?? 0;
var title = string.IsNullOrWhiteSpace(scene.Title)
? $"Scene {scene.OrderIndex + 1}"
: scene.Title;
chapterElements.Add(new XElement("chapter",
new XElement("name", title),
new XElement("starttime", cumulativeSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture))
));
cumulativeSeconds += duration;
}
var root = new XElement("movie",
new XElement("title", group.Name),
new XElement("originaltitle", group.Name),
new XElement("sorttitle", group.Name),
group.Date != null ? new XElement("releasedate", group.Date) : null!,
group.Date != null ? new XElement("year", group.Date.Length >= 4 ? group.Date[..4] : group.Date) : null!,
new XElement("plot", group.Synopsis ?? string.Empty),
new XElement("outline", group.Synopsis ?? string.Empty),
group.Director != null ? new XElement("director", group.Director) : null!,
group.Studio != null ? new XElement("studio", group.Studio.Name) : null!,
group.Duration.HasValue ? new XElement("runtime", (group.Duration.Value / 60).ToString(System.Globalization.CultureInfo.InvariantCulture)) : null!,
// Stash ID for reference
new XElement("uniqueid", new XAttribute("type", "stash"), new XAttribute("default", tmdbId == null ? "true" : "false"), group.Id),
// TMDB ID — when present, Jellyfin uses this to match and fetch all metadata + images automatically
tmdbId != null ? new XElement("uniqueid", new XAttribute("type", "tmdb"), new XAttribute("default", "true"), tmdbId) : null!,
group.Tags.Count > 0
? group.Tags.Select(t => new XElement("genre", t.Name)).ToArray<object>()
: null!,
// Embed TMDB poster URL directly so Jellyfin doesn't have to fetch it separately
tmdbImages?.PosterPath != null
? new XElement("thumb",
new XAttribute("aspect", "poster"),
new XAttribute("preview", $"{TmdbImageBase}{tmdbImages.PosterPath}"),
$"{TmdbImageBase}{tmdbImages.PosterPath}")
: null!,
// Embed TMDB backdrop/fanart URL
tmdbImages?.BackdropPath != null
? new XElement("thumb",
new XAttribute("aspect", "landscape"),
new XAttribute("preview", $"{TmdbImageBase}{tmdbImages.BackdropPath}"),
$"{TmdbImageBase}{tmdbImages.BackdropPath}")
: null!,
tmdbImages?.BackdropPath != null
? new XElement("fanart",
new XElement("thumb",
new XAttribute("preview", $"{TmdbImageBase}{tmdbImages.BackdropPath}"),
$"{TmdbImageBase}{tmdbImages.BackdropPath}"))
: null!
);
foreach (var ch in chapterElements)
root.Add(ch);
var xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), root);
xml.Descendants().Where(e => e.IsEmpty && !e.HasAttributes && string.IsNullOrEmpty(e.Value)).Remove();
xml.Save(nfoPath);
}
/// <summary>
/// Calls the TMDB API to get poster and backdrop paths for a movie.
/// </summary>
private async Task<TmdbImages?> FetchTmdbImagesAsync(string tmdbId, string apiKey, CancellationToken ct)
{
try
{
using var client = _httpClientFactory.CreateClient("StashSync");
var url = $"{TmdbApiBase}/movie/{tmdbId}?api_key={apiKey}&language=en-US";
var response = await client.GetAsync(url, ct).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
_logger.LogWarning("[StashSync] TMDB API returned {Code} for movie {Id}", response.StatusCode, tmdbId);
return null;
}
var json = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var posterPath = root.TryGetProperty("poster_path", out var pp) && pp.ValueKind == JsonValueKind.String
? pp.GetString()
: null;
var backdropPath = root.TryGetProperty("backdrop_path", out var bp) && bp.ValueKind == JsonValueKind.String
? bp.GetString()
: null;
_logger.LogInformation("[StashSync] TMDB images fetched for movie {Id} — poster: {Poster}", tmdbId, posterPath ?? "none");
return new TmdbImages(posterPath, backdropPath);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "[StashSync] Failed to fetch TMDB images for movie {Id}", tmdbId);
return null;
}
}
/// <summary>
/// Parses a TMDB movie ID from a URL like https://www.themoviedb.org/movie/12345
/// </summary>
private static string? ExtractTmdbId(string url)
{
try
{
var uri = new Uri(url);
if (!uri.Host.EndsWith("themoviedb.org", StringComparison.OrdinalIgnoreCase))
return null;
var segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length >= 2 &&
segments[0].Equals("movie", StringComparison.OrdinalIgnoreCase))
{
var idPart = segments[1].Split('-')[0];
if (int.TryParse(idPart, out _))
return idPart;
}
}
catch (UriFormatException)
{
// Not a valid URL
}
return null;
}
private void WriteChapterMetadata(StashGroup group, PluginConfiguration config, string chaptersPath)
{
double cumulativeMs = 0;
var chaptersEl = new XElement("chapters");
chaptersEl.Add(new XAttribute("group_id", group.Id));
chaptersEl.Add(new XAttribute("group_name", group.Name));
foreach (var scene in group.Scenes)
{
var duration = scene.Files.FirstOrDefault()?.Duration ?? 0;
var streamUrl = ResolveStreamTarget(scene, config);
var title = string.IsNullOrWhiteSpace(scene.Title)
? $"Scene {scene.OrderIndex + 1}"
: scene.Title;
var chapterEl = new XElement("chapter",
new XAttribute("index", scene.OrderIndex),
new XAttribute("scene_id", scene.Id),
new XAttribute("title", title),
new XAttribute("start_ms", (long)cumulativeMs),
new XAttribute("duration_ms", (long)(duration * 1000)),
new XAttribute("stream_url", streamUrl)
);
chaptersEl.Add(chapterEl);
cumulativeMs += duration * 1000;
}
new XDocument(new XDeclaration("1.0", "utf-8", "yes"), chaptersEl)
.Save(chaptersPath);
}
/// <summary>
/// Downloads an image to disk. Pass null for config when the URL is already absolute (e.g. TMDB).
/// </summary>
private async Task DownloadImageAsync(
string imageUrl,
PluginConfiguration? config,
string destinationPath,
CancellationToken ct)
{
try
{
var fullUrl = imageUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? imageUrl
: $"{config?.StashUrl.TrimEnd('/')}{imageUrl}";
using var client = _httpClientFactory.CreateClient("StashSync");
if (config != null && !string.IsNullOrWhiteSpace(config.StashApiKey))
client.DefaultRequestHeaders.Add("ApiKey", config.StashApiKey);
var bytes = await client.GetByteArrayAsync(new Uri(fullUrl), ct).ConfigureAwait(false);
await File.WriteAllBytesAsync(destinationPath, bytes, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "[StashSync] Failed to download image from {Url}", imageUrl);
}
}
private static string SanitiseFileName(string name)
{
var invalid = Path.GetInvalidFileNameChars();
var sb = new StringBuilder();
foreach (var c in name)
sb.Append(invalid.Contains(c) ? '_' : c);
return sb.ToString().Trim().TrimEnd('.');
}
/// <summary>Holds poster and backdrop paths returned from the TMDB API.</summary>
private sealed record TmdbImages(string? PosterPath, string? BackdropPath);
}