using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using eMCPy.Mcp; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; // Optional read-only live fixture replay. Fixture paths never enter reports or Git. static class InventoryReplay { public static void Run() { var path=Environment.GetEnvironmentVariable("EMCPY_INVENTORY_FIXTURE"); if(string.IsNullOrEmpty(path))return; var pages=JsonSerializer.Deserialize(File.ReadAllText(path)); var first=pages[0]; var series=new Series{InternalId=long.Parse(first.GetProperty("seriesId").GetString()),Id=Guid.Parse(first.GetProperty("seriesGuid").GetString()),Name=first.GetProperty("seriesTitle").GetString()}; int? Integer(JsonElement item,string name)=>item.GetProperty(name).ValueKind==JsonValueKind.Null?null:item.GetProperty(name).GetInt32(); long? Long(JsonElement item,string name)=>item.GetProperty(name).ValueKind==JsonValueKind.Null?null:item.GetProperty(name).GetInt64(); string Text(JsonElement item,string name)=>item.GetProperty(name).GetString(); var dtos=pages.SelectMany(p=>p.GetProperty("items").EnumerateArray()).Select(i=>new BaseItemDto{ Id=Text(i,"id"),SeriesId=Text(i,"seriesId"),Name=Text(i,"title"),ParentIndexNumber=Integer(i,"seasonNumber"),IndexNumber=Integer(i,"episodeNumber"),IndexNumberEnd=Integer(i,"episodeNumberEnd"),Path=Text(i,"path"),RunTimeTicks=Long(i,"runtimeTicks"), ProviderIds=i.GetProperty("providerIds").ValueKind==JsonValueKind.Null?null:new ProviderIdDictionary(i.GetProperty("providerIds").EnumerateObject().ToDictionary(p=>p.Name,p=>p.Value.GetString())), MediaSources=i.GetProperty("mediaSources").ValueKind==JsonValueKind.Null?null:i.GetProperty("mediaSources").EnumerateArray().Select(m=>new MediaSourceInfo{Id=Text(m,"id"),Path=Text(m,"path"),Container=Text(m,"container"),RunTimeTicks=Long(m,"runtimeTicks"),IsRemote=m.GetProperty("isRemote").GetBoolean()}).ToArray() }).ToDictionary(d=>long.Parse(d.Id)); var items=dtos.ToDictionary(p=>p.Key,p=>(BaseItem)new Episode{InternalId=p.Key}); var library=Fake.Make((m,a)=>m.Name switch{ "GetItemById"=>series, "GetInternalItemIds"=>items.Keys.ToArray(), "GetItemsResult"=>new QueryResult{Items=((InternalItemsQuery)a[0]).ItemIds.Select(id=>items[id]).ToArray()}, _=>throw new Exception("Unexpected library call") }); var dto=Fake.Make((m,a)=>((BaseItem[])a[0]).Select(i=>dtos[i.InternalId]).ToArray()); var tool=new SeriesInventory(library,dto); var sizes=new Dictionary();var bytes=new Dictionary(); foreach(var mode in new[]{"full","compact","omitted"}){ int chars=0,utf8=0;var ids=new List(); foreach(var original in pages){ var args=new Dictionary{{"seriesId",series.InternalId.ToString()},{"start",original.GetProperty("start").GetInt32()},{"limit",original.GetProperty("limit").GetInt32()},{"expectedRevision",first.GetProperty("inventoryRevision").GetString()}}; if(mode!="omitted")args["detail"]=mode; var result=JsonSerializer.SerializeToElement(tool.Execute(JsonSerializer.SerializeToElement(args))); if(mode!="compact"&&result.GetRawText()!=JsonSerializer.Serialize(original))throw new Exception("Replay full/default differs from live output"); foreach(var item in result.GetProperty("items").EnumerateArray())ids.Add(item.GetProperty("id").GetString()); chars+=result.GetRawText().EnumerateRunes().Count();utf8+=Encoding.UTF8.GetByteCount(result.GetRawText()); } if(ids.Count!=dtos.Count||ids.Distinct().Count()!=dtos.Count)throw new Exception("Replay inventory omitted or duplicated episodes"); sizes[mode]=chars;bytes[mode]=utf8; } Console.WriteLine(JsonSerializer.Serialize(new{test="live-fixture replay through new SeriesInventory (not active MCP)",episodes=dtos.Count,pages=pages.Length,characters=sizes,utf8Bytes=bytes,reductionPercent=Math.Round(100*(1-(double)sizes["compact"]/sizes["full"]),2),fullAndDefaultExactlyMatchLive=true})); } }