示例#1
0
        /// <summary>
        /// Gets the top videos on Twitch
        /// </summary>
        /// <param name="game">The name of the game to get videos for</param>
        /// <param name="period">The time period you want to look in</param>
        /// <param name="offset">Object offset for pagination. Default is 0.</param>
        /// <param name="limit">How many videos to get at one time. Default is 10. Maximum is 100</param>
        /// <param name="version">Twitch API version</param>
        /// <returns>A list of videos</returns>
        public async Task<List<Video>> RetrieveTopVideos(string game = null,
            TwitchConstants.Period period = TwitchConstants.Period.Week,
            int offset = 0, int limit = 25,
            APIVersion version = APIVersion.None)
        {
            if (version == APIVersion.None)
            {
                version = DefaultVersion;
            }
            Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("videos", "top");
            url.SetQueryParam("game", game);
            if (limit <= 100)
            {
                url.SetQueryParam("limit", limit);
            }
            else
            {
                url.SetQueryParam("limit", 100);
            }
            url.SetQueryParam("period", TwitchConstants.PeriodToString(period));

            Uri uri = new Uri(url.ToString());
            string responseString;
            try
            {
                responseString = await GetWebData(uri, version);
            }
            catch (TwitchException ex)
            {
                throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex);
            }
            return HelperMethods.LoadVideos(JObject.Parse(responseString), version);
        }
示例#2
0
 /// <summary>
 /// Gets a Total object containing a list of Follow objects of type Channel this user is following
 /// </summary>
 /// <param name="offset">Object offset for pagination. Default is 0.</param>
 /// <param name="limit">How many channels to get at one time. Default is 25. Maximum is 100</param>
 /// <param name="direction">Sorting direction. Default is Decending.</param>
 /// <param name="sortBy">Sort by. Default is CreatedAt.</param>
 /// <returns>A Total object containing a list of Follow objects of type Channel</returns>
 public async Task<Total<List<Follow<Channel>>>> RetrieveFollowing(int offset = 0, int limit = 25,
     TwitchConstants.Direction direction = TwitchConstants.Direction.Decending,
     TwitchConstants.SortBy sortBy = TwitchConstants.SortBy.CreatedAt)
 {
     Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "follows", "channels");
     if (limit <= 100)
     {
         url.SetQueryParam("limit", limit);
     }
     else
     {
         url.SetQueryParam("limit", 100);
     }
     url.SetQueryParams(new
     {
         offset = offset,
         direction = TwitchConstants.DirectionToString(direction),
         sortby = TwitchConstants.SortByToString(sortBy)
     });
     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<Channel>> channels = HelperMethods.LoadChannelFollows(responseObject, version);
     return HelperMethods.LoadTotal(responseObject, channels, version);
 }
示例#3
0
 private string MissingPermissionError(TwitchConstants.Scope scope)
 {
     return name + " has not given " + TwitchConstants.ScopeToString(scope) + " permissions.";
 }
示例#4
0
 /// <summary>
 /// Starts a commercial on this user's live stream.
 /// Requires authorization.
 /// Requires Twitch partnership.
 /// Requires channel_commercial.
 /// </summary>
 /// <param name="length">The length of the commercial</param>
 /// <returns>
 /// Returns true if the request succeeded.
 /// Throws an exception if the user is not partnered.
 /// </returns>
 public async Task<bool> StartCommercial(TwitchConstants.CommercialLength length)
 {
     TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelCommercial;
     if (authorized && authorizedScopes.Contains(relevantScope) && partnered)
     {
         Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "commercial");
         Uri uri = new Uri(url.ToString());
         string responseString;
         try
         {
             responseString = await Twixel.PostWebData(uri, accessToken,
                 "length=" + TwitchConstants.LengthToInt(length).ToString(), version);
         }
         catch (TwitchException ex)
         {
             if (ex.Status == 422)
             {
                 throw new TwixelException(ex.Message, ex);
             }
             else
             {
                 throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex);
             }
         }
         if (string.IsNullOrEmpty(responseString))
         {
             return true;
         }
         else
         {
             throw new TwixelException(TwitchConstants.unknownErrorString);
         }
     }
     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);
         }
     }
 }
示例#5
0
 /// <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);
         }
     }
 }