Web视频分享处理类库的设计

最近做一个仿微博的项目,需要用到类似微博中的微博分享;

用户分享一个视频页面的链接(优酷、土豆、酷6等);

系统要根据这个链接,向视频网站的api请求视频的基本信息,得到诸如视频swf文件地址、视频缩略图、视频时长、描述、标题等等信息;

看完下面文章

http://www.cnblogs.com/leoo2sk/archive/2009/06/17/di-and-ioc.html

仿照着做了点设计:

通过一个反射工厂:VideoFactory 得到符合IVideoSite接口的具体服务类,具体的api操作都在具体服务类中

IVideoSite用来封装不变的方面也就是对视频的操作方法,变化的是视频的提供网站

反射工厂保证了以后如果有新的视频网站,不需要修改现有代码,只需实现特定的服务类即可,

调用时使用反射工厂传入类名,使得设计符合OCP原则,即开放封闭原则

Microsoft practice unity

IVideoSite接口:
 1     ///<summary>
2 /// 视频接口
3 ///</summary>
4 public interface IVideoSite
5 {
6
7 ///<summary>
8 /// 获取视频信息
9 ///</summary>
10 ///<param name="pageUrl">视频页面地址</param>
11 VideoDetail GetVideoDetail(string pageUrl);
12 }//end IVideoSite

土豆网具体服务类:

 1     ///<summary>
2 /// 土豆网
3 ///</summary>
4 internal class TuDou : IVideoSite
5 {
6
7 public TuDou()
8 {
9
10 }
11
12
13 ///<summary>
14 /// 获取视频信息
15 ///</summary>
16 ///<param name="pageUrl">视频页面地址</param>
17 public VideoDetail GetVideoDetail(string pageUrl)
18 {
19 string rgxStrSite = ConfigurationManager.AppSettings["SiteUrlRegex_TuDou"];
20
21
22 if ((new Regex(rgxStrSite)).IsMatch(pageUrl))//判断是否是合法视频地址
23 {
24 //取视频唯一标志
25 string itemCode = GetItemCode(pageUrl);
26 if (!string.IsNullOrEmpty(itemCode))// 如果取到了视频唯一标志
27 {
28 //请求土豆api
29 string apiUrl = ConfigurationManager.AppSettings["videoInfoUrl_TuDou"];
30 apiUrl = string.Format(apiUrl, "myKey", "json", itemCode);
31 JsonHelper help = new JsonHelper();
32 string jsonString = help.SendRequest(apiUrl);
33
34 var anType = new
35 {
36 multiResult = new
37 {
38 results = new[]
39 {
40 new
41 {
42 description = string.Empty,
43 title = string.Empty,
44 picUrl = string.Empty,
45 totalTime = 0,
46 outerPlayerUrl = string.Empty,
47 picChoiceUrl = new string[] { "", "" }
48 }
49 }
50 }
51 };
52
53 //返回视频信息
54 var tudou = JsonConvert.DeserializeAnonymousType(jsonString, anType);
55 return VideoDetail.CreateSimpleInstance(DateTime.Now,
56 tudou.multiResult.results[0].title,
57 tudou.multiResult.results[0].description,
58 tudou.multiResult.results[0].picUrl,
59 tudou.multiResult.results[0].outerPlayerUrl);
60 }
61
62 }
63 return null;
64 }
65
66
67 ///<summary>
68 /// 查找视频唯一标志符
69 ///</summary>
70 ///<param name="pageUrl">页面地址</param>
71 ///<returns></returns>
72 private string GetItemCode ( string pageUrl )
73 {
74 string rgxItemCode = ConfigurationManager.AppSettings["videoCodeRegex_TuDou"];
75
76 Regex rgx = new Regex(rgxItemCode);
77 if (rgx.IsMatch(pageUrl))
78 {
79 return rgx.Match(pageUrl).ToString();
80 }
81 return string.Empty;
82 }
83
84
85 }//end TuDou

反射工厂类:

View Code
 1     ///<summary>
2 /// 工厂类
3 ///</summary>
4 public class VideoFactory
5 {
6 ///<summary>
7 /// 视频站类型
8 ///</summary>
9 private string _siteType = string.Empty;
10 ///<summary>
11 ///
12 ///</summary>
13 static VideoFactory ()
14 {
15
16 }
17
18
19 ///<summary>
20 /// 工厂类构造函数
21 /// 通过反射获得具体的
22 ///</summary>
23 ///<param name="site">网站类名称</param>
24 public VideoFactory ( string site )
25 {
26 _siteType = site;
27 }
28
29
30 ///<summary>
31 /// 创建视频处理类实例
32 ///</summary>
33 public IVideoSite MakeVideoSite ()
34 {
35 return Assembly.Load("VideoSiteSDK").CreateInstance("VideoSiteSDK." + _siteType) as IVideoSite;
36 }
37
38 }//end VideoFactory




原文地址:https://www.cnblogs.com/scottgu/p/2230729.html