diff --git a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs
deleted file mode 100644
index 7dbeaf6b7..000000000
--- a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs
+++ /dev/null
@@ -1,634 +0,0 @@
-using MediaBrowser.Common.ScheduledTasks;
-using MediaBrowser.Controller.Configuration;
-using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Plugins;
-using MediaBrowser.Model.Configuration;
-using MediaBrowser.Model.Logging;
-using Microsoft.Win32;
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Threading.Tasks;
-using CommonIO;
-using MediaBrowser.Controller;
-
-namespace MediaBrowser.Server.Implementations.IO
-{
- public class LibraryMonitor : ILibraryMonitor
- {
- ///
- /// The file system watchers
- ///
- private readonly ConcurrentDictionary _fileSystemWatchers = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
- ///
- /// The affected paths
- ///
- private readonly List _activeRefreshers = new List();
-
- ///
- /// A dynamic list of paths that should be ignored. Added to during our own file sytem modifications.
- ///
- private readonly ConcurrentDictionary _tempIgnoredPaths = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
-
- ///
- /// Any file name ending in any of these will be ignored by the watchers
- ///
- private readonly IReadOnlyList _alwaysIgnoreFiles = new List
- {
- "small.jpg",
- "albumart.jpg",
-
- // WMC temp recording directories that will constantly be written to
- "TempRec",
- "TempSBE"
- };
-
- private readonly IReadOnlyList _alwaysIgnoreSubstrings = new List
- {
- // Synology
- "eaDir",
- "#recycle",
- ".wd_tv",
- ".actors"
- };
-
- private readonly IReadOnlyList _alwaysIgnoreExtensions = new List
- {
- // thumbs.db
- ".db",
-
- // bts sync files
- ".bts",
- ".sync"
- };
-
- ///
- /// Add the path to our temporary ignore list. Use when writing to a path within our listening scope.
- ///
- /// The path.
- private void TemporarilyIgnore(string path)
- {
- _tempIgnoredPaths[path] = path;
- }
-
- public void ReportFileSystemChangeBeginning(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException("path");
- }
-
- TemporarilyIgnore(path);
- }
-
- public bool IsPathLocked(string path)
- {
- var lockedPaths = _tempIgnoredPaths.Keys.ToList();
- return lockedPaths.Any(i => string.Equals(i, path, StringComparison.OrdinalIgnoreCase) || _fileSystem.ContainsSubPath(i, path));
- }
-
- public async void ReportFileSystemChangeComplete(string path, bool refreshPath)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException("path");
- }
-
- // This is an arbitraty amount of time, but delay it because file system writes often trigger events long after the file was actually written to.
- // Seeing long delays in some situations, especially over the network, sometimes up to 45 seconds
- // But if we make this delay too high, we risk missing legitimate changes, such as user adding a new file, or hand-editing metadata
- await Task.Delay(45000).ConfigureAwait(false);
-
- string val;
- _tempIgnoredPaths.TryRemove(path, out val);
-
- if (refreshPath)
- {
- try
- {
- ReportFileSystemChanged(path);
- }
- catch (Exception ex)
- {
- Logger.ErrorException("Error in ReportFileSystemChanged for {0}", ex, path);
- }
- }
- }
-
- ///
- /// Gets or sets the logger.
- ///
- /// The logger.
- private ILogger Logger { get; set; }
-
- ///
- /// Gets or sets the task manager.
- ///
- /// The task manager.
- private ITaskManager TaskManager { get; set; }
-
- private ILibraryManager LibraryManager { get; set; }
- private IServerConfigurationManager ConfigurationManager { get; set; }
-
- private readonly IFileSystem _fileSystem;
- private readonly IServerApplicationHost _appHost;
-
- ///
- /// Initializes a new instance of the class.
- ///
- public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IServerApplicationHost appHost)
- {
- if (taskManager == null)
- {
- throw new ArgumentNullException("taskManager");
- }
-
- LibraryManager = libraryManager;
- TaskManager = taskManager;
- Logger = logManager.GetLogger(GetType().Name);
- ConfigurationManager = configurationManager;
- _fileSystem = fileSystem;
- _appHost = appHost;
-
- SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
- }
-
- ///
- /// Handles the PowerModeChanged event of the SystemEvents control.
- ///
- /// The source of the event.
- /// The instance containing the event data.
- void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
- {
- Restart();
- }
-
- private void Restart()
- {
- Stop();
- Start();
- }
-
- private bool IsLibraryMonitorEnabaled(BaseItem item)
- {
- if (item is BasePluginFolder)
- {
- return false;
- }
-
- var options = LibraryManager.GetLibraryOptions(item);
-
- if (options != null)
- {
- return options.EnableRealtimeMonitor;
- }
-
- return false;
- }
-
- public void Start()
- {
- LibraryManager.ItemAdded += LibraryManager_ItemAdded;
- LibraryManager.ItemRemoved += LibraryManager_ItemRemoved;
-
- var pathsToWatch = new List { };
-
- var paths = LibraryManager
- .RootFolder
- .Children
- .Where(IsLibraryMonitorEnabaled)
- .OfType()
- .SelectMany(f => f.PhysicalLocations)
- .Distinct(StringComparer.OrdinalIgnoreCase)
- .OrderBy(i => i)
- .ToList();
-
- foreach (var path in paths)
- {
- if (!ContainsParentFolder(pathsToWatch, path))
- {
- pathsToWatch.Add(path);
- }
- }
-
- foreach (var path in pathsToWatch)
- {
- StartWatchingPath(path);
- }
- }
-
- private void StartWatching(BaseItem item)
- {
- if (IsLibraryMonitorEnabaled(item))
- {
- StartWatchingPath(item.Path);
- }
- }
-
- ///
- /// Handles the ItemRemoved event of the LibraryManager control.
- ///
- /// The source of the event.
- /// The instance containing the event data.
- void LibraryManager_ItemRemoved(object sender, ItemChangeEventArgs e)
- {
- if (e.Item.GetParent() is AggregateFolder)
- {
- StopWatchingPath(e.Item.Path);
- }
- }
-
- ///
- /// Handles the ItemAdded event of the LibraryManager control.
- ///
- /// The source of the event.
- /// The instance containing the event data.
- void LibraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
- {
- if (e.Item.GetParent() is AggregateFolder)
- {
- StartWatching(e.Item);
- }
- }
-
- ///
- /// Examine a list of strings assumed to be file paths to see if it contains a parent of
- /// the provided path.
- ///
- /// The LST.
- /// The path.
- /// true if [contains parent folder] [the specified LST]; otherwise, false.
- /// path
- private static bool ContainsParentFolder(IEnumerable lst, string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- {
- throw new ArgumentNullException("path");
- }
-
- path = path.TrimEnd(Path.DirectorySeparatorChar);
-
- return lst.Any(str =>
- {
- //this should be a little quicker than examining each actual parent folder...
- var compare = str.TrimEnd(Path.DirectorySeparatorChar);
-
- return path.Equals(compare, StringComparison.OrdinalIgnoreCase) || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == Path.DirectorySeparatorChar);
- });
- }
-
- ///
- /// Starts the watching path.
- ///
- /// The path.
- private void StartWatchingPath(string path)
- {
- // Creating a FileSystemWatcher over the LAN can take hundreds of milliseconds, so wrap it in a Task to do them all in parallel
- Task.Run(() =>
- {
- try
- {
- var newWatcher = new FileSystemWatcher(path, "*")
- {
- IncludeSubdirectories = true
- };
-
- if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- {
- newWatcher.InternalBufferSize = 32767;
- }
-
- newWatcher.NotifyFilter = NotifyFilters.CreationTime |
- NotifyFilters.DirectoryName |
- NotifyFilters.FileName |
- NotifyFilters.LastWrite |
- NotifyFilters.Size |
- NotifyFilters.Attributes;
-
- newWatcher.Created += watcher_Changed;
- newWatcher.Deleted += watcher_Changed;
- newWatcher.Renamed += watcher_Changed;
- newWatcher.Changed += watcher_Changed;
-
- newWatcher.Error += watcher_Error;
-
- if (_fileSystemWatchers.TryAdd(path, newWatcher))
- {
- newWatcher.EnableRaisingEvents = true;
- Logger.Info("Watching directory " + path);
- }
- else
- {
- Logger.Info("Unable to add directory watcher for {0}. It already exists in the dictionary.", path);
- newWatcher.Dispose();
- }
-
- }
- catch (Exception ex)
- {
- Logger.ErrorException("Error watching path: {0}", ex, path);
- }
- });
- }
-
- ///
- /// Stops the watching path.
- ///
- /// The path.
- private void StopWatchingPath(string path)
- {
- FileSystemWatcher watcher;
-
- if (_fileSystemWatchers.TryGetValue(path, out watcher))
- {
- DisposeWatcher(watcher);
- }
- }
-
- ///
- /// Disposes the watcher.
- ///
- /// The watcher.
- private void DisposeWatcher(FileSystemWatcher watcher)
- {
- try
- {
- using (watcher)
- {
- Logger.Info("Stopping directory watching for path {0}", watcher.Path);
-
- watcher.EnableRaisingEvents = false;
- }
- }
- catch
- {
-
- }
- finally
- {
- RemoveWatcherFromList(watcher);
- }
- }
-
- ///
- /// Removes the watcher from list.
- ///
- /// The watcher.
- private void RemoveWatcherFromList(FileSystemWatcher watcher)
- {
- FileSystemWatcher removed;
-
- _fileSystemWatchers.TryRemove(watcher.Path, out removed);
- }
-
- ///
- /// Handles the Error event of the watcher control.
- ///
- /// The source of the event.
- /// The instance containing the event data.
- void watcher_Error(object sender, ErrorEventArgs e)
- {
- var ex = e.GetException();
- var dw = (FileSystemWatcher)sender;
-
- Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex);
-
- DisposeWatcher(dw);
- }
-
- ///
- /// Handles the Changed event of the watcher control.
- ///
- /// The source of the event.
- /// The instance containing the event data.
- void watcher_Changed(object sender, FileSystemEventArgs e)
- {
- try
- {
- Logger.Debug("Changed detected of type " + e.ChangeType + " to " + e.FullPath);
-
- var path = e.FullPath;
-
- // For deletes, use the parent path
- if (e.ChangeType == WatcherChangeTypes.Deleted)
- {
- var parentPath = Path.GetDirectoryName(path);
-
- if (!string.IsNullOrWhiteSpace(parentPath))
- {
- path = parentPath;
- }
- }
-
- ReportFileSystemChanged(path);
- }
- catch (Exception ex)
- {
- Logger.ErrorException("Exception in ReportFileSystemChanged. Path: {0}", ex, e.FullPath);
- }
- }
-
- public void ReportFileSystemChanged(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException("path");
- }
-
- var filename = Path.GetFileName(path);
-
- var monitorPath = !string.IsNullOrEmpty(filename) &&
- !_alwaysIgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase) &&
- !_alwaysIgnoreExtensions.Contains(Path.GetExtension(path) ?? string.Empty, StringComparer.OrdinalIgnoreCase) &&
- _alwaysIgnoreSubstrings.All(i => path.IndexOf(i, StringComparison.OrdinalIgnoreCase) == -1);
-
- // Ignore certain files
- var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
-
- // If the parent of an ignored path has a change event, ignore that too
- if (tempIgnorePaths.Any(i =>
- {
- if (string.Equals(i, path, StringComparison.OrdinalIgnoreCase))
- {
- Logger.Debug("Ignoring change to {0}", path);
- return true;
- }
-
- if (_fileSystem.ContainsSubPath(i, path))
- {
- Logger.Debug("Ignoring change to {0}", path);
- return true;
- }
-
- // Go up a level
- var parent = Path.GetDirectoryName(i);
- if (!string.IsNullOrEmpty(parent))
- {
- if (string.Equals(parent, path, StringComparison.OrdinalIgnoreCase))
- {
- Logger.Debug("Ignoring change to {0}", path);
- return true;
- }
- }
-
- return false;
-
- }))
- {
- monitorPath = false;
- }
-
- if (monitorPath)
- {
- // Avoid implicitly captured closure
- CreateRefresher(path);
- }
- }
-
- private void CreateRefresher(string path)
- {
- var parentPath = Path.GetDirectoryName(path);
-
- lock (_activeRefreshers)
- {
- var refreshers = _activeRefreshers.ToList();
- foreach (var refresher in refreshers)
- {
- // Path is already being refreshed
- if (string.Equals(path, refresher.Path, StringComparison.Ordinal))
- {
- refresher.RestartTimer();
- return;
- }
-
- // Parent folder is already being refreshed
- if (_fileSystem.ContainsSubPath(refresher.Path, path))
- {
- refresher.AddPath(path);
- return;
- }
-
- // New path is a parent
- if (_fileSystem.ContainsSubPath(path, refresher.Path))
- {
- refresher.ResetPath(path, null);
- return;
- }
-
- // They are siblings. Rebase the refresher to the parent folder.
- if (string.Equals(parentPath, Path.GetDirectoryName(refresher.Path), StringComparison.Ordinal))
- {
- refresher.ResetPath(parentPath, path);
- return;
- }
- }
-
- var newRefresher = new FileRefresher(path, _fileSystem, ConfigurationManager, LibraryManager, TaskManager, Logger);
- newRefresher.Completed += NewRefresher_Completed;
- _activeRefreshers.Add(newRefresher);
- }
- }
-
- private void NewRefresher_Completed(object sender, EventArgs e)
- {
- var refresher = (FileRefresher)sender;
- DisposeRefresher(refresher);
- }
-
- ///
- /// Stops this instance.
- ///
- public void Stop()
- {
- LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
- LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
-
- foreach (var watcher in _fileSystemWatchers.Values.ToList())
- {
- watcher.Created -= watcher_Changed;
- watcher.Deleted -= watcher_Changed;
- watcher.Renamed -= watcher_Changed;
- watcher.Changed -= watcher_Changed;
-
- try
- {
- watcher.EnableRaisingEvents = false;
- }
- catch (InvalidOperationException)
- {
- // Seeing this under mono on linux sometimes
- // Collection was modified; enumeration operation may not execute.
- }
-
- watcher.Dispose();
- }
-
- _fileSystemWatchers.Clear();
- DisposeRefreshers();
- }
-
- private void DisposeRefresher(FileRefresher refresher)
- {
- lock (_activeRefreshers)
- {
- refresher.Dispose();
- _activeRefreshers.Remove(refresher);
- }
- }
-
- private void DisposeRefreshers()
- {
- lock (_activeRefreshers)
- {
- foreach (var refresher in _activeRefreshers.ToList())
- {
- refresher.Dispose();
- }
- _activeRefreshers.Clear();
- }
- }
-
- ///
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- ///
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- ///
- /// Releases unmanaged and - optionally - managed resources.
- ///
- /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
- protected virtual void Dispose(bool dispose)
- {
- if (dispose)
- {
- Stop();
- }
- }
- }
-
- public class LibraryMonitorStartup : IServerEntryPoint
- {
- private readonly ILibraryMonitor _monitor;
-
- public LibraryMonitorStartup(ILibraryMonitor monitor)
- {
- _monitor = monitor;
- }
-
- public void Run()
- {
- _monitor.Start();
- }
-
- public void Dispose()
- {
- }
- }
-}
diff --git a/MediaBrowser.Server.Implementations/Intros/DefaultIntroProvider.cs b/MediaBrowser.Server.Implementations/Intros/DefaultIntroProvider.cs
deleted file mode 100644
index b86a5f5d4..000000000
--- a/MediaBrowser.Server.Implementations/Intros/DefaultIntroProvider.cs
+++ /dev/null
@@ -1,384 +0,0 @@
-using MediaBrowser.Common.Configuration;
-using MediaBrowser.Common.Security;
-using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Entities.Movies;
-using MediaBrowser.Controller.Entities.TV;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Localization;
-using MediaBrowser.Model.Configuration;
-using MediaBrowser.Model.Entities;
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Threading.Tasks;
-using CommonIO;
-using MoreLinq;
-
-namespace MediaBrowser.Server.Implementations.Intros
-{
- public class DefaultIntroProvider : IIntroProvider
- {
- private readonly ISecurityManager _security;
- private readonly ILocalizationManager _localization;
- private readonly IConfigurationManager _serverConfig;
- private readonly ILibraryManager _libraryManager;
- private readonly IFileSystem _fileSystem;
- private readonly IMediaSourceManager _mediaSourceManager;
-
- public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
- {
- _security = security;
- _localization = localization;
- _serverConfig = serverConfig;
- _libraryManager = libraryManager;
- _fileSystem = fileSystem;
- _mediaSourceManager = mediaSourceManager;
- }
-
- public async Task> GetIntros(BaseItem item, User user)
- {
- var config = GetOptions();
-
- if (item is Movie)
- {
- if (!config.EnableIntrosForMovies)
- {
- return new List();
- }
- }
- else if (item is Episode)
- {
- if (!config.EnableIntrosForEpisodes)
- {
- return new List();
- }
- }
- else
- {
- return new List();
- }
-
- var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
- ? null
- : _localization.GetRatingLevel(item.OfficialRating);
-
- var candidates = new List();
-
- var trailerTypes = new List();
- var sourceTypes = new List();
-
- if (config.EnableIntrosFromMoviesInLibrary)
- {
- trailerTypes.Add(TrailerType.LocalTrailer);
- sourceTypes.Add(SourceType.Library);
- }
-
- if (IsSupporter)
- {
- if (config.EnableIntrosFromUpcomingTrailers)
- {
- trailerTypes.Add(TrailerType.ComingSoonToTheaters);
- sourceTypes.Clear();
- }
- if (config.EnableIntrosFromUpcomingDvdMovies)
- {
- trailerTypes.Add(TrailerType.ComingSoonToDvd);
- sourceTypes.Clear();
- }
- if (config.EnableIntrosFromUpcomingStreamingMovies)
- {
- trailerTypes.Add(TrailerType.ComingSoonToStreaming);
- sourceTypes.Clear();
- }
- if (config.EnableIntrosFromSimilarMovies)
- {
- trailerTypes.Add(TrailerType.Archive);
- sourceTypes.Clear();
- }
- }
-
- if (trailerTypes.Count > 0)
- {
- var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
- {
- IncludeItemTypes = new[] { typeof(Trailer).Name },
- TrailerTypes = trailerTypes.ToArray(),
- SimilarTo = item,
- IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
- MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
- BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
-
- // Account for duplicates by imdb id, since the database doesn't support this yet
- Limit = config.TrailerLimit * 2,
- SourceTypes = sourceTypes.ToArray()
-
- }).Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase)).Take(config.TrailerLimit);
-
- candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
- {
- Item = i,
- Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
- LibraryManager = _libraryManager
- }));
- }
-
- return GetResult(item, candidates, config);
- }
-
- private IEnumerable GetResult(BaseItem item, IEnumerable candidates, CinemaModeConfiguration config)
- {
- var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
- GetCustomIntros(config) :
- new List();
-
- var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
- GetMediaInfoIntros(config, item) :
- new List();
-
- // Avoid implicitly captured closure
- return candidates.Select(i => i.IntroInfo)
- .Concat(customIntros.Take(1))
- .Concat(mediaInfoIntros);
- }
-
- private CinemaModeConfiguration GetOptions()
- {
- return _serverConfig.GetConfiguration("cinemamode");
- }
-
- private List GetCustomIntros(CinemaModeConfiguration options)
- {
- try
- {
- return GetCustomIntroFiles(options, true, false)
- .OrderBy(i => Guid.NewGuid())
- .Select(i => new IntroInfo
- {
- Path = i
-
- }).ToList();
- }
- catch (IOException)
- {
- return new List();
- }
- }
-
- private IEnumerable GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
- {
- try
- {
- var hasMediaSources = item as IHasMediaSources;
-
- if (hasMediaSources == null)
- {
- return new List();
- }
-
- var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
- .FirstOrDefault();
-
- if (mediaSource == null)
- {
- return new List();
- }
-
- var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
- var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
-
- var allIntros = GetCustomIntroFiles(options, false, true)
- .OrderBy(i => Guid.NewGuid())
- .Select(i => new IntroInfo
- {
- Path = i
-
- }).ToList();
-
- var returnResult = new List();
-
- if (videoStream != null)
- {
- returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
- }
-
- if (audioStream != null)
- {
- returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
- }
-
- returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
-
- return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
- }
- catch (IOException)
- {
- return new List();
- }
- }
-
- private IEnumerable GetMediaInfoIntrosByVideoStream(List allIntros, MediaStream stream)
- {
- var codec = stream.Codec;
-
- if (string.IsNullOrWhiteSpace(codec))
- {
- return new List();
- }
-
- return allIntros
- .Where(i => IsMatch(i.Path, codec))
- .OrderBy(i => Guid.NewGuid());
- }
-
- private IEnumerable GetMediaInfoIntrosByAudioStream(List allIntros, MediaStream stream)
- {
- var codec = stream.Codec;
-
- if (string.IsNullOrWhiteSpace(codec))
- {
- return new List();
- }
-
- return allIntros
- .Where(i => IsAudioMatch(i.Path, stream))
- .OrderBy(i => Guid.NewGuid());
- }
-
- private IEnumerable GetMediaInfoIntrosByTags(List allIntros, List tags)
- {
- return allIntros
- .Where(i => tags.Any(t => IsMatch(i.Path, t)))
- .OrderBy(i => Guid.NewGuid());
- }
-
- private bool IsMatch(string file, string attribute)
- {
- var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
- filename = Normalize(filename);
-
- if (string.IsNullOrWhiteSpace(filename))
- {
- return false;
- }
-
- attribute = Normalize(attribute);
- if (string.IsNullOrWhiteSpace(attribute))
- {
- return false;
- }
-
- return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
- }
-
- private string Normalize(string value)
- {
- return value;
- }
-
- private bool IsAudioMatch(string path, MediaStream stream)
- {
- if (!string.IsNullOrWhiteSpace(stream.Codec))
- {
- if (IsMatch(path, stream.Codec))
- {
- return true;
- }
- }
- if (!string.IsNullOrWhiteSpace(stream.Profile))
- {
- if (IsMatch(path, stream.Profile))
- {
- return true;
- }
- }
-
- return false;
- }
-
- private IEnumerable GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
- {
- var list = new List();
-
- if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
- {
- list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
- .Where(_libraryManager.IsVideoFile));
- }
-
- if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
- {
- list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
- .Where(_libraryManager.IsVideoFile));
- }
-
- return list.Distinct(StringComparer.OrdinalIgnoreCase);
- }
-
- public IEnumerable GetAllIntroFiles()
- {
- return GetCustomIntroFiles(GetOptions(), true, true);
- }
-
- private bool IsSupporter
- {
- get { return _security.IsMBSupporter; }
- }
-
- public string Name
- {
- get { return "Default"; }
- }
-
- internal class ItemWithTrailer
- {
- internal BaseItem Item;
- internal ItemWithTrailerType Type;
- internal ILibraryManager LibraryManager;
-
- public IntroInfo IntroInfo
- {
- get
- {
- var id = Item.Id;
-
- if (Type == ItemWithTrailerType.ItemWithTrailer)
- {
- var hasTrailers = Item as IHasTrailers;
-
- if (hasTrailers != null)
- {
- id = hasTrailers.LocalTrailerIds.FirstOrDefault();
- }
- }
- return new IntroInfo
- {
- ItemId = id
- };
- }
- }
- }
-
- internal enum ItemWithTrailerType
- {
- ChannelTrailer,
- ItemWithTrailer
- }
- }
-
- public class CinemaModeConfigurationFactory : IConfigurationFactory
- {
- public IEnumerable GetConfigurations()
- {
- return new[]
- {
- new ConfigurationStore
- {
- ConfigurationType = typeof(CinemaModeConfiguration),
- Key = "cinemamode"
- }
- };
- }
- }
-
-}
diff --git a/MediaBrowser.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs b/MediaBrowser.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs
deleted file mode 100644
index d5abcf98e..000000000
--- a/MediaBrowser.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs
+++ /dev/null
@@ -1,1310 +0,0 @@
-using System.Net;
-using MediaBrowser.Common;
-using MediaBrowser.Common.Net;
-using MediaBrowser.Controller.LiveTv;
-using MediaBrowser.Model.Dto;
-using MediaBrowser.Model.LiveTv;
-using MediaBrowser.Model.Logging;
-using MediaBrowser.Model.Net;
-using MediaBrowser.Model.Serialization;
-using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace MediaBrowser.Server.Implementations.LiveTv.Listings
-{
- public class SchedulesDirect : IListingsProvider
- {
- private readonly ILogger _logger;
- private readonly IJsonSerializer _jsonSerializer;
- private readonly IHttpClient _httpClient;
- private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
- private readonly IApplicationHost _appHost;
-
- private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
-
- private readonly Dictionary> _channelPairingCache =
- new Dictionary>(StringComparer.OrdinalIgnoreCase);
-
- public SchedulesDirect(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient, IApplicationHost appHost)
- {
- _logger = logger;
- _jsonSerializer = jsonSerializer;
- _httpClient = httpClient;
- _appHost = appHost;
- }
-
- private string UserAgent
- {
- get { return "Emby/" + _appHost.ApplicationVersion; }
- }
-
- private List GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
- {
- List dates = new List();
-
- var start = new List { startDateUtc, startDateUtc.ToLocalTime() }.Min().Date;
- var end = new List { endDateUtc, endDateUtc.ToLocalTime() }.Max().Date;
-
- while (start <= end)
- {
- dates.Add(start.ToString("yyyy-MM-dd"));
- start = start.AddDays(1);
- }
-
- return dates;
- }
-
- public async Task> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
- {
- List programsInfo = new List();
-
- var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
-
- if (string.IsNullOrWhiteSpace(token))
- {
- _logger.Warn("SchedulesDirect token is empty, returning empty program list");
- return programsInfo;
- }
-
- if (string.IsNullOrWhiteSpace(info.ListingsId))
- {
- _logger.Warn("ListingsId is null, returning empty program list");
- return programsInfo;
- }
-
- var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
-
- ScheduleDirect.Station station = GetStation(info.ListingsId, channelNumber, channelName);
-
- if (station == null)
- {
- _logger.Info("No Schedules Direct Station found for channel {0} with name {1}", channelNumber, channelName);
- return programsInfo;
- }
-
- string stationID = station.stationID;
-
- _logger.Info("Channel Station ID is: " + stationID);
- List requestList =
- new List()
- {
- new ScheduleDirect.RequestScheduleForChannel()
- {
- stationID = stationID,
- date = dates
- }
- };
-
- var requestString = _jsonSerializer.SerializeToString(requestList);
- _logger.Debug("Request string for schedules is: " + requestString);
-
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/schedules",
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- // The data can be large so give it some extra time
- TimeoutMs = 60000,
- LogErrorResponseBody = true
- };
-
- httpOptions.RequestHeaders["token"] = token;
-
- httpOptions.RequestContent = requestString;
- using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
- {
- StreamReader reader = new StreamReader(response.Content);
- string responseString = reader.ReadToEnd();
- var dailySchedules = _jsonSerializer.DeserializeFromString>(responseString);
- _logger.Debug("Found " + dailySchedules.Count + " programs on " + channelNumber + " ScheduleDirect");
-
- httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/programs",
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true,
- // The data can be large so give it some extra time
- TimeoutMs = 60000
- };
-
- httpOptions.RequestHeaders["token"] = token;
-
- List programsID = new List();
- programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct().ToList();
- var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
- httpOptions.RequestContent = requestBody;
-
- using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
- {
- StreamReader innerReader = new StreamReader(innerResponse.Content);
- responseString = innerReader.ReadToEnd();
-
- var programDetails =
- _jsonSerializer.DeserializeFromString>(
- responseString);
- var programDict = programDetails.ToDictionary(p => p.programID, y => y);
-
- var images = await GetImageForPrograms(info, programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken);
-
- var schedules = dailySchedules.SelectMany(d => d.programs);
- foreach (ScheduleDirect.Program schedule in schedules)
- {
- //_logger.Debug("Proccesing Schedule for statio ID " + stationID +
- // " which corresponds to channel " + channelNumber + " and program id " +
- // schedule.programID + " which says it has images? " +
- // programDict[schedule.programID].hasImageArtwork);
-
- if (images != null)
- {
- var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10));
- if (imageIndex > -1)
- {
- var programEntry = programDict[schedule.programID];
-
- var data = images[imageIndex].data ?? new List();
- data = data.OrderByDescending(GetSizeOrder).ToList();
-
- programEntry.primaryImage = GetProgramImage(ApiUrl, data, "Logo", true, 600);
- //programEntry.thumbImage = GetProgramImage(ApiUrl, data, "Iconic", false);
- //programEntry.bannerImage = GetProgramImage(ApiUrl, data, "Banner", false) ??
- // GetProgramImage(ApiUrl, data, "Banner-L1", false) ??
- // GetProgramImage(ApiUrl, data, "Banner-LO", false) ??
- // GetProgramImage(ApiUrl, data, "Banner-LOT", false);
- }
- }
-
- programsInfo.Add(GetProgram(channelNumber, schedule, programDict[schedule.programID]));
- }
- _logger.Info("Finished with EPGData");
- }
- }
-
- return programsInfo;
- }
-
- private int GetSizeOrder(ScheduleDirect.ImageData image)
- {
- if (!string.IsNullOrWhiteSpace(image.height))
- {
- int value;
- if (int.TryParse(image.height, out value))
- {
- return value;
- }
- }
-
- return 0;
- }
-
- private readonly object _channelCacheLock = new object();
- private ScheduleDirect.Station GetStation(string listingsId, string channelNumber, string channelName)
- {
- lock (_channelCacheLock)
- {
- Dictionary channelPair;
- if (_channelPairingCache.TryGetValue(listingsId, out channelPair))
- {
- ScheduleDirect.Station station;
-
- if (channelPair.TryGetValue(channelNumber, out station))
- {
- return station;
- }
-
- if (!string.IsNullOrWhiteSpace(channelName))
- {
- channelName = NormalizeName(channelName);
-
- var result = channelPair.Values.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
-
- if (result != null)
- {
- return result;
- }
- }
-
- if (!string.IsNullOrWhiteSpace(channelNumber))
- {
- return channelPair.Values.FirstOrDefault(i => string.Equals(NormalizeName(i.stationID ?? string.Empty), channelNumber, StringComparison.OrdinalIgnoreCase));
- }
- }
-
- return null;
- }
- }
-
- private void AddToChannelPairCache(string listingsId, string channelNumber, ScheduleDirect.Station schChannel)
- {
- lock (_channelCacheLock)
- {
- Dictionary cache;
- if (_channelPairingCache.TryGetValue(listingsId, out cache))
- {
- cache[channelNumber] = schChannel;
- }
- else
- {
- cache = new Dictionary();
- cache[channelNumber] = schChannel;
- _channelPairingCache[listingsId] = cache;
- }
- }
- }
-
- private void ClearPairCache(string listingsId)
- {
- lock (_channelCacheLock)
- {
- Dictionary cache;
- if (_channelPairingCache.TryGetValue(listingsId, out cache))
- {
- cache.Clear();
- }
- }
- }
-
- private int GetChannelPairCacheCount(string listingsId)
- {
- lock (_channelCacheLock)
- {
- Dictionary cache;
- if (_channelPairingCache.TryGetValue(listingsId, out cache))
- {
- return cache.Count;
- }
-
- return 0;
- }
- }
-
- private string NormalizeName(string value)
- {
- return value.Replace(" ", string.Empty).Replace("-", string.Empty);
- }
-
- public async Task AddMetadata(ListingsProviderInfo info, List channels,
- CancellationToken cancellationToken)
- {
- var listingsId = info.ListingsId;
- if (string.IsNullOrWhiteSpace(listingsId))
- {
- throw new Exception("ListingsId required");
- }
-
- var token = await GetToken(info, cancellationToken);
-
- if (string.IsNullOrWhiteSpace(token))
- {
- throw new Exception("token required");
- }
-
- ClearPairCache(listingsId);
-
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/lineups/" + listingsId,
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true,
- // The data can be large so give it some extra time
- TimeoutMs = 60000
- };
-
- httpOptions.RequestHeaders["token"] = token;
-
- using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
- {
- var root = _jsonSerializer.DeserializeFromStream(response);
- _logger.Info("Found " + root.map.Count + " channels on the lineup on ScheduleDirect");
- _logger.Info("Mapping Stations to Channel");
- foreach (ScheduleDirect.Map map in root.map)
- {
- var channelNumber = map.logicalChannelNumber;
-
- if (string.IsNullOrWhiteSpace(channelNumber))
- {
- channelNumber = map.channel;
- }
- if (string.IsNullOrWhiteSpace(channelNumber))
- {
- channelNumber = map.atscMajor + "." + map.atscMinor;
- }
- channelNumber = channelNumber.TrimStart('0');
-
- _logger.Debug("Found channel: " + channelNumber + " in Schedules Direct");
-
- var schChannel = (root.stations ?? new List()).FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase));
- if (schChannel != null)
- {
- AddToChannelPairCache(listingsId, channelNumber, schChannel);
- }
- else
- {
- AddToChannelPairCache(listingsId, channelNumber, new ScheduleDirect.Station
- {
- stationID = map.stationID
- });
- }
- }
- _logger.Info("Added " + GetChannelPairCacheCount(listingsId) + " channels to the dictionary");
-
- foreach (ChannelInfo channel in channels)
- {
- var station = GetStation(listingsId, channel.Number, channel.Name);
-
- if (station != null)
- {
- if (station.logo != null)
- {
- channel.ImageUrl = station.logo.URL;
- channel.HasImage = true;
- }
-
- if (!string.IsNullOrWhiteSpace(station.name))
- {
- channel.Name = station.name;
- }
- }
- else
- {
- _logger.Info("Schedules Direct doesnt have data for channel: " + channel.Number + " " + channel.Name);
- }
- }
- }
- }
-
- private ProgramInfo GetProgram(string channel, ScheduleDirect.Program programInfo,
- ScheduleDirect.ProgramDetails details)
- {
- //_logger.Debug("Show type is: " + (details.showType ?? "No ShowType"));
- DateTime startAt = GetDate(programInfo.airDateTime);
- DateTime endAt = startAt.AddSeconds(programInfo.duration);
- ProgramAudio audioType = ProgramAudio.Stereo;
-
- bool repeat = programInfo.@new == null;
- string newID = programInfo.programID + "T" + startAt.Ticks + "C" + channel;
-
- if (programInfo.audioProperties != null)
- {
- if (programInfo.audioProperties.Exists(item => string.Equals(item, "atmos", StringComparison.OrdinalIgnoreCase)))
- {
- audioType = ProgramAudio.Atmos;
- }
- else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
- {
- audioType = ProgramAudio.DolbyDigital;
- }
- else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd", StringComparison.OrdinalIgnoreCase)))
- {
- audioType = ProgramAudio.DolbyDigital;
- }
- else if (programInfo.audioProperties.Exists(item => string.Equals(item, "stereo", StringComparison.OrdinalIgnoreCase)))
- {
- audioType = ProgramAudio.Stereo;
- }
- else
- {
- audioType = ProgramAudio.Mono;
- }
- }
-
- string episodeTitle = null;
- if (details.episodeTitle150 != null)
- {
- episodeTitle = details.episodeTitle150;
- }
-
- var showType = details.showType ?? string.Empty;
-
- var info = new ProgramInfo
- {
- ChannelId = channel,
- Id = newID,
- StartDate = startAt,
- EndDate = endAt,
- Name = details.titles[0].title120 ?? "Unkown",
- OfficialRating = null,
- CommunityRating = null,
- EpisodeTitle = episodeTitle,
- Audio = audioType,
- IsRepeat = repeat,
- IsSeries = showType.IndexOf("series", StringComparison.OrdinalIgnoreCase) != -1,
- ImageUrl = details.primaryImage,
- IsKids = string.Equals(details.audience, "children", StringComparison.OrdinalIgnoreCase),
- IsSports = showType.IndexOf("sports", StringComparison.OrdinalIgnoreCase) != -1,
- IsMovie = showType.IndexOf("movie", StringComparison.OrdinalIgnoreCase) != -1 || showType.IndexOf("film", StringComparison.OrdinalIgnoreCase) != -1,
- ShowId = programInfo.programID,
- Etag = programInfo.md5
- };
-
- if (programInfo.videoProperties != null)
- {
- info.IsHD = programInfo.videoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
- info.Is3D = programInfo.videoProperties.Contains("3d", StringComparer.OrdinalIgnoreCase);
- }
-
- if (details.contentRating != null && details.contentRating.Count > 0)
- {
- info.OfficialRating = details.contentRating[0].code.Replace("TV", "TV-").Replace("--", "-");
-
- var invalid = new[] { "N/A", "Approved", "Not Rated", "Passed" };
- if (invalid.Contains(info.OfficialRating, StringComparer.OrdinalIgnoreCase))
- {
- info.OfficialRating = null;
- }
- }
-
- if (details.descriptions != null)
- {
- if (details.descriptions.description1000 != null)
- {
- info.Overview = details.descriptions.description1000[0].description;
- }
- else if (details.descriptions.description100 != null)
- {
- info.ShortOverview = details.descriptions.description100[0].description;
- }
- }
-
- if (info.IsSeries)
- {
- info.SeriesId = programInfo.programID.Substring(0, 10);
-
- if (details.metadata != null)
- {
- var gracenote = details.metadata.Find(x => x.Gracenote != null).Gracenote;
- info.SeasonNumber = gracenote.season;
- info.EpisodeNumber = gracenote.episode;
- }
- }
-
- if (!string.IsNullOrWhiteSpace(details.originalAirDate) && (!info.IsSeries || info.IsRepeat))
- {
- info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
- info.ProductionYear = info.OriginalAirDate.Value.Year;
- }
-
- if (details.genres != null)
- {
- info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
- info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
-
- if (info.Genres.Contains("children", StringComparer.OrdinalIgnoreCase))
- {
- info.IsKids = true;
- }
- }
-
- return info;
- }
-
- private DateTime GetDate(string value)
- {
- var date = DateTime.ParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
-
- if (date.Kind != DateTimeKind.Utc)
- {
- date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
- }
- return date;
- }
-
- private string GetProgramImage(string apiUrl, List images, string category, bool returnDefaultImage, int desiredWidth)
- {
- string url = null;
-
- var matches = images
- .Where(i => string.Equals(i.category, category, StringComparison.OrdinalIgnoreCase))
- .ToList();
-
- if (matches.Count == 0)
- {
- if (!returnDefaultImage)
- {
- return null;
- }
- matches = images;
- }
-
- var match = matches.FirstOrDefault(i =>
- {
- if (!string.IsNullOrWhiteSpace(i.width))
- {
- int value;
- if (int.TryParse(i.width, out value))
- {
- return value <= desiredWidth;
- }
- }
-
- return false;
- });
-
- if (match == null)
- {
- // Get the second lowest quality image, when possible
- if (matches.Count > 1)
- {
- match = matches[matches.Count - 2];
- }
- else
- {
- match = matches.FirstOrDefault();
- }
- }
-
- if (match == null)
- {
- return null;
- }
-
- var uri = match.uri;
-
- if (!string.IsNullOrWhiteSpace(uri))
- {
- if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
- {
- url = uri;
- }
- else
- {
- url = apiUrl + "/image/" + uri;
- }
- }
- //_logger.Debug("URL for image is : " + url);
- return url;
- }
-
- private async Task> GetImageForPrograms(
- ListingsProviderInfo info,
- List programIds,
- CancellationToken cancellationToken)
- {
- var imageIdString = "[";
-
- programIds.ForEach(i =>
- {
- if (!imageIdString.Contains(i.Substring(0, 10)))
- {
- imageIdString += "\"" + i.Substring(0, 10) + "\",";
- }
- });
- imageIdString = imageIdString.TrimEnd(',') + "]";
-
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/metadata/programs",
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- RequestContent = imageIdString,
- LogErrorResponseBody = true,
- // The data can be large so give it some extra time
- TimeoutMs = 60000
- };
- List images;
- using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
- {
- images = _jsonSerializer.DeserializeFromStream>(
- innerResponse2.Content);
- }
-
- return images;
- }
-
- public async Task> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
- {
- var token = await GetToken(info, cancellationToken);
-
- var lineups = new List();
-
- if (string.IsNullOrWhiteSpace(token))
- {
- return lineups;
- }
-
- var options = new HttpRequestOptions()
- {
- Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true
- };
-
- options.RequestHeaders["token"] = token;
-
- try
- {
- using (Stream responce = await Get(options, false, info).ConfigureAwait(false))
- {
- var root = _jsonSerializer.DeserializeFromStream>(responce);
-
- if (root != null)
- {
- foreach (ScheduleDirect.Headends headend in root)
- {
- foreach (ScheduleDirect.Lineup lineup in headend.lineups)
- {
- lineups.Add(new NameIdPair
- {
- Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
- Id = lineup.uri.Substring(18)
- });
- }
- }
- }
- else
- {
- _logger.Info("No lineups available");
- }
- }
- }
- catch (Exception ex)
- {
- _logger.Error("Error getting headends", ex);
- }
-
- return lineups;
- }
-
- private readonly ConcurrentDictionary _tokens = new ConcurrentDictionary();
- private DateTime _lastErrorResponse;
- private async Task GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
- {
- var username = info.Username;
-
- // Reset the token if there's no username
- if (string.IsNullOrWhiteSpace(username))
- {
- return null;
- }
-
- var password = info.Password;
- if (string.IsNullOrWhiteSpace(password))
- {
- return null;
- }
-
- // Avoid hammering SD
- if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
- {
- return null;
- }
-
- NameValuePair savedToken = null;
- if (!_tokens.TryGetValue(username, out savedToken))
- {
- savedToken = new NameValuePair();
- _tokens.TryAdd(username, savedToken);
- }
-
- if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
- {
- long ticks;
- if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ticks))
- {
- // If it's under 24 hours old we can still use it
- if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
- {
- return savedToken.Name;
- }
- }
- }
-
- await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
- try
- {
- var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
- savedToken.Name = result;
- savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
- return result;
- }
- catch (HttpException ex)
- {
- if (ex.StatusCode.HasValue)
- {
- if ((int)ex.StatusCode.Value == 400)
- {
- _tokens.Clear();
- _lastErrorResponse = DateTime.UtcNow;
- }
- }
- throw;
- }
- finally
- {
- _tokenSemaphore.Release();
- }
- }
-
- private async Task Post(HttpRequestOptions options,
- bool enableRetry,
- ListingsProviderInfo providerInfo)
- {
- try
- {
- return await _httpClient.Post(options).ConfigureAwait(false);
- }
- catch (HttpException ex)
- {
- _tokens.Clear();
-
- if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
- {
- enableRetry = false;
- }
-
- if (!enableRetry)
- {
- throw;
- }
- }
-
- var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
- options.RequestHeaders["token"] = newToken;
- return await Post(options, false, providerInfo).ConfigureAwait(false);
- }
-
- private async Task Get(HttpRequestOptions options,
- bool enableRetry,
- ListingsProviderInfo providerInfo)
- {
- try
- {
- return await _httpClient.Get(options).ConfigureAwait(false);
- }
- catch (HttpException ex)
- {
- _tokens.Clear();
-
- if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
- {
- enableRetry = false;
- }
-
- if (!enableRetry)
- {
- throw;
- }
- }
-
- var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
- options.RequestHeaders["token"] = newToken;
- return await Get(options, false, providerInfo).ConfigureAwait(false);
- }
-
- private async Task GetTokenInternal(string username, string password,
- CancellationToken cancellationToken)
- {
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/token",
- UserAgent = UserAgent,
- RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true
- };
- //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
- // httpOptions.RequestContent);
-
- using (var responce = await Post(httpOptions, false, null).ConfigureAwait(false))
- {
- var root = _jsonSerializer.DeserializeFromStream(responce.Content);
- if (root.message == "OK")
- {
- _logger.Info("Authenticated with Schedules Direct token: " + root.token);
- return root.token;
- }
-
- throw new ApplicationException("Could not authenticate with Schedules Direct Error: " + root.message);
- }
- }
-
- private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
- {
- var token = await GetToken(info, cancellationToken);
-
- if (string.IsNullOrWhiteSpace(token))
- {
- throw new ArgumentException("Authentication required.");
- }
-
- if (string.IsNullOrWhiteSpace(info.ListingsId))
- {
- throw new ArgumentException("Listings Id required");
- }
-
- _logger.Info("Adding new LineUp ");
-
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/lineups/" + info.ListingsId,
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true,
- BufferContent = false
- };
-
- httpOptions.RequestHeaders["token"] = token;
-
- using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
- {
- }
- }
-
- public string Name
- {
- get { return "Schedules Direct"; }
- }
-
- public static string TypeName = "SchedulesDirect";
- public string Type
- {
- get { return TypeName; }
- }
-
- private async Task HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
- {
- if (string.IsNullOrWhiteSpace(info.ListingsId))
- {
- throw new ArgumentException("Listings Id required");
- }
-
- var token = await GetToken(info, cancellationToken);
-
- if (string.IsNullOrWhiteSpace(token))
- {
- throw new Exception("token required");
- }
-
- _logger.Info("Headends on account ");
-
- var options = new HttpRequestOptions()
- {
- Url = ApiUrl + "/lineups",
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true
- };
-
- options.RequestHeaders["token"] = token;
-
- try
- {
- using (var response = await Get(options, false, null).ConfigureAwait(false))
- {
- var root = _jsonSerializer.DeserializeFromStream(response);
-
- return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
- }
- }
- catch (HttpException ex)
- {
- // Apparently we're supposed to swallow this
- if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.BadRequest)
- {
- return false;
- }
-
- throw;
- }
- }
-
- public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
- {
- if (validateLogin)
- {
- if (string.IsNullOrWhiteSpace(info.Username))
- {
- throw new ArgumentException("Username is required");
- }
- if (string.IsNullOrWhiteSpace(info.Password))
- {
- throw new ArgumentException("Password is required");
- }
- }
- if (validateListings)
- {
- if (string.IsNullOrWhiteSpace(info.ListingsId))
- {
- throw new ArgumentException("Listings Id required");
- }
-
- var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
-
- if (!hasLineup)
- {
- await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
- }
- }
- }
-
- public Task> GetLineups(ListingsProviderInfo info, string country, string location)
- {
- return GetHeadends(info, country, location, CancellationToken.None);
- }
-
- public async Task> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
- {
- var listingsId = info.ListingsId;
- if (string.IsNullOrWhiteSpace(listingsId))
- {
- throw new Exception("ListingsId required");
- }
-
- await AddMetadata(info, new List(), cancellationToken).ConfigureAwait(false);
-
- var token = await GetToken(info, cancellationToken);
-
- if (string.IsNullOrWhiteSpace(token))
- {
- throw new Exception("token required");
- }
-
- var httpOptions = new HttpRequestOptions()
- {
- Url = ApiUrl + "/lineups/" + listingsId,
- UserAgent = UserAgent,
- CancellationToken = cancellationToken,
- LogErrorResponseBody = true,
- // The data can be large so give it some extra time
- TimeoutMs = 60000
- };
-
- httpOptions.RequestHeaders["token"] = token;
-
- var list = new List();
-
- using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
- {
- var root = _jsonSerializer.DeserializeFromStream(response);
- _logger.Info("Found " + root.map.Count + " channels on the lineup on ScheduleDirect");
- _logger.Info("Mapping Stations to Channel");
- foreach (ScheduleDirect.Map map in root.map)
- {
- var channelNumber = map.logicalChannelNumber;
-
- if (string.IsNullOrWhiteSpace(channelNumber))
- {
- channelNumber = map.channel;
- }
- if (string.IsNullOrWhiteSpace(channelNumber))
- {
- channelNumber = map.atscMajor + "." + map.atscMinor;
- }
- channelNumber = channelNumber.TrimStart('0');
-
- var name = channelNumber;
- var station = GetStation(listingsId, channelNumber, null);
-
- if (station != null && !string.IsNullOrWhiteSpace(station.name))
- {
- name = station.name;
- }
-
- list.Add(new ChannelInfo
- {
- Number = channelNumber,
- Name = name
- });
- }
- }
-
- return list;
- }
-
- public class ScheduleDirect
- {
- public class Token
- {
- public int code { get; set; }
- public string message { get; set; }
- public string serverID { get; set; }
- public string token { get; set; }
- }
- public class Lineup
- {
- public string lineup { get; set; }
- public string name { get; set; }
- public string transport { get; set; }
- public string location { get; set; }
- public string uri { get; set; }
- }
-
- public class Lineups
- {
- public int code { get; set; }
- public string serverID { get; set; }
- public string datetime { get; set; }
- public List lineups { get; set; }
- }
-
-
- public class Headends
- {
- public string headend { get; set; }
- public string transport { get; set; }
- public string location { get; set; }
- public List lineups { get; set; }
- }
-
-
-
- public class Map
- {
- public string stationID { get; set; }
- public string channel { get; set; }
- public string logicalChannelNumber { get; set; }
- public int uhfVhf { get; set; }
- public int atscMajor { get; set; }
- public int atscMinor { get; set; }
- }
-
- public class Broadcaster
- {
- public string city { get; set; }
- public string state { get; set; }
- public string postalcode { get; set; }
- public string country { get; set; }
- }
-
- public class Logo
- {
- public string URL { get; set; }
- public int height { get; set; }
- public int width { get; set; }
- public string md5 { get; set; }
- }
-
- public class Station
- {
- public string stationID { get; set; }
- public string name { get; set; }
- public string callsign { get; set; }
- public List broadcastLanguage { get; set; }
- public List descriptionLanguage { get; set; }
- public Broadcaster broadcaster { get; set; }
- public string affiliate { get; set; }
- public Logo logo { get; set; }
- public bool? isCommercialFree { get; set; }
- }
-
- public class Metadata
- {
- public string lineup { get; set; }
- public string modified { get; set; }
- public string transport { get; set; }
- }
-
- public class Channel
- {
- public List