Ejemplo n.º 1
0
        protected async Task <JobStats> GetBeastSaberAsync(BeatSyncConfig config, IJobBuilder jobBuilder, JobManager jobManager, CancellationToken cancellationToken)
        {
            BeastSaberConfig sourceConfig = config.BeastSaber;
            JobStats         sourceStats  = new JobStats();

            if (!sourceConfig.Enabled)
            {
                return(sourceStats);
            }
            BeastSaberReader reader = new BeastSaberReader(sourceConfig.Username, sourceConfig.MaxConcurrentPageChecks);

            FeedConfigBase[] feedConfigs = new FeedConfigBase[] { sourceConfig.Bookmarks, sourceConfig.Follows, sourceConfig.CuratorRecommended };
            if (!feedConfigs.Any(f => f.Enabled))
            {
                Logger.log?.Info($"No feeds enabled for {reader.Name}");
                return(sourceStats);
            }

            SourceStarted?.Invoke(this, "BeastSaber");
            foreach (FeedConfigBase?feedConfig in feedConfigs.Where(c => c.Enabled))
            {
                //if (string.IsNullOrEmpty(sourceConfig.Username) && feedConfig.GetType() != typeof(BeastSaberCuratorRecommended))
                //{
                //    Logger.log?.Warn($"  {feedConfig.GetType().Name} feed not available without a valid username.");
                //    continue;
                //}
                Logger.log?.Info($"  Starting {feedConfig.GetType().Name} feed...");
                FeedResult results = await reader.GetSongsFromFeedAsync(feedConfig.ToFeedSettings()).ConfigureAwait(false);

                if (results.Successful)
                {
                    IEnumerable <IJob>?jobs       = CreateJobs(results, jobBuilder, jobManager, cancellationToken);
                    JobResult[]        jobResults = await Task.WhenAll(jobs.Select(j => j.JobTask).ToArray());

                    JobStats feedStats = new JobStats(jobResults);
                    ProcessFinishedJobs(jobs, jobBuilder.SongTargets, config, feedConfig);
                    Logger.log?.Info($"  Finished {feedConfig.GetType().Name} feed: ({feedStats}).");
                    sourceStats += feedStats;
                }
                else
                {
                    if (results.Exception != null)
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}: {results.Exception.Message}");
                        Logger.log?.Debug(results.Exception);
                    }
                    else
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}: Unknown error.");
                    }
                }
            }
            Logger.log?.Info($"  Finished BeastSaber reading: ({sourceStats}).");
            return(sourceStats);
        }
Ejemplo n.º 2
0
        protected async Task <JobStats> GetScoreSaberAsync(BeatSyncConfig config, IJobBuilder jobBuilder, JobManager jobManager, CancellationToken cancellationToken)
        {
            ScoreSaberConfig sourceConfig = config.ScoreSaber;
            JobStats         sourceStats  = new JobStats();

            if (!sourceConfig.Enabled)
            {
                return(sourceStats);
            }
            ScoreSaberReader reader = new ScoreSaberReader();

            FeedConfigBase[] feedConfigs = new FeedConfigBase[] { sourceConfig.TopRanked, sourceConfig.LatestRanked, sourceConfig.Trending, sourceConfig.TopPlayed };
            if (!feedConfigs.Any(f => f.Enabled))
            {
                Logger.log?.Info($"No feeds enabled for {reader.Name}");
                return(sourceStats);
            }
            SourceStarted?.Invoke(this, "ScoreSaber");
            foreach (FeedConfigBase?feedConfig in feedConfigs.Where(c => c.Enabled))
            {
                Logger.log?.Info($"  Starting {feedConfig.GetType().Name} feed...");
                FeedResult results = await reader.GetSongsFromFeedAsync(feedConfig.ToFeedSettings()).ConfigureAwait(false);

                if (results.Successful)
                {
                    IEnumerable <IJob>?jobs       = CreateJobs(results, jobBuilder, jobManager, cancellationToken);
                    JobResult[]        jobResults = await Task.WhenAll(jobs.Select(j => j.JobTask).ToArray());

                    JobStats feedStats = new JobStats(jobResults);
                    ProcessFinishedJobs(jobs, jobBuilder.SongTargets, config, feedConfig);
                    Logger.log?.Info($"  Finished {feedConfig.GetType().Name} feed: ({feedStats}).");
                    sourceStats += feedStats;
                }
                else
                {
                    if (results.Exception != null)
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}: {results.Exception.Message}");
                        Logger.log?.Debug(results.Exception);
                    }
                    else
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}: Unknown error.");
                    }
                }
            }

            Logger.log?.Info($"  Finished ScoreSaber reading: ({sourceStats}).");
            return(sourceStats);
        }
Ejemplo n.º 3
0
        public static void ProcessFinishedJobs(IEnumerable <IJob> jobs, IEnumerable <SongTarget> songTargets, BeatSyncConfig beatSyncConfig, FeedConfigBase feedConfig)
        {
            if (!jobs.Any(j => j.Result?.Successful ?? false))
            {
                return;
            }
            List <IPlaylist> playlists       = new List <IPlaylist>();
            List <IPlaylist> feedPlaylists   = new List <IPlaylist>();
            List <IPlaylist> recentPlaylists = new List <IPlaylist>();

            foreach (ITargetWithPlaylists?targetWithPlaylist in songTargets.Where(t => t is ITargetWithPlaylists).Select(t => (ITargetWithPlaylists)t))
            {
                PlaylistManager?playlistManager = targetWithPlaylist.PlaylistManager;
                if (playlistManager != null)
                {
                    if (beatSyncConfig.RecentPlaylistDays > 0)
                    {
                        recentPlaylists.Add(playlistManager.GetOrAddPlaylist(BuiltInPlaylist.BeatSyncRecent));
                    }
                    if (beatSyncConfig.AllBeatSyncSongsPlaylist)
                    {
                        playlists.Add(playlistManager.GetOrAddPlaylist(BuiltInPlaylist.BeatSyncAll));
                    }
                    if (feedConfig.CreatePlaylist)
                    {
                        try
                        {
                            IPlaylist feedPlaylist = playlistManager.GetOrAddPlaylist(feedConfig.FeedPlaylist);
                            playlists.Add(feedPlaylist);
                            feedPlaylists.Add(feedPlaylist);
                        }
                        catch (ArgumentException ex)
                        {
                            Logger.log?.Error($"Error getting playlist for FavoriteMappers: {ex.Message}");
                            Logger.log?.Debug(ex);
                        }
                        //catch (PlaylistSerializationException ex) // Caught in GetOrAddPlaylist
                    }
                }
            }
            if (jobs.Any(j => j.Result?.Successful ?? false) && feedConfig.PlaylistStyle == PlaylistStyle.Replace)
            {
                foreach (IPlaylist?feedPlaylist in feedPlaylists)
                {
                    // TODO: This should only apply to successful targets.
                    feedPlaylist.Clear();
                    feedPlaylist.RaisePlaylistChanged();
                }
            }
            ProcessFinishedJobs(jobs, playlists, recentPlaylists);
        }
Ejemplo n.º 4
0
        protected async Task <JobStats> GetBeatSaverAsync(BeatSyncConfig config, IJobBuilder jobBuilder, JobManager jobManager, CancellationToken cancellationToken)
        {
            BeatSaverConfig sourceConfig = config.BeatSaver;
            JobStats        sourceStats  = new JobStats();

            if (!sourceConfig.Enabled)
            {
                return(sourceStats);
            }

            BeatSaverReader reader = new BeatSaverReader();

            FeedConfigBase[] feedConfigs = new FeedConfigBase[] { sourceConfig.Hot, sourceConfig.Downloads, sourceConfig.Latest };
            if (!(feedConfigs.Any(f => f.Enabled) || sourceConfig.FavoriteMappers.Enabled))
            {
                Logger.log?.Info($"No feeds enabled for {reader.Name}");
                return(sourceStats);
            }

            SourceStarted?.Invoke(this, "BeatSaver");
            foreach (FeedConfigBase?feedConfig in feedConfigs.Where(c => c.Enabled))
            {
                Logger.log?.Info($"  Starting {feedConfig.GetType().Name} feed...");
                FeedResult results = await reader.GetSongsFromFeedAsync(feedConfig.ToFeedSettings(), cancellationToken).ConfigureAwait(false);

                if (results.Successful)
                {
                    IEnumerable <IJob>?jobs       = CreateJobs(results, jobBuilder, jobManager, cancellationToken);
                    JobResult[]        jobResults = await Task.WhenAll(jobs.Select(j => j.JobTask).ToArray());

                    JobStats feedStats = new JobStats(jobResults);
                    ProcessFinishedJobs(jobs, jobBuilder.SongTargets, config, feedConfig);
                    Logger.log?.Info($"  Finished {feedConfig.GetType().Name} feed: ({feedStats}).");
                    sourceStats += feedStats;
                }
                else
                {
                    if (results.Exception != null)
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}{results.Exception.Message}");
                        Logger.log?.Debug($"{results.Exception}");
                    }
                    else
                    {
                        Logger.log?.Error($"  Error getting results from {feedConfig.GetType().Name}: Unknown error.");
                    }
                }
            }

            string[] mappers = sourceConfig.FavoriteMappers.Mappers ?? Array.Empty <string>();
            if (sourceConfig.FavoriteMappers.Enabled)
            {
                FeedConfigBase feedConfig = sourceConfig.FavoriteMappers;
                if (mappers.Length > 0)
                {
                    Logger.log?.Info("  Starting FavoriteMappers feed...");
                    List <IPlaylist> playlists       = new List <IPlaylist>();
                    List <IPlaylist> feedPlaylists   = new List <IPlaylist>();
                    List <IPlaylist> recentPlaylists = new List <IPlaylist>();
                    JobStats         feedStats       = new JobStats();
                    foreach (string?mapper in mappers)
                    {
                        Logger.log?.Info($"  Getting songs by {mapper}...");
                        playlists.Clear();

                        FeedResult results = await reader.GetSongsFromFeedAsync(sourceConfig.FavoriteMappers.ToFeedSettings(mapper)).ConfigureAwait(false);

                        if (results.Successful)
                        {
                            foreach (ITargetWithPlaylists?targetWithPlaylist in jobBuilder.SongTargets.Where(t => t is ITargetWithPlaylists).Select(t => (ITargetWithPlaylists)t))
                            {
                                PlaylistManager?playlistManager = targetWithPlaylist.PlaylistManager;
                                if (playlistManager != null)
                                {
                                    if (config.RecentPlaylistDays > 0)
                                    {
                                        recentPlaylists.Add(playlistManager.GetOrAddPlaylist(BuiltInPlaylist.BeatSyncRecent));
                                    }
                                    if (config.AllBeatSyncSongsPlaylist)
                                    {
                                        playlists.Add(playlistManager.GetOrAddPlaylist(BuiltInPlaylist.BeatSyncAll));
                                    }
                                    if (sourceConfig.FavoriteMappers.CreatePlaylist)
                                    {
                                        IPlaylist feedPlaylist;
                                        try
                                        {
                                            if (sourceConfig.FavoriteMappers.SeparateMapperPlaylists)
                                            {
                                                feedPlaylist = playlistManager.GetOrCreateAuthorPlaylist(mapper);
                                            }
                                            else
                                            {
                                                feedPlaylist = playlistManager.GetOrAddPlaylist(BuiltInPlaylist.BeatSaverFavoriteMappers);
                                            }
                                            feedPlaylists.Add(feedPlaylist);
                                            playlists.Add(feedPlaylist);
                                        }
                                        catch (ArgumentException ex)
                                        {
                                            Logger.log?.Error($"Error getting playlist for FavoriteMappers: {ex.Message}");
                                            Logger.log?.Debug(ex);
                                        }
                                    }
                                }
                            }
                            IEnumerable <IJob> jobs       = CreateJobs(results, jobBuilder, jobManager, cancellationToken) ?? Array.Empty <IJob>();
                            JobResult[]        jobResults = await Task.WhenAll(jobs.Select(j => j.JobTask).ToArray());

                            JobStats mapperStats = new JobStats(jobResults);
                            feedStats += mapperStats;
                            if (jobs.Any(j => j.Result?.Successful ?? false) && feedConfig.PlaylistStyle == PlaylistStyle.Replace)
                            {
                                // TODO: This should only apply to successful targets.
                                foreach (IPlaylist?feedPlaylist in feedPlaylists)
                                {
                                    feedPlaylist.Clear();
                                    feedPlaylist.RaisePlaylistChanged();
                                }
                            }
                            ProcessFinishedJobs(jobs, playlists, recentPlaylists);

                            Logger.log?.Info($"  Finished getting songs by {mapper}: ({mapperStats}).");
                        }
                        else
                        {
                            if (results.Exception != null)
                            {
                                Logger.log?.Error($"Error getting songs by {mapper}: {results.Exception.Message}");
                                Logger.log?.Debug(results.Exception);
                            }
                            else
                            {
                                Logger.log?.Error($"Error getting songs by {mapper}");
                            }
                        }
                    }
                    sourceStats += feedStats;
                    Logger.log?.Info($"  Finished {feedConfig.GetType().Name} feed: ({feedStats}).");
                }
                else
                {
                    Logger.log?.Warn("  No FavoriteMappers found, skipping...");
                }
            }

            Logger.log?.Info($"  Finished BeatSaver reading: ({sourceStats}).");
            return(sourceStats);
        }
Ejemplo n.º 5
0
        public static SubMenu CreateFeedSettings(string feedName, string feedSource, FeedConfigBase feedConfig, SubMenu parent, string menuHintText = "")
        {
            if (string.IsNullOrEmpty(menuHintText))
            {
                menuHintText = $"Settings for {feedSource}'s {feedName} feed";
            }
            var feedSubMenu = parent.AddSubMenu(feedName, menuHintText, true);
            var enabled     = feedSubMenu.AddBool("Enable",
                                                  $"Enable the {feedName} feed from {feedSource}.");

            enabled.GetValue += delegate { return(feedConfig.Enabled); };
            enabled.SetValue += delegate(bool value)
            {
                if (feedConfig.Enabled == value)
                {
                    return;
                }
                feedConfig.Enabled = value;
                // Config.ConfigChanged = true;
            };

            var maxSongs = feedSubMenu.AddInt("Max Songs",
                                              "Maximum number of songs to download (0 for all).",
                                              0, 500, 5);

            maxSongs.GetValue += delegate { return(feedConfig.MaxSongs); };
            maxSongs.SetValue += delegate(int value)
            {
                if (feedConfig.MaxSongs == value)
                {
                    return;
                }
                feedConfig.MaxSongs = value;
                // Config.ConfigChanged = true;
            };

            var createPlaylist = feedSubMenu.AddBool("Create Playlist",
                                                     $"Maintain a playlist for this feed.");

            createPlaylist.GetValue += delegate { return(feedConfig.CreatePlaylist); };
            createPlaylist.SetValue += delegate(bool value)
            {
                if (feedConfig.CreatePlaylist == value)
                {
                    return;
                }
                feedConfig.CreatePlaylist = value;
                // Config.ConfigChanged = true;
            };

            string[] textSegmentOptions  = new string[] { "Append", "Replace" };
            var      textSegmentsExample = feedSubMenu.AddTextSegments("Playlist Style", "Select 'Append' to add new songs to playlist, 'Replace' to create a fresh playlist with songs read from the feed this session.", textSegmentOptions);

            textSegmentsExample.GetValue += delegate { return(feedConfig.PlaylistStyle == PlaylistStyle.Append ? 0 : 1); };
            textSegmentsExample.SetValue += delegate(int value)
            {
                PlaylistStyle newStyle = value == 0 ? PlaylistStyle.Append : PlaylistStyle.Replace;
                if (feedConfig.PlaylistStyle == newStyle)
                {
                    return;
                }
                feedConfig.PlaylistStyle = newStyle;
                // Config.ConfigChanged = true;
            };
            return(feedSubMenu);
        }