public async void LoadAsync(PlexViewModel viewModel) { version = settings.Values[KEY_VERSION] as int? ?? 1; this.viewModel = viewModel; foreach (string showKey in settings.Containers.Keys) { ApplicationDataContainer showContainer = settings.Containers[showKey]; Debug.WriteLine($"Show: {showContainer.Values["name"] as string}|{showContainer.Values["showPath"] as string}"); Show show = new Show(showKey, showContainer.Values["name"] as string, showContainer.Values["showPath"] as string); StorageFolder showFolder; try { showFolder = await StorageFolder.GetFolderFromPathAsync(show.ShowPath); } catch (FileNotFoundException) { settings.DeleteContainer(showKey); continue; } catch (UnauthorizedAccessException) { settings.DeleteContainer(showKey); continue; } viewModel.Shows.Add(show); SortedList <int, Season> seasons = new SortedList <int, Season>(); Dictionary <string, HashSet <string> > filenames = new Dictionary <string, HashSet <string> >(); foreach (string seasonKey in showContainer.Containers.Keys) { ApplicationDataContainer seasonContainer = showContainer.Containers[seasonKey]; Debug.WriteLine($"Season: {seasonContainer.Values["number"] as int? ?? -1}|{seasonKey}"); Season season = new Season(seasonContainer.Values["number"] as int? ?? -1, seasonKey) { Show = show }; StorageFolder seasonFolder; try { seasonFolder = await showFolder.GetFolderAsync(season.GetFolderName()); } catch (FileNotFoundException) { showContainer.DeleteContainer(seasonKey); continue; } filenames.Add(seasonFolder.Name, new HashSet <string>()); SortedList <int, Episode> episodes = new SortedList <int, Episode>(); foreach (string episodeKey in seasonContainer.Containers.Keys) { ApplicationDataContainer episodeContainer = seasonContainer.Containers[episodeKey]; Debug.WriteLine($"Episode: {episodeContainer.Values["number"] as int? ?? -1}|{episodeContainer.Values["episodePath"] as string}|{episodeContainer.Values["originalFilename"] as string}"); Episode episode = new Episode(episodeContainer.Values["number"] as int? ?? -1, episodeContainer.Values["episodePath"] as string, episodeContainer.Values["originalFilename"] as string) { ManuallySet = episodeContainer.Values["manuallySet"] as bool? ?? false }; StorageFile file; try { file = await seasonFolder.GetFileAsync(episode.Filename); } catch (FileNotFoundException) { seasonContainer.DeleteContainer(episodeKey); continue; } filenames[season.Name].Add(episode.Filename); if (!episodes.ContainsKey(episode.Number)) { episodes.Add(episode.Number, episode); } } foreach (Episode episode in episodes.Values) { season.AddEpisode(episode); } seasons.Add(season.Number, season); } foreach (Season season in seasons.Values) { show.AddSeason(season); } // check for unsorted eps QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, MainPage.VideoExtensions) { FolderDepth = FolderDepth.Shallow }; StorageFileQueryResult queryResult = showFolder.CreateFileQueryWithOptions(queryOptions); IReadOnlyList <StorageFile> unsortedEpisodeFiles = await queryResult.GetFilesAsync(); foreach (StorageFile episodeFile in unsortedEpisodeFiles) { if (IsEpisodeNamedCorrectly(episodeFile.Name, show.Name)) { string correctFolderName = GetCorrectFolderName(episodeFile.Name); Episode episode = new Episode(GetEpisodeNumber(episodeFile.Name), episodeFile.Path, episodeFile.Name); StorageFolder correctSeasonFolder; try { correctSeasonFolder = await showFolder.GetFolderAsync(correctFolderName); } catch (FileNotFoundException) { correctSeasonFolder = await showFolder.CreateFolderAsync(correctFolderName); } // move to correct folder await episodeFile.MoveAsync(correctSeasonFolder); // restore to season int seasonNumber = GetSeasonNumber(episode.Filename); if (!seasons.ContainsKey(seasonNumber)) { Season newSeason = new Season(seasonNumber, correctFolderName); seasons.Add(seasonNumber, newSeason); show.InsertSeason(newSeason); showContainer.CreateContainer(correctSeasonFolder.Name, ApplicationDataCreateDisposition.Always); } Season season = seasons[seasonNumber]; int i; for (i = 0; i < season.NumberEpisodes; i++) { if (episode.Number < season.Episodes[i].Number) { break; } } season.InsertEpisode(i, episode); // save ep data ApplicationDataContainer episodeContainer = showContainer.Containers[correctSeasonFolder.Name].CreateContainer(episode.Filename, ApplicationDataCreateDisposition.Always); episodeContainer.Values["number"] = episode.Number; episodeContainer.Values["manuallySet"] = episode.ManuallySet; episodeContainer.Values["episodePath"] = episode.EpisodePath; episodeContainer.Values["originalFilename"] = episode.OriginalFilename; } else { Debug.WriteLine($"UnsortedEpisode: -1 {episodeFile.Path}"); Episode episode = new Episode(-1, episodeFile.Path); show.UnsortedSeason.AddEpisode(episode); } } IReadOnlyList <StorageFolder> folders = await showFolder.GetFoldersAsync(); foreach (StorageFolder seasonFolder in folders) { if (!filenames.ContainsKey(seasonFolder.Name)) { filenames.Add(seasonFolder.Name, new HashSet <string>()); } queryResult = seasonFolder.CreateFileQueryWithOptions(queryOptions); IReadOnlyList <StorageFile> episodeFiles = await queryResult.GetFilesAsync(); foreach (StorageFile episodeFile in episodeFiles) { if (!filenames[seasonFolder.Name].Contains(episodeFile.Name)) { if (IsEpisodeNamedCorrectly(episodeFile.Name, show.Name)) { string correctFolderName = GetCorrectFolderName(episodeFile.Name); Episode episode = new Episode(GetEpisodeNumber(episodeFile.Name), episodeFile.Path, episodeFile.Name); StorageFolder correctSeasonFolder; if (correctFolderName != seasonFolder.Name) { try { correctSeasonFolder = await showFolder.GetFolderAsync(correctFolderName); } catch (FileNotFoundException) { correctSeasonFolder = await showFolder.CreateFolderAsync(correctFolderName); } // move to correct folder await episodeFile.MoveAsync(correctSeasonFolder); } else { correctSeasonFolder = seasonFolder; } // restore to season int seasonNumber = GetSeasonNumber(episode.Filename); if (!seasons.ContainsKey(seasonNumber)) { Season newSeason = new Season(seasonNumber, correctFolderName); seasons.Add(seasonNumber, newSeason); show.InsertSeason(newSeason); showContainer.CreateContainer(correctSeasonFolder.Name, ApplicationDataCreateDisposition.Always); } Season season = seasons[seasonNumber]; int i; for (i = 0; i < season.NumberEpisodes; i++) { if (episode.Number < season.Episodes[i].Number) { break; } } season.InsertEpisode(i, episode); // save ep data ApplicationDataContainer episodeContainer = showContainer.Containers[correctSeasonFolder.Name].CreateContainer(Path.GetFileNameWithoutExtension(episode.Filename), ApplicationDataCreateDisposition.Always); episodeContainer.Values["number"] = episode.Number; episodeContainer.Values["manuallySet"] = episode.ManuallySet; episodeContainer.Values["episodePath"] = episode.EpisodePath; episodeContainer.Values["originalFilename"] = episode.OriginalFilename; } else { Debug.WriteLine($"UnsortedEpisode: -1 {episodeFile.Path}"); Episode episode = new Episode(-1, episodeFile.Path); show.UnsortedSeason.AddEpisode(episode); } } } } } Debug.WriteLine("done loading"); }
private async void OpenShow_ClickAsync(object sender, RoutedEventArgs e) { FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Desktop }; picker.FileTypeFilter.Add("*"); StorageFolder folder = await picker.PickSingleFolderAsync(); if (folder != null) { Show show = new Show(null, folder.Name, folder.Path); // add to list of places allowed Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace(show.Id, folder); QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, VideoExtensions) { FolderDepth = FolderDepth.Shallow }; StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(queryOptions); IReadOnlyList <StorageFile> unsortedEpisodeFiles = await queryResult.GetFilesAsync(); foreach (StorageFile unsortedEpisodeFile in unsortedEpisodeFiles) { Episode episode = new Episode(-1, unsortedEpisodeFile.Path); show.UnsortedSeason.AddEpisode(episode); } IReadOnlyList <StorageFolder> folders = await folder.GetFoldersAsync(); foreach (StorageFolder seasonFolder in folders) { Season season; if (IsCorrectSeasonName(seasonFolder.Name)) { season = new Season(GetSeasonNumberFromFolder(seasonFolder.Name), seasonFolder.Name); show.AddSeason(season); } else { season = show.UnsortedSeason; } queryResult = seasonFolder.CreateFileQueryWithOptions(queryOptions); IReadOnlyList <StorageFile> episodeFiles = await queryResult.GetFilesAsync(); foreach (StorageFile episodeFile in episodeFiles) { Episode episode = new Episode(-1, episodeFile.Path); season.AddEpisode(episode); } } PlexViewModel.Shows.Add(show); } }