Exemplo n.º 1
0
        public async Task <ActionResult> GetChannelPictureAsync(string userId)
        {
            try
            {
                if (memoryCache != null && memoryCache.TryGetValue("logo_" + userId, out var logoData) && logoData is byte[] data)
                {
                    return(File(data, "image/png"));
                }

                var twitch  = new TwitchRequests(clientId: settings.TwitchClientId, clientSecret: settings.TwitchClientSecret);
                var profile = await twitch.GetUserAsync(userId);

                if (profile != null)
                {
                    using (var wc = new WebClient())
                    {
                        var binaryData = await wc.DownloadDataTaskAsync(new Uri(profile.logo));

                        return(File(memoryCache.Set("logo_" + userId, binaryData), "image/png"));
                    }
                }
                return(NotFound());
            }
            catch { return(NotFound()); }
        }
Exemplo n.º 2
0
        public async Task <TwitchRequests.TwitchUser> GetTwitchUserAsync(string sessionId, string token = null)
        {
            var str = GetString(sessionId, TwitchUser);

            if (string.IsNullOrEmpty(str) && !string.IsNullOrEmpty(token))
            {
                var twitch = new TwitchRequests(token, settings.TwitchClientId, settings.TwitchClientSecret);
                var user   = await twitch.GetUserAsync();

                if (user != null)
                {
                    await SetTwitchUserAsync(sessionId, user);
                }
                str = user;
            }

            if (string.IsNullOrEmpty(str))
            {
                return(null);
            }

            try
            {
                return(JSON.Parse <TwitchRequests.TwitchUserData>(str).Data?.FirstOrDefault());
            }
            catch (Exception exc)
            {
                logger.LogError("GET TWITCH USER (" + sessionId + "): " + str + " --- PARSE ERROR (EXCEPTION): " + exc);
                return(JSON.Parse <TwitchRequests.TwitchUser>(str));
            }
        }
Exemplo n.º 3
0
        public async Task <string> GetTwitchUser()
        {
            if (sessionInfoProvider.TryGet(HttpContext.Session, out var session))
            {
                return($"{{ \"login\": \"{session.UserName}\", \"id\": \"{session.UserId}\"}}");
            }

            if (!this.sessionInfoProvider.TryGetTwitchToken(HttpContext.Session, out var key))
            {
                return("nope");
            }

            var twitch     = new TwitchRequests(key, settings.TwitchClientId, settings.TwitchClientSecret);
            var twitchUser = await twitch.GetUserAsync();

            await this.sessionInfoProvider.SetTwitchUserAsync(HttpContext.Session, twitchUser);

            return(twitchUser);
        }