using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Net;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.SessionState;
using DotNet.FrameWork.Data;

namespace HteamDd.Web.ashx
{
    /// <summary>
    /// Dd_search_video_image 的摘要说明
    /// </summary>
    public class Dd_search_video_image : IHttpHandler,IRequiresSessionState
    {
        HaTeam.Dd.BLL.Dd_Video bll = new HaTeam.Dd.BLL.Dd_Video();
        HaTeam.Dd.Model.Dd_Video model = new HaTeam.Dd.Model.Dd_Video();
        public void ProcessRequest(HttpContext context)
        {
           
                string mark = context.Request["mark"].ToString();
                if (mark == "1")
                {
                    string url = context.Request["url"].ToString();//用户输入视频地址
                    try
                    {
                        string html = GetSourceCode(context,url);
                        int title_indexof = html.IndexOf("<meta name="+'"'+"description");
                        string title_sum = html.Substring(title_indexof+33,100);
                        string [] arr=title_sum.Split('"');
                        string discript = arr[1];

                        string imageUrl = GetVideoImgUrl(html);//图片地址                      
                        context.Response.Write(imageUrl + "," + discript);
                    }
                    catch
                    {
                        context.Response.Write("error");
                    }
                }
                             
        }

     
        public string GetPlayUrl(string url)
        {
            int index = url.IndexOf("id_",20,25);
            string subString = url.Substring(index,21);
            string[] sub = subString.Split('.');
            string urlname = sub[0];
            string[] urlid = urlname.Split('_');
            return urlid[1];
        }
        private string GetVideoImgUrl(string html)
        {
            int index = html.IndexOf("|http://g",25000, 10000);
            string subString = html.Substring(index, 96);
            string[] maybeUrl = subString.Split('|');
            return maybeUrl[1];

        }
        private string GetSourceCode(HttpContext context,string url)
        {
            WebRequest req = WebRequest.Create(url);  //获取网页源代码
            string html = new StreamReader(req.GetResponse().GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();  //需指定网页的编码方式，如gb2312或 utf-8
            context.Session["html"] = html;
            return html;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}