APIHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. namespace MusicPlayer
  9. {
  10. public class APIHandler
  11. {
  12. private NetworkHandler nw;
  13. public APIHandler(NetworkHandler nw)
  14. {
  15. this.nw = nw;
  16. }
  17. public string GetSongURLByID(string id)
  18. {
  19. JObject o = nw.SendString("getsongbyid?id=" + id);
  20. if (o["result"].ToString() == "OK") {
  21. return o["songurl"].ToString();
  22. }
  23. return o["errormsg"].ToString();
  24. }
  25. public List<Song> GetSongsByAlbum(string albumname)
  26. {
  27. return GetSongsByArgs("album=" + albumname);
  28. }
  29. public List<Song> GetSongsByArtist(string artistname)
  30. {
  31. return GetSongsByArgs("artist=" + artistname);
  32. }
  33. public List<Song> GetSongsByGenre(string genre)
  34. {
  35. return GetSongsByArgs("genre=" + genre);
  36. }
  37. public List<Song> GetSongsByYear(string year)
  38. {
  39. return GetSongsByArgs("album=" + year);
  40. }
  41. public List<Song> GetAllSongs()
  42. {
  43. List<Song> allsongslist = new List<Song>();
  44. JObject o = nw.SendString("getallsongs?");
  45. if (o["result"].ToString() == "OK")
  46. {
  47. dynamic songs = o["songs"];
  48. for (int i = 0; i < songs.Count; i++)
  49. {
  50. allsongslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), this));
  51. }
  52. }
  53. return allsongslist;
  54. }
  55. public List<Song> GetSongsByArgs(string args)
  56. {
  57. List<Song> songslist = new List<Song>();
  58. JObject o = nw.SendString("getsongs?"+args);
  59. if (o["result"].ToString() == "OK")
  60. {
  61. dynamic songs = o["songs"];
  62. for (int i = 0; i < songs.Count; i++)
  63. {
  64. 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));
  65. }
  66. }
  67. return songslist;
  68. }
  69. public List<Artist> GetArtists()
  70. {
  71. List<Artist> artistlist = new List<Artist>();
  72. JObject o = nw.SendString("getartists?id=hallo");
  73. if (o["result"].ToString() == "OK")
  74. {
  75. for (int i = 0; i < o["artists"].Count(); i++) {
  76. artistlist.Add(new Artist(o["artists"][i][0].ToString()));
  77. }
  78. }
  79. return artistlist;
  80. }
  81. public List<Album> GetAlbums()
  82. {
  83. List<Album> albumlist = new List<Album>();
  84. JObject o = nw.SendString("getalbums?id=hallo");
  85. if (o["result"].ToString() == "OK")
  86. {
  87. for (int i = 0; i < o["albums"].Count(); i++)
  88. {
  89. albumlist.Add(new Album(o["albums"][i][0].ToString()));
  90. }
  91. }
  92. return albumlist;
  93. }
  94. public List<Year> GetYears()
  95. {
  96. List<Year> yearlist = new List<Year> ();
  97. JObject o = nw.SendString("getyears?id=hallo");
  98. if (o["result"].ToString() == "OK")
  99. {
  100. for (int i = 0; i < o["years"].Count(); i++)
  101. {
  102. yearlist.Add(new Year(o["years"][i][0].ToString()));
  103. }
  104. }
  105. return yearlist;
  106. }
  107. public List<Genre> GetGenres()
  108. {
  109. List<Genre> genreslist = new List<Genre>();
  110. JObject o = nw.SendString("getgenres?id=hallo");
  111. if (o["result"].ToString() == "OK")
  112. {
  113. for (int i = 0; i < o["genres"].Count(); i++)
  114. {
  115. genreslist.Add(new Genre(o["genres"][i][0].ToString()));
  116. }
  117. }
  118. return genreslist;
  119. }
  120. }
  121. }