YouTube Extension

Topics: Controls
Jun 19, 2009 at 12:57 AM

Hello,

I used a youtube extension from here:

http://buddylindsey.com/Blog/post/YouTube-BlogEngineNET-Extension.aspx

but it would only display one video on the article at a time. I modified it to show all videos on the article. I couldn't find any other ones that did the same thing, so I am posting it here. I probably won't be returning, so if this is the wrong place maybe someone can put it where it goes.

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
/// <summary>
/// Summary description for YouTube
/// </summary>
/// 
[Extension("Converts YouTube URL to video", "1.0.0", "Buddy Lindsey")]
public class YouTube
{
    public YouTube()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
    }

    private void Post_Serving(object sender, ServingEventArgs e)
    {
        string[] urls = FindUrl(e.Body);
        string id;
        string html;

        foreach (string s in urls)
        {
            if (!string.IsNullOrEmpty(s))
            {
                id = VideoID(s);
                html = "<br /><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + id + "&rel=1\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/" + id + "&rel=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object><br />";
                e.Body = e.Body.Replace(s, html);
            }
        }
    }

    private string[] FindUrl(string text)
    {
        char[] splitOn = { ' ', ';', '<', '>' };
        string[] strings = text.Split(splitOn);
        System.Collections.Generic.List<string> youtubeurls = new System.Collections.Generic.List<string>();

        foreach (string s in strings)
        {
            if (s.Contains("youtube.com/"))
            {
                youtubeurls.Add(s);
            }
        }
        return youtubeurls.ToArray();
    }

    private string VideoID(string text)
    {
        char[] splitOn = { '=' };
        string[] strings = text.Split(splitOn);

        if (!String.IsNullOrEmpty(strings[1]))
            return strings[1];

        return "";
    }
}

BTW, I am not a C# expert so, even though it works, it may not be the best form.

I hope it helps.