public static async Task UpdatePlaylist(IPlaylist playlist, IChannel selectedChannel) { switch (playlist.Site) { case SiteType.YouTube: HashSet <string> dbids = selectedChannel.ChannelItems.Select(x => x.ID).ToHashSet(); List <string> plitemsIdsNet = await YouTubeSite.GetPlaylistItemsIdsListNetAsync(playlist.ID, 0).ConfigureAwait(false); List <string> ids = plitemsIdsNet.Where(netid => !playlist.PlItems.Contains(netid)).ToList(); if (!ids.Any()) { return; } var lstInDb = new List <string>(); var lstNoInDb = new List <string>(); foreach (string id in ids) { if (dbids.Contains(id)) { lstInDb.Add(id); } else { lstNoInDb.Add(id); } } foreach (string id in lstInDb) { await db.UpdatePlaylistAsync(playlist.ID, id, selectedChannel.ID).ConfigureAwait(false); playlist.PlItems.Add(id); } IEnumerable <List <string> > chanks = lstNoInDb.SplitList(); foreach (List <string> list in chanks) { List <VideoItemPOCO> res = await YouTubeSite.GetVideosListByIdsAsync(list).ConfigureAwait(false); // получим скопом foreach (IVideoItem vi in res.Select(poco => VideoItemFactory.CreateVideoItem(poco, SiteType.YouTube))) { vi.SyncState = SyncState.Added; if (vi.ParentID == selectedChannel.ID) { selectedChannel.AddNewItem(vi); await db.InsertItemAsync(vi).ConfigureAwait(false); await db.UpdatePlaylistAsync(playlist.ID, vi.ID, selectedChannel.ID).ConfigureAwait(false); } else { selectedChannel.ChannelItems.Add(vi); } playlist.PlItems.Add(vi.ID); } } break; } }
private static async Task InsertNewItems(IEnumerable <string> trueIds, IChannel channel, string playlistId = null, ICollection <string> dbIds = null, Action <IVideoItem, object> stateAction = null) { List <VideoItemPOCO> res = await YouTubeSite.GetVideosListByIdsAsync(trueIds).ConfigureAwait(true); // получим скопом IEnumerable <IVideoItem> result = res.Select(poco => VideoItemFactory.CreateVideoItem(poco, channel.Site, false, SyncState.Added)) .Reverse() .Where(vi => vi.ParentID == channel.ID) .ToList(); await db.InsertChannelItemsAsync(result).ConfigureAwait(false); foreach (IVideoItem vi in result) { channel.AddNewItem(vi); stateAction?.Invoke(vi, SyncState.Added); if (playlistId != null) { await db.UpdatePlaylistAsync(playlistId, vi.ID, channel.ID).ConfigureAwait(false); } if (dbIds == null) { continue; } if (!dbIds.Contains(vi.ID)) { dbIds.Add(vi.ID); } } }
public static async void FillChannelItemsFromDbAsync(IChannel channel, int basePage, List <string> excepted = null) { List <VideoItemPOCO> items = await Task.Run(() => db.GetChannelItemsBaseAsync(channel.ID, basePage, excepted)).ConfigureAwait(true); foreach (VideoItemPOCO poco in items) { IVideoItem vi = VideoItemFactory.CreateVideoItem(poco, channel.Site); vi.IsHasLocalFileFound(channel.DirPath); channel.AddNewItem(vi, false, false); } channel.RefreshView("Timestamp"); }
private async void ReloadFilteredLists(object state) { ChannelItems.Clear(); if (state is WatchState) { var st = (WatchState)state; List <IVideoItem> readyel; List <string> readyAddedIds; List <string> notreadyList; List <VideoItemPOCO> items; switch (st) { case WatchState.Watched: readyel = allchannels.SelectMany(x => x.ChannelItems).Where(y => y.WatchState == WatchState.Watched).ToList(); foreach (IVideoItem item in readyel.Where(item => !watchedList.Contains(item))) { watchedList.Add(item); } readyAddedIds = readyel.Select(x => x.ID).ToList(); foreach (string id in watchedList.Select(x => x.ID).Where(id => !readyAddedIds.Contains(id))) { readyAddedIds.Add(id); } notreadyList = watchedListIds.Except(readyAddedIds).ToList(); if (notreadyList.Any()) { items = await db.GetItemsByIdsAndState(WatchState.Watched, notreadyList).ConfigureAwait(false); foreach (VideoItemPOCO poco in items) { IChannel parent = allchannels.First(x => x.ID == poco.ParentID); IVideoItem item = VideoItemFactory.CreateVideoItem(poco, parent.Site); parent.AddNewItem(item, false); parent.IsHasNewFromSync = true; watchedList.Add(item); } } watchedList.OrderBy(x => x.Timestamp).ForEach(x => AddNewItem(x)); break; case WatchState.Planned: readyel = allchannels.SelectMany(x => x.ChannelItems).Where(y => y.WatchState == WatchState.Planned).ToList(); foreach (IVideoItem item in readyel.Where(item => !plannedList.Contains(item))) { plannedList.Add(item); } readyAddedIds = readyel.Select(x => x.ID).ToList(); foreach (string id in plannedList.Select(x => x.ID).Where(id => !readyAddedIds.Contains(id))) { readyAddedIds.Add(id); } notreadyList = plannedListIds.Except(readyAddedIds).ToList(); if (notreadyList.Any()) { items = await db.GetItemsByIdsAndState(WatchState.Planned, notreadyList).ConfigureAwait(false); foreach (VideoItemPOCO poco in items) { IChannel parent = allchannels.First(x => x.ID == poco.ParentID); IVideoItem item = VideoItemFactory.CreateVideoItem(poco, parent.Site); parent.AddNewItem(item, false); parent.IsHasNewFromSync = true; plannedList.Add(item); } } plannedList.OrderBy(x => x.Timestamp).ForEach(x => AddNewItem(x)); break; } } else if (state is SyncState) { var st = (SyncState)state; switch (st) { case SyncState.Added: List <IVideoItem> readyel = allchannels.SelectMany(x => x.ChannelItems).Where(y => y.SyncState == SyncState.Added).ToList(); foreach (IVideoItem item in readyel.Where(item => !addedList.Contains(item))) { addedList.Add(item); } List <string> readyAddedIds = readyel.Select(x => x.ID).ToList(); foreach (string id in addedList.Select(x => x.ID).Where(id => !readyAddedIds.Contains(id))) { readyAddedIds.Add(id); } List <string> notreadyList = addedListIds.Except(readyAddedIds).ToList(); if (notreadyList.Any()) { List <VideoItemPOCO> items = await db.GetItemsByIdsAndState(SyncState.Added, notreadyList).ConfigureAwait(false); foreach (VideoItemPOCO poco in items) { IChannel parent = allchannels.First(x => x.ID == poco.ParentID); IVideoItem item = VideoItemFactory.CreateVideoItem(poco, parent.Site); parent.AddNewItem(item, false); parent.IsHasNewFromSync = true; addedList.Add(item); } } addedList.OrderBy(x => x.ParentID).ThenBy(y => y.Timestamp).ForEach(x => AddNewItem(x)); break; } } }
public static async Task SyncChannelPlaylistsAsync(IChannel channel) { switch (channel.Site) { case SiteType.YouTube: List <PlaylistPOCO> fbres = await YouTubeSite.GetChannelPlaylistsNetAsync(channel.ID).ConfigureAwait(true); var pls = new List <IPlaylist>(); pls.AddRange(fbres.Select(poco => PlaylistFactory.CreatePlaylist(poco, channel.Site))); if (pls.Any()) { List <string> ids = await db.GetChannelItemsIdListDbAsync(channel.ID, 0, 0).ConfigureAwait(false); await db.DeleteChannelPlaylistsAsync(channel.ID).ConfigureAwait(false); channel.ChannelPlaylists.Clear(); channel.PlaylistCount = pls.Count; foreach (IPlaylist playlist in pls) { await db.InsertPlaylistAsync(playlist).ConfigureAwait(false); List <string> plv = await YouTubeSite.GetPlaylistItemsIdsListNetAsync(playlist.ID, 0).ConfigureAwait(true); foreach (string id in plv) { if (ids.Contains(id)) { await db.UpdatePlaylistAsync(playlist.ID, id, channel.ID).ConfigureAwait(false); if (!playlist.PlItems.Contains(id)) { playlist.PlItems.Add(id); } } else { IVideoItem item = await VideoItemFactory.GetVideoItemNetAsync(id, channel.Site).ConfigureAwait(false); if (item.ParentID != channel.ID) { continue; } channel.AddNewItem(item); await db.InsertItemAsync(item).ConfigureAwait(false); await db.UpdatePlaylistAsync(playlist.ID, item.ID, channel.ID).ConfigureAwait(false); if (!playlist.PlItems.Contains(id)) { playlist.PlItems.Add(id); } } } channel.ChannelPlaylists.Add(playlist); } } break; } }