Skip to content

Commit

Permalink
Closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
3rob3 committed Mar 15, 2024
1 parent 691d196 commit a02ca6c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
62 changes: 61 additions & 1 deletion ImmichFrame/Helpers/AssetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ public Dictionary<Guid, AssetInfo> AlbumAssetInfos
return _albumAssetInfos;
}
}
private Dictionary<Guid, AssetInfo> _peopleAssetInfos;

Check warning on line 46 in ImmichFrame/Helpers/AssetHelper.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_peopleAssetInfos' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private DateTime lastPeopleAssetRefesh;
public Dictionary<Guid, AssetInfo> PeopleAssetInfos
{
get
{
// Refresh if no assets loaded or lastAlbumRefesh is older than one day
// TODO: Put refresh duration in config
if (_peopleAssetInfos == null || lastPeopleAssetRefesh.AddDays(1) < DateTime.Now)
{
lastPeopleAssetRefesh = DateTime.Now;
var peopleAssetIds = GetPeopleAssetIds();

// Remove duplicate Ids
var uniquePeopleAssetIds = peopleAssetIds.GroupBy(x => x.Id).Select(group => group.First());
_peopleAssetInfos = uniquePeopleAssetIds.ToDictionary(x => Guid.Parse(x.Id));
}

return _peopleAssetInfos;
}
}

public AssetInfo? GetNextAsset()
{
Expand All @@ -51,7 +72,7 @@ public Dictionary<Guid, AssetInfo> AlbumAssetInfos
return GetRandomMemoryAsset();
}

return Settings.CurrentSettings.Albums.Any() ? GetRandomAlbumAsset() : GetRandomAsset();
return Settings.CurrentSettings.Albums.Any() ? GetRandomAlbumAsset() : Settings.CurrentSettings.People.Any() ? GetRandomPeopleAsset() : GetRandomAsset();
}

private IEnumerable<AssetInfo> GetMemoryAssetIds()
Expand Down Expand Up @@ -126,6 +147,36 @@ private IEnumerable<AssetInfo> GetAlbumAssetIds()
}
}

private IEnumerable<AssetInfo> GetPeopleAssetIds()
{
using (var client = new HttpClient())
{
var allAssets = new List<AssetInfo>();
var settings = Settings.CurrentSettings;

client.UseApiKey(settings.ApiKey);
foreach (var personId in settings.People!)
{
string url = $"{settings.ImmichServerUrl}/api/person/{personId}/assets";

var response = client.GetAsync(url).Result;

if (!response.IsSuccessStatusCode)
throw new AlbumNotFoundException($"Person '{personId}' was not found, check your settings file");

var responseContent = response.Content.ReadAsStringAsync().Result;

var peopleInfo = JsonDocument.Parse(responseContent);

var assetList = JsonSerializer.Deserialize<IEnumerable<AssetInfo>>(peopleInfo) ?? new List<AssetInfo>();

allAssets.AddRange(assetList);
}

return allAssets;
}
}

private Random _random = new Random();
private AssetInfo? GetRandomAlbumAsset()
{
Expand All @@ -136,6 +187,15 @@ private IEnumerable<AssetInfo> GetAlbumAssetIds()

return AlbumAssetInfos.ElementAt(rnd).Value;
}
private AssetInfo? GetRandomPeopleAsset()
{
if (!PeopleAssetInfos.Any())
throw new AssetNotFoundException();

var rnd = _random.Next(PeopleAssetInfos.Count);

return PeopleAssetInfos.ElementAt(rnd).Value;
}
private AssetInfo? GetRandomMemoryAsset()
{
if (!MemoryAssetInfos.Any())
Expand Down
2 changes: 2 additions & 0 deletions ImmichFrame/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Settings
public bool OnlyMemories { get; set; }
public int RenewImagesDuration { get; set; }
public List<Guid> Albums { get; set; }

Check warning on line 18 in ImmichFrame/Models/Settings.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Albums' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public List<Guid> People { get; set; }

Check warning on line 19 in ImmichFrame/Models/Settings.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'People' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public bool ShowClock { get; set; }
public int ClockFontSize { get; set; }
public string? ClockFormat { get; set; }
Expand Down Expand Up @@ -53,6 +54,7 @@ private static Settings Parse()
ImmichServerUrl = doc.Element("ImmichServerUrl")!.Value,

Check warning on line 54 in ImmichFrame/Models/Settings.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
ApiKey = doc.Element("ApiKey")?.Value ?? string.Empty,
Albums = doc.Element("Albums")?.DescendantNodes().OfType<XElement>().Select(x => Guid.Parse(x.Value)).Distinct().ToList() ?? new(),
People = doc.Element("People")?.DescendantNodes().OfType<XElement>().Select(x => Guid.Parse(x.Value)).Distinct().ToList() ?? new(),
Interval = int.Parse(doc.Element("Interval")!.Value),
DownloadImages = Convert.ToBoolean(doc.Element("DownloadImages")?.Value),
OnlyMemories = Convert.ToBoolean(doc.Element("OnlyMemories")?.Value),
Expand Down
7 changes: 7 additions & 0 deletions ImmichFrame/Settings.example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
<Album>First Album UID</Album>
<Album>Second Album UID</Album>
</Albums>

<!--OPTIONAL: Choose one or multiple People to have in the rotation-->
<!--Remove this section if you want to show random images-->
<People>
<Person>First person UID</Person>
<Person>Second person UID</Person>
</People>

<!--Interval for photo cycling in seconds-->
<Interval>8</Interval>
Expand Down

0 comments on commit a02ca6c

Please sign in to comment.