Ejemplo n.º 1
0
        private async void LoadingView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_url == null)
                {
                    throw new NullReferenceException(nameof(_url));
                }
                var videos = await VideoService.GetVideosAsync(_url);

                IReadOnlyList <VideoDownloadOption>?videoOptions;
                string?title = default;

                if (videos.Length == 1)
                {
                    videoOptions = await VideoService.GetVideoDownloadOptionsAsync(_url);
                }
                else
                {
                    videoOptions = YoutubeVideoService.GetOptionPlaylist().Reverse().ToArray();
                    title        = await VideoService.GetPlaylistTitle(_url);
                }

                DialogHost.Close("MainDialog", new YoutubeVideoModel(videos, videoOptions, title));
            }
            catch (Exception)
            {
                DialogHost.Close("MainDialog");
            }
        }
Ejemplo n.º 2
0
 private void ShouldReloadDatabaseWithNewVideos(bool wantsToRefreshDatabase, string query)
 {
     if (wantsToRefreshDatabase)
     {
         DatabaseClient.RefreshDatabase();
         var videos = YoutubeVideoService.GetVideos(query);
         DatabaseClient.LoadVideosIntoDatabase(videos);
     }
 }
        private async void AddVideoToList(object s)
        {
            if (IsLoading || string.IsNullOrWhiteSpace(s as string))
            {
                return;
            }
            string url = (string)s;

            try
            {
                IsLoading = true;
                var videos = await _youtubeservise.GetVideosAsync(url);

                IReadOnlyList <VideoDownloadOption>?videoOptions;
                string?title = default;

                if (videos.Length == 1)
                {
                    videoOptions = await _youtubeservise.GetVideoDownloadOptionsAsync(url);
                }
                else
                {
                    videoOptions = YoutubeVideoService.GetOptionPlaylist().Reverse().ToArray();
                    title        = await _youtubeservise.GetPlaylistTitle(url);
                }
                IsLoading = false;
                DialogHost.Close("MainDialog", new YoutubeVideoModel(videos, videoOptions, title));
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            IsLoading = false;
        }
Ejemplo n.º 4
0
        public async void StartDownload()
        {
            if (Isloading)
            {
                return;
            }
            _cancellationToken ??= new CancellationTokenSource();
            try
            {
                if (IsPlaylist)
                {
                    var savePath = YoutubeVideoService.PromptDirectoryPath();
                    if (savePath == null)
                    {
                        return;
                    }
                    string fullpath = Path.Combine(savePath, YoutubeVideoService.FixFileName(PlaylistTitle !));
                    Directory.CreateDirectory(fullpath);
                    SavedPath = fullpath;
                    Isloading = true;
                    for (var i = 0; i < _videos.Length; i++)
                    {
                        try
                        {
                            PlaylistCount      = $"{i + 1}/{_videos.Length}";
                            DownloadPercentage = 0;
                            IsIndeterminate    = true;

                            var options = await _youtubeService.TryGetBestVideoDownloadOptionAsync(_videos[i].Id,
                                                                                                   "mp4",
                                                                                                   CurrerntVideoOption?.QualityPreference ?? VideoQualityPreference.Maximum);

                            IsIndeterminate = false;

                            await DownloadAsync(_videos[i], options ?? throw new NullReferenceException(),
                                                Path.Combine(fullpath, YoutubeVideoService.FixFileName(_videos[i].Title) + ".mp4"));

                            if (i + 1 < _videos.Length)
                            {
                                CurrerntVideo = _videos[i + 1];
                                Image         = CurrerntVideo.Thumbnails.MaxResUrl;
                                Title         = CurrerntVideo.Title;
                            }
                        }
                        catch (Exception e)
                        {
                            if (e.Message != "A task was canceled." && e.Message != "The operation was canceled.")
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                    }

                    Finished = true;
                }
                else
                {
                    var savePath =
                        YoutubeVideoService.PromptSaveFilePath(CurrerntVideo?.Title ?? "Unknow", CurrerntVideoOption?.Format ?? "Unknow");
                    SavedPath = savePath;
                    if (savePath == null)
                    {
                        return;
                    }

                    Isloading = true;
                    await DownloadAsync(CurrerntVideo ?? throw new NullReferenceException(), CurrerntVideoOption ?? throw new NullReferenceException(), savePath);

                    Finished = true;
                }
            }
            catch (Exception e)
            {
                if (e.Message != "A task was canceled." && e.Message != "The operation was canceled.")
                {
                    MessageBox.Show(e.Message);
                }
            }
            finally
            {
                Isloading = false;
                _cancellationToken?.Dispose();
                _cancellationToken = null;
            }
        }
 public AddVideoPopupViewModel()
 {
     _youtubeservise = new YoutubeVideoService();
     AddVideoCommand = new Command(AddVideoToList);
 }
        public IHttpActionResult GetVideoBySearch(String searchTerm)
        {
            var videoContent = new YoutubeVideoService().GetVideosBySearch(searchTerm);

            return(Ok(videoContent));
        }
        public IHttpActionResult GetVideoById(String videoId)
        {
            var videoContent = new YoutubeVideoService().GetVideoById(videoId);

            return(Ok(videoContent));
        }