Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add the Download methods to Play API

  • Loading branch information...
commit 0032b8dfe81a10cc11febdf55c985c50bc4976df 1 parent cdf18bb
@paulcbetts paulcbetts authored
Showing with 53 additions and 1 deletion.
  1. +19 −1 Play.Tests/Models/PlayApiTests.cs
  2. +34 −0 Play/Models/PlayApi.cs
View
20 Play.Tests/Models/PlayApiTests.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
@@ -112,5 +113,22 @@ public void MakeAlbumSearchIntegrationTest()
result.Count.Should().BeGreaterThan(2);
}
+ [Fact(Skip = "Only enable this to test downloads")]
+ public void DownloadAlbumIntegrationTest()
+ {
+ var kernel = new MoqMockingKernel();
+ var client = new RestClient(IntegrationTestUrl.Current);
+
+ client.AddDefaultHeader("Authorization", IntegrationTestUrl.Token);
+ kernel.Bind<IBlobCache>().To<TestBlobCache>();
+
+ var api = new PlayApi(client, kernel.Get<IBlobCache>());
+
+ var result = api.DownloadAlbum("Beirut", "Lon Gisland EP").First();
+
+ using (var of = File.OpenWrite("C:\\Users\\Administrator\\" + result.Item1)) {
+ new MemoryStream(result.Item2).CopyTo(of);
+ }
+ }
}
-}
+}
View
34 Play/Models/PlayApi.cs
@@ -5,6 +5,7 @@
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Akavache;
@@ -28,6 +29,8 @@ public interface IPlayApi
IObservable<List<Song>> Search(string query);
IObservable<List<Song>> AllSongsForArtist(string name);
IObservable<List<Song>> AllSongsOnAlbum(string artist, string album);
+ IObservable<Tuple<string, byte[]>> DownloadSong(Song song);
+ IObservable<Tuple<string, byte[]>> DownloadAlbum(string artist, string album);
IObservable<Unit> ConnectToSongChangeNotifications();
}
@@ -124,6 +127,24 @@ public IObservable<List<Song>> AllSongsOnAlbum(string artist, string album)
return client.RequestAsync<SongQueue>(rq).Select(x => x.Data.songs);
}
+ public IObservable<Tuple<string, byte[]>> DownloadSong(Song song)
+ {
+ var rq = new RestRequest(String.Format("song/{0}/download", song.id));
+
+ return client.RequestAsync(rq)
+ .Select(x => Tuple.Create(fileNameFromResponse(x) ?? "song.mp3", x.RawBytes));
+ }
+
+ public IObservable<Tuple<string, byte[]>> DownloadAlbum(string artist, string album)
+ {
+ var rq = new RestRequest(String.Format("artist/{0}/album/{1}/download",
+ HttpUtility.UrlEncode(artist).Replace("+", "%20"),
+ HttpUtility.UrlEncode(album).Replace("+", "%20")));
+
+ return client.RequestAsync(rq)
+ .Select(x => Tuple.Create(fileNameFromResponse(x) ?? "songs.zip", x.RawBytes));
+ }
+
public IObservable<Unit> ConnectToSongChangeNotifications()
{
var rq = new RestRequest("streaming_info");
@@ -138,5 +159,18 @@ public IObservable<string> ListenUrl()
var rq = new RestRequest("streaming_info");
return client.RequestAsync<StreamingInfo>(rq).Select(x => x.Data.stream_url);
}
+
+ string fileNameFromResponse(RestResponse response)
+ {
+ var disp = response.Headers.FirstOrDefault(y => y.Name.Equals("content-disposition", StringComparison.InvariantCultureIgnoreCase));
+ if (disp == null) {
+ return null;
+ }
+
+ var re = new Regex("filename=\"([^\"]+)\"(;|$)");
+ var m = re.Match((string) disp.Value);
+
+ return m.Success ? m.Groups[1].Value : null;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.