示例#1
0
		public static ActionResult SendComment(this HttpListenerResponse response, CommentResponse r)
		{
			//buffer
			byte[] buffer = new byte[r.BufferSize];

			if (r.PlayerName.Trim().Equals("acfun", StringComparison.CurrentCultureIgnoreCase))
			{
				if (r.CommentNumber == 0)
				{
					byte[] bf = r.Encode.GetBytes("[{}]");
					response.OutputStream.Write(bf, 0, bf.Length);
					response.OutputStream.Close();
					return ActionResult.Handled;
				}
			}

			//解析所有弹幕文件
			List<StandardItem> items = new List<StandardItem>();
			foreach (var file in r.SubtitleFiles)
			{
				string filepath;
				if (Path.IsPathRooted(file))
					filepath = file;
				else
					filepath = Path.Combine(r.StartupPath, file);
				if (!File.Exists(filepath)) continue;

				var identifier = new CommentIdentifier();
				string belongto = identifier.Identify(filepath, Encoding.UTF8);
				IFormatter f = null;
				if (belongto.Equals("acfun"))
				{
					f = new AcPlay.Formatter.AcfunFormatter();
				}
				else if (belongto.Equals("bilibili"))
				{
					f = new AcPlay.Formatter.BilibiliFormatter();
				}
				else if (belongto.Equals("doupao"))
				{
					f = new AcPlay.Formatter.DoupaoFormatter();
				}
				if (f != null)
					items.AddRange(f.Format(File.ReadAllText(filepath)));
			}

			//生成合并后的弹幕文件
			string finalCommentFile;
			ICombiner c = null;
			if (r.PlayerName.Equals("acfun", StringComparison.CurrentCultureIgnoreCase))
			{
				c = new AcPlay.Combiner.AcfunCombiner();
			}
			else if (r.PlayerName.Equals("bilibili", StringComparison.CurrentCultureIgnoreCase))
			{
				c = new AcPlay.Combiner.BilibiliCombiner();
			}
			finalCommentFile = c.Combine(items);

			//Content-Length
			//context.Response.ContentLength64 = finalCommentFile.Length;

			//content-type
			switch (r.PlayerName.ToUpper().Trim())
			{
				case "ACFUN":
					response.ContentType = "application/json";
					break;
				case "BILIBILI":
					response.ContentType = "text/xml";
					break;
				default:
					response.ContentType = "text/xml";
					break;
			}


			//open output stream
			using (var net = response.OutputStream)
			{
				//memory stream
				using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(finalCommentFile)))
				{
					while (true)
					{
						int count = ms.Read(buffer, 0, buffer.Length);
						if (count <= 0)
							break;
						net.Write(buffer, 0, count);
					}
				}//end memory stream
			}//end write

			finalCommentFile = null;
			items.Clear();

			return ActionResult.Handled;
		}
示例#2
0
        public static ActionResult SendComment(this HttpListenerResponse response, CommentResponse r)
        {
            //buffer
            byte[] buffer = new byte[r.BufferSize];

            if (r.PlayerName.Trim().Equals("acfun", StringComparison.CurrentCultureIgnoreCase))
            {
                if (r.CommentNumber == 0)
                {
                    byte[] bf = r.Encode.GetBytes("[{}]");
                    response.OutputStream.Write(bf, 0, bf.Length);
                    response.OutputStream.Close();
                    return(ActionResult.Handled);
                }
            }

            //解析所有弹幕文件
            List <StandardItem> items = new List <StandardItem>();

            foreach (var file in r.SubtitleFiles)
            {
                string filepath;
                if (Path.IsPathRooted(file))
                {
                    filepath = file;
                }
                else
                {
                    filepath = Path.Combine(r.StartupPath, file);
                }
                if (!File.Exists(filepath))
                {
                    continue;
                }

                var        identifier = new CommentIdentifier();
                string     belongto   = identifier.Identify(filepath, Encoding.UTF8);
                IFormatter f          = null;
                if (belongto.Equals("acfun"))
                {
                    f = new AcPlay.Formatter.AcfunFormatter();
                }
                else if (belongto.Equals("bilibili"))
                {
                    f = new AcPlay.Formatter.BilibiliFormatter();
                }
                else if (belongto.Equals("doupao"))
                {
                    f = new AcPlay.Formatter.DoupaoFormatter();
                }
                if (f != null)
                {
                    items.AddRange(f.Format(File.ReadAllText(filepath)));
                }
            }

            //生成合并后的弹幕文件
            string    finalCommentFile;
            ICombiner c = null;

            if (r.PlayerName.Equals("acfun", StringComparison.CurrentCultureIgnoreCase))
            {
                c = new AcPlay.Combiner.AcfunCombiner();
            }
            else if (r.PlayerName.Equals("bilibili", StringComparison.CurrentCultureIgnoreCase))
            {
                c = new AcPlay.Combiner.BilibiliCombiner();
            }
            finalCommentFile = c.Combine(items);

            //Content-Length
            //context.Response.ContentLength64 = finalCommentFile.Length;

            //content-type
            switch (r.PlayerName.ToUpper().Trim())
            {
            case "ACFUN":
                response.ContentType = "application/json";
                break;

            case "BILIBILI":
                response.ContentType = "text/xml";
                break;

            default:
                response.ContentType = "text/xml";
                break;
            }


            //open output stream
            using (var net = response.OutputStream)
            {
                //memory stream
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(finalCommentFile)))
                {
                    while (true)
                    {
                        int count = ms.Read(buffer, 0, buffer.Length);
                        if (count <= 0)
                        {
                            break;
                        }
                        net.Write(buffer, 0, count);
                    }
                }        //end memory stream
            }            //end write

            finalCommentFile = null;
            items.Clear();

            return(ActionResult.Handled);
        }