public static YoutubeEntry Create(Uri uri, YoutubeEntry parent = null)
 {
     var entry = new YoutubeEntry(parent);
     if (uri != null)
         entry.Uri = uri;
     return entry;
 }
        public YoutubeEntry Clone()
        {
            var entry = new YoutubeEntry {
                Title           = Title,
                BaseFolder      = BaseFolder,
                Parent          = Parent,
                Description     = Description,
                DownloadFolder  = DownloadFolder,
                ProviderFolder  = ProviderFolder,
                MediaType       = MediaType,
                ThumbnailUrl    = ThumbnailUrl,
                Uri             = Uri,
                VideoExtension  = VideoExtension,
                VideoFolder     = VideoFolder,
                ExecutionStatus = ExecutionStatus
            };

            if (entry.ExecutionStatus == ExecutionStatus.Deleted)
            {
                entry.DownloadState = DownloadState.Deleted;
            }
            if (ThumbnailUrls != null && ThumbnailUrls.Length > 0)
            {
                entry.ThumbnailUrls = new string[ThumbnailUrls.Length];
                for (var i = 0; i < ThumbnailUrls.Length; i++)
                {
                    entry.ThumbnailUrls[i] = ThumbnailUrls[i];
                }
            }
            return(entry);
        }
 public static YoutubeEntry Create(Uri uri, string html)
 {
     var entry = new YoutubeEntry();
     if (uri != null)
         entry.Uri = uri;
     Parse(entry, html);
     return entry;
 }
 private YoutubeEntry(YoutubeEntry parent = null)
 {
     Parent    = parent;
     _settings = new MSYoutubeSettings("MS.Youtube.Downloader", "AI39si76x-DO4bui7H1o0P6x8iLHPBvQ24exnPiM8McsJhVW_pnCWXOXAa1D8-ymj0Bm07XrtRqxBC7veH6flVIYM7krs36kQg")
     {
         AutoPaging = true, PageSize = 50
     };
 }
 private void UpdateStatus(DownloadState state, YoutubeEntry entry, double percentage)
 {
     DownloadState = state;
     Percentage    = percentage;
     if (OnListDownloadStatusChange != null)
     {
         OnListDownloadStatusChange(this, entry, DownloadState, Percentage);
     }
 }
        public static YoutubeEntry Create(Uri uri, YoutubeEntry parent = null)
        {
            var entry = new YoutubeEntry(parent);

            if (uri != null)
            {
                entry.Uri = uri;
            }
            return(entry);
        }
        public static YoutubeEntry Create(Uri uri, string html)
        {
            var entry = new YoutubeEntry();

            if (uri != null)
            {
                entry.Uri = uri;
            }
            Parse(entry, html);
            return(entry);
        }
        private static void Parse(YoutubeEntry entry, string html)
        {
            var doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(html);
            try {
                var titleNode = doc.DocumentNode.SelectSingleNode("//meta[@name='title']");
                if (titleNode != null)
                {
                    entry.Title = titleNode.Attributes["content"].Value;
                }
                else
                {
                    titleNode = doc.DocumentNode.SelectSingleNode("//title");
                    if (titleNode != null)
                    {
                        var txt = titleNode.InnerText ?? "";
                        txt = txt.Trim();
                        if (txt.ToLowerInvariant().EndsWith(" - youtube"))
                        {
                            txt = txt.Substring(0, txt.Length - " - youtube".Length).Trim();
                        }
                        entry.Title = txt;
                    }
                }
            } catch {
                entry.Title = "";
            }

            var nodes = doc.DocumentNode.SelectNodes("//*[@data-context-item-id]"); //[@class contains 'feed-item-container']

            if (nodes == null)
            {
                return;
            }
            foreach (var node in nodes)
            {
                var id           = node.Attributes["data-context-item-id"].Value;
                var youtubeEntry = Create(new Uri("http://www.youtube.com/watch?v=" + id), entry);
                try {
                    youtubeEntry.Title        = node.Attributes["data-context-item-title"].Value;
                    youtubeEntry.ThumbnailUrl = node.SelectSingleNode("//img[@data-thumb]").Attributes["data-thumb"].Value;
                    if (!(youtubeEntry.ThumbnailUrl.StartsWith("http:") || youtubeEntry.ThumbnailUrl.StartsWith("https:")))
                    {
                        youtubeEntry.ThumbnailUrl = "http://" + youtubeEntry.ThumbnailUrl;
                    }
                } catch { }
                entry.Entries.Add(youtubeEntry);
            }
        }
 public static void DownloadToFileAsync(YoutubeEntry entry, Uri uri, StorageFile storageFile, MSYoutubeLoading onYoutubeLoading)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal)
     {
         return;
     }
     using (var destinationStream = storageFile.OpenStreamForWrite()) {
         if (destinationStream == null)
         {
             return;
         }
         var start = destinationStream.Length;
         destinationStream.Position = destinationStream.Length;
         AddToFile(entry, uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading, storageFile);
     }
 }
        private static void AddToFile(YoutubeEntry entry, Uri uri, Stream destinationStream, long?start, long?stop, MSYoutubeLoading onYoutubeLoading, StorageFile storageFile, bool retry = false)
        {
            if (entry.ExecutionStatus != ExecutionStatus.Normal)
            {
                return;
            }
            var response = DownloadToStreamAsync(uri, start, stop);
            var cache    = CacheManager.Instance;

            if (response == null || response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
            {
                cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, (new FileInfo(storageFile.ToString())).Length);
                return;
            }
            var range = GetRange(response);
            var total = range.Length;

            cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, total);
            var to = range.To;

            using (var stream = response.GetResponseStream()) {
                if (stream == null)
                {
                    return;
                }
                try {
                    stream.CopyTo(destinationStream);
                    destinationStream.Flush();
                } catch (WebException) {
                    if (retry)
                    {
                        return;
                    }
                    AddToFile(entry, uri, destinationStream, start, stop, onYoutubeLoading, storageFile, true);
                }
                if (onYoutubeLoading != null && entry.ExecutionStatus == ExecutionStatus.Normal)
                {
                    onYoutubeLoading(to, total);
                }
                if (total > to + 1)
                {
                    AddToFile(entry, uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading, storageFile);
                }
            }
        }
        private void FillEntriesUser(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading)
        {
            var youtubeUrl = YoutubeUrl;
            var request    = new MSYoutubeRequest(_settings);
            var uri        = new Uri(String.Format("https://gdata.youtube.com/feeds/api/users/{0}/playlists?v=2", youtubeUrl.UserId));
            var items      = request.GetAsync(YoutubeUrl, uri, onYoutubeLoading);

            if (items == null)
            {
                return;
            }
            Entries = new ObservableCollection <Feed>();
            try {
                if (!String.IsNullOrEmpty(items.AuthorId))
                {
                    var favoritesEntry = new YoutubeEntry(this)
                    {
                        Title = "Favorite Videos", Uri = new Uri("http://www.youtube.com/playlist?list=FL" + items.AuthorId)
                    };
                    Entries.Add(favoritesEntry);
                }
                foreach (var member in items.Entries)
                {
                    var entry = new YoutubeEntry(this)
                    {
                        Title = member.Title, Uri = member.Uri, Description = member.Description
                    };
                    Entries.Add(entry);
                }
            } catch {
                Entries.Clear();
            }
            if (onEntriesReady != null)
            {
                onEntriesReady(Entries);
            }
        }
        private static void Parse(YoutubeEntry entry, string html)
        {
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            try {
                var titleNode = doc.DocumentNode.SelectSingleNode("//meta[@name='title']");
                if (titleNode != null)
                    entry.Title = titleNode.Attributes["content"].Value;
                else {
                    titleNode = doc.DocumentNode.SelectSingleNode("//title");
                    if (titleNode != null) {
                        var txt = titleNode.InnerText ?? "";
                        txt = txt.Trim();
                        if (txt.ToLowerInvariant().EndsWith(" - youtube"))
                            txt = txt.Substring(0, txt.Length - " - youtube".Length).Trim();
                        entry.Title = txt;
                    }
                }
            } catch {
                entry.Title = "";
            }

            var nodes = doc.DocumentNode.SelectNodes("//*[@data-context-item-id]"); //[@class contains 'feed-item-container']
            if (nodes == null) return;
            foreach (var node in nodes) {
                var id = node.Attributes["data-context-item-id"].Value;
                var youtubeEntry = Create(new Uri("http://www.youtube.com/watch?v=" + id), entry);
                try {
                    youtubeEntry.Title = node.Attributes["data-context-item-title"].Value;
                    youtubeEntry.ThumbnailUrl = node.SelectSingleNode("//img[@data-thumb]").Attributes["data-thumb"].Value;
                    if (!(youtubeEntry.ThumbnailUrl.StartsWith("http:") || youtubeEntry.ThumbnailUrl.StartsWith("https:")))
                        youtubeEntry.ThumbnailUrl = "http://" + youtubeEntry.ThumbnailUrl;
                } catch { }
                entry.Entries.Add(youtubeEntry);
            }
        }
 public static void DownloadToFileAsync(YoutubeEntry entry, Uri uri, StorageFile storageFile, MSYoutubeLoading onYoutubeLoading)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     using (var destinationStream = storageFile.OpenStreamForWrite()) {
         if (destinationStream == null) return;
         var start = destinationStream.Length;
         destinationStream.Position = destinationStream.Length;
         AddToFile(entry, uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading, storageFile);
     }
 }
 private static void AddToFile(YoutubeEntry entry, Uri uri, Stream destinationStream, long? start, long? stop, MSYoutubeLoading onYoutubeLoading, StorageFile storageFile, bool retry = false)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     var response = DownloadToStreamAsync(uri, start, stop);
     var cache = CacheManager.Instance;
     if (response == null || response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable) {
         cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, (new FileInfo(storageFile.ToString())).Length);
         return;
     }
     var range = GetRange(response);
     var total =  range.Length;
     cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, total);
     var to = range.To;
     using (var stream = response.GetResponseStream()) {
         if (stream == null) return;
         try {
             stream.CopyTo(destinationStream);
             destinationStream.Flush();
         } catch (WebException) {
             if (retry) return;
             AddToFile(entry, uri, destinationStream, start, stop, onYoutubeLoading, storageFile, true);
         }
         if (onYoutubeLoading != null && entry.ExecutionStatus == ExecutionStatus.Normal) onYoutubeLoading(to, total);
             if (total > to + 1)
                 AddToFile(entry, uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading, storageFile);
     }
 }
 public YoutubeEntry Clone()
 {
     var entry = new YoutubeEntry {
         Title = Title,
         BaseFolder = BaseFolder,
         Parent = Parent,
         Description = Description,
         DownloadFolder = DownloadFolder,
         ProviderFolder = ProviderFolder,
         MediaType = MediaType,
         ThumbnailUrl = ThumbnailUrl,
         Uri = Uri,
         VideoExtension = VideoExtension,
         VideoFolder = VideoFolder,
         ExecutionStatus = ExecutionStatus
     };
     if (entry.ExecutionStatus == ExecutionStatus.Deleted) entry.DownloadState = DownloadState.Deleted;
     if (ThumbnailUrls != null && ThumbnailUrls.Length > 0) {
         entry.ThumbnailUrls = new string[ThumbnailUrls.Length];
         for (var i = 0; i < ThumbnailUrls.Length; i++)
             entry.ThumbnailUrls[i] = ThumbnailUrls[i];
     }
     return entry;
 }
 private void FillEntriesUser(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading)
 {
     var youtubeUrl = YoutubeUrl;
     var request = new MSYoutubeRequest(_settings);
     var uri = new Uri(String.Format("https://gdata.youtube.com/feeds/api/users/{0}/playlists?v=2", youtubeUrl.UserId));
     var items = request.GetAsync(YoutubeUrl, uri, onYoutubeLoading);
     if (items == null) return;
     Entries = new ObservableCollection<Feed>();
     try {
         if (!String.IsNullOrEmpty(items.AuthorId)) {
             var favoritesEntry = new YoutubeEntry(this) {Title = "Favorite Videos", Uri = new Uri("http://www.youtube.com/playlist?list=FL" + items.AuthorId)};
             Entries.Add(favoritesEntry);
         }
         foreach (var member in items.Entries) {
             var entry = new YoutubeEntry(this) {Title = member.Title, Uri = member.Uri, Description = member.Description};
             Entries.Add(entry);
         }
     } catch {
         Entries.Clear();
     }
     if (onEntriesReady != null) onEntriesReady(Entries);
 }
 private YoutubeEntry(YoutubeEntry parent = null)
 {
     Parent = parent;
     _settings = new MSYoutubeSettings( "MS.Youtube.Downloader", "AI39si76x-DO4bui7H1o0P6x8iLHPBvQ24exnPiM8McsJhVW_pnCWXOXAa1D8-ymj0Bm07XrtRqxBC7veH6flVIYM7krs36kQg" ) {AutoPaging = true, PageSize = 50};
 }
 public AudioConverter(YoutubeEntry youtubeEntry, EntryDownloadStatusEventHandler onEntryDownloadStatusChange)
 {
     _youtubeEntry = youtubeEntry;
     _onEntryDownloadStatusChange = onEntryDownloadStatusChange;
     _applicationPath             = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
 }
 private void UpdateStatus(DownloadState state, YoutubeEntry entry, double percentage)
 {
     DownloadState = state;
     Percentage = percentage;
     if (OnListDownloadStatusChange != null) OnListDownloadStatusChange(this, entry, DownloadState, Percentage);
 }
Exemplo n.º 20
0
 private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
 {
     Url.Text = e.Uri.ToString();
     MixpanelTrack("Navigated", new {Url = e.Uri.ToString(), Guid = _settings.ApplicationConfiguration.Guid});
     _youtubeUrl = YoutubeUrl.Create(e.Uri);
     var doc = Browser.Document as IHTMLDocument3; ;
     if (doc == null) return;
     var html = doc.documentElement.outerHTML;
     _youtubeEntry = YoutubeEntry.Create(e.Uri, html);
     Loading();
 }