Exemple #1
0
 public void GetEntries(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading = null)
 {
     if(YoutubeUrl.Type == VideoUrlType.Channel || YoutubeUrl.ChannelId != "" || YoutubeUrl.FeedId != "")
         FillEntriesChannel(onEntriesReady, onYoutubeLoading);
     else if(YoutubeUrl.Type == VideoUrlType.User)
         FillEntriesUser(onEntriesReady, onYoutubeLoading);
 }
Exemple #2
0
 private async 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 = await 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);
 }
 public async Task _GetAsync(Uri uri, MSYoutubeEntry feed, MSYoutubeLoading loading = null)
 {
     if (feed.NextPageUri == null) return;
     var xml = await GetXmlDocumentAsync(feed.NextPageUri);
     FillFeed(xml, feed);
     if (loading != null) loading(feed.Entries.Count, feed.Total);
     await _GetAsync(uri, feed, loading);
 }
 public void _GetAsync(Uri uri, MSYoutubeEntry feed, MSYoutubeLoading loading)
 {
     
     if (feed.NextPageUri == null) return;
     var document = GetXmlDocumentAsync(feed.NextPageUri);
     FillFeed(document, feed);
     if (loading != null) loading(feed.Entries.Count, feed.Total);
     _GetAsync(uri, feed, loading);
 }
 public MSYoutubeEntry GetAsync(YoutubeUrl youtubeUrl, Uri uri, MSYoutubeLoading loading)
 {
     var feed = new MSYoutubeEntry {
         YoutubeUrl = youtubeUrl,
         NextPageUri =
             new Uri(uri + ((String.IsNullOrEmpty(uri.Query)) ? "?" : "&") + "start-index=1&max-results=40")
     };
     _GetAsync(uri, feed, loading);
     return feed;
 }
        public MSYoutubeEntry GetAsync(YoutubeUrl youtubeUrl, Uri uri, MSYoutubeLoading loading)
        {
            var feed = new MSYoutubeEntry {
                YoutubeUrl  = youtubeUrl,
                NextPageUri =
                    new Uri(uri + ((String.IsNullOrEmpty(uri.Query)) ? "?" : "&") + "start-index=1&max-results=40")
            };

            _GetAsync(uri, feed, loading);
            return(feed);
        }
 public void GetEntries(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading = null)
 {
     Task.Factory.StartNew(() => {
         if (YoutubeUrl.Type == VideoUrlType.Channel || YoutubeUrl.ChannelId != "" || YoutubeUrl.FeedId != "")
         {
             FillEntriesChannel(onEntriesReady, onYoutubeLoading);
         }
         else if (YoutubeUrl.Type == VideoUrlType.User)
         {
             FillEntriesUser(onEntriesReady, onYoutubeLoading);
         }
     });
 }
        public void _GetAsync(Uri uri, MSYoutubeEntry feed, MSYoutubeLoading loading)
        {
            if (feed.NextPageUri == null)
            {
                return;
            }
            var document = GetXmlDocumentAsync(feed.NextPageUri);

            FillFeed(document, feed);
            if (loading != null)
            {
                loading(feed.Entries.Count, feed.Total);
            }
            _GetAsync(uri, feed, loading);
        }
        private void FillEntriesChannel(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading)
        {
            var url = "";

            if (!String.IsNullOrEmpty(YoutubeUrl.ChannelId))
            {
                url = "https://gdata.youtube.com/feeds/api/playlists/" + YoutubeUrl.ChannelId;
            }
            else if (!String.IsNullOrEmpty(YoutubeUrl.FeedId))
            {
                url = String.Format("https://gdata.youtube.com/feeds/api/users/{0}/uploads", YoutubeUrl.FeedId);
            }
            if (url.Length <= 0)
            {
                return;
            }

            try {
                var request = new MSYoutubeRequest(_settings);
                var items   = request.GetAsync(YoutubeUrl, new Uri(url), onYoutubeLoading);
                if (items == null)
                {
                    Entries = new ObservableCollection <Feed>();
                }
                else
                {
                    if (String.IsNullOrEmpty(Title))
                    {
                        Title = items.Title;
                    }
                    Entries = GetMembers(items);
                }
            } catch {
                Entries = new ObservableCollection <Feed>();
            }
            if (onEntriesReady != null)
            {
                onEntriesReady(Entries);
            }
        }
        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);
            }
        }
 public static void DownloadToFileAsync(YoutubeEntry entry, Uri uri, StorageFolder folder, string fileName, MSYoutubeLoading onYoutubeLoading)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     var storageFile = GetFile(folder, fileName);
     using (var destinationStream = storageFile.OpenStreamForWriteAsync()) {
         if (destinationStream == null) return;
         //var properties = await storageFile.GetBasicPropertiesAsync();
         var start = destinationStream.Length; // (long)properties.Size;
         destinationStream.Position = destinationStream.Length;
         AddToFile(entry, uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading);
     }
 }
 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 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)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     var response = DownloadToStreamAsync(uri, start, stop);
     if (response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable) return;
     var range = GetRange(response);
     var total =  range.Length; // (response.Headers.ContentRange.Length ?? 0);
     var to = range.To; // (response.Headers.ContentRange.To ?? 0);
     using (var stream = response.GetResponseStream()) {
         if (stream == null) return;
         CopyStream(stream, destinationStream);
         destinationStream.Flush();
         if (onYoutubeLoading != null && entry.ExecutionStatus == ExecutionStatus.Normal) onYoutubeLoading(to, total);
         if (total > to + 1)
             AddToFile(entry, uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading);
     }
 }
        private static async Task AddToFile(Uri uri, Stream destinationStream, long? start, long? stop, MSYoutubeLoading onYoutubeLoading)
        {
            var content = await DownloadToStreamAsync(uri, start, stop);
            if (content.Headers.ContentRange == null) return;
            long total = (content.Headers.ContentRange.Length ?? 0);
            long to = (content.Headers.ContentRange.To ?? 0);
            //long downloadedLength = (long) content.Headers.ContentLength;
            using (var stream = await content.ReadAsStreamAsync()) {
                if (stream == null) return;
                await stream.CopyToAsync(destinationStream);
                await destinationStream.FlushAsync();
                if (onYoutubeLoading != null) onYoutubeLoading(to, total);
                if (total > to + 1)
                    await AddToFile(uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading);
            }

        }
        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);
                }
            }
        }
Exemple #18
0
        private async void FillEntriesChannel(EntriesReady onEntriesReady, MSYoutubeLoading onYoutubeLoading)
        {
            var url = "";
            if (!String.IsNullOrEmpty(YoutubeUrl.ChannelId)) 
                url = "https://gdata.youtube.com/feeds/api/playlists/" + YoutubeUrl.ChannelId;
            else if (!String.IsNullOrEmpty(YoutubeUrl.FeedId))
                url = String.Format("https://gdata.youtube.com/feeds/api/users/{0}/uploads", YoutubeUrl.FeedId);
            if (url.Length <= 0) return;

            try {
                var request = new MSYoutubeRequest(_settings);
                var items = await request.GetAsync(YoutubeUrl, new Uri(url), onYoutubeLoading);
                if (items == null)
                    Entries = new ObservableCollection<Feed>();
                else {
                    if (String.IsNullOrEmpty(Title)) Title = items.Title;
                    Entries = GetMembers(items);
                }
            } catch {
                Entries = new ObservableCollection<Feed>();
            }
            if (onEntriesReady != null) onEntriesReady(Entries);
        }
 public static async Task DownloadToFileAsync(Uri uri, StorageFolder folder, string fileName, MSYoutubeLoading onYoutubeLoading)
 {
     var storageFile = await GetFile(folder, fileName);
     using (var destinationStream = await storageFile.OpenStreamForWriteAsync()) {
         //var properties = await storageFile.GetBasicPropertiesAsync();
         var start = destinationStream.Length; // (long)properties.Size;
         destinationStream.Position = destinationStream.Length;
         await AddToFile(uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading);
     }
 }