Beispiel #1
0
        public CouchPotatoMovies GetMovies(Uri baseUrl, string apiKey, string[] status)
        {
            var request = new RestRequest
            {
                Resource = "/api/{apikey}/movie.list?status={status}",
                OnBeforeDeserialization = (x =>
                {
                    x.Content = x.Content.Replace("[]", "{}");
                })
            };

            request.AddUrlSegment("apikey", apiKey);
            request.AddUrlSegment("status", string.Join(",", status));
            try
            {
                var obj = RetryHandler.Execute(() => Api.Execute <CouchPotatoMovies>(request, baseUrl),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetMovies for CP, Retrying {0}", timespan), new[] {
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30)
                });

                return(obj);
            }
            catch (Exception e) // Request error is already logged in the ApiRequest class
            {
                Log.Error("Error when attempting to GetMovies.");
                Log.Error(e);
                return(new CouchPotatoMovies());
            }
        }
Beispiel #2
0
        public PlexSearch GetAllEpisodes(string authToken, Uri host, string section, int startPage, int returnCount)
        {
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "/library/sections/{section}/all"
            };

            request.AddQueryParameter("type", 4.ToString());
            AddLimitHeaders(ref request, startPage, returnCount);
            request.AddUrlSegment("section", section);
            AddHeaders(ref request, authToken, false);

            try
            {
                var lib = RetryHandler.Execute(() => Api.ExecuteXml <PlexSearch>(request, host),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetAllEpisodes for Plex, Retrying {0}", timespan));

                return(lib);
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been a API Exception when attempting to get GetAllEpisodes");
                return(new PlexSearch());
            }
        }
Beispiel #3
0
        public PlexRecentlyAddedModel RecentlyAdded(string authToken, Uri plexFullHost, string sectionId)
        {
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "library/sections/{sectionId}/recentlyAdded"
            };

            request.AddUrlSegment("sectionId", sectionId);
            AddHeaders(ref request, authToken, true);
            AddLimitHeaders(ref request, 0, 25);

            try
            {
                var lib = RetryHandler.Execute(() => Api.ExecuteJson <PlexRecentlyAddedModel>(request, plexFullHost),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling PlexRecentlyAddedModel for Plex, Retrying {0}", timespan), new[] {
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30)
                });

                return(lib);
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been a API Exception when attempting to get the Plex RecentlyAddedModel");
                return(new PlexRecentlyAddedModel());
            }
        }
Beispiel #4
0
        public PlexSeasonMetadata GetSeasons(string authToken, Uri plexFullHost, string ratingKey)
        {
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "library/metadata/{ratingKey}/children"
            };

            request.AddUrlSegment("ratingKey", ratingKey);
            AddHeaders(ref request, authToken, false);

            try
            {
                var lib = RetryHandler.Execute(() => Api.ExecuteXml <PlexSeasonMetadata>(request, plexFullHost),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetMetadata for Plex, Retrying {0}", timespan), new[] {
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30)
                });

                return(lib);
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been a API Exception when attempting to get the Plex GetMetadata");
                return(new PlexSeasonMetadata());
            }
        }
Beispiel #5
0
        public PlexEpisodeMetadata GetEpisodeMetaData(string authToken, Uri host, string ratingKey)
        {
            //192.168.1.69:32400/library/metadata/3662/allLeaves
            // The metadata ratingkey should be in the Cache
            // Search for it and then call the above with the Directory.RatingKey
            // THEN! We need the episode metadata using result.Vide.Key ("/library/metadata/3664")
            // We then have the GUID which contains the TVDB ID plus the season and episode number: guid="com.plexapp.agents.thetvdb://269586/2/8?lang=en"
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "/library/metadata/{ratingKey}"
            };

            request.AddUrlSegment("ratingKey", ratingKey);
            AddHeaders(ref request, authToken, false);

            try
            {
                var lib = RetryHandler.Execute(() => Api.ExecuteXml <PlexEpisodeMetadata>(request, host),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetEpisodeMetaData for Plex, Retrying {0}", timespan));

                return(lib);
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been a API Exception when attempting to get GetEpisodeMetaData");
                return(new PlexEpisodeMetadata());
            }
        }
Beispiel #6
0
        public PlexSearch GetLibrary(string authToken, Uri plexFullHost, string libraryId)
        {
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "library/sections/{libraryId}/all"
            };

            request.AddUrlSegment("libraryId", libraryId);
            AddHeaders(ref request, authToken, false);

            try
            {
                var lib = RetryHandler.Execute(() => Api.ExecuteXml <PlexSearch> (request, plexFullHost),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetLibrary for Plex, Retrying {0}", timespan), new TimeSpan[] {
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30)
                });

                return(lib);
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been a API Exception when attempting to get the Plex Library");
                return(new PlexSearch());
            }
        }
Beispiel #7
0
        public PlexAccount GetAccount(string authToken)
        {
            var request = new RestRequest
            {
                Method = Method.GET,
            };

            AddHeaders(ref request, authToken, false);

            var account = RetryHandler.Execute(() => Api.ExecuteXml <PlexAccount> (request, new Uri(GetAccountUri)),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetAccount for Plex, Retrying {0}", timespan), null);

            return(account);
        }
Beispiel #8
0
        public PlexStatus GetStatus(string authToken, Uri uri)
        {
            var request = new RestRequest
            {
                Method = Method.GET,
            };

            AddHeaders(ref request, authToken, false);

            var users = RetryHandler.Execute(() => Api.ExecuteXml <PlexStatus> (request, uri),
                                             (exception, timespan) => Log.Error(exception, "Exception when calling GetStatus for Plex, Retrying {0}", timespan), null);

            return(users);
        }
Beispiel #9
0
        public CouchPotatoProfiles GetProfiles(Uri url, string apiKey)
        {
            var request = new RestRequest
            {
                Resource = "api/{apikey}/profile.list/",
                Method   = Method.GET
            };

            request.AddUrlSegment("apikey", apiKey);

            var obj = RetryHandler.Execute(() => Api.Execute <CouchPotatoProfiles>(request, url),
                                           (exception, timespan) => Log.Error(exception, "Exception when calling GetProfiles for CP, Retrying {0}", timespan), null);

            return(obj);
        }
Beispiel #10
0
        public PlexServer GetServer(string authToken)
        {
            var request = new RestRequest
            {
                Method = Method.GET,
            };

            AddHeaders(ref request, authToken, false);

            var servers = RetryHandler.Execute(() => Api.ExecuteXml <PlexServer>(request, new Uri(ServerUri)),
                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetServer for Plex, Retrying {0}", timespan));


            return(servers);
        }
Beispiel #11
0
        public CouchPotatoApiKey GetApiKey(Uri baseUrl, string username, string password)
        {
            var request = new RestRequest
            {
                Resource = "getkey/?u={username}&p={password}",
                Method   = Method.GET
            };

            request.AddUrlSegment("username", StringHasher.CalcuateMd5Hash(username));
            request.AddUrlSegment("password", StringHasher.CalcuateMd5Hash(password));

            var obj = RetryHandler.Execute(() => Api.Execute <CouchPotatoApiKey>(request, baseUrl),
                                           (exception, timespan) => Log.Error(exception, "Exception when calling GetApiKey for CP, Retrying {0}", timespan), null);

            return(obj);
        }
Beispiel #12
0
        /// <summary>
        /// Gets the users.
        /// </summary>
        /// <param name="authToken">The authentication token.</param>
        /// <param name="searchTerm">The search term.</param>
        /// <param name="plexFullHost">The full plex host.</param>
        /// <returns></returns>
        public PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFullHost)
        {
            var request = new RestRequest
            {
                Method   = Method.GET,
                Resource = "search?query={searchTerm}"
            };

            request.AddUrlSegment("searchTerm", searchTerm);
            AddHeaders(ref request, authToken, false);

            var search = RetryHandler.Execute(() => Api.ExecuteXml <PlexSearch> (request, plexFullHost),
                                              (exception, timespan) => Log.Error(exception, "Exception when calling SearchContent for Plex, Retrying {0}", timespan), null);

            return(search);
        }
Beispiel #13
0
        public bool AddMovie(string imdbid, string apiKey, string title, Uri baseUrl, string profileId = default(string))
        {
            RestRequest request;

            request = string.IsNullOrEmpty(profileId)
                ? new RestRequest {
                Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}"
            }
                : new RestRequest {
                Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}&profile_id={profileId}"
            };

            if (!string.IsNullOrEmpty(profileId))
            {
                request.AddUrlSegment("profileId", profileId);
            }

            request.AddUrlSegment("apikey", apiKey);
            request.AddUrlSegment("imdbid", imdbid);
            request.AddUrlSegment("title", title);

            var obj = RetryHandler.Execute(() => Api.ExecuteJson <JObject>(request, baseUrl),
                                           (exception, timespan) => Log.Error(exception, "Exception when calling AddMovie for CP, Retrying {0}", timespan), new[] {
                TimeSpan.FromSeconds(2)
            });


            if (obj.Count > 0)
            {
                try
                {
                    var result = (bool)obj["success"];
                    return(result);
                }
                catch (Exception e)
                {
                    Log.Fatal(e);
                    return(false);
                }
            }
            return(false);
        }
Beispiel #14
0
        /// <summary>
        /// Gets the status.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="apiKey">The API key.</param>
        /// <returns></returns>
        public CouchPotatoStatus GetStatus(Uri url, string apiKey)
        {
            var request = new RestRequest
            {
                Resource = "api/{apikey}/app.available/",
                Method   = Method.GET
            };

            request.AddUrlSegment("apikey", apiKey);


            var obj = RetryHandler.Execute <CouchPotatoStatus>(() => Api.Execute <CouchPotatoStatus>(request, url),
                                                               (exception, timespan) => Log.Error(exception, "Exception when calling GetStatus for CP, Retrying {0}", timespan), new TimeSpan[] {
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(10)
            });

            return(obj);
        }
Beispiel #15
0
        public PlexAuthentication SignIn(string username, string password)
        {
            var userModel = new PlexUserRequest
            {
                user = new UserRequest
                {
                    password = password,
                    login    = username
                }
            };
            var request = new RestRequest
            {
                Method = Method.POST
            };

            AddHeaders(ref request, false);

            request.AddJsonBody(userModel);

            var obj = RetryHandler.Execute <PlexAuthentication>(() => Api.Execute <PlexAuthentication> (request, new Uri(SignInUri)),
                                                                (exception, timespan) => Log.Error(exception, "Exception when calling SignIn for Plex, Retrying {0}", timespan));

            return(obj);
        }