Beispiel #1
0
        public async Task <List <VkAudio> > GetById(IList <string> ids)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            var parameters = new Dictionary <string, string>();

            parameters.Add("audios", string.Join(",", ids));

            _vkontakte.SignMethod(parameters);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "audio.getById"), parameters).Execute();

            if (VkErrorProcessor.ProcessError(response))
            {
                return(null);
            }

            if (response["response"].HasValues)
            {
                return((from a in response["response"] select VkAudio.FromJson(a)).ToList());
            }

            return(null);
        }
Beispiel #2
0
        internal static VkAudio FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentException("Json can not be null.");
            }

            var result = new VkAudio();

            result.Id       = json["id"].Value <long>();
            result.OwnerId  = json["owner_id"].Value <long>();
            result.Duration = TimeSpan.FromSeconds(json["duration"].Value <double>());
            result.Url      = json["url"].Value <string>();

            try
            {
                result.Title  = WebUtility.HtmlDecode(json["title"].Value <string>()).Trim();
                result.Artist = WebUtility.HtmlDecode(json["artist"].Value <string>()).Trim();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                result.Title  = json["title"].Value <string>().Trim();
                result.Artist = json["artist"].Value <string>().Trim();
            }

            if (json["album_id"] != null)
            {
                result.AlbumId = json["album_id"].Value <long>();
            }

            if (json["lyrics_id"] != null)
            {
                result.LyricsId = json["lyrics_id"].Value <long>();
            }

            if (json["genre_id"] != null)
            {
                result.GenreId = json["genre_id"].Value <long>();
            }

            if (json["is_licensed"] != null)
            {
                result.IsLicensed = json["is_licensed"].Value <bool>();
            }

            if (json["access_key"] != null)
            {
                result.AccessKey = json["access_key"].Value <string>();
            }

            if (json["album"] != null)
            {
                result.Album = VkAudioAlbum.FromJson(json["album"]);
            }

            return(result);
        }
Beispiel #3
0
 public VkAudioAttachment(VkAudio audio)
 {
     this.Id = audio.Id;
     this.OwnerId = audio.OwnerId;
     this.Artist = audio.Artist;
     this.Title = audio.Title;
     this.Duration = audio.Duration;
     this.Url = audio.Url;
 }
Beispiel #4
0
        public async Task <VkItemsResponse <VkAudio> > GetRecommendations(string targetAudio = null, int count = 0, int offset = 0, long userId = 0, bool shuffle = false)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            var parameters = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(targetAudio))
            {
                parameters.Add("target_audio", targetAudio);
            }

            if (userId > 0)
            {
                parameters.Add("user_id", userId.ToString(CultureInfo.InvariantCulture));
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            }

            if (offset > 0)
            {
                parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture));
            }

            if (shuffle)
            {
                parameters.Add("shuffle", "1");
            }

            _vkontakte.SignMethod(parameters);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "audio.getRecommendations"), parameters).Execute();

            if (VkErrorProcessor.ProcessError(response))
            {
                return(VkItemsResponse <VkAudio> .Empty);
            }

            var token = response.SelectToken("response.items");

            if (token != null && token.HasValues)
            {
                return(new VkItemsResponse <VkAudio>((from a in token select VkAudio.FromJson(a)).ToList()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }
Beispiel #5
0
        public async Task <List <VkAudio> > GetById(IList <string> ids)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("audios", string.Join(",", ids));

            _vkontakte.SignMethod(parameters);

            var response = await VkRequest.GetAsync(VkConst.MethodBase + "audio.getById", parameters);

            if (response["response"]?.HasValues == true)
            {
                return((from a in response["response"] select VkAudio.FromJson(a)).ToList());
            }

            return(null);
        }
Beispiel #6
0
        public async Task <VkItemsResponse <VkAudio> > GetRecommendations(string targetAudio = null, int count = 0, int offset = 0, long userId = 0, bool shuffle = false)
        {
            var parameters = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(targetAudio))
            {
                parameters.Add("target_audio", targetAudio);
            }

            if (userId > 0)
            {
                parameters.Add("user_id", userId.ToString(CultureInfo.InvariantCulture));
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            }

            if (offset > 0)
            {
                parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture));
            }

            if (shuffle)
            {
                parameters.Add("shuffle", "1");
            }

            _vkontakte.SignMethod(parameters);

            var response = await VkRequest.GetAsync(VkConst.MethodBase + "audio.getRecommendations", parameters);

            var token = response.SelectToken("response.items");

            if (token != null && token.HasValues)
            {
                return(new VkItemsResponse <VkAudio>((from a in token select VkAudio.FromJson(a)).ToList()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }
Beispiel #7
0
        internal static VkAudio FromJson(JToken json)
        {
            if (json == null)
                throw new ArgumentException("Json can not be null.");

            var result = new VkAudio();

            result.Id = json["id"].Value<long>();
            result.OwnerId = json["owner_id"].Value<long>();
            result.Duration = TimeSpan.FromSeconds(json["duration"].Value<double>());
            result.Url = json["url"].Value<string>();

            try
            {
                result.Title = WebUtility.HtmlDecode(json["title"].Value<string>()).Trim();
                result.Artist = WebUtility.HtmlDecode(json["artist"].Value<string>()).Trim();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                result.Title = json["title"].Value<string>().Trim();
                result.Artist = json["artist"].Value<string>().Trim();
            }

            if (json["album_id"] != null)
                result.AlbumId = json["album_id"].Value<long>();

            if (json["lyrics_id"] != null)
                result.LyricsId = json["lyrics_id"].Value<long>();

            if (json["genre_id"] != null)
                result.GenreId = json["genre_id"].Value<long>();

            return result;
        }
Beispiel #8
0
        public async Task <VkItemsResponse <VkAudio> > GetPopular(bool onlyEng = false, int count = 0, int offset = 0, int genreId = 0)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            var parameters = new Dictionary <string, string>();

            if (onlyEng)
            {
                parameters.Add("only_eng", "1");
            }

            if (genreId != 0)
            {
                parameters.Add("genre_id", genreId.ToString(CultureInfo.InvariantCulture));
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            }

            if (offset > 0)
            {
                parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture));
            }

            _vkontakte.SignMethod(parameters);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "audio.getPopular"), parameters).Execute();

            if (VkErrorProcessor.ProcessError(response))
            {
                return(null);
            }

            if (response["response"].HasValues)
            {
                return(new VkItemsResponse <VkAudio>((from a in response["response"] select VkAudio.FromJson(a)).ToList()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }
Beispiel #9
0
        /// <summary>
        /// Search audios.
        /// See also <see cref="http://vk.com/dev/audio.search"/>
        /// </summary>
        /// <param name="query">Query</param>
        /// <param name="count">Count</param>
        /// <param name="offset">Offset</param>
        /// <param name="sort">Sort</param>
        /// <param name="withLyricsOnly">If true will show only audios with lyrics</param>
        /// <param name="autoFix">If true will fix incorrect queries</param>
        /// <param name="artistOnly">If true will search only by artist</param>
        /// <param name="ownOnly">If true will search only in audios of current user</param>
        /// <returns></returns>
        public async Task <VkItemsResponse <VkAudio> > Search(string query, int count = 0, int offset       = 0, VkAudioSortType sort = VkAudioSortType.DateAdded, bool withLyricsOnly = false, bool autoFix = true,
                                                              bool artistOnly         = false, bool ownOnly = false)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            if (count > MAX_AUDIO_COUNT)
            {
                throw new ArgumentException("Maximum count is " + MAX_AUDIO_COUNT + ".");
            }

            if (query == null)
            {
                throw new ArgumentException("Query must not be null.");
            }

            var parameters = new Dictionary <string, string>();

            parameters.Add("q", query);

            if (autoFix)
            {
                parameters.Add("auto_complete", "1");
            }

            parameters.Add("sort", ((int)sort).ToString(CultureInfo.InvariantCulture));

            if (withLyricsOnly)
            {
                parameters.Add("lyrics", "1");
            }

            if (artistOnly)
            {
                parameters.Add("performer_only", "1");
            }

            if (ownOnly)
            {
                parameters.Add("search_own", "1");
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                parameters.Add("count", MAX_AUDIO_COUNT.ToString(CultureInfo.InvariantCulture));
            }

            if (offset > 0)
            {
                parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture));
            }

            _vkontakte.SignMethod(parameters);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "audio.search"), parameters).Execute();

            if (VkErrorProcessor.ProcessError(response))
            {
                return(null);
            }

            if (response.SelectToken("response.items") != null)
            {
                return(new VkItemsResponse <VkAudio>((from a in response["response"]["items"] where a.HasValues && !string.IsNullOrEmpty(a["url"].Value <string>()) select VkAudio.FromJson(a)).ToList(),
                                                     response["response"]["count"].Value <int>()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }
Beispiel #10
0
        public async Task <VkItemsResponse <VkAudio> > GetPopular(bool onlyEng = false, int count = 0, int offset = 0, int genreId = 0)
        {
            var parameters = new Dictionary <string, string>();

            if (onlyEng)
            {
                parameters.Add("only_eng", "1");
            }

            if (genreId != 0)
            {
                parameters.Add("genre_id", genreId.ToString(CultureInfo.InvariantCulture));
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString(CultureInfo.InvariantCulture));
            }

            if (offset > 0)
            {
                parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture));
            }

            _vkontakte.SignMethod(parameters);

            var response = await VkRequest.GetAsync(VkConst.MethodBase + "audio.getPopular", parameters);

            if (response["response"]?.HasValues == true)
            {
                return(new VkItemsResponse <VkAudio>((from a in response["response"] select VkAudio.FromJson(a)).ToList()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }
Beispiel #11
0
        /// <summary>
        /// Search audios.
        /// See also <see cref="http://vk.com/dev/audio.search"/>
        /// </summary>
        /// <param name="query">Query</param>
        /// <param name="count">Count</param>
        /// <param name="offset">Offset</param>
        /// <param name="sort">Sort</param>
        /// <param name="withLyricsOnly">If true will show only audios with lyrics</param>
        /// <param name="autoFix">If true will fix incorrect queries</param>
        /// <param name="artistOnly">If true will search only by artist</param>
        /// <param name="ownOnly">If true will search only in audios of current user</param>
        /// <returns></returns>
        public async Task <VkItemsResponse <VkAudio> > Search(string query, int count = 0, int offset       = 0, VkAudioSortType sort = VkAudioSortType.DateAdded, bool withLyricsOnly = false, bool autoFix = true,
                                                              bool artistOnly         = false, bool ownOnly = false)
        {
            if (count > MAX_AUDIO_COUNT)
            {
                throw new ArgumentException("Maximum count is " + MAX_AUDIO_COUNT);
            }

            if (query == null)
            {
                throw new ArgumentException("Query must not be null.");
            }

            var parameters = new Dictionary <string, string>();

            parameters["q"] = query;

            if (autoFix)
            {
                parameters["auto_complete"] = "1";
            }

            parameters["sort"] = ((int)sort).ToString(CultureInfo.InvariantCulture);

            if (withLyricsOnly)
            {
                parameters["lyrics"] = "1";
            }

            if (artistOnly)
            {
                parameters["performer_only"] = "1";
            }

            if (ownOnly)
            {
                parameters["search_own"] = "1";
            }

            if (count > 0)
            {
                parameters["count"] = count.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                parameters["count"] = MAX_AUDIO_COUNT.ToString(CultureInfo.InvariantCulture);
            }

            if (offset > 0)
            {
                parameters["offset"] = offset.ToString(CultureInfo.InvariantCulture);
            }

            _vkontakte.SignMethod(parameters);

            var response = await VkRequest.GetAsync(VkConst.MethodBase + "audio.search", parameters);

            if (response.SelectToken("response.items") != null)
            {
                return(new VkItemsResponse <VkAudio>((from a in response["response"]["items"] where a.HasValues && !string.IsNullOrEmpty(a["url"].Value <string>()) select VkAudio.FromJson(a)).ToList(),
                                                     response["response"]["count"].Value <int>()));
            }

            return(VkItemsResponse <VkAudio> .Empty);
        }