private async Task search(IClientServiceRequest request, String searchInfo, bool isNextPage, CancellationToken token) { SearchListResponse searchResponse = null; PlaylistItemListResponse playlistItemResponse = null; PlaylistListResponse playlistResponse = null; SearchResource.ListRequest searchRequest = request as SearchResource.ListRequest; PlaylistItemsResource.ListRequest playlistItemsRequest = request as PlaylistItemsResource.ListRequest; PlaylistsResource.ListRequest playlistRequest = request as PlaylistsResource.ListRequest; List <YoutubeItem> items = new List <YoutubeItem>(); int relevance; if (isNextPage) { if (NextPageToken == null) { // Final page return; } if (searchRequest != null) { searchRequest.PageToken = NextPageToken; } else if (playlistItemsRequest != null) { playlistItemsRequest.PageToken = NextPageToken; } else if (playlistRequest != null) { playlistRequest.PageToken = NextPageToken; } relevance = MediaState.UIMediaCollection.Count; } else { MediaState.clearUIState(searchInfo, DateTime.Now, MediaStateType.SearchResult); CurrentQuery = request; relevance = 0; } // Call the search.list method to retrieve results matching the specified query term. if (searchRequest != null) { searchResponse = await searchRequest.ExecuteAsync(token); NextPageToken = searchResponse.NextPageToken; foreach (SearchResult searchResult in searchResponse.Items) { YoutubeItem newItem = null; switch (searchResult.Id.Kind) { case "youtube#video": newItem = new YoutubeVideoItem(searchResult, relevance); break; case "youtube#channel": newItem = new YoutubeChannelItem(searchResult, relevance); break; case "youtube#playlist": newItem = new YoutubePlaylistItem(searchResult, relevance); break; default: break; } if (newItem == null || MediaState.UIMediaCollection.Contains(newItem)) { continue; } items.Add(newItem); relevance++; } } if (playlistItemsRequest != null) { playlistItemResponse = await playlistItemsRequest.ExecuteAsync(token); NextPageToken = playlistItemResponse.NextPageToken; foreach (PlaylistItem playlistItem in playlistItemResponse.Items) { YoutubeVideoItem newItem = new YoutubeVideoItem(playlistItem, relevance); items.Add(newItem); relevance++; } } if (playlistRequest != null) { playlistResponse = await playlistRequest.ExecuteAsync(token); NextPageToken = playlistResponse.NextPageToken; foreach (Playlist playlist in playlistResponse.Items) { YoutubePlaylistItem newItem = new YoutubePlaylistItem(playlist, relevance); if (!items.Contains(newItem)) { items.Add(newItem); } relevance++; } } // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. MediaState.addUIState(items); }
public YoutubeViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) { TokenSource = new CancellationTokenSource(); RegionManager = regionManager; EventAggregator = eventAggregator; NrColumns = 4; MediaState = new MediaState(); MediaStateCollectionView = new YoutubeCollectionView(MediaState); MediaState.clearUIState("Empty", DateTime.Now, MediaStateType.SearchResult); MediaStateCollectionView.SelectionChanged += mediaStateCollectionView_SelectionChanged; ViewCommand = new Command <SelectableMediaItem>((selectableItem) => { if (selectableItem.Item.Metadata == null) { return; } if (selectableItem.Item is YoutubeVideoItem) { YoutubeVideoItem item = selectableItem.Item as YoutubeVideoItem; if (item.IsEmbeddedOnly) { Process.Start("https://www.youtube.com/watch?v=" + item.VideoId); } else { YoutubeVideoStreamedItem video, audio; item.getStreams(out video, out audio, (int)Properties.Settings.Default.MaxPlaybackResolution); Shell.ShellViewModel.navigateToVideoView(video, null, audio); } } }); ViewChannelCommand = new AsyncCommand <SelectableMediaItem>(async(selectableItem) => { if (selectableItem.Item.Metadata == null) { return; } YoutubeItem item = selectableItem.Item as YoutubeItem; SearchResource.ListRequest searchListRequest = Youtube.Search.List("snippet"); searchListRequest.ChannelId = item.ChannelId; searchListRequest.MaxResults = YoutubeSearchViewModel.maxResults; searchListRequest.Order = Google.Apis.YouTube.v3.SearchResource.ListRequest.OrderEnum.Date; MediaStateCollectionView.FilterModes.MoveCurrentToFirst(); await searchAsync(searchListRequest, item.ChannelTitle, false); }); ViewPlaylistCommand = new AsyncCommand <SelectableMediaItem>(async(selectableItem) => { if (selectableItem.Item.Metadata == null) { return; } YoutubePlaylistItem item = selectableItem.Item as YoutubePlaylistItem; PlaylistItemsResource.ListRequest searchListRequest = Youtube.PlaylistItems.List("snippet"); searchListRequest.PlaylistId = item.PlaylistId; searchListRequest.MaxResults = YoutubeSearchViewModel.maxResults; MediaStateCollectionView.FilterModes.MoveCurrentToFirst(); await searchAsync(searchListRequest, item.Name, false); }); SubscribeCommand = new Command <SelectableMediaItem>((selectableItem) => { YoutubeChannelItem item = selectableItem.Item as YoutubeChannelItem; EventAggregator.GetEvent <AddFavoriteChannelEvent>().Publish(item); }); DownloadCommand = new AsyncCommand <SelectableMediaItem>(async(selectableItem) => { List <MediaItem> items = MediaStateCollectionView.getSelectedItems(); if (items.Count == 0) { items.Add(selectableItem.Item); } String outputPath = null; switch (YoutubePlugin.Properties.Settings.Default.VideoSaveMode) { case MediaViewer.Infrastructure.Constants.SaveLocation.Current: { outputPath = MediaFileWatcher.Instance.Path; break; } case MediaViewer.Infrastructure.Constants.SaveLocation.Ask: { DirectoryPickerView directoryPicker = new DirectoryPickerView(); directoryPicker.DirectoryPickerViewModel.InfoString = "Select Output Directory"; directoryPicker.DirectoryPickerViewModel.SelectedPath = MediaFileWatcher.Instance.Path; if (directoryPicker.ShowDialog() == false) { return; } outputPath = directoryPicker.DirectoryPickerViewModel.SelectedPath; break; } case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed: { outputPath = YoutubePlugin.Properties.Settings.Default.FixedDownloadPath; break; } default: break; } CancellableOperationProgressView progressView = new CancellableOperationProgressView(); DownloadProgressViewModel vm = new DownloadProgressViewModel(); progressView.DataContext = vm; progressView.Show(); vm.OkCommand.IsExecutable = false; vm.CancelCommand.IsExecutable = true; await Task.Factory.StartNew(() => { vm.startDownload(outputPath, items); }); vm.OkCommand.IsExecutable = true; vm.CancelCommand.IsExecutable = false; }); LoadNextPageCommand = new AsyncCommand(async() => { await searchAsync(CurrentQuery, "", true); }); SelectAllCommand = new Command(() => { MediaStateCollectionView.selectAll(); }, false); DeselectAllCommand = new Command(() => { MediaStateCollectionView.deselectAll(); }); ShutdownCommand = new Command(() => { Properties.Settings.Default.Save(); }); MediaState.UIMediaCollection.IsLoadingChanged += UIMediaCollection_IsLoadingChanged; MediaViewer.Model.Global.Commands.GlobalCommands.ShutdownCommand.RegisterCommand(ShutdownCommand); setupViews(); EventAggregator.GetEvent <SearchEvent>().Subscribe(searchEvent); SearchTask = null; }
async Task doSearch(ImageSearchQuery state, int imageOffset) { if (String.IsNullOrEmpty(state.Query) || String.IsNullOrWhiteSpace(state.Query)) { return; } var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri)); // Configure bingContainer to use your credentials. bingContainer.Credentials = new NetworkCredential(BingAccountKey.accountKey, BingAccountKey.accountKey); // Build the query. String imageFilters = null; if (!state.Size.Equals(size[0])) { imageFilters = "Size:" + state.Size; } if (!state.Layout.Equals(layout[0])) { if (imageFilters != null) { imageFilters += "+"; } imageFilters += "Aspect:" + state.Layout; } if (!state.Type.Equals(type[0])) { if (imageFilters != null) { imageFilters += "+"; } imageFilters += "Style:" + state.Type; } if (!state.People.Equals(type[0])) { if (imageFilters != null) { imageFilters += "+"; } imageFilters += "Face:" + state.People; } if (!state.Color.Equals(type[0])) { if (imageFilters != null) { imageFilters += "+"; } imageFilters += "Color:" + state.Color; } var imageQuery = bingContainer.Image(Query, null, null, state.SafeSearch, state.GeoTag.LatDecimal, state.GeoTag.LonDecimal, imageFilters); imageQuery = imageQuery.AddQueryOption("$top", 50); imageQuery = imageQuery.AddQueryOption("$skip", imageOffset); IEnumerable <Bing.ImageResult> imageResults = await Task.Factory.FromAsync <IEnumerable <Bing.ImageResult> >(imageQuery.BeginExecute(null, null), imageQuery.EndExecute); if (imageOffset == 0) { MediaState.clearUIState(Query, DateTime.Now, MediaStateType.SearchResult); } List <MediaItem> results = new List <MediaItem>(); int relevance = imageOffset; MediaState.UIMediaCollection.EnterReadLock(); foreach (var image in imageResults) { MediaItem item = new ImageResultItem(image, relevance); if (MediaState.UIMediaCollection.Contains(item)) { continue; } results.Add(item); relevance++; } MediaState.UIMediaCollection.ExitReadLock(); MediaState.addUIState(results); }