コード例 #1
0
        public async Task <IActionResult> GetSessions([Required] string authKey, [Required] string plexServerUrl, string playerMachineId)
        {
            if (string.IsNullOrEmpty(authKey) || string.IsNullOrEmpty(plexServerUrl))
            {
                return(BadRequest());
            }

            if (string.IsNullOrEmpty(playerMachineId))
            {
                return(Ok(await _plexService.GetActiveSessionForPlayer(authKey, plexServerUrl, playerMachineId)));
            }

            return(Ok(await _plexService.GetActiveSessions(authKey, plexServerUrl)));
        }
コード例 #2
0
        public async Task <SessionModel> InitiateMovieSession(string authKey, string serverHost, string playerId, string[] movieLibraries, int delaySeconds = 30)
        {
            const int plexSessionDelayMs = 5000;
            var       serverHostFullUri  = serverHost.TrimEnd('/');

            while (true)
            {
                var session = await _plexService.GetActiveSessionForPlayer(authKey, serverHostFullUri, playerId);

                if (session?.Player != null && (string.Equals(session.Type, "movie", StringComparison.OrdinalIgnoreCase) ||
                                                string.Equals(session.Type, "episode", StringComparison.OrdinalIgnoreCase)))
                {
                    var sessionModel = new SessionModel
                    {
                        PlayerState     = session.Player.State,
                        Duration        = session.Duration,
                        PercentComplete = decimal.Parse(session.ViewOffset) / decimal.Parse(session.Duration),
                        Year            = session.Year,
                        Type            = session.Type
                    };

                    if (string.Equals(session.Type, "movie", StringComparison.OrdinalIgnoreCase))
                    {
                        sessionModel.Title     = session.Title;
                        sessionModel.ArtUrl    = Path.Join(serverHostFullUri, session.Art.TrimEnd('/'), "?X-Plex-Token=" + authKey);
                        sessionModel.PosterUrl = Path.Join(serverHostFullUri, session.Thumb.TrimEnd('/'), "?X-Plex-Token=" + authKey);
                    }
                    else
                    {
                        sessionModel.Title     = session.GrandparentTitle + " : " + session.ParentTitle + " : " + session.Title;
                        sessionModel.ArtUrl    = Path.Join(serverHostFullUri, session.GrandparentArt.TrimEnd('/'), "?X-Plex-Token=" + authKey);
                        sessionModel.PosterUrl = Path.Join(serverHostFullUri, session.GrandparentThumb.TrimEnd('/'), "?X-Plex-Token=" + authKey);
                    }

                    await Clients.Caller.SendAsync("ReceiveSession", sessionModel);

                    Thread.Sleep(plexSessionDelayMs);
                }
                else
                {
                    // Pull random movie poster from Plex
                    var movies = await _plexService.GetRandomMovies(authKey, serverHostFullUri, movieLibraries, 1);

                    Metadata movie = movies.FirstOrDefault();

                    var randomPosterModel = new SessionModel
                    {
                        PlayerState = "none",
                        Title       = movie.Title,
                        ArtUrl      = Path.Join(serverHostFullUri, movie.Art.TrimEnd('/'), "?X-Plex-Token=" + authKey),
                        PosterUrl   = Path.Join(serverHostFullUri, movie.Thumb.TrimEnd('/'), "?X-Plex-Token=" + authKey),
                        Duration    = movie.Duration.ToString(),
                        Year        = movie.Year,
                        Type        = movie.Type
                    };
                    await Clients.Caller.SendAsync("ReceiveSession", randomPosterModel);

                    Thread.Sleep(delaySeconds * 1000);
                }
            }
        }