PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Torshify.Shell/Program.cs

http://github.com/torshy/torshify
C# | 258 lines | 205 code | 53 blank | 0 comment | 20 complexity | 613ab80a080889a8ed5500180e3b385d MD5 | raw file
  1. using System;
  2. using System.Threading;
  3. namespace Torshify.Shell
  4. {
  5. class Program
  6. {
  7. #region Fields
  8. private ManualResetEventSlim _logInEvent = new ManualResetEventSlim(false);
  9. #endregion Fields
  10. #region Properties
  11. public ISession Session
  12. {
  13. get;
  14. private set;
  15. }
  16. #endregion Properties
  17. #region Public Methods
  18. public void Run()
  19. {
  20. InitializeSession();
  21. ConsoleEx.Write("Enter username >> ", ConsoleColor.Green);
  22. string userName = Console.ReadLine();
  23. ConsoleEx.Write("Enter password >> ", ConsoleColor.Green);
  24. string password = ConsoleEx.GetPassword();
  25. for (int i = 0; i < password.Length; i++)
  26. {
  27. Console.Write("*");
  28. }
  29. Console.WriteLine();
  30. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
  31. {
  32. ConsoleEx.WriteLine("Please provide a username and password", ConsoleColor.Red);
  33. return;
  34. }
  35. ConsoleEx.WriteLine("Logging in..");
  36. Session.Login(userName, password, rememberMe: false);
  37. _logInEvent.Wait(5000);
  38. ConsoleKeyInfo keyInfo;
  39. do
  40. {
  41. using (ConsoleEx.BeginColorBlock(ConsoleColor.Cyan))
  42. {
  43. Console.WriteLine("=== Main menu ===");
  44. Console.WriteLine("1: Search");
  45. Console.WriteLine("2: Toplists");
  46. Console.WriteLine("3: Playlists");
  47. Console.WriteLine("6: Current user info");
  48. Console.WriteLine("7: Run GC");
  49. Console.WriteLine("=================");
  50. }
  51. ConsoleEx.Write("Enter menu >> ", ConsoleColor.Green);
  52. keyInfo = Console.ReadKey();
  53. Console.Write(Environment.NewLine);
  54. switch (keyInfo.Key)
  55. {
  56. case ConsoleKey.D1:
  57. SearchMenu();
  58. break;
  59. case ConsoleKey.D2:
  60. ToplistsMenu();
  61. break;
  62. case ConsoleKey.D3:
  63. PlaylistsMenu();
  64. break;
  65. case ConsoleKey.D6:
  66. CurrentUserInfoMenu();
  67. break;
  68. case ConsoleKey.D7:
  69. GC.Collect();
  70. GC.WaitForPendingFinalizers();
  71. GC.Collect();
  72. break;
  73. }
  74. if (keyInfo.Key != ConsoleKey.Escape)
  75. {
  76. ConsoleEx.Write("Press any key to continue (except Esc which will exit)", ConsoleColor.Yellow);
  77. keyInfo = Console.ReadKey();
  78. Console.WriteLine();
  79. }
  80. } while (keyInfo.Key != ConsoleKey.Escape);
  81. if (Session.ConnectionState == ConnectionState.LoggedIn)
  82. {
  83. Session.Logout();
  84. Session.Dispose();
  85. }
  86. }
  87. #endregion Public Methods
  88. #region Protected Methods
  89. protected void InitializeSession()
  90. {
  91. Session = SessionFactory
  92. .CreateSession(
  93. Constants.ApplicationKey,
  94. Constants.CacheFolder,
  95. Constants.SettingsFolder,
  96. Constants.UserAgent)
  97. .SetPreferredBitrate(Bitrate.Bitrate320k)
  98. .SetPreferredOfflineBitrate(Bitrate.Bitrate96k, false);
  99. Session.LoginComplete += UserLoggedIn;
  100. Session.LogoutComplete += UserLoggedOut;
  101. Session.ConnectionError += ConnectionError;
  102. }
  103. protected void SearchMenu()
  104. {
  105. ConsoleEx.WriteLine("=== Search ===", ConsoleColor.Cyan);
  106. ConsoleEx.Write("Query >> ", ConsoleColor.Green);
  107. string query = Console.ReadLine();
  108. if (string.IsNullOrEmpty(query))
  109. {
  110. ConsoleEx.WriteLine("Query string for search can't be empty", ConsoleColor.Red);
  111. return;
  112. }
  113. ConsoleEx.WriteLine("Searching..", ConsoleColor.Yellow);
  114. ISearch search = Session
  115. .Search(query, 0, 25, 0, 25, 0, 25, 0, 25, SearchType.Standard)
  116. .WaitForCompletion();
  117. if (!string.IsNullOrEmpty(search.DidYouMean))
  118. {
  119. ConsoleEx.WriteLine("Maybe you ment " + search.DidYouMean + "?", ConsoleColor.Magenta);
  120. }
  121. for (int i = 0; i < search.Tracks.Count; i++)
  122. {
  123. ITrack track = search.Tracks[i];
  124. ConsoleEx.Write("{0:00} : {1,-20}", ConsoleColor.White, (i + 1), ConsoleEx.Truncate(track.Name, 20));
  125. ConsoleEx.Write(" {0,-16}", ConsoleColor.Gray, ConsoleEx.Truncate(track.Album.Artist.Name, 15));
  126. ConsoleEx.WriteLine(" {0,-16}", ConsoleColor.DarkGray, ConsoleEx.Truncate(track.Album.Name, 15));
  127. }
  128. for (int i = 0; i < search.Playlists.Count; i++)
  129. {
  130. IPlaylistSearchResult playlist = search.Playlists[i];
  131. ConsoleEx.Write("{0:00} : {1,-20}", ConsoleColor.White, (i + 1), ConsoleEx.Truncate(playlist.Name, 20));
  132. ConsoleEx.Write(" {0,-16}", ConsoleColor.Gray, ConsoleEx.Truncate(playlist.Uri, 15));
  133. ConsoleEx.WriteLine(" {0,-16}", ConsoleColor.DarkGray, ConsoleEx.Truncate(playlist.ImageUri, 15));
  134. }
  135. }
  136. protected void ToplistsMenu()
  137. {
  138. ConsoleEx.WriteLine("=== Toplist ===", ConsoleColor.Cyan);
  139. IToplistBrowse toplistBrowse = Session
  140. .Browse(ToplistType.Tracks)
  141. .WaitForCompletion();
  142. for (int i = 0; i < toplistBrowse.Tracks.Count; i++)
  143. {
  144. ITrack track = toplistBrowse.Tracks[i];
  145. ConsoleEx.Write("{0:00} : {1,-20}", ConsoleColor.White, (i + 1), ConsoleEx.Truncate(track.Name, 20));
  146. ConsoleEx.Write(" {0,-16}", ConsoleColor.Gray, ConsoleEx.Truncate(track.Album.Artist.Name, 15));
  147. ConsoleEx.WriteLine(" {0,-16}", ConsoleColor.DarkGray, ConsoleEx.Truncate(track.Album.Name, 15));
  148. }
  149. }
  150. protected void PlaylistsMenu()
  151. {
  152. ConsoleEx.WriteLine("=== Playlists ===", ConsoleColor.Cyan);
  153. for (int i = 0; i < Session.PlaylistContainer.Playlists.Count; i++)
  154. {
  155. IPlaylist playlist = Session.PlaylistContainer.Playlists[i];
  156. ConsoleEx.WriteLine("{0:00} : {1,-20}", ConsoleColor.White, (i + 1), playlist.Name);
  157. }
  158. }
  159. protected void CurrentUserInfoMenu()
  160. {
  161. ConsoleEx.WriteLine("=== Current user info ===", ConsoleColor.Cyan);
  162. ConsoleEx.Write("Username: ", ConsoleColor.DarkYellow);
  163. ConsoleEx.WriteLine(Session.LoggedInUser.CanonicalName, ConsoleColor.White);
  164. ConsoleEx.Write("Display name: ", ConsoleColor.DarkYellow);
  165. ConsoleEx.WriteLine(Session.LoggedInUser.DisplayName, ConsoleColor.White);
  166. ConsoleEx.Write("Country code: ", ConsoleColor.DarkYellow);
  167. ConsoleEx.WriteLine(Session.LoggedInUserCountry.ToString(), ConsoleColor.White);
  168. }
  169. #endregion Protected Methods
  170. #region Private Static Methods
  171. static void Main(string[] args)
  172. {
  173. Program program = new Program();
  174. program.Run();
  175. }
  176. #endregion Private Static Methods
  177. #region Private Methods
  178. private void ConnectionError(object sender, SessionEventArgs e)
  179. {
  180. if (e.Status != Error.OK)
  181. {
  182. ConsoleEx.WriteLine("Connection error: " + e.Message, ConsoleColor.Red);
  183. }
  184. }
  185. private void UserLoggedOut(object sender, SessionEventArgs e)
  186. {
  187. ConsoleEx.WriteLine("Logged out..", ConsoleColor.Yellow);
  188. }
  189. private void UserLoggedIn(object sender, SessionEventArgs e)
  190. {
  191. if (e.Status == Error.OK)
  192. {
  193. ConsoleEx.WriteLine("Successfully logged in", ConsoleColor.Yellow);
  194. }
  195. else
  196. {
  197. ConsoleEx.WriteLine("Unable to log in: " + e.Status.GetMessage(), ConsoleColor.Red);
  198. Console.ReadLine();
  199. Environment.Exit(-1);
  200. }
  201. _logInEvent.Set();
  202. }
  203. #endregion Private Methods
  204. }
  205. }