private void TrackChanged(object sender, EventArgs args) { NowPlayingService nowPlaying = ServiceManager.Get <NowPlayingService>(); PubSubItem itemElement = new PubSubItem(m_Account.Client.Document); itemElement.SetAttribute("id", "current"); Tune tune = new Tune(m_Account.Client.Document); itemElement.AppendChild(tune); if (nowPlaying.IsPlaying) { tune.Artist = nowPlaying.CurrentTrackArtist; tune.Length = nowPlaying.CurrentTrackLength; tune.Rating = nowPlaying.CurrentTrackRating; tune.Source = nowPlaying.CurrentTrackSource; tune.Title = nowPlaying.CurrentTrackTitle; tune.Track = nowPlaying.CurrentTrackNumber; tune.Uri = nowPlaying.CurrentTrackUri; } m_Account.GetFeature <PersonalEventing>().Publish(Namespace.Tune, itemElement); }
/// <summary> /// Process returns a readonly list of now playing media items, filtered by optional parameters /// </summary> public void Process(UriWrapper uri, IHttpProcessor processor, User user) { // Get NowPlayingService instance NowPlayingService nowPlayingService = (NowPlayingService)ServiceManager.GetInstance("nowplaying"); // Ensure service is running if (nowPlayingService == null) { processor.WriteJson(new NowPlayingResponse("NowPlayingService is not running!", null)); return; } // Store list of now playing objects IList <NowPlaying> nowPlaying = nowPlayingService.Playing; // Filter by user name if (uri.Parameters.ContainsKey("user")) { nowPlaying = nowPlaying.Where(x => x.User.UserName == uri.Parameters["user"]).ToList(); } // Filter by client name if (uri.Parameters.ContainsKey("client")) { nowPlaying = nowPlaying.Where(x => x.User.CurrentSession.ClientName == uri.Parameters["client"]).ToList(); } // Return list of now playing items processor.WriteJson(new NowPlayingResponse(null, nowPlaying)); return; }
/// <summary> /// Process records play stats for artists, albums, songs /// </summary> public void Process(UriWrapper uri, IHttpProcessor processor, User user) { // Ensure an event is present if (!uri.Parameters.ContainsKey("event")) { processor.WriteJson(new StatsResponse("Please specify an event parameter with comma separated list of events")); return; } // Split events into id, stat type, UNIX timestamp triples string[] events = uri.Parameters["event"].Split(','); // Ensure data sent if (events.Length < 1) { // Report empty data list processor.WriteJson(new StatsResponse("Event data list is empty")); return; } // Ensure events are in triples if (events.Length % 3 != 0) { processor.WriteJson(new StatsResponse("Event data list must be in comma-separated triples of form itemId,statType,timestamp")); return; } // Iterate all events for (int i = 0; i <= events.Length - 3; i += 3) { // Store item ID, stat type, and UNIX timestamp string itemId = events[i]; string statType = events[i + 1]; string timeStamp = events[i + 2]; // Initialize to null defaults int itemIdInt = -1; StatType statTypeEnum = StatType.Unknown; long timeStampLong = -1; // Perform three checks for valid item ID, stat type, UNIX timestamp bool success = Int32.TryParse(itemId, out itemIdInt); if (success) { success = Enum.TryParse <StatType>(statType, true, out statTypeEnum); } if (success) { success = Int64.TryParse(timeStamp, out timeStampLong); } if (success) { // If all three are successful, generate an item type from the ID ItemType itemType = Injection.Kernel.Get <IItemRepository>().ItemTypeForItemId(itemIdInt); // Case: type is song, stat is playcount if ((itemType == ItemType.Song) && (statTypeEnum == StatType.PLAYED)) { // Also record a play for the artist, album, and folder Song song = Injection.Kernel.Get <ISongRepository>().SongForId(itemIdInt); // Trigger now playing service if available NowPlayingService nowPlaying = (NowPlayingService)ServiceManager.GetInstance("nowplaying"); if (nowPlaying != null) { nowPlaying.Register(user, song, timeStampLong); } if ((object)song.AlbumId != null) { Injection.Kernel.Get <IStatRepository>().RecordStat((int)song.AlbumId, statTypeEnum, timeStampLong); } if ((object)song.ArtistId != null) { Injection.Kernel.Get <IStatRepository>().RecordStat((int)song.ArtistId, statTypeEnum, timeStampLong); } if ((object)song.FolderId != null) { Injection.Kernel.Get <IStatRepository>().RecordStat((int)song.FolderId, statTypeEnum, timeStampLong); } } // Record stats for the generic item Injection.Kernel.Get <IStatRepository>().RecordStat(itemIdInt, statTypeEnum, timeStampLong); } } // After all stat iterations, return a successful response processor.WriteJson(new StatsResponse(null)); }