public bool CanSkip(PandoraStation station) { // remove any skip history records older than an hour foreach (Queue <DateTime> currHistory in stationSkipHistory.Values) { while (currHistory.Count > 0 && DateTime.Now - currHistory.Peek() > new TimeSpan(1, 0, 0)) { currHistory.Dequeue(); } } // remove any daily skip history older than a day while (globalSkipHistory.Count > 0 && DateTime.Now - globalSkipHistory.Peek() > new TimeSpan(24, 0, 0)) { globalSkipHistory.Dequeue(); } // if the current station has no skip history, add it if (!stationSkipHistory.ContainsKey(station.Id)) { stationSkipHistory.Add(station.Id, new Queue <DateTime>()); } // if we are allowed to skip, record the current time and finish if (IsGlobalSkipAllowed() && IsStationSkipAllowed(station)) { return(true); } // too bad, no skip for you! return(false); }
private async void DownloadSongs_Click(object sender, RoutedEventArgs e) { using (var client = new WebClient()) { List <PandoraStation> stations = await pandoraService.GetPandoraStationsAsync(config, restfulService, pandoraState); PandoraStation station = stations.Where(s => s.Name == Station.Text).FirstOrDefault(); if (station != null) { int songsLeft = int.Parse(NumberOfSongs.Text); int count = 0; StationCache cache = new StationCache(); cache.Name = station.Name; while (songsLeft > 0) { List <PandoraSong> playlist = await pandoraService.GetPandoraPlaylistAsync(restfulService, pandoraState, station); foreach (PandoraSong song in playlist) { if (!IsInLibrary(song.Title, song.AlbumName, song.ArtistName) && songsLeft > 0) { string formattedTitle = song.Title; client.DownloadFile(song.AudioUrl, PandoraDirectory.Text + "/" + SanitizeFilename(formattedTitle) + ".mp4"); if (song.AlbumArtUrl != null) { client.DownloadFile(song.AlbumArtUrl, PandoraDirectory.Text + "/" + SanitizeFilename(song.AlbumName) + ".png"); } TagLib.File mp3File = TagLib.File.Create(PandoraDirectory.Text + "/" + SanitizeFilename(formattedTitle) + ".mp4"); mp3File.Tag.Title = song.Title; mp3File.Tag.Album = song.AlbumName; mp3File.Tag.AlbumArtists = song.ArtistName.Split(','); mp3File.Tag.Pictures = new TagLib.IPicture[] { new TagLib.Picture(PandoraDirectory.Text + "/" + SanitizeFilename(song.AlbumName) + ".png") }; mp3File.Save(); Log.AppendText("\n" + "Downloading (" + (count + 1) + "/" + NumberOfSongs.Text + "): " + song.Title + " by " + song.ArtistName); Files.Items.Add(new Song() { Title = song.Title, Album = song.AlbumName, Artist = song.ArtistName }); cache.Songs.Add(song); songsLeft--; count++; } } } string serializedSongList = JsonConvert.SerializeObject(cache); File.WriteAllText(PandoraDirectory.Text + "/Songs_" + Guid.NewGuid().ToString() + ".json", serializedSongList); Files.Items.SortDescriptions.Add(new SortDescription(Files.Columns[0].SortMemberPath, ListSortDirection.Ascending)); } else { Log.Text = Log.Text + "\n" + "Station not found: " + Station.Text; } } }
/// <summary> /// Retrieves a playlist for the given station. /// </summary> public List <PandoraSong> GetSongs(PandoraSession session, PandoraStation station, WebProxy proxy) { if (session == null || session.User == null) { throw new PandoraException("User must be logged in to make this request."); } GetPlaylistResponse response = (GetPlaylistResponse)ExecuteRequest(new GetPlaylistRequest(session, station.Token), proxy); return(response.Songs); }
public void Skip(PandoraStation station) { if (!CanSkip(station)) { throw new PandoraException("User is not currently allowed to skip tracks."); } // log the current time as a skip event stationSkipHistory[station.Id].Enqueue(DateTime.Now); globalSkipHistory.Enqueue(DateTime.Now); }
public PandoraSongFeedback RateSong(PandoraSession session, PandoraStation station, PandoraSong song, PandoraRating rating, WebProxy proxy) { if (session == null || session.User == null) { throw new PandoraException("User must be logged in to make this request."); } PandoraSongFeedback feedbackObj = (PandoraSongFeedback)ExecuteRequest(new AddFeedbackRequest(session, station.Token, song.Token, rating == PandoraRating.Love), proxy); song.Rating = rating; return(feedbackObj); }
private bool IsStationSkipAllowed(PandoraStation station) { if (AllowedStationsSkipsPerHour == null) { return(true); } if (stationSkipHistory[station.Id].Count < AllowedStationsSkipsPerHour) { return(true); } return(false); }
protected void Clear() { if (PreviousSongs == null) { PreviousSongs = new List <PandoraSong>(); } if (AvailableStations == null) { AvailableStations = new List <PandoraStation>(); } _currentStation = null; CurrentSong = null; PreviousSongs.Clear(); AvailableStations.Clear(); SkipHistory = null; User = null; playlist.Clear(); }
public void PromptAndChangeStation() { if (!initialized) { return; } try { PandoraStation newStation = ShowStationChooser(); if (newStation != null && newStation != Core.MusicBox.CurrentStation) { setWorkingAnimationStatus(true); Core.MusicBox.CurrentStation = newStation; Core.Settings.LastStation = newStation; PlayNextTrack(true); setWorkingAnimationStatus(false); logger.Info("Switched Station: " + newStation.Name); } } catch (Exception ex) { GracefullyFail(ex); } }
public PandoraSongFeedback RateSong(PandoraSession session, PandoraStation station, PandoraSong song, PandoraRating rating) { return(RateSong(session, station, song, rating, null)); }
/// <summary> /// Retrieves a playlist for the given station. /// </summary> public List <PandoraSong> GetSongs(PandoraSession session, PandoraStation station) { return(GetSongs(session, station, null)); }
private void ShowSongContext(PandoraSong song) { if (song.IsAdvertisement) { return; } IDialogbox dialog = (IDialogbox)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU); dialog.Reset(); dialog.SetHeading("Song Options - " + song.Title); GUIListItem thumbsUpItem = new GUIListItem("I like this song."); dialog.Add(thumbsUpItem); GUIListItem thumbsDownItem = new GUIListItem("I don't like this song."); dialog.Add(thumbsDownItem); GUIListItem tempBanItem = new GUIListItem("I am tired of this song."); dialog.Add(tempBanItem); dialog.DoModal(GUIWindowManager.ActiveWindow); GUIListItem whySelectedItem = new GUIListItem("Why was this song selected?"); //dialog.Add(whySelectedItem); GUIListItem moveSongItem = new GUIListItem("Move Song to Another Station..."); dialog.Add(moveSongItem); if (dialog.SelectedId == thumbsUpItem.ItemId) { RateSong(song, PandoraRating.Love); } else if (dialog.SelectedId == thumbsDownItem.ItemId) { RateSong(song, PandoraRating.Hate); } else if (dialog.SelectedId == whySelectedItem.ItemId) { // todo: show the reason song was selected } else if (dialog.SelectedId == moveSongItem.ItemId) { PandoraStation newStation = ShowStationChooser(); if (newStation != null) { // todo: move song to new station } } else if (dialog.SelectedId == tempBanItem.ItemId) { TemporarilyBanSong(song); } }
private PandoraStation ShowStationChooser() { int childWidth = 54; int innerWidth = childWidth - 4; int childHeight = Console.WindowHeight - 5; int childX = (Console.WindowWidth - childWidth) / 2; int childY = 2; int selectedIndex = 0; int scrollTop = 0; int lineCount = childHeight - 4; bool needStationMenuUpdate = true; modalWindowDisplayed = true; try { List <PandoraStation> stations = new List <PandoraStation>(); foreach (PandoraStation station in musicBox.AvailableStations) { if (station.IsQuickMix) { continue; } stations.Add(station); if (station == musicBox.CurrentStation) { selectedIndex = stations.Count - 1; } } ConsoleKeyInfo key; do { if (needStationMenuUpdate) { // print window border PrintWindowBorder(childX, childY, childWidth, childHeight, ConsoleColor.DarkCyan, "Available Stations"); string hint = string.Format(" {0}/{1} ", selectedIndex + 1, stations.Count); PrintText(hint, childX + 2, childY + childHeight, ConsoleColor.DarkCyan); // right align PrintText("Select a new station, or [ESC]", childX + childWidth - 32, childY + childHeight - 1, ConsoleColor.DarkGray); // determine if the selected item is visible if (selectedIndex < scrollTop) { scrollTop = selectedIndex; } while (selectedIndex >= scrollTop + lineCount) { scrollTop++; } // print menu items for (int line = 0; line < lineCount; line++) { if (line + scrollTop >= stations.Count) { break; } PandoraStation currStation = stations[line + scrollTop]; int songNumber = line + scrollTop + 1; string printLine = String.Format("{0}: {1}", songNumber.ToString().PadLeft(2), currStation.Name); if (printLine.Length > innerWidth) { printLine = printLine.Substring(0, innerWidth); } ConsoleColor backgroundColor = ConsoleColor.Black; if (line + scrollTop == selectedIndex) { backgroundColor = ConsoleColor.DarkCyan; } PrintText(printLine.PadRight(innerWidth), childX + 2, childY + 2 + line, ConsoleColor.Gray, backgroundColor); } needStationMenuUpdate = false; } key = Console.ReadKey(true); if (key.Key == ConsoleKey.Escape) { modalWindowDisplayed = false; needUIUpdate = true; return(null); } if (key.Key == ConsoleKey.DownArrow) { selectedIndex++; if (selectedIndex > stations.Count - 1) { selectedIndex = stations.Count - 1; } else { needStationMenuUpdate = true; } } if (key.Key == ConsoleKey.UpArrow) { selectedIndex--; if (selectedIndex < 0) { selectedIndex = 0; } else { needStationMenuUpdate = true; } } } while (key.Key != ConsoleKey.Enter); return(stations[selectedIndex]); } finally { modalWindowDisplayed = false; } }
public void RunMainLoop() { ConsoleKeyInfo choice; PrintUI(); do { while (!Console.KeyAvailable) { if (needUIUpdate) { PrintUI(); needUIUpdate = false; } PrintSongPosition(); Thread.Sleep(100); } try { choice = Console.ReadKey(true); switch (char.ToLower(choice.KeyChar)) { case 'x': case 'q': return; case ' ': if (player.IsPlaying()) { player.Stop(); } else { player.Play(); } break; case 'n': PlayNext(true); break; case 's': PandoraStation newStation = ShowStationChooser(); if (newStation != null && newStation != musicBox.CurrentStation) { ShowWaitIcon(true); musicBox.CurrentStation = newStation; Settings.Default.LastStationId = musicBox.CurrentStation.Id; Settings.Default.Save(); PlayNext(false); ShowWaitIcon(false); } break; case '?': case 'h': showHelp = !showHelp; modalWindowDisplayed = showHelp; needUIUpdate = true; break; case '+': musicBox.RateSong(musicBox.CurrentSong, PandoraRating.Love); needUIUpdate = true; break; case '-': musicBox.RateSong(musicBox.CurrentSong, PandoraRating.Hate); PlayNext(true); break; case 'b': musicBox.TemporarilyBanSong(musicBox.CurrentSong); PlayNext(true); break; case 'p': PrintText(" ", 0, 7); Settings.Default.DisplayPosition = !Settings.Default.DisplayPosition; Settings.Default.Save(); break; } if (choice.Key == ConsoleKey.Escape) { if (showHelp) { showHelp = false; modalWindowDisplayed = false; needUIUpdate = true; } else { return; } } if (choice.Key == ConsoleKey.UpArrow) { player.Volume += 0.01; PrintVolume(); Settings.Default.Volume = player.Volume; Settings.Default.Save(); } if (choice.Key == ConsoleKey.DownArrow) { player.Volume -= 0.01; PrintVolume(); Settings.Default.Volume = player.Volume; Settings.Default.Save(); } if (choice.Key == ConsoleKey.RightArrow) { PlayNext(true); } } catch (PandoraException e) { PrintText(e.ErrorCode + ": " + e.Message, 0, Console.WindowHeight - 5, ConsoleColor.Red); } } while (true); }