APIHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MusicPlayer;
  7. using Newtonsoft.Json.Linq;
  8. using System.IO;
  9. using System.Drawing;
  10. namespace MusicPlayer
  11. {
  12. public class APIHandler
  13. {
  14. private NetworkHandler nw;
  15. private Image defaultCover;
  16. public APIHandler(NetworkHandler nw)
  17. {
  18. this.nw = nw;
  19. defaultCover = Image.FromStream(nw.downloadArtwork("default-cover.png"));
  20. }
  21. public JObject GetAllBySearch(string search, string album, string artist, string genre)
  22. {
  23. // Q artist genre album
  24. JObject o = nw.SendString($"search?q={search}&album={album}&genre={genre}&artist={artist}");
  25. if (o != null)
  26. {
  27. Console.WriteLine(o.PropertyValues().ToString());
  28. if (o["result"].ToString() == "OK") { return o; }
  29. }
  30. return null;
  31. }
  32. public string GetSongURLByID(string id)
  33. {
  34. JObject o = nw.SendString("getsongbyid?id=" + id);
  35. if (o["result"].ToString() == "OK") {
  36. return o["songurl"].ToString();
  37. }
  38. return o["errormsg"].ToString();
  39. }
  40. public List<Song> GetSongsByAlbum(string albumname)
  41. {
  42. return GetSongsByArgs("album=" + albumname);
  43. }
  44. public List<Song> GetSongsByArtist(string artistname)
  45. {
  46. return GetSongsByArgs("artist=" + artistname);
  47. }
  48. public List<Song> GetSongsByGenre(string genre)
  49. {
  50. return GetSongsByArgs("genre=" + genre);
  51. }
  52. public List<Song> GetSongsByYear(string year)
  53. {
  54. return GetSongsByArgs("album=" + year);
  55. }
  56. public List<Song> GetSongsBySearch(string search)
  57. {
  58. return GetSongsByArgs("search=" + search);
  59. }
  60. public List<Genre> Genrify(JObject o)
  61. {
  62. List<Genre> genreslist = new List<Genre>();
  63. if (o["result"].ToString() == "OK")
  64. {
  65. for (int i = 0; i < o["genres"].Count(); i++)
  66. {
  67. genreslist.Add(new Genre(o["genres"][i][0].ToString()));
  68. }
  69. }
  70. return genreslist;
  71. }
  72. public List<Artist> Artistify(JObject o)
  73. {
  74. List<Artist> artistlist = new List<Artist>();
  75. if (o["result"].ToString() == "OK")
  76. {
  77. for (int i = 0; i < o["artists"].Count(); i++)
  78. {
  79. artistlist.Add(new Artist(o["artists"][i][0].ToString()));
  80. }
  81. }
  82. return artistlist;
  83. }
  84. public List<Song> Songify(JObject o)
  85. {
  86. List<Song> allsongslist = new List<Song>();
  87. if (o["result"].ToString() == "OK")
  88. {
  89. dynamic songs = o["songs"];
  90. for (int i = 0; i < songs.Count; i++)
  91. {
  92. allsongslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), songs[i][1].ToString(), (int)songs[i][9], this));
  93. }
  94. }
  95. return allsongslist;
  96. }
  97. public List<Album> Albumify(JObject o)
  98. {
  99. List<Album> albumlist = new List<Album>();
  100. if (o["result"].ToString() == "OK")
  101. {
  102. for (int i = 0; i < o["albums"].Count(); i++)
  103. {
  104. albumlist.Add(new Album(o["albums"][i][0].ToString()));
  105. }
  106. }
  107. return albumlist;
  108. }
  109. public List<Song> GetAllSongs()
  110. {
  111. List<Song> allsongslist = new List<Song>();
  112. JObject o = nw.SendString("getallsongs?");
  113. if (o["result"].ToString() == "OK")
  114. {
  115. dynamic songs = o["songs"];
  116. for (int i = 0; i < songs.Count; i++)
  117. {
  118. allsongslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), songs[i][1].ToString(), (int)songs[i][9], this));
  119. }
  120. }
  121. return allsongslist;
  122. }
  123. public List<Song> GetSongsByArgs(string args)
  124. {
  125. List<Song> songslist = new List<Song>();
  126. JObject o = nw.SendString("getsongs?"+args);
  127. if (o["result"].ToString() == "OK")
  128. {
  129. dynamic songs = o["songs"];
  130. for (int i = 0; i < songs.Count; i++)
  131. {
  132. if(songs[i][2].ToString().EndsWith(".mp3"))
  133. songslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), songs[i][1].ToString(), (int)songs[i][9], this));
  134. }
  135. }
  136. return songslist;
  137. }
  138. public List<Artist> GetArtists()
  139. {
  140. List<Artist> artistlist = new List<Artist>();
  141. JObject o = nw.SendString("getartists?");
  142. if (o["result"].ToString() == "OK")
  143. {
  144. for (int i = 0; i < o["artists"].Count(); i++) {
  145. artistlist.Add(new Artist(o["artists"][i][0].ToString()));
  146. }
  147. }
  148. return artistlist;
  149. }
  150. public Image getAlbumCover(string album)
  151. {
  152. MemoryStream stream = nw.downloadArtwork(album + ".jpg");
  153. if(stream != null)
  154. {
  155. return Image.FromStream(stream);
  156. }
  157. return defaultCover;
  158. }
  159. public List<Album> GetAlbums()
  160. {
  161. List<Album> albumlist = new List<Album>();
  162. JObject o = nw.SendString("getalbums?");
  163. if (o["result"].ToString() == "OK")
  164. {
  165. for (int i = 0; i < o["albums"].Count(); i++)
  166. {
  167. albumlist.Add(new Album(o["albums"][i][0].ToString()));
  168. }
  169. }
  170. return albumlist;
  171. }
  172. public List<Year> GetYears()
  173. {
  174. List<Year> yearlist = new List<Year> ();
  175. JObject o = nw.SendString("getyears?");
  176. if (o["result"].ToString() == "OK")
  177. {
  178. for (int i = 0; i < o["years"].Count(); i++)
  179. {
  180. yearlist.Add(new Year(o["years"][i][0].ToString()));
  181. }
  182. }
  183. return yearlist;
  184. }
  185. public List<Genre> GetGenres()
  186. {
  187. List<Genre> genreslist = new List<Genre>();
  188. JObject o = nw.SendString("getgenres?");
  189. if (o["result"].ToString() == "OK")
  190. {
  191. for (int i = 0; i < o["genres"].Count(); i++)
  192. {
  193. genreslist.Add(new Genre(o["genres"][i][0].ToString()));
  194. }
  195. }
  196. return genreslist;
  197. }
  198. }
  199. }