/// <summary>
        /// </summary>
        /// <param name="userCredentials">
        /// </param>
        /// <returns>
        /// </returns>
        public IObservable <UserLoginResponseContract> AuthenticateAsync(UserCredentialsContract userCredentials)
        {
            if (userCredentials == null || string.IsNullOrWhiteSpace(userCredentials.UserName) ||
                string.IsNullOrWhiteSpace(userCredentials.Password))
            {
                return(Observable.Empty <UserLoginResponseContract>());
            }

            // downloader.UserCredentials = null;
            this.downloader.UserToken = null;
            string postData = string.Format(
                "login={0}&password={1}",
                ApiUrl.Escape(userCredentials.UserName),
                ApiUrl.Escape(userCredentials.Password));
            IObservable <UserLoginResponseContract> userLogin =
                from response in this.downloader.PostAndGetStringAsync(ApiUrl.Authenticate(), postData)
                let user = Json <UserLoginResponseContract> .Instance.DeserializeFromString(response)
                               where !string.IsNullOrWhiteSpace(user.UserToken)
                           select user;

            return((from loginResponse in userLogin
                    from _ in this.SaveCredentialsAsync(userCredentials)
                    from __ in this.SaveUserTokenAsync(loginResponse)
                    select loginResponse).Do(
                       response =>
            {
                PlayerService.DeletePlayToken();
                this.downloader.UserToken = response.UserToken;

                // downloader.UserCredentials = userCredentials;
            }));
        }
        /// <summary>
        /// </summary>
        /// <param name="userId">
        /// </param>
        /// <param name="pageNumber">
        /// </param>
        /// <param name="pageSize">
        /// </param>
        /// <returns>
        /// </returns>
        public IObservable <MixesResponseContract> GetMixFeedAsync(string userId, int pageNumber, int pageSize)
        {
            string cacheFile = string.Format(MixFeedCacheFile, userId, pageNumber, pageSize);

            return
                (this.downloader.GetDeserializedCachedAndRefreshedAsync <MixesResponseContract>(
                     ApiUrl.UserMixes(userId, "mix_feed", pageNumber, pageSize), cacheFile).NotNull());
        }
Beispiel #3
0
        public static Uri SearchMixes(string query, string sort, int pageNumber, int perPage)
        {
            var urlFormat = string.Format(
                "http://8tracks.com/mixes.json?q={0}&sort={1}&page={2}&per_page={3}&api_version={4}",
                ApiUrl.Escape(query),
                sort,
                pageNumber,
                perPage,
                ApiVersion);

            return(new Uri(urlFormat, UriKind.Absolute));
        }
        /// <summary>
        /// </summary>
        /// <param name="mixId">
        /// </param>
        /// <param name="review">
        /// </param>
        /// <returns>
        /// </returns>
        public IObservable <ReviewResponseContract> AddMixReviewAsync(string mixId, string review)
        {
            var    url  = new Uri("http://8tracks.com/reviews", UriKind.Absolute);
            string body = string.Format(
                "review%5Bbody%5D={0}&review%5Bmix_id%5D={1}&format=json", ApiUrl.Escape(review), mixId);

            return(from response in this.downloader.PostStringAndGetDeserializedAsync <ReviewResponseContract>(url, body)
                   from responseWithUser in this.GetUserProfileAsync(response.Review.UserId).Select(
                       userResponse =>
            {
                if (response.Review.User == null)
                {
                    response.Review.User = userResponse.User;
                }

                return response;
            })
                   select responseWithUser);
        }
        public static IObservable <PlayingMixContract> StartPlayingAsync(this MixContract mix)
        {
            var playingMix = from playToken in GetOrCreatePlayTokenAsync()
                             from response in Downloader.GetDeserializedAsync <PlayResponseContract>(ApiUrl.Play(playToken, mix.Id))
                             .Repeat(2)
                             .Log("StartPlayingAsync: Attempting")
                             .TakeFirst(ValidResponse)
                             .Log("StartPlayingAsync: Valid Response")
                             from added in AddToRecentlyPlayedAsync(mix).Log("AddToRecentlyPlayedAsync: Done")

                             select new PlayingMixContract
            {
                PlayToken = playToken,
                MixId     = mix.Id,
                MixName   = mix.Name,
                Cover     = mix.Cover,
                Set       = response.Set
            };

            return(from playing in playingMix
                   from tile in SetNowPlayingTileAsync(
                       playing,
                       DataStrings.Title_ApplicationName,
                       DataStrings.Title_NowPlaying)
                   .Log("SetNowPlayingTileAsync: Done")
                   select playing);
        }
 /// <summary>
 /// Gets the next similar mix, ensuring that nulls are never returned and that the same mix is never returned.
 /// </summary>
 /// <param name="mixId"></param>
 /// <returns></returns>
 public static IObservable <MixContract> GetNextMixAsync(string mixId)
 {
     return(from playToken in GetOrCreatePlayTokenAsync()
            from response in Downloader.GetDeserializedAsync <NextMixResponseContract>(ApiUrl.NextMix(playToken, mixId))
            where response != null && response.NextMix != null && response.NextMix.Id != mixId
            select response.NextMix);
 }
 /// <summary>
 /// </summary>
 /// <param name="userId">
 /// </param>
 /// <returns>
 /// </returns>
 public IObservable <UserProfileResponseContract> GetUserProfileAsync(string userId)
 {
     return(this.downloader.GetDeserializedAsync <UserProfileResponseContract>(ApiUrl.UserProfile(userId)).NotNull());
 }
 public IObservable <FavouritedTrackListResponseContract> GetFavouriteTracksAsync(string userId, int pageNumber, int pageSize)
 {
     return(this.downloader.GetDeserializedAsync <FavouritedTrackListResponseContract>(ApiUrl.FavouriteTracks(userId, pageNumber, pageSize)).NotNull().Catch(Observable.Return(new FavouritedTrackListResponseContract())));
 }
Beispiel #9
0
 public static IObservable <MixesResponseContract> GetHistoryMixesAsync()
 {
     return(Downloader.GetDeserializedAsync <MixesResponseContract>(ApiUrl.HistoryMixes()).NotNull()); //, LatestMixesCacheFile);
 }
 /// <summary>
 /// </summary>
 /// <param name="userId">
 /// </param>
 /// <param name="pageNumber">
 /// </param>
 /// <param name="pageSize">
 /// </param>
 /// <returns>
 /// </returns>
 public IObservable <MixesResponseContract> GetUserMixesAsync(string userId, int pageNumber, int pageSize)
 {
     return(this.downloader.GetDeserializedAsync <MixesResponseContract>(
                ApiUrl.UserMixes(userId, pageNumber, pageSize)).NotNull());
 }
Beispiel #11
0
 private static IObservable <PlayResponseContract> SkipToNextTrackAsync(this PlayingMixContract playing, TimeSpan timePlayed)
 {
     return(from addToHistory in AddToMixTrackHistoryAsync(playing, timePlayed)
            from response in Downloader.GetDeserializedAsync <PlayResponseContract>(ApiUrl.SkipTrack(playing.PlayToken, playing.MixId))
            where ValidResponse(response)
            select response);
 }
Beispiel #12
0
 public static IObservable <MixesResponseContract> GetLatestMixesAsync()
 {
     return(Downloader.GetDeserializedCachedAndRefreshedAsync <MixesResponseContract>(ApiUrl.LatestMixes(), LatestMixesCacheFile).NotNull());
 }
Beispiel #13
0
 private static IObservable <PlayResponseContract> NextTrackAsync(this PlayingMixContract playing, TimeSpan timePlayed)
 {
     return(from addToHistory in AddToMixTrackHistoryAsync(playing, timePlayed)
            from response in Downloader.GetDeserializedAsync <PlayResponseContract>(ApiUrl.NextTrack(playing.PlayToken, playing.MixId))
            .Repeat(2)
            .Log("NextTrackAsync: Attempting")
            .TakeFirst(ValidResponse)
            .Log("NextTrackAsync: ValidResponse")
            select response);
 }
Beispiel #14
0
 public static IObservable <MixesResponseContract> GetTagMixesAsync(string tag, string sort, int pageNumber, int perPage)
 {
     return(Downloader.GetDeserializedAsync <MixesResponseContract>(ApiUrl.TaggedMixes(tag, sort, pageNumber, perPage)).NotNull());
 }
Beispiel #15
0
 public static IObservable <TagsResponseContract> GetTagsAsync(int pageNumber)
 {
     return(Downloader.GetDeserializedAsync <TagsResponseContract>(ApiUrl.Tags(pageNumber)));
 }
Beispiel #16
0
 public static IObservable <MixContract> GetMixAsync(string mixId)
 {
     return(from response in Downloader.GetDeserializedCachedAndRefreshedAsync <MixResponseContract>(ApiUrl.Mix(mixId), string.Format(MixCacheFile, mixId)).NotNull()
            where response.Mix != null
            select response.Mix);
 }
Beispiel #17
0
 public static IObservable <MixesResponseContract> GetSearchMixesAsync(string query, string sort, int pageNumber, int perPage)
 {
     return(Downloader.GetDeserializedAsync <MixesResponseContract>(ApiUrl.SearchMixes(query, sort, pageNumber, perPage)).NotNull());
 }
Beispiel #18
0
        public static IObservable <PortableUnit> AddToMixTrackHistoryAsync(this PlayingMixContract playing, TimeSpan timePlayed)
        {
            // If play duration was more than 30 seconds, post the report to Pay The Man
            if (timePlayed < TimeSpan.FromSeconds(30))
            {
                return(AddToMixesPlayedTracks(playing.MixId, new List <TrackContract>()
                {
                    playing.Set.Track
                }).ToUnit().Catch <PortableUnit, Exception>(ex => ObservableEx.SingleUnit()));
            }


            var payment = from response in Downloader.GetDeserializedAsync <ResponseContract>(ApiUrl.ReportTrack(playing.PlayToken, playing.MixId, playing.Set.Track.Id))
                          from mixTrackList in AddToMixesPlayedTracks(playing.MixId, new List <TrackContract>()
            {
                playing.Set.Track
            })
                          select new PortableUnit();

            return(payment.Catch <PortableUnit, Exception>(ex => ObservableEx.SingleUnit()));
        }
 /// <summary>
 /// </summary>
 /// <param name="userId">
 /// </param>
 /// <param name="pageNumber">
 /// </param>
 /// <param name="pageSize">
 /// </param>
 /// <returns>
 /// </returns>
 public IObservable <FollowingUserResponseContract> GetFollowsUsersAsync(
     string userId, int pageNumber, int pageSize)
 {
     return(this.downloader.GetDeserializedAsync <FollowingUserResponseContract>(ApiUrl.FollowsUsers(userId, pageNumber, pageSize)).NotNull().Catch(Observable.Return(new FollowingUserResponseContract())));
 }
Beispiel #20
0
        public static IObservable <PlayedTracksResponseContract> PlayedTracksAsync(this MixContract mix)
        {
            var playedTracks = from playToken in GetOrCreatePlayTokenAsync()
                               from response in Downloader.GetDeserializedAsync <PlayedTracksResponseContract>(ApiUrl.PlayedTracks(playToken, mix.Id))
                               .Coalesce(() => new PlayedTracksResponseContract
            {
                Tracks = new List <TrackContract>()
            })
                               from updatedList in AddToMixesPlayedTracks(mix.Id, response.Tracks)
                               select updatedList;

            return(playedTracks);
        }
Beispiel #21
0
 public static IObservable <ReviewsResponseContract> GetMixReviewsAsync(string mixId, int pageNumber, int perPage)
 {
     return(Downloader.GetDeserializedAsync <ReviewsResponseContract>(ApiUrl.MixReviews(mixId, pageNumber, perPage)).Catch <ReviewsResponseContract>(Observable.Return(new ReviewsResponseContract())).NotNull());
 }