using System; using System.Collections.Generic; using System.Globalization; using System.IO; using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Plugins; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; namespace ConfusedPolarBear.Plugin.IntroSkipper; /// /// Intro skipper plugin. Uses audio analysis to find common sequences of audio shared between episodes. /// public class Plugin : BasePlugin, IHasWebPages { private IXmlSerializer _xmlSerializer; private string _introPath; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of the interface. public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer) : base(applicationPaths, xmlSerializer) { _xmlSerializer = xmlSerializer; // Create the base & cache directories (if needed). FingerprintCachePath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "cache"); if (!Directory.Exists(FingerprintCachePath)) { Directory.CreateDirectory(FingerprintCachePath); } _introPath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "intros.xml"); Intros = new Dictionary(); AnalysisQueue = new Dictionary>(); Instance = this; RestoreTimestamps(); } /// /// Gets the results of fingerprinting all episodes. /// public Dictionary Intros { get; } /// /// Gets the mapping of season ids to episodes that have been queued for fingerprinting. /// public Dictionary> AnalysisQueue { get; } /// /// Gets or sets the total number of episodes in the queue. /// public int TotalQueued { get; set; } /// /// Gets the directory to cache fingerprints in. /// public string FingerprintCachePath { get; private set; } /// public override string Name => "Intro Skipper"; /// public override Guid Id => Guid.Parse("c83d86bb-a1e0-4c35-a113-e2101cf4ee6b"); /// /// Gets the plugin instance. /// public static Plugin? Instance { get; private set; } /// /// Save timestamps to disk. /// public void SaveTimestamps() { var introList = new List(); foreach (var intro in Plugin.Instance!.Intros) { introList.Add(intro.Value); } _xmlSerializer.SerializeToFile(introList, _introPath); } /// /// Restore previous analysis results from disk. /// public void RestoreTimestamps() { if (!File.Exists(_introPath)) { return; } // Since dictionaries can't be easily serialized, analysis results are stored on disk as a list. var introList = (List)_xmlSerializer.DeserializeFromFile(typeof(List), _introPath); foreach (var intro in introList) { Plugin.Instance!.Intros[intro.EpisodeId] = intro; } } /// public IEnumerable GetPages() { return new[] { new PluginPageInfo { Name = this.Name, EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.configPage.html", GetType().Namespace) } }; } }