コード例 #1
0
        /// <summary>
        /// Tries to get a youtube link from a post. If it fails
        /// it returns null.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        private static async Task <YouTubeUri> GetYouTubeVideoUrl(ContentPanelSource source)
        {
            if (String.IsNullOrWhiteSpace(source.Url))
            {
                return(null);
            }

            try
            {
                // Try to find the ID
                string youtubeVideoId = TryToGetYouTubeId(source);

                if (!String.IsNullOrWhiteSpace(youtubeVideoId))
                {
                    // We found it!
                    // #todo make this quality an option dependent on device!
                    return(await YouTube.GetVideoUriAsync(youtubeVideoId, YouTubeQuality.QualityMedium));
                }
            }
            catch (Exception)
            {
                App.BaconMan.TelemetryMan.ReportEvent("YoutubeString", "Failed to find youtube video");
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Fired when the user has tapped the panel requesting the content to load.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void FlipViewPostPanel_OnContentLoadRequest(object sender, ContentLoadRequestArgs e)
        {
            // Find the post
            Post post = null;

            lock (_postsLists)
            {
                foreach (var item in _postsLists)
                {
                    if (!item.Context.Post.Id.Equals(e.SourceId))
                    {
                        continue;
                    }
                    post = item.Context.Post;
                    break;
                }
            }

            // Send off a command to load it.
            if (post != null)
            {
                await Task.Run(() =>
                {
                    ContentPanelMaster.Current.AddAllowedContent(ContentPanelSource.CreateFromPost(post), _uniqueId);
                });
            }
        }
コード例 #3
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        static public bool CanHandlePost(ContentPanelSource source)
        {
            // Note! We can't do the full Uri get because it relays on an Internet request and
            // we can't lose the time for this quick check. If we can get the youtube id assume we are good.

            // See if we can get a link
            return !String.IsNullOrWhiteSpace(TryToGetYouTubeId(source));
        }
コード例 #4
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        static public bool CanHandlePost(ContentPanelSource source)
        {
            // Note! We can't do the full Uri get because it relays on an Internet request and
            // we can't lose the time for this quick check. If we can get the youtube id assume we are good.

            // See if we can get a link
            return(!String.IsNullOrWhiteSpace(TryToGetYouTubeId(source)));
        }
コード例 #5
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // Check if we have the spoiler tag.
     if (!String.IsNullOrWhiteSpace(source.Url) && source.Url.TrimStart().ToLower().StartsWith("/s"))
     {
         return true;
     }
     return false;
 }
コード例 #6
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     string urlLower = source.Url.ToLower();
     if (urlLower.Contains("microsoft.com") && (urlLower.Contains("/store/apps/") || urlLower.Contains("/store/games/")))
     {
         return true;
     }
     return false;
 }
コード例 #7
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // See if we can find a imgur, gfycat gif, or a normal gif we can send to gfycat.
     if (String.IsNullOrWhiteSpace(GetImgurUrl(source.Url)) && String.IsNullOrWhiteSpace(GetGfyCatApiUrl(source.Url)) && String.IsNullOrWhiteSpace(GetGifUrl(source.Url)))
     {
         return(false);
     }
     return(true);
 }
コード例 #8
0
 /// <summary>
 /// Called by the host when it queries if we can handle a source.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // See if we can get an image from the url
     if (String.IsNullOrWhiteSpace(ImageManager.GetImageUrl(source.Url)))
     {
         return false;
     }
     return true;
 }
コード例 #9
0
 /// <summary>
 /// Called by the host when it queries if we can handle a source.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // See if we can get an image from the url
     if (String.IsNullOrWhiteSpace(ImageManager.GetImageUrl(source.Url)))
     {
         return(false);
     }
     return(true);
 }
コード例 #10
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <param name="source"></param>
 /// <returns></returns>
 public static bool CanHandlePost(ContentPanelSource source)
 {
     // See if we can find a imgur, gfycat gif, or a normal gif we can send to gfycat.
     return
         (!string.IsNullOrWhiteSpace(GetImgurUrl(source.Url)) ||
          !string.IsNullOrWhiteSpace(GetGfyCatApiUrl(source.Url)) ||
          !string.IsNullOrWhiteSpace(GetRedGifUrl(source.Url)) ||
          !string.IsNullOrWhiteSpace(GetGifUrl(source.Url)));
 }
コード例 #11
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // See if we can find a imgur, gfycat gif, or a normal gif we can send to gfycat.
     if (String.IsNullOrWhiteSpace(GetImgurUrl(source.Url)) && String.IsNullOrWhiteSpace(GetGfyCatApiUrl(source.Url)) && String.IsNullOrWhiteSpace(GetGifUrl(source.Url)))
     {
         return false;
     }
     return true;
 }
コード例 #12
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // Check if we have the spoiler tag.
     if (!String.IsNullOrWhiteSpace(source.Url) && source.Url.TrimStart().ToLower().StartsWith("/s"))
     {
         return(true);
     }
     return(false);
 }
コード例 #13
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        static public bool CanHandlePost(ContentPanelSource source)
        {
            string urlLower = source.Url.ToLower();

            if (urlLower.Contains("microsoft.com") && (urlLower.Contains("/store/apps/") || urlLower.Contains("/store/games/")))
            {
                return(true);
            }
            return(false);
        }
コード例 #14
0
        /// <summary>
        /// Sets the post content. For now all we give the flip view control is the URL
        /// and it must figure out the rest on it's own.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="isVisiblePost"></param>
        private async Task SetPostContent(FlipViewPostItem item, bool isVisiblePost)
        {
            // Set that the post is visible if it is
            item.IsVisible = isVisiblePost;

            // Only load the content if we are doing it with out action. (most of the time)
            if (App.BaconMan.UiSettingsMan.FlipViewLoadPostContentWithoutAction)
            {
                await Task.Run(() =>
                {
                    ContentPanelMaster.Current.AddAllowedContent(ContentPanelSource.CreateFromPost(item.Context.Post), _uniqueId, !isVisiblePost);
                });
            }
        }
コード例 #15
0
        public static Type GetControlType(ContentPanelSource source, object callingClass = null)
        {
            try
            {
                if (GifImageContentPanel.CanHandlePost(source))
                {
                    return(typeof(GifImageContentPanel));
                }
                if (RedditVideoContentPanel.CanHandlePost(source))
                {
                    return(typeof(RedditVideoContentPanel));
                }
                if (YoutubeContentPanel.CanHandlePost(source))
                {
                    return(typeof(YoutubeContentPanel));
                }
                if (BasicImageContentPanel.CanHandlePost(source))
                {
                    return(typeof(BasicImageContentPanel));
                }
                if (MarkdownContentPanel.CanHandlePost(source))
                {
                    return(typeof(MarkdownContentPanel));
                }
                if (RedditContentPanel.CanHandlePost(source))
                {
                    return(typeof(RedditContentPanel));
                }
                if (CommentSpoilerContentPanel.CanHandlePost(source))
                {
                    return(typeof(CommentSpoilerContentPanel));
                }
                if (WindowsAppContentPanel.CanHandlePost(source))
                {
                    return(typeof(WindowsAppContentPanel));
                }
            }
            catch (Exception e)
            {
                // If we fail here we will fall back to the web browser.
                App.BaconMan.MessageMan.DebugDia("Failed to query can handle post", e);
                TelemetryManager.ReportUnexpectedEvent(callingClass, "FailedToQueryCanHandlePost", e);
            }

            return(typeof(WebPageContentPanel));
        }
コード例 #16
0
        /// <summary>
        /// Tries to get a youtube link from a post. If it fails
        /// it returns null.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        private static async Task <YouTubeUri> GetYouTubeVideoUrl(ContentPanelSource source)
        {
            if (String.IsNullOrWhiteSpace(source.Url))
            {
                return(null);
            }

            // Try to find the ID
            string youtubeVideoId = TryToGetYouTubeId(source);

            if (!String.IsNullOrWhiteSpace(youtubeVideoId))
            {
                // We found it!
                // #todo make this quality an option dependent on device!
                return(await YouTube.GetVideoUriAsync(youtubeVideoId, YouTubeQuality.QualityMedium));
            }

            return(null);
        }
コード例 #17
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool CanHandlePost(ContentPanelSource source)
        {
            // If this is a self post with no text we will handle this.
            if (source.IsSelf && string.IsNullOrWhiteSpace(source.SelfText))
            {
                return(true);
            }

            // Check if this is a reddit content post, if so this is us
            if (string.IsNullOrWhiteSpace(source.Url))
            {
                return(false);
            }

            // Check the content
            var container = MiscellaneousHelper.TryToFindRedditContentInLink(source.Url);

            // If we got a container and it isn't a web site return it.
            return(container != null && container.Type != RedditContentType.Website);
        }
コード例 #18
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        static public bool CanHandlePost(ContentPanelSource source)
        {
            // If this is a self post with no text we will handle this.
            if (source.IsSelf && String.IsNullOrWhiteSpace(source.SelfText))
            {
                return true;
            }

            // Check if this is a reddit content post, if so this is us
            if (!String.IsNullOrWhiteSpace(source.Url))
            {
                // Check the content
                RedditContentContainer container = MiscellaneousHelper.TryToFindRedditContentInLink(source.Url);

                // If we got a container and it isn't a web site return it.
                if (container != null && container.Type != RedditContentType.Website)
                {
                    return true;
                }
            }
            return false;
        }
コード例 #19
0
        /// <summary>
        /// Called when we should show something
        /// </summary>
        /// <param name="link"></param>
        public void ShowContent(string link)
        {
            // Make sure we are in the correct state
            lock (this)
            {
                if (State != GlobalContentStates.Idle)
                {
                    return;
                }
                State = GlobalContentStates.Opening;
            }

            // Create the content control
            m_contentControl = new ContentPanelHost();

            // Disable full screen
            m_contentControl.CanGoFullscreen = false;

            // This isn't great, but for now mock a post
            m_source = ContentPanelSource.CreateFromUrl(link);

            // Approve the content to be shown
            Task.Run(() =>
            {
                ContentPanelMaster.Current.AddAllowedContent(m_source);
            });

            // Add the control to the UI
            ui_contentRoot.Children.Add(m_contentControl);

            // Set the post to begin loading
            m_contentControl.SourceId  = m_source.Id;
            m_contentControl.IsVisible = true;

            // Show the panel
            ToggleShown(true);
        }
コード例 #20
0
        /// <summary>
        /// Tries to get a youtube link from a post. If it fails
        /// it returns null.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static async Task <YouTubeVideoInfo> GetYouTubeVideoInfoAsync(ContentPanelSource source)
        {
            if (string.IsNullOrWhiteSpace(source.Url))
            {
                return(null);
            }

            try
            {
                // Try to find the ID
                var youtubeVideoId = TryToGetYouTubeId(source);

                if (!string.IsNullOrWhiteSpace(youtubeVideoId))
                {
                    return(await YouTubeHelper.GetVideoInfoAsync(youtubeVideoId));
                }
            }
            catch (Exception ex)
            {
                TelemetryManager.ReportEvent("YoutubeString", "Failed to find youtube video");
            }

            return(null);
        }
コード例 #21
0
        public async Task <bool> CreateContentPanel(ContentPanelSource source, bool canLoadLargePanels)
        {
            // Indicates if the panel was loaded.
            bool loadedPanel = true;

            // Capture the source
            Source = source;

            // We default to web page
            Type controlType = typeof(MarkdownContentPanel);

            // If we are not forcing web find the control type.
            if (!source.ForceWeb)
            {
                // Try to figure out the type.
                try
                {
                    if (GifImageContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(GifImageContentPanel);
                    }
                    else if (BasicImageContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(BasicImageContentPanel);
                    }

                    if (controlType == typeof(MarkdownContentPanel))
                    {
                        if (!source.Url.ToLower().Contains("reddit.com"))
                        {
                            controlType = typeof(WebPageContentPanel);
                        }
                    }
                    //if (GifImageContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(GifImageContentPanel);
                    //}
                    //else if (YoutubeContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(YoutubeContentPanel);
                    //}
                    //else if (BasicImageContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(BasicImageContentPanel);
                    //}
                    //else if (MarkdownContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(MarkdownContentPanel);
                    //}
                    //else if (RedditContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(RedditContentPanel);
                    //}
                    //else if (CommentSpoilerContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(CommentSpoilerContentPanel);
                    //}
                    //else if (WindowsAppContentPanel.CanHandlePost(source))
                    //{
                    //    controlType = typeof(WindowsAppContentPanel);
                    //}
                    //else
                    //{
                    //    // This is a web browser

                    //    // If we are blocking large panels don't allow the
                    //    // browser.
                    //    if (!canLoadLargePanels)
                    //    {
                    //        loadedPanel = false;
                    //    }
                    //}
                }
                catch (Exception e)
                {
                    // If we fail here we will fall back to the web browser.
                    App.BaconMan.MessageMan.DebugDia("Failed to query can handle post", e);
                }
            }

            // Check if we should still load.
            if (loadedPanel)
            {
                // Make the control on the UI thread.
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    try
                    {
                        // Create the panel
                        Panel = (IContentPanel)Activator.CreateInstance(controlType, this);

                        // Fire OnPrepareContent
                        Panel.OnPrepareContent();
                    }
                    catch (Exception e)
                    {
                        loadedPanel = false;
                        HasError    = true;
                        App.BaconMan.MessageMan.DebugDia("failed to create content control", e);
                    }
                });
            }

            // Indicate that we have loaded.
            return(loadedPanel);
        }
コード例 #22
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static bool CanHandlePost(ContentPanelSource source)
 {
     // If it is a self post and it has text it is for us.
     return(source.IsSelf && !string.IsNullOrWhiteSpace(source.SelfText));
 }
コード例 #23
0
        public async Task <bool> CreateContentPanel(ContentPanelSource source, bool canLoadLargePanels)
        {
            // Indicates if the panel was loaded.
            var loadedPanel = true;

            // Capture the source
            Source = source;

            // We default to web page
            var controlType = typeof(WebPageContentPanel);

            // If we are not forcing web find the control type.
            if (!source.ForceWeb)
            {
                // Try to figure out the type.
                controlType = GetControlType(source, this);
                if (controlType == typeof(WebPageContentPanel))
                {
                    // This is a web browser

                    // If we are blocking large panels don't allow the
                    // browser.
                    if (!canLoadLargePanels)
                    {
                        loadedPanel = false;
                    }
                }
            }

            // Check if we should still load.
            if (loadedPanel)
            {
                // Make the control on the UI thread.
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    try
                    {
                        // Create the panel
                        Panel = (IContentPanel)Activator.CreateInstance(controlType, this);

#if DEBUG
                        Debug.WriteLine($"Content Panel: {controlType.Name}");
                        Debug.WriteLine($"Source: {source}");
#endif

                        // Fire OnPrepareContent
                        Panel.OnPrepareContent();
                    }
                    catch (Exception e)
                    {
                        loadedPanel = false;
                        HasError    = true;
                        App.BaconMan.MessageMan.DebugDia("failed to create content control", e);
                        TelemetryManager.ReportUnexpectedEvent(this, "FailedToCreateContentPanel", e);
                    }
                });
            }

            // Indicate that we have loaded.
            return(loadedPanel);
        }
コード例 #24
0
 public static bool CanHandlePost(ContentPanelSource source)
 {
     return(source.IsRedditVideo && !string.IsNullOrWhiteSpace(source.VideoUrl.AbsoluteUri));
 }
コード例 #25
0
ファイル: ContentPanelBase.cs プロジェクト: RareNCool/Baconit
        public async Task<bool> CreateContentPanel(ContentPanelSource source, bool canLoadLargePanels)
        {
            // Indicates if the panel was loaded.
            bool loadedPanel = true;

            // Capture the source 
            Source = source;

            // We default to web page
            Type controlType = typeof(WebPageContentPanel);

            // If we are not forcing web find the control type.
            if (!source.ForceWeb)
            {
                // Try to figure out the type.
                try
                {
                    if (GifImageContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(GifImageContentPanel);
                    }
                    else if (YoutubeContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(YoutubeContentPanel);
                    }
                    else if (BasicImageContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(BasicImageContentPanel);
                    }
                    else if (MarkdownContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(MarkdownContentPanel);
                    }
                    else if (RedditContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(RedditContentPanel);
                    }
                    else if (CommentSpoilerContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(CommentSpoilerContentPanel);
                    }
                    else if (WindowsAppContentPanel.CanHandlePost(source))
                    {
                        controlType = typeof(WindowsAppContentPanel);
                    }
                    else
                    {
                        // This is a web browser

                        // If we are blocking large panels don't allow the
                        // browser.
                        if (!canLoadLargePanels)
                        {
                            loadedPanel = false;
                        }
                    }
                }
                catch (Exception e)
                {
                    // If we fail here we will fall back to the web browser.
                    App.BaconMan.MessageMan.DebugDia("Failed to query can handle post", e);
                    App.BaconMan.TelemetryMan.ReportUnexpectedEvent(this, "FailedToQueryCanHandlePost", e);
                }
            }

            // Check if we should still load.
            if (loadedPanel)
            {
                // Make the control on the UI thread.
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    try
                    {
                        // Create the panel
                        Panel = (IContentPanel)Activator.CreateInstance(controlType, this);

                        // Fire OnPrepareContent 
                        Panel.OnPrepareContent();
                    }
                    catch (Exception e)
                    {
                        loadedPanel = false;
                        HasError = true;
                        App.BaconMan.MessageMan.DebugDia("failed to create content control", e);
                        App.BaconMan.TelemetryMan.ReportUnexpectedEvent(this, "FailedToCreateContentPanel", e);
                    }
                });
            }

            // Indicate that we have loaded.
            return loadedPanel;
        }
コード例 #26
0
        /// <summary>
        /// Called by the host when it queries if we can handle a post.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool CanHandlePost(ContentPanelSource source)
        {
            var urlLower = source.Url.ToLower();

            return(urlLower.Contains("microsoft.com") && (urlLower.Contains("/store/apps/") || urlLower.Contains("/store/games/")));
        }
コード例 #27
0
        /// <summary>
        /// Attempts to get a youtube id from a url.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static string TryToGetYouTubeId(ContentPanelSource source)
        {
            if (String.IsNullOrWhiteSpace(source.Url))
            {
                return null;
            }

            try
            {
                // Try to find the ID
                string youtubeVideoId = String.Empty;
                string urlLower = source.Url.ToLower();
                if (urlLower.Contains("youtube.com"))
                {
                    // Check for an attribution link
                    int attribution = urlLower.IndexOf("attribution_link?");
                    if (attribution != -1)
                    {
                        // We need to parse out the video id
                        // looks like this attribution_link?a=bhvqtDGQD6s&amp;u=%2Fwatch%3Fv%3DrK0D1ehO7CA%26feature%3Dshare
                        int uIndex = urlLower.IndexOf("u=", attribution);
                        string encodedUrl = source.Url.Substring(uIndex + 2);
                        string decodedUrl = WebUtility.UrlDecode(encodedUrl);
                        urlLower = decodedUrl.ToLower();
                        // At this point urlLower should be something like "v=jfkldfjl&feature=share"
                    }

                    int beginId = urlLower.IndexOf("v=");
                    int endId = urlLower.IndexOf("&", beginId);
                    if (beginId != -1)
                    {
                        if (endId == -1)
                        {
                            endId = urlLower.Length;
                        }
                        // Important! Since this might be case sensitive use the original url!
                        beginId += 2;
                        youtubeVideoId = source.Url.Substring(beginId, endId - beginId);
                    }
                }
                else if (urlLower.Contains("youtu.be"))
                {
                    int domain = urlLower.IndexOf("youtu.be");
                    int beginId = urlLower.IndexOf("/", domain);
                    int endId = urlLower.IndexOf("?", beginId);
                    // If we can't find a ? search for a &
                    if (endId == -1)
                    {
                        endId = urlLower.IndexOf("&", beginId);
                    }

                    if (beginId != -1)
                    {
                        if (endId == -1)
                        {
                            endId = urlLower.Length;
                        }
                        // Important! Since this might be case sensitive use the original url!
                        beginId++;
                        youtubeVideoId = source.Url.Substring(beginId, endId - beginId);
                    }
                }

                return youtubeVideoId;
            }
            catch (Exception)
            {
                App.BaconMan.TelemetryMan.ReportEvent("YoutubeString", "Failed to find youtube video");
            }
            return null;
        }
コード例 #28
0
        /// <summary>
        /// Tries to get a youtube link from a post. If it fails
        /// it returns null.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        private static async Task<YouTubeUri> GetYouTubeVideoUrl(ContentPanelSource source)
        {
            if (String.IsNullOrWhiteSpace(source.Url))
            {
                return null;
            }

            try
            {
                // Try to find the ID
                string youtubeVideoId = TryToGetYouTubeId(source);

                if (!String.IsNullOrWhiteSpace(youtubeVideoId))
                {
                    // We found it!
                    // #todo make this quality an option dependent on device!
                    return await YouTube.GetVideoUriAsync(youtubeVideoId, YouTubeQuality.QualityMedium);
                }
            }
            catch (Exception)
            {
                App.BaconMan.TelemetryMan.ReportEvent("YoutubeString", "Failed to find youtube video");
            }

            return null;
        }
コード例 #29
0
 /// <summary>
 /// Called by the host when it queries if we can handle a post.
 /// </summary>
 /// <param name="post"></param>
 /// <returns></returns>
 static public bool CanHandlePost(ContentPanelSource source)
 {
     // If it is a self post and it has text it is for us.
     return source.IsSelf && !String.IsNullOrWhiteSpace(source.SelfText);
 }
コード例 #30
0
        /// <summary>
        /// Attempts to get a youtube id from a url.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static string TryToGetYouTubeId(ContentPanelSource source)
        {
            if (String.IsNullOrWhiteSpace(source.Url))
            {
                return(null);
            }

            try
            {
                // Try to find the ID
                string youtubeVideoId = String.Empty;
                string postUrl        = WebUtility.HtmlDecode(source.Url);
                string urlLower       = postUrl.ToLower();
                if (urlLower.Contains("youtube.com"))
                {
                    // Check for an attribution link
                    int attribution = urlLower.IndexOf("attribution_link?");
                    if (attribution != -1)
                    {
                        // We need to parse out the video id
                        // looks like this attribution_link?a=bhvqtDGQD6s&amp;u=%2Fwatch%3Fv%3DrK0D1ehO7CA%26feature%3Dshare
                        int    uIndex     = urlLower.IndexOf("u=", attribution);
                        string encodedUrl = postUrl.Substring(uIndex + 2);
                        postUrl  = WebUtility.UrlDecode(encodedUrl);
                        urlLower = postUrl.ToLower();
                        // At this point urlLower should be something like "v=jfkldfjl&feature=share"
                    }

                    int beginId = urlLower.IndexOf("v=");
                    int endId   = urlLower.IndexOf("&", beginId);
                    if (beginId != -1)
                    {
                        if (endId == -1)
                        {
                            endId = urlLower.Length;
                        }
                        // Important! Since this might be case sensitive use the original url!
                        beginId       += 2;
                        youtubeVideoId = postUrl.Substring(beginId, endId - beginId);
                    }
                }
                else if (urlLower.Contains("youtu.be"))
                {
                    int domain  = urlLower.IndexOf("youtu.be");
                    int beginId = urlLower.IndexOf("/", domain);
                    int endId   = urlLower.IndexOf("?", beginId);
                    // If we can't find a ? search for a &
                    if (endId == -1)
                    {
                        endId = urlLower.IndexOf("&", beginId);
                    }

                    if (beginId != -1)
                    {
                        if (endId == -1)
                        {
                            endId = urlLower.Length;
                        }
                        // Important! Since this might be case sensitive use the original url!
                        beginId++;
                        youtubeVideoId = postUrl.Substring(beginId, endId - beginId);
                    }
                }

                return(youtubeVideoId);
            }
            catch (Exception)
            {
                App.BaconMan.TelemetryMan.ReportEvent("YoutubeString", "Failed to find youtube video");
            }
            return(null);
        }