OSDN Git Service

Merge pull request #22 from moccos/FixConn
[opentween/open-tween.git] / OpenTween / Thumbnail / Services / Nicovideo.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Text.RegularExpressions;
32 using System.Threading;
33 using System.Threading.Tasks;
34 using System.Xml;
35 using OpenTween.Connection;
36
37 namespace OpenTween.Thumbnail.Services
38 {
39     class Nicovideo : IThumbnailService
40     {
41         public static readonly Regex UrlPatternRegex =
42             new Regex(@"^http://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$");
43
44         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
45         {
46             return Task.Run(() =>
47             {
48                 var match = Nicovideo.UrlPatternRegex.Match(url);
49                 if (!match.Success)
50                     return null;
51
52                 var apiUrl = "http://www.nicovideo.jp/api/getthumbinfo/" + match.Groups["id"].Value;
53
54                 var http = new HttpVarious();
55                 var src = "";
56                 var imgurl = "";
57                 string errmsg;
58                 if (http.GetData(apiUrl, null, out src, 0, out errmsg, Networking.GetUserAgentString()))
59                 {
60                     var sb = new StringBuilder();
61                     var xdoc = new XmlDocument();
62                     try
63                     {
64                         xdoc.LoadXml(src);
65                         var status = xdoc.SelectSingleNode("/nicovideo_thumb_response").Attributes["status"].Value;
66                         if (status == "ok")
67                         {
68                             imgurl = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/thumbnail_url").InnerText;
69
70                             //ツールチップに動画情報をセットする
71                             string tmp;
72
73                             try
74                             {
75                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/title").InnerText;
76                                 if (!string.IsNullOrEmpty(tmp))
77                                 {
78                                     sb.Append(Properties.Resources.NiconicoInfoText1);
79                                     sb.Append(tmp);
80                                     sb.AppendLine();
81                                 }
82                             }
83                             catch (Exception)
84                             {
85
86                             }
87
88                             try
89                             {
90                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/length").InnerText;
91                                 if (!string.IsNullOrEmpty(tmp))
92                                 {
93                                     sb.Append(Properties.Resources.NiconicoInfoText2);
94                                     sb.Append(tmp);
95                                     sb.AppendLine();
96                                 }
97                             }
98                             catch (Exception)
99                             {
100
101                             }
102
103                             try
104                             {
105                                 var tm = new DateTime();
106                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/first_retrieve").InnerText;
107                                 if (DateTime.TryParse(tmp, out tm))
108                                 {
109                                     sb.Append(Properties.Resources.NiconicoInfoText3);
110                                     sb.Append(tm.ToString());
111                                     sb.AppendLine();
112                                 }
113                             }
114                             catch (Exception)
115                             {
116
117                             }
118
119                             try
120                             {
121                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/view_counter").InnerText;
122                                 if (!string.IsNullOrEmpty(tmp))
123                                 {
124                                     sb.Append(Properties.Resources.NiconicoInfoText4);
125                                     sb.Append(tmp);
126                                     sb.AppendLine();
127                                 }
128                             }
129                             catch (Exception)
130                             {
131
132                             }
133
134                             try
135                             {
136                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/comment_num").InnerText;
137                                 if (!string.IsNullOrEmpty(tmp))
138                                 {
139                                     sb.Append(Properties.Resources.NiconicoInfoText5);
140                                     sb.Append(tmp);
141                                     sb.AppendLine();
142                                 }
143                             }
144                             catch (Exception)
145                             {
146
147                             }
148                             try
149                             {
150                                 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/mylist_counter").InnerText;
151                                 if (!string.IsNullOrEmpty(tmp))
152                                 {
153                                     sb.Append(Properties.Resources.NiconicoInfoText6);
154                                     sb.Append(tmp);
155                                     sb.AppendLine();
156                                 }
157                             }
158                             catch (Exception)
159                             {
160
161                             }
162                         }
163                         else if (status == "fail")
164                         {
165                             var errcode = xdoc.SelectSingleNode("/nicovideo_thumb_response/error/code").InnerText;
166                             errmsg = errcode;
167                             imgurl = "";
168                         }
169                         else
170                         {
171                             errmsg = "UnknownResponse";
172                             imgurl = "";
173                         }
174
175                     }
176                     catch (Exception)
177                     {
178                         imgurl = "";
179                         errmsg = "Invalid XML";
180                     }
181
182                     if (!string.IsNullOrEmpty(imgurl))
183                     {
184                         return new ThumbnailInfo
185                         {
186                             ImageUrl = url,
187                             ThumbnailUrl = imgurl,
188                             TooltipText = sb.ToString().Trim()
189                         };
190                     }
191                 }
192
193                 return null;
194             }, token);
195         }
196     }
197 }