Is there a way to combine this block of code with the Post Serving event?

Topics: Controls
Jul 28, 2014 at 11:19 AM
Edited Jul 28, 2014 at 11:20 AM
I am trying to combine this block of code with the 2nd one to embed Youtube videos by just pasting the url. The 2nd block is the current way that embedding youtube is handled so I need both so as not break previous blog entries. I have tried everything I can think of and either the frist block does not work or it breaks the previous of embedding. Also can someone check if I have the regex properly escaped in c#? Any ideas?
 string regex__2 = @"http(?:s?)://(?:www\\.)?youtu(?:be\\.com/watch\\?v=|\\.be/)([\\w\\-]+)(&(amp;)?[\\w\\?=‌​]*)?";
    MatchCollection matches1 = Regex.Matches(e.Body, regex__2);
    if (matches1.Count == 0)
    {
        return;
    }
    Int32 width1 = 620;
    Int32 height1 = 349;
    WillStrohl.API.oEmbed.Providers.YouTube objYouTube = new WillStrohl.API.oEmbed.Providers.YouTube();
    string strVideo;

    for (int x = 0; x <= matches1.Count - 1; x++)
    {
        string mediaFile = matches1[x].Value;
        strVideo = objYouTube.GetVideo(matches1[x].Value, width1, height1);
        e.Body = e.Body.Replace(matches1[x].Value, strVideo);
    }


  private void Post_Serving(object sender, ServingEventArgs e)
{
    if (e.Location != ServingLocation.PostList && e.Location != ServingLocation.SinglePost)
        return;

    string regex__1 = @"\[youtube:.*?\]";
    MatchCollection matches = Regex.Matches(e.Body, regex__1);

    if (matches.Count == 0)
        return;

    Int32 width = default(Int32);
    Int32 height = default(Int32);
    Int32 border = default(Int32);
    String color1 = default(String);
    String color2 = default(String);
    String BaseURL = default(String);

    try
    {
        width = Int32.Parse(settings.GetSingleValue("width"));
        height = Int32.Parse(settings.GetSingleValue("height"));
        border = Int32.Parse(settings.GetSingleValue("border"));
        color1 = settings.GetSingleValue("color1");
        color2 = settings.GetSingleValue("color2");
        BaseURL = "http://www.youtube.com/embed/";
        // BaseURL = "http://www.youtube.com/v/";
    }
    catch
    {
        return;
    }


    for (int i = 0; i < matches.Count; i++)
    {
        Int32 length = "[youtube:".Length;
        string mediaFile = matches[i].Value.Substring(length, matches[i].Value.Length - length - 1);

        string player = @"<iframe width=""{2}"" height=""{3}"" src=""{7}{1}"" frameborder=""0"" allowfullscreen></iframe>";
        /*string player = @"<div id=""YouTubePlayer_{0}"" style=""width:{2}px; height:{3}px;"">
                             <object width=""{2}"" height=""{3}"">
                             <param name=""movie"" value=""{7}{1}&fs=1&border={4}&color1=0x{5}&color2=0x{6}""></param>
                             <param name=""allowFullScreen"" value=""true""></param>
                             <embed src=""{7}{1}&fs=1&border={4}&color1=0x{5}&color2=0x{6}""
                             type = ""application/x-shockwave-flash""
                             width=""{2}"" height=""{3}"" allowfullscreen=""true""></embed>
                             </object>
                         </div>";*/

        e.Body = e.Body.Replace(matches[i].Value, String.Format(player, i, mediaFile, width, height, border, color1, color2, BaseURL));

    }
}
Jul 28, 2014 at 7:16 PM
Solved it myself:
private void Post_Serving(object sender, ServingEventArgs e)
{
    if (e.Location != ServingLocation.PostList && e.Location != ServingLocation.SinglePost)
        return;



   // Int32 width = default(Int32);
    //Int32 height = default(Int32);
    Int32 border = default(Int32);
    String color1 = default(String);
    String color2 = default(String);
    String BaseURL = default(String);

    try
    {
       // width = Int32.Parse(settings.GetSingleValue("width"));
        //height = Int32.Parse(settings.GetSingleValue("height"));
        border = Int32.Parse(settings.GetSingleValue("border"));
        color1 = settings.GetSingleValue("color1");
        color2 = settings.GetSingleValue("color2");
        BaseURL = "http://www.youtube.com/embed/";
        // BaseURL = "http://www.youtube.com/v/";
    }
    catch
    {
        return;
    }


    {
        string regex__1 = null;
        MatchCollection matches = default(MatchCollection);
        Int32 width = 620;
        Int32 height = 349;
        string mediaFile = null;

        string regex__2 = "\\[youtube:.*?\\]";
        regex__1 = "http(?:s?)://(?:www\\.)?youtu(?:be\\.com/watch\\?v=|\\.be/)([\\w\\-]+)(&(amp;)?[\\w\\?=‌​]*)?";

        if (Regex.IsMatch(e.Body, regex__1))
        {
            matches = Regex.Matches(e.Body, regex__1);
            if (matches.Count == 0)
                return;

            WillStrohl.API.oEmbed.Providers.YouTube objYouTube = new WillStrohl.API.oEmbed.Providers.YouTube();
            string strVideo = null;
            for (int i = 0; i <= matches.Count - 1; i += 1)
            {
                mediaFile = matches[i].Value;
                strVideo = objYouTube.GetVideo(matches[i].Value, 620, 349);
                e.Body = e.Body.Replace(matches[i].Value, strVideo);
            }


        }
        else if (Regex.IsMatch(e.Body, regex__2))
        {
            matches = Regex.Matches(e.Body, regex__2);

            if (matches.Count == 0)
                return;

           // string BaseURL = "http://www.youtube.com/v/";
            Int32 length = default(Int32);
            string player = null;
            for (int i = 0; i <= matches.Count - 1; i += 1)
            {
                length = "[youtube:".Length;
                mediaFile = matches[i].Value.Substring(length, matches[i].Value.Length - length - 1);
                player = "<div class=\"video-container\"><iframe width=\"{2}\" height=\"{3}\" src=\"{4}{1}\" frameborder=\"0\" allowfullscreen></iframe></div>";
                e.Body = e.Body.Replace(matches[i].Value, String.Format(player, i, mediaFile, width, height, BaseURL));
            }
        }
    }


}
Marked as answer by lsudvm on 7/28/2014 at 11:16 AM