/// <summary> /// Gets a list of users that the user has blocked. Requires user authorization. /// </summary> /// <returns>A list of users</returns> public async Task <List <User> > RetrieveBlockedUsers() { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.UserBlocksRead)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/blocks"); string responseString = await Twixel.GetWebData(uri, accessToken); foreach (JObject user in JObject.Parse(responseString)["blocks"]) { User temp = twixel.LoadUser((JObject)user["user"]); if (!ContainsBlockedUser(temp.name)) { blockedUsers.Add(temp); } } return(blockedUsers); } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.UserBlocksRead)) { twixel.CreateError(name + " has not given user_blocks_read permissions"); } return(null); } }
/// <summary> /// Gets the list of users that can edit this user's channel. /// Requires authorization. /// Requires channel_read. /// </summary> /// <returns>A list of users</returns> public async Task <List <User> > RetrieveChannelEditors() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "editors"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadUsers(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Gets a Total object containing a list of Follow objects of type User following the specified user /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">How many users to get at one time. Default is 25. Maximum is 100</param> /// <param name="direction">Creation date sorting direction. Default is Descending.</param> /// <returns>A Total object containing a list of Follow objects of type User</returns> public async Task <Total <List <Follow <User> > > > RetrieveFollowers(int offset = 0, int limit = 25, TwitchConstants.Direction direction = TwitchConstants.Direction.Decending) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "follows"); if (limit <= 100) { url.SetQueryParam("limit", limit); } else { url.SetQueryParam("limit", 100); } url.SetQueryParams(new { offset = offset, direction = TwitchConstants.DirectionToString(direction) }); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); List <Follow <User> > follows = HelperMethods.LoadUserFollows(responseObject, version); return(HelperMethods.LoadTotal(responseObject, follows, version)); }
/// <summary> /// Gets a list of channels this user is following /// </summary> /// <param name="limit">How many channels to get at one time. Default is 25. Maximum is 100</param> /// <returns>A list of channels</returns> public async Task <List <Channel> > RetrieveFollowing(int limit) { Uri uri; if (limit <= 100) { uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/follows/channels?limit=" + limit.ToString()); } else { twixel.CreateError("You cannot get more than 100 channels at a time"); return(null); } string responseString = await Twixel.GetWebData(uri); if (Twixel.GoodStatusCode(responseString)) { nextFollowing = new WebUrl((string)JObject.Parse(responseString)["_links"]["next"]); List <Channel> followedChannels = new List <Channel>(); foreach (JObject o in (JArray)JObject.Parse(responseString)["follows"]) { followedChannels.Add(twixel.LoadChannel((JObject)o["channel"])); } return(followedChannels); } else { twixel.CreateError(responseString); return(null); } }
/// <summary> /// Gets the channel object for this user. Requires user authorization. /// </summary> /// <returns>A channel object</returns> public async Task <Channel> RetrieveChannel() { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelRead)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/channel"); string responseString = await Twixel.GetWebData(uri, accessToken); if (Twixel.GoodStatusCode(responseString)) { streamKey = (string)JObject.Parse(responseString)["stream_key"]; channel = twixel.LoadChannel(JObject.Parse(responseString)); return(channel); } else { twixel.CreateError(responseString); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelRead)) { twixel.CreateError(name + " has not given channel_read permissions"); } return(null); } }
/// <summary> /// Get a list of users subscribed to this user. Requires user authorization. Requires Twitch partnership. /// </summary> /// <param name="limit">How many subscriptions to get at one time. Default is 25. Maximum is 100</param> /// <param name="direction">Creation date sorting direction</param> /// <returns>A list of subscriptions</returns> public async Task <List <Subscription> > RetriveSubscribers(int limit, TwitchConstants.Direction direction) { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelSubscriptions)) { Uri uri; string url = "https://api.twitch.tv/kraken/channels/" + name + "/subscriptions"; if (limit <= 100) { url += "?limit=" + limit.ToString(); } else { twixel.CreateError("You cannot fetch more than 100 subs at a time"); return(null); } if (direction != TwitchConstants.Direction.None) { url += "&direction=" + TwitchConstants.DirectionToString(direction); } uri = new Uri(url); string responseString = await Twixel.GetWebData(uri, accessToken); if (responseString != "422") { totalSubscribers = (int)JObject.Parse(responseString)["_total"]; nextSubs = new WebUrl((string)JObject.Parse(responseString)["_links"]["next"]); foreach (JObject o in (JArray)JObject.Parse(responseString)["subscriptions"]) { if (!ContainsSubscriber((string)o["user"]["name"])) { subscribedUsers.Add(LoadSubscriber(o)); } } return(subscribedUsers); } else { twixel.CreateError("You aren't partnered so you cannot have subs"); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelSubscriptions)) { twixel.CreateError(name + " has not given channel_subscriptions permissions"); } return(null); } }
/// <summary> /// Get a Total object containing a list of Subscription of type User /// Requires authorization. /// Requires Twitch partnership. /// Requires channel_subscriptions. /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">How many subscriptions to get at one time. Default is 25. Maximum is 100</param> /// <param name="direction">Creation date sorting direction. Default is Ascending.</param> /// <returns>A Total object containing a list of Subscription objects of type User</returns> public async Task <Total <List <Subscription <User> > > > RetriveSubscribers(int offset = 0, int limit = 25, TwitchConstants.Direction direction = TwitchConstants.Direction.Ascending) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelSubscriptions; if (authorized && authorizedScopes.Contains(relevantScope) && partnered) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "subscriptions"); if (limit <= 100) { url.SetQueryParam("limit", limit); } else { url.SetQueryParam("limit", 100); } url.SetQueryParams(new { offset = offset, direction = TwitchConstants.DirectionToString(direction) }); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); List <Subscription <User> > subs = HelperMethods.LoadUserSubscriptions(JObject.Parse(responseString), version); return(HelperMethods.LoadTotal(responseObject, subs, version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else if (!partnered) { throw new TwixelException(NotPartneredError()); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Checks to see if this user is subcribed to a specified channel. /// Requires authorization. /// Requires user_subscriptions. /// </summary> /// <param name="channel">The name of the channel</param> /// <returns> /// A subscription object of type Channel if the request succeeds. /// Throws an exception if the user is not subscribed to the channel. /// Throws an exception if the channel is not partnered.</returns> public async Task <Subscription <Channel> > RetrieveSubscription(string channel) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserSubcriptions; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "subscriptions", channel); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException("You are not subscribed to " + channel, ex); } else if (ex.Status == 422) { throw new TwixelException(channel + " has no subscription program", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadChannelSubscription(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Checks to see if a specified user is subscribed to this user. /// Requires authorization. /// Requires Twitch partnership. /// Requires channel_check_subscription. /// </summary> /// <param name="username">The name of the user</param> /// <returns>A Subscription object of type User if the user is subscribed</returns> public async Task <Subscription <User> > RetrieveSubsciber(string username) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelCheckSubscription; if (authorized && authorizedScopes.Contains(relevantScope) && partnered) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "subcriptions", username); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException(username + " is not subscribed.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadUserSubscription(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else if (!partnered) { throw new TwixelException(NotPartneredError()); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Gets a list of users that the user has blocked. /// Requires authorization. /// Requires user_blocks_read. /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">Maximum number of objects in array. Default is 25. Maximum is 100.</param> /// <returns>A list of blocked users</returns> public async Task <List <Block> > RetrieveBlockedUsers(int offset = 0, int limit = 25) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserBlocksRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "blocks"); if (limit <= 100) { url.SetQueryParam("limit", limit); } else { url.SetQueryParam("limit", 100); } url.SetQueryParam("offset", offset); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadBlocks(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Checks to see if this user is subcribed to a specified channel. Requires user authorization. /// </summary> /// <param name="channel">The name of the channel</param> /// <returns>A subscription object</returns> public async Task <Subscription> RetrieveSubscription(string channel) { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.UserSubcriptions)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/subscriptions/" + channel); string responseString = await Twixel.GetWebData(uri, accessToken); if (responseString != "422" && responseString != "404") { return(LoadSubscription(JObject.Parse(responseString))); } else if (responseString == "404") { twixel.CreateError("You are not subscribed to " + channel); return(null); } else if (responseString == "422") { twixel.CreateError(channel + " has no subscription program"); return(null); } else { twixel.CreateError(responseString); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.UserSubcriptions)) { twixel.CreateError(name + " has not given user_subscriptions permissions"); } return(null); } }
/// <summary> /// Checks to see if a specified user is subscribed to this user. Requires user authorization. Requires Twitch partnership. /// </summary> /// <param name="username">The name of the user</param> /// <returns>A subscription object if the user is subscribed</returns> public async Task <Subscription> RetrieveSubsciber(string username) { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelCheckSubscription)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/channels/" + name + "/subscriptions/" + username); string responseString = await Twixel.GetWebData(uri, accessToken); if (responseString != "422" && responseString != "404") { return(LoadSubscriber(JObject.Parse(responseString))); } else if (responseString == "404") { twixel.CreateError(username + " is not subscribed"); return(null); } else if (responseString == "422") { twixel.CreateError("You aren't partnered so you cannot have subs"); return(null); } else { twixel.CreateError(responseString); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelCheckSubscription)) { twixel.CreateError(name + " has not given channel_check_subscription permissions"); } return(null); } }
/// <summary> /// Get a list of videos from the channels this user follows /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">How many videos to get at once. Default is 25. Maximum is 100.</param> /// <returns>A list of videos.</returns> public async Task <List <Video> > RetrieveFollowedVideos(int offset = 0, int limit = 10) { if (version == Twixel.APIVersion.v3) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("videos", "followed"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadVideos(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } } else { throw new TwixelException(TwitchConstants.v2UnsupportedErrorString); } }
/// <summary> /// Gets a list of users subscribed to this user. Requires user authorization. Requires Twitch partnership. /// </summary> /// <param name="getNext">If this method was called before then this will get the next page of subscriptions</param> /// <returns>A list of subscriptions</returns> public async Task <List <Subscription> > RetriveSubscribers(bool getNext) { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelSubscriptions)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/channels/" + name + "/subscriptions"); string responseString = await Twixel.GetWebData(uri, accessToken); if (responseString != "422") { totalSubscribers = (int)JObject.Parse(responseString)["_total"]; nextSubs = new WebUrl((string)JObject.Parse(responseString)["_links"]["next"]); foreach (JObject o in (JArray)JObject.Parse(responseString)["subscriptions"]) { if (!ContainsSubscriber((string)o["user"]["name"])) { subscribedUsers.Add(LoadSubscriber(o)); } } return(subscribedUsers); } else { twixel.CreateError("You aren't partnered so you cannot have subs"); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelSubscriptions)) { twixel.CreateError(name + " has not given channel_subscriptions permissions"); } return(null); } }
/// <summary> /// Checks to see if this user is following a specified channel /// </summary> /// <param name="channel">The name of the channel</param> /// <returns>A channel object</returns> public async Task <Channel> RetrieveFollowing(string channel) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/follows/channels/" + channel); string responseString = await Twixel.GetWebData(uri); if (responseString != "404") { return(twixel.LoadChannel((JObject)JObject.Parse(responseString)["channel"])); } else if (responseString == "404") { twixel.CreateError(name + " is not following " + channel); return(null); } else { twixel.CreateError(responseString); return(null); } }
/// <summary> /// Gets the list of users that can edit this user's channel. Requires user authorization. /// </summary> /// <returns>A list of users</returns> public async Task <List <User> > RetrieveChannelEditors() { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelRead)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/channels/" + name + "/editors"); string responseString = await Twixel.GetWebData(uri, accessToken); if (Twixel.GoodStatusCode(responseString)) { foreach (JObject o in (JArray)JObject.Parse(responseString)["users"]) { if (!ContainsEditor((string)o["name"])) { channelEditors.Add(twixel.LoadUser(o)); } } return(channelEditors); } else { twixel.CreateError(responseString); return(null); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelRead)) { twixel.CreateError(name + " has not given channel_read permissions"); } return(null); } }
/// <summary> /// Gets the status of an access token, if the token is valid this returns an /// authorized user object /// </summary> /// <param name="accessToken">The access token</param> /// <param name="version">Twitch API version</param> /// <returns> /// An authorized user if the request succeeds. /// Throws an exception if the token is not valid.</returns> public async Task <User> RetrieveUserWithAccessToken(string accessToken, APIVersion version = APIVersion.None) { if (version == APIVersion.None) { version = DefaultVersion; } Url url = new Url(TwitchConstants.baseUrl); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); JObject token = (JObject)responseObject["token"]; if ((bool)token["valid"]) { JArray userScopesA = (JArray)token["authorization"]["scopes"]; List <TwitchConstants.Scope> userScopes = new List <TwitchConstants.Scope>(); foreach (string scope in userScopesA) { userScopes.Add(TwitchConstants.StringToScope(scope)); } return(await RetrieveAuthenticatedUser(accessToken, userScopes, version)); } else { throw new TwixelException(accessToken + " is not a valid access token", (JObject)responseObject["_links"]); } }
/// <summary> /// Checks to see if this user is following a specified channel /// </summary> /// <param name="channel">The name of the channel</param> /// <returns> /// A Follow object of type Channel if the request succeeded. /// Throws an exception if the user is not following the specified channel. /// </returns> public async Task <Follow <Channel> > RetrieveFollowing(string channel) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "follows", "channels", channel); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException(name + " is not following " + channel, ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadChannelFollow(JObject.Parse(responseString), version)); }
/// <summary> /// Gets the channel object for this user. Also retrieves their stream key. /// Updates channel object. /// Requires authorization. /// Requires channel_read. /// </summary> /// <returns>A channel</returns> public async Task <Channel> RetrieveChannel() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegment("channel"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); streamKey = (string)responseObject["stream_key"]; channel = HelperMethods.LoadChannel(responseObject, version); return(channel); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Gets a list of channels this user is following /// </summary> /// <param name="getNext">If this method was called before then this will get the next page of channels</param> /// <returns>A list of channels</returns> public async Task <List <Channel> > RetrieveFollowing(bool getNext) { Uri uri; if (!getNext) { uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/follows/channels"); } else { if (nextFollowing != null) { uri = nextFollowing.url; } else { uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/follows/channels"); } } string responseString = await Twixel.GetWebData(uri); if (Twixel.GoodStatusCode(responseString)) { nextFollowing = new WebUrl((string)JObject.Parse(responseString)["_links"]["next"]); List <Channel> followedChannels = new List <Channel>(); foreach (JObject o in (JArray)JObject.Parse(responseString)["follows"]) { followedChannels.Add(twixel.LoadChannel((JObject)o["channel"])); } return(followedChannels); } else { twixel.CreateError(responseString); return(null); } }
/// <summary> /// Gets a list of live streams that the user is following. Requires user authorization. /// </summary> /// <returns>A list of streams</returns> public async Task <List <Stream> > RetrieveOnlineFollowedStreams() { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.UserRead)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/streams/followed"); string responseString = await Twixel.GetWebData(uri, accessToken); followedStreams = twixel.LoadStreams(JObject.Parse(responseString)); return(followedStreams); } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.UserRead)) { twixel.CreateError(name + " has not given user_read permissions"); } return(null); } }