Пример #1
0
        public VideoUrlParseResult ParseBySoundCloudUrl(string url)
        {
            var apiUrl = string.Format("http://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id=YOUR_CLIENT_ID", url);

            var       request = WebRequest.Create(apiUrl);
            XDocument doc;

            try {
                using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream()) {
                        doc = XDocument.Load(stream);
                    }
            } catch (WebException x) {
                log.WarnException("Unable to load SoundCloud URL " + url, x);
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException("Unable to load SoundCloud URL: " + x.Message, x)));
            }

            var trackIdElem = doc.XPathSelectElement("//track/id");
            var titleElem   = doc.XPathSelectElement("//track/title");

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

            var trackId  = trackIdElem.Value;
            var title    = titleElem.Value;
            var author   = XmlHelper.GetNodeTextOrEmpty(doc, "//track/user/username");
            var thumbUrl = XmlHelper.GetNodeTextOrEmpty(doc, "//track/artwork-url");
            var id       = new SoundCloudId(trackId, url);

            return(VideoUrlParseResult.CreateOk(url, PVService.SoundCloud, id.ToString(), VideoTitleParseResult.CreateSuccess(title, author, thumbUrl)));
        }
Пример #2
0
        public async Task <VideoUrlParseResult> ParseBySoundCloudUrl(string url)
        {
            var apikey = AppConfig.SoundCloudClientId;
            var apiUrl = string.Format("https://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id={1}", url, 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 = string.Format("Unable to load SoundCloud URL '{0}'.{1}", url, additionalInfo != null ? " " + additionalInfo + ".": string.Empty);

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

            try {
                result = await JsonRequest.ReadObjectAsync <SoundCloudResult>(apiUrl, timeoutMs : 10000);
            } 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));
            }

            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)));
        }
Пример #3
0
        public VideoUrlParseResult ParseBySoundCloudUrl(string url)
        {
            var apikey = AppConfig.SoundCloudClientId;
            var apiUrl = string.Format("http://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id={1}", url, apikey);

            SoundCloudResult result;

            try {
                result = JsonRequest.ReadObject <SoundCloudResult>(apiUrl, timeoutMs: 10000);
            } catch (WebException x) {
                var msg = string.Format("Unable to load SoundCloud URL '{0}'.", url);

                // Forbidden most likely means the artist has prevented API access to their tracks, http://stackoverflow.com/a/36529330
                if (((HttpWebResponse)x.Response).StatusCode == HttpStatusCode.Forbidden)
                {
                    msg += " This track cannot be embedded.";
                }

                log.Warn(x, msg);
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException(msg, x)));
            } catch (JsonSerializationException x) {
                var msg = string.Format("Unable to load SoundCloud URL '{0}'.", url);
                log.Warn(x, msg);
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException(msg, 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);

            return(VideoUrlParseResult.CreateOk(url, PVService.SoundCloud, id.ToString(), VideoTitleParseResult.CreateSuccess(title, author, thumbUrl, length, uploadDate: uploadDate)));
        }
Пример #4
0
        public VideoUrlParseResult ParseBySoundCloudUrl(string url)
        {
            var apiUrl = string.Format("http://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id=YOUR_CLIENT_ID", url);

            var request = WebRequest.Create(apiUrl);

            request.Timeout = 10000;
            SoundCloudResult result;

            try {
                using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream())
                        using (var streamReader = new StreamReader(stream))
                            using (var jsonReader = new JsonTextReader(streamReader)) {
                                var serializer = new JsonSerializer();
                                result = serializer.Deserialize <SoundCloudResult>(jsonReader);
                            }
            } catch (WebException x) {
                var msg = string.Format("Unable to load SoundCloud URL {0}", url);
                log.Warn(msg, x);
                return(VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException(msg, 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 id = new SoundCloudId(trackId, url);

            return(VideoUrlParseResult.CreateOk(url, PVService.SoundCloud, id.ToString(), VideoTitleParseResult.CreateSuccess(title, author, thumbUrl, length)));
        }
Пример #5
0
		public VideoUrlParseResult ParseBySoundCloudUrl(string url) {

			var apiUrl = string.Format("http://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id=YOUR_CLIENT_ID", url);

			var request = WebRequest.Create(apiUrl);
			request.Timeout = 10000;
			XDocument doc;

			try {
				using (var response = request.GetResponse())
				using (var stream = response.GetResponseStream()) {
					doc = XDocument.Load(stream);
				}
			} catch (WebException x) {
				log.WarnException("Unable to load SoundCloud URL " + url, x);
				return VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException("Unable to load SoundCloud URL: " + x.Message, x));
			}

			var trackIdElem = doc.XPathSelectElement("//track/id");
			var titleElem = doc.XPathSelectElement("//track/title");

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

			var trackId = trackIdElem.Value;
			var title = titleElem.Value;
			var author = XmlHelper.GetNodeTextOrEmpty(doc, "//track/user/username");
			var length = GetLength(XmlHelper.GetNodeTextOrEmpty(doc, "//track/duration"));

			var thumbUrl = XmlHelper.GetNodeTextOrEmpty(doc, "//track/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 = XmlHelper.GetNodeTextOrEmpty(doc, "//track/user/avatar-url");				
			}

			var id = new SoundCloudId(trackId, url);

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

		}
Пример #6
0
        public VideoUrlParseResult ParseBySoundCloudUrl(string url)
        {
            var apiUrl = string.Format("http://api.soundcloud.com/resolve?url=http://soundcloud.com/{0}&client_id=YOUR_CLIENT_ID", url);

            var request = WebRequest.Create(apiUrl);
            XDocument doc;

            try {
                using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream()) {
                    doc = XDocument.Load(stream);
                }
            } catch (WebException x) {
                log.WarnException("Unable to load SoundCloud URL " + url, x);
                return VideoUrlParseResult.CreateError(url, VideoUrlParseResultType.LoadError, new VideoParseException("Unable to load SoundCloud URL: " + x.Message, x));
            }

            var trackIdElem = doc.XPathSelectElement("//track/id");
            var titleElem = doc.XPathSelectElement("//track/title");

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

            var trackId = trackIdElem.Value;
            var title = titleElem.Value;
            var author = XmlHelper.GetNodeTextOrEmpty(doc, "//track/user/username");
            var thumbUrl = XmlHelper.GetNodeTextOrEmpty(doc, "//track/artwork-url");
            var id = new SoundCloudId(trackId, url);

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