void NewOrder() { var previousEntry = PlaylistOrder == null ? null : PlaylistOrder.Last(); PlaylistOrder = Playlist.OrderBy(_ => Guid.NewGuid()).ToList(); if (PlaylistOrder.First() == previousEntry) { // 次周最初の要素が直前の要素とかぶった場合はその要素を末尾に持ってきて連続再生を防ぐ PlaylistOrder.RemoveAt(0); PlaylistOrder.Add(previousEntry); } Index = 0; }
// Video search overload for playlists. public IList<Video> GetPlaylistVideos(string playlistId, bool includeHowLongSincePublished = false, int resultsCount = 10, int startResultsIndex = 1, PlaylistOrder orderBy = PlaylistOrder.position, bool allowRestrictedLocation = false, bool explicitlyEmbeddableOnly = false) { return GetVideos(playlistId, true, includeHowLongSincePublished, resultsCount, startResultsIndex, Enum.GetName(typeof(PlaylistOrder), orderBy), allowRestrictedLocation, explicitlyEmbeddableOnly); }
/// <summary> /// Returns a Playlist object with or without videos, depending on YouTube request settings. /// </summary> private Playlist DeserializeJsonPlaylist(string jsonObjectString, bool withVideos, bool includeHowLongSincePublished, int videoResultsCount, PlaylistOrder orderBy) { var requestedPlaylist = JsonConvert.DeserializeObject<Playlist>(jsonObjectString); // Include playlist videos. if (withVideos) { requestedPlaylist.Entries = GetPlaylistVideos(requestedPlaylist.Id, includeHowLongSincePublished, videoResultsCount, 1, orderBy); } return requestedPlaylist; }
public IList<Playlist> GetChannelPlaylists(string userName, string playlistTitle = "", bool includeHowLongSincePublished = false, int resultsCount = 50, int startResultsIndex = 1, bool withVideos = true, int videoResultsCount = 10, PlaylistOrder orderBy = PlaylistOrder.position) { resultsCount = CorrectRequestResultsCount(resultsCount); var requestUri = "users/" + userName + "/playlists?alt=json" + "&max-results=" + resultsCount + "&start-index=" + startResultsIndex; var jsonObject = GetJsonRequestResults(requestUri); jsonObject = jsonObject != null ? jsonObject.SelectToken("feed").SelectToken("entry") : null; if (jsonObject != null) { var requestedPlaylists = new List<Playlist>(); // If looking for specific playlist, select entry with correct title. if (playlistTitle != "") { jsonObject = jsonObject.FirstOrDefault(entry => (string) entry.SelectToken("title").First == playlistTitle); // If no playlist with that title found, fetch and search further playlist results. if (jsonObject == null) { return GetChannelPlaylists(userName, playlistTitle, includeHowLongSincePublished, resultsCount, startResultsIndex + resultsCount, withVideos, videoResultsCount, orderBy); } requestedPlaylists.Add(DeserializeJsonPlaylist(jsonObject.ToString(), withVideos, includeHowLongSincePublished, videoResultsCount, orderBy)); } else { foreach (var entry in jsonObject) { requestedPlaylists.Add(DeserializeJsonPlaylist(entry.ToString(), withVideos, includeHowLongSincePublished, videoResultsCount, orderBy)); } } return requestedPlaylists; } // No entries found for this request. return new List<Playlist>(); }