Example #1
0
        public static YPlaylistResponse FromJson(JToken json)
        {
            var playlist = new YPlaylistResponse
            {
                Revision              = json.SelectToken("revision")?.ToObject <int>(),
                Kind                  = json.SelectToken("kind")?.ToObject <string>(),
                Title                 = json.SelectToken("title")?.ToObject <string>(),
                Description           = json.SelectToken("description")?.ToObject <string>(),
                DescriptionFormatted  = json.SelectToken("descriptionFormatted")?.ToObject <string>(),
                TrackCount            = json.SelectToken("trackCount")?.ToObject <int>(),
                Visibility            = json.SelectToken("visibility")?.ToObject <string>(),
                Cover                 = YPlaylistCover.FromJson(json.SelectToken("cover")),
                Owner                 = YOwner.FromJson(json["owner"]),
                Tracks                = json.SelectToken("tracks")?.Select(x => YTrack.FromJson(x)).ToList(),
                Modified              = json.SelectToken("modified")?.ToObject <string>(),
                TrackIds              = json.SelectToken("trackIds")?.Select(x => x.ToObject <int>()).ToList(),
                OgImage               = json.SelectToken("ogImage")?.ToObject <string>(),
                Tags                  = json.SelectToken("tags")?.Select(x => x.ToObject <string>()).ToList(),
                LikesCount            = json.SelectToken("likesCount")?.ToObject <int>(),
                Duration              = json.SelectToken("duration")?.ToObject <long>(),
                Collective            = json.SelectToken("collective")?.ToObject <bool>(),
                Prerolls              = json.SelectToken("prerolls")?.Select(x => YPlaylistPrerolls.FromJson(x)).ToList(),
                PlayCounter           = YPlaylistPlayCounter.FromJson(json.SelectToken("playCounter")),
                IdForFrom             = json.SelectToken("idForFrom")?.ToObject <string>(),
                GeneratedPlaylistType = json.SelectToken("generatedPlaylistType")?.ToObject <string>(),
                UrlPart               = json.SelectToken("urlPart")?.ToObject <string>(),
                MadeFor               = YPlaylistMadeFor.FromJson(json.SelectToken("madeFor")),
                OgTitle               = json.SelectToken("ogTitle")?.ToObject <string>(),
                DoNotIndex            = json.SelectToken("doNotIndex")?.ToObject <bool>()
            };

            return(playlist);
        }
Example #2
0
        /// <summary>
        /// Получение ссылки для загрузки
        /// </summary>
        /// <param name="storage">Хранилище</param>
        /// <param name="track">Трек</param>
        /// <returns></returns>
        public string GetFileLink(YAuthStorage storage, YTrack track)
        {
            var mainDownloadResponse    = GetMetadataForDownload(storage, track).First(m => m.Codec == "mp3");
            var storageDownloadResponse = GetDownloadFileInfo(storage, mainDownloadResponse);

            return(BuildLinkForDownload(mainDownloadResponse, storageDownloadResponse));
        }
        public IMusic ParseUri(string uri)
        {
            //"https://music.yandex.ru/album/12360955/track/72099136"
            //https://music.yandex.ru/album/12343211
            //https://music.yandex.ru/artist/1810
            string[] uriParts = uri.Split("/");

            if (uriParts.Contains("track"))
            {
                YTrack ymTrack = yandexMusicClient.Track.Get(yAuthStorage,
                                                             uriParts.Last( ))
                                 .Result
                                 .First();
                return(new Track(ymTrack.Title,
                                 ymTrack.Artists.First().Name,
                                 ymTrack.Albums.First().Title,
                                 0,
                                 null,
                                 ymTrack.Id));
            }

            if (uriParts.Contains("album") && !uriParts.Contains("track"))
            {
                YAlbum ymAlbum = yandexMusicClient.Album.Get(yAuthStorage,
                                                             uriParts.Last( ))
                                 .Result;
                return(new Album(ymAlbum.Title,
                                 ymAlbum.Artists.First( ).Name,
                                 ymAlbum.Year.ToString( ),
                                 null,
                                 ymAlbum.Id));
            }

            if (uriParts.Contains("artist"))
            {
                YArtistBriefInfo ymArtist = yandexMusicClient.Artist.Get(yAuthStorage,
                                                                         uriParts.Last( ))
                                            .Result;
                YAlbum ymSampleAlbum = ymArtist.Albums.First();
                Album  sampleAlbum   = new Album(ymSampleAlbum.Title,
                                                 ymArtist.Artist.Name,
                                                 ymSampleAlbum.Year.ToString( ),
                                                 null,
                                                 ymSampleAlbum.Id);
                return(new Artist(ymArtist.Artist.Name,
                                  sampleAlbum,
                                  null,
                                  uriParts.Last()));
            }

            return(null);
        }
Example #4
0
        /// <summary>
        /// Выгрузка в файл
        /// </summary>
        /// <param name="storage">Хранилище</param>
        /// <param name="track">Трек</param>
        /// <param name="filePath">Путь для файла</param>
        public void ExtractToFile(YAuthStorage storage, YTrack track, string filePath)
        {
            var fileLink = GetFileLink(storage, track.GetKey().ToString());

            try
            {
                using (var client = new WebClient())
                {
                    client.DownloadFile(fileLink, filePath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Example #5
0
        /// <summary>
        /// Получение двоичного массива данных
        /// </summary>
        /// <param name="storage">Хранилище</param>
        /// <param name="track">Трек</param>
        /// <returns></returns>
        public byte[] ExtractData(YAuthStorage storage, YTrack track)
        {
            var fileLink = GetFileLink(storage, track.GetKey().ToString());

            var bytes = default(byte[]);

            try
            {
                using (var client = new WebClient())
                {
                    bytes = client.DownloadData(fileLink);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(bytes);
        }
        static void Main(string[] args)
        {
            string login;
            string password;

            if (args.Length > 0)
            {
                login    = args[0];
                password = args[1];
            }
            else
            {
                Console.WriteLine("Введите логин");
                login = Console.ReadLine();
                Console.WriteLine("Введите пароль");
                password = Console.ReadLine();
                Console.Clear();
            }
            DebugSettings debugSettings = new DebugSettings(@"C:\yandex_music", @"C:\yandex_music\log.txt");

            debugSettings.Clear();
            AuthStorage authStorage = new AuthStorage(debugSettings);

            authStorage.User.Login = login;
            var api = new YandexMusicApi();

            api.User.Authorize(authStorage, authStorage.User.Login, password);
            if (authStorage.IsAuthorized)
            {
                Console.WriteLine("Успешная авторизация");
            }
            else
            {
                Console.WriteLine("Неверный логин или пароль");
                return;
            }

            Console.WriteLine("Какой трек Вы хотите прослушать?");
            string searchingQuery = Console.ReadLine();
            var    search         = api.Search.Track(authStorage, searchingQuery);

            if (search.Result.Tracks == null)
            {
                Console.WriteLine("По Вашему запросу ничего не найдено :(");
                return;
            }

            int count = 1;

            foreach (var item in search.Result.Tracks.Results)
            {
                Console.WriteLine($"{count}. {item.Artists[0].Name} - {item.Title}");
                count++;
            }

            Console.WriteLine("Выберите трек из списка:");
            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice < 1 || choice > 20)
            {
                Console.WriteLine("Вы ввели неверное число.");
                return;
            }

            YTrack track = api.Track.Get(authStorage, search.Result.Tracks.Results[choice - 1].Id).Result[0];

            var fileLink = api.Track.GetFileLink(authStorage, track);

            using (var client = new WebClient())
            {
                client.DownloadFile(fileLink, @$ "{debugSettings.OutputDir}\{track.Title}.mp3");
Example #7
0
 /// <summary>
 /// Получение метаданных для загрузки
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <param name="direct">Должен ли ответ содержать прямую ссылку на загрузку</param>
 /// <returns></returns>
 public async Task <List <YTrackDownloadInfoResponse> > GetMetadataForDownloadAsync(YAuthStorage storage, YTrack track, bool direct = false)
 {
     return(await GetMetadataForDownloadAsync(storage, track.GetKey().ToString(), direct));
 }
Example #8
0
 /// <summary>
 /// Получение метаданных для загрузки
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <param name="direct">Должен ли ответ содержать прямую ссылку на загрузку</param>
 /// <returns></returns>
 public List <YTrackDownloadInfoResponse> GetMetadataForDownload(YAuthStorage storage, YTrack track, bool direct = false)
 {
     return(GetMetadataForDownloadAsync(storage, track.GetKey().ToString(), direct).GetAwaiter().GetResult());
 }
Example #9
0
 public AudioPlayer(AuthStorage authStorage, YTrack track)
 {
     _authStorage = authStorage;
     _track       = track;
     _currentTime = new TimeSpan();
 }
Example #10
0
 /// <summary>
 /// Добавить трек в список дизлайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public async Task <int> AddTrackDislikeAsync(YAuthStorage storage, YTrack track)
 {
     return(await new YLibraryAddRequest(storage)
            .Create(track.GetKey().ToString(), YLibrarySection.Tracks, YLibrarySectionType.Dislikes)
            .GetResponseAsync <int>("revision"));
 }
Example #11
0
 public static int RemoveDislike(this YTrack track)
 {
     return(track.Context.API.Library.RemoveTrackDislike(track.Context.Storage, track).Result.Revision);
 }
Example #12
0
 public static string GetLink(this YTrack track)
 {
     return(track.Context.API.Track.GetFileLink(track.Context.Storage, track));
 }
Example #13
0
 /// <summary>
 /// Удалить трек из списка лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public async Task <int> RemoveTrackLikeAsync(YAuthStorage storage, YTrack track)
 {
     return(await new YLibraryRemoveRequest(storage)
            .Create(track.GetKey().ToString(), YLibrarySection.Tracks)
            .GetResponseAsync <int>("revision"));
 }
Example #14
0
 public static int AddLike(this YTrack track)
 {
     return(track.Context.API.Library.AddTrackLike(track.Context.Storage, track).Result.Revision);
 }
Example #15
0
 /// <summary>
 /// Удалить трек из списка лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public int RemoveTrackDislike(YAuthStorage storage, YTrack track)
 {
     return(RemoveTrackLikeAsync(storage, track).GetAwaiter().GetResult());
 }
Example #16
0
 /// <summary>
 /// Добавить трек в список лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public async Task <YResponse <YPlaylist> > AddTrackLikeAsync(AuthStorage storage, YTrack track)
 {
     return(await new YLibraryAddRequest(api, storage)
            .Create(track.GetKey().ToString(), YLibrarySection.Tracks)
            .GetResponseAsync <YResponse <YPlaylist> >());
 }
Example #17
0
 /// <summary>
 /// Удалить трек из списка лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public YResponse <YRevision> RemoveTrackDislike(AuthStorage storage, YTrack track)
 {
     return(RemoveTrackLikeAsync(storage, track).GetAwaiter().GetResult());
 }
Example #18
0
 /// <summary>
 /// Удалить трек из списка дизлайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public async Task <YResponse <int> > RemoveTrackDislikeAsync(AuthStorage storage, YTrack track)
 {
     return(await new YLibraryRemoveRequest(api, storage)
            .Create(track.GetKey().ToString(), YLibrarySection.Tracks, YLibrarySectionType.Dislikes)
            .GetResponseAsync <YResponse <int> >());
 }
Example #19
0
 /// <summary>
 /// Добавить трек в список лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public YResponse <YPlaylist> AddTrackLike(AuthStorage storage, YTrack track)
 {
     return(AddTrackLikeAsync(storage, track).GetAwaiter().GetResult());
 }
Example #20
0
 public static void Save(this YTrack track, string filePath)
 {
     track.Context.API.Track.ExtractToFile(track.Context.Storage, track, filePath);
 }
Example #21
0
 /// <summary>
 /// Добавить трек в список лайкнутых
 /// </summary>
 /// <param name="storage">Хранилище</param>
 /// <param name="track">Трек</param>
 /// <returns></returns>
 public int AddTrackLike(YAuthStorage storage, YTrack track)
 {
     return(AddTrackLikeAsync(storage, track).GetAwaiter().GetResult());
 }