Monday, June 07, 2010

How to read value with in a external web page

Although it is not adviable, sometimes it might need to read some specific text with in external webpages as information extration. This post domonstrate how to read views within given video URL either YouTube or MetaCafe

string youTubeMarker = "<strong class=\"watch-view-count\">";
string metaCafeMarker = "<li>Views: <strong>";
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    ///
    /// I explan this using hard corded value
    /// But in real world please get this form your relevent textbox
    /// url = this.txtUrl.Text;
    /// You may not call this in page load
    /// But you may call this code in PostVideoLink event handler
    ///
    string url = "http://www.youtube.com/watch?v=3Kk-yZ7VpeA";
    string views = this.GetViews(url);
    Response.Write(string.Format("YouTube video views: {0}", views));
    url = "http://www.metacafe.com/watch/yt-ByGgMeQSK8Y
    /asp_net_tutorial_master_pages_site_navigation_part1/";
    views = this.GetViews(url);
    Response.Write(string.Format("MetaCafe video views: {0}", views));
}
protected void PostVideoLink(object sender, EventArgs e)
{
}
private string GetViews(string url)
{
    string marker = string.Empty;
    if (url.Contains("www.youtube.com"))
        marker = this.youTubeMarker;
    else if (url.Contains("www.metacafe.com"))
        marker = this.metaCafeMarker;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());
    if(string.IsNullOrEmpty(marker)) return string.Empty;
    string html = sr.ReadToEnd();
    int lb = html.IndexOf(marker) + marker.Length;
    int ub = html.IndexOf("<", lb);
    string views = html.Substring(lb, ub - lb);
    return views;
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...