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.
10 // This file is part of OpenTween.
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)
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
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.
28 using System.Collections.Generic;
31 using System.Text.RegularExpressions;
32 using System.Threading;
33 using System.Threading.Tasks;
35 using OpenTween.Connection;
37 namespace OpenTween.Thumbnail.Services
39 class Nicovideo : IThumbnailService
41 public static readonly Regex UrlPatternRegex =
42 new Regex(@"^http://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$");
44 public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
48 var match = Nicovideo.UrlPatternRegex.Match(url);
52 var apiUrl = "http://www.nicovideo.jp/api/getthumbinfo/" + match.Groups["id"].Value;
54 var http = new HttpVarious();
58 if (http.GetData(apiUrl, null, out src, 0, out errmsg, Networking.GetUserAgentString()))
60 var sb = new StringBuilder();
61 var xdoc = new XmlDocument();
65 var status = xdoc.SelectSingleNode("/nicovideo_thumb_response").Attributes["status"].Value;
68 imgurl = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/thumbnail_url").InnerText;
75 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/title").InnerText;
76 if (!string.IsNullOrEmpty(tmp))
78 sb.Append(Properties.Resources.NiconicoInfoText1);
90 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/length").InnerText;
91 if (!string.IsNullOrEmpty(tmp))
93 sb.Append(Properties.Resources.NiconicoInfoText2);
105 var tm = new DateTime();
106 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/first_retrieve").InnerText;
107 if (DateTime.TryParse(tmp, out tm))
109 sb.Append(Properties.Resources.NiconicoInfoText3);
110 sb.Append(tm.ToString());
121 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/view_counter").InnerText;
122 if (!string.IsNullOrEmpty(tmp))
124 sb.Append(Properties.Resources.NiconicoInfoText4);
136 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/comment_num").InnerText;
137 if (!string.IsNullOrEmpty(tmp))
139 sb.Append(Properties.Resources.NiconicoInfoText5);
150 tmp = xdoc.SelectSingleNode("/nicovideo_thumb_response/thumb/mylist_counter").InnerText;
151 if (!string.IsNullOrEmpty(tmp))
153 sb.Append(Properties.Resources.NiconicoInfoText6);
163 else if (status == "fail")
165 var errcode = xdoc.SelectSingleNode("/nicovideo_thumb_response/error/code").InnerText;
171 errmsg = "UnknownResponse";
179 errmsg = "Invalid XML";
182 if (!string.IsNullOrEmpty(imgurl))
184 return new ThumbnailInfo
187 ThumbnailUrl = imgurl,
188 TooltipText = sb.ToString().Trim()