예제 #1
0
        public ActionResult LoginTwitter(string returnUrl)
        {
            log.Info($"{WebHelper.GetRealHost(Request)} login via Twitter");

            // Make sure session ID is initialized
// ReSharper disable UnusedVariable
            var sessionId = Session.SessionID;
// ReSharper restore UnusedVariable

            var twitterSignIn = new TwitterConsumer().TwitterSignIn;

            var targetUrl = Url.Action("LoginTwitterComplete", new { returnUrl });
            var uri       = new Uri(new Uri(AppConfig.HostAddress), targetUrl);

            SslHelper.ForceStrongTLS();

            UserAuthorizationRequest request;

            try {
                request = twitterSignIn.PrepareRequestUserAuthorization(uri, null, null);
            } catch (ProtocolException x) {
                log.Error(x, "Exception while attempting to send Twitter request");
                TempData.SetErrorMessage("There was an error while connecting to Twitter - please try again later.");

                return(RedirectToAction("Login"));
            }

            var response = twitterSignIn.Channel.PrepareResponse(request);

            response.Send();
            Response.End();

            return(new EmptyResult());
        }
예제 #2
0
        public async Task <VideoUrlParseResult> ParseBySoundCloudUrl(string url)
        {
            SslHelper.ForceStrongTLS();

            var apikey = AppConfig.SoundCloudClientId;
            var apiUrl = $"https://api.soundcloud.com/resolve?url=http://soundcloud.com/{url}&client_id={apikey}";

            SoundCloudResult result;

            bool HasStatusCode(WebException x, HttpStatusCode statusCode) => x.Response != null && ((HttpWebResponse)x.Response).StatusCode == statusCode;

            VideoUrlParseResult ReturnError(Exception x, string additionalInfo = null)
            {
                var msg = $"Unable to load SoundCloud URL '{url}'.{(additionalInfo != null ? " " + additionalInfo + "." : string.Empty)}";

                s_log.Warn(x, msg);
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException(msg, x)));
            }

            try
            {
                result = await JsonRequest.ReadObjectAsync <SoundCloudResult>(apiUrl, timeout : TimeSpan.FromSeconds(10));
            }
            catch (WebException x) when(HasStatusCode(x, HttpStatusCode.Forbidden))
            {
                // Forbidden most likely means the artist has prevented API access to their tracks, http://stackoverflow.com/a/36529330
                return(ReturnError(x, "This track cannot be embedded"));
            }
            catch (WebException x) when(HasStatusCode(x, HttpStatusCode.NotFound))
            {
                return(ReturnError(x, "Not found"));
            }
            catch (WebException x)
            {
                return(ReturnError(x));
            }
            catch (JsonSerializationException x)
            {
                return(ReturnError(x));
            }
            catch (HttpRequestException x)
            {
                return(ReturnError(x));
            }

            var trackId = result.Id;
            var title   = result.Title;

            if (trackId == null || title == null)
            {
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, "Unable to load SoundCloud URL: Invalid response."));
            }

            var author = result.User.Username;
            var length = result.Duration / 1000;

            var thumbUrl = result.Artwork_url;

            // Substitute song thumbnail with user avatar, if no actual thumbnail is provided. This is what the SoundCloud site does as well.
            if (string.IsNullOrEmpty(thumbUrl))
            {
                thumbUrl = result.User.Avatar_url;
            }

            var uploadDate = result.Created_at;

            var id       = new SoundCloudId(trackId, url);
            var authorId = result.User.Permalink;             // Using permalink because that's the public URL

            return(VideoUrlParseResult.CreateOk(url, PVService.SoundCloud, id.ToString(), VideoTitleParseResult.CreateSuccess(title, author, authorId, thumbUrl, length, uploadDate: uploadDate)));
        }