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;
30 using System.Net.Http;
32 using System.Text.RegularExpressions;
33 using System.Threading;
34 using System.Threading.Tasks;
36 using System.Xml.Linq;
37 using OpenTween.Connection;
38 using OpenTween.Models;
40 namespace OpenTween.Thumbnail.Services
42 class Nicovideo : IThumbnailService
44 public static readonly Regex UrlPatternRegex =
45 new Regex(@"^https?://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$");
47 public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
49 var match = Nicovideo.UrlPatternRegex.Match(url);
55 var requestUri = new Uri("http://www.nicovideo.jp/api/getthumbinfo/" + match.Groups["id"].Value);
56 var responseText = await Networking.Http.GetStringAsync(requestUri);
58 var xdoc = XDocument.Parse(responseText);
60 var responseElement = xdoc.Element("nicovideo_thumb_response");
61 if (responseElement == null || responseElement.Attribute("status").Value != "ok")
64 var thumbElement = responseElement.Element("thumb");
65 if (thumbElement == null)
68 var thumbUrlElement = thumbElement.Element("thumbnail_url");
69 if (thumbUrlElement == null)
72 return new ThumbnailInfo
75 ThumbnailImageUrl = thumbUrlElement.Value,
76 TooltipText = BuildTooltip(thumbElement),
80 catch (HttpRequestException) { }
85 internal static string BuildTooltip(XElement thumbElement)
87 var tooltip = new StringBuilder(200);
89 var titleElement = thumbElement.Element("title");
90 if (titleElement != null)
92 tooltip.Append(Properties.Resources.NiconicoInfoText1);
93 tooltip.Append(titleElement.Value);
97 var lengthElement = thumbElement.Element("length");
98 if (lengthElement != null)
100 tooltip.Append(Properties.Resources.NiconicoInfoText2);
101 tooltip.Append(lengthElement.Value);
102 tooltip.AppendLine();
105 var firstRetrieveElement = thumbElement.Element("first_retrieve");
106 DateTime firstRetrieveDate;
107 if (firstRetrieveElement != null && DateTime.TryParse(firstRetrieveElement.Value, out firstRetrieveDate))
109 tooltip.Append(Properties.Resources.NiconicoInfoText3);
110 tooltip.Append(firstRetrieveDate.ToString());
111 tooltip.AppendLine();
114 var viewCounterElement = thumbElement.Element("view_counter");
115 if (viewCounterElement != null)
117 tooltip.Append(Properties.Resources.NiconicoInfoText4);
118 tooltip.Append(viewCounterElement.Value);
119 tooltip.AppendLine();
122 var commentNumElement = thumbElement.Element("comment_num");
123 if (commentNumElement != null)
125 tooltip.Append(Properties.Resources.NiconicoInfoText5);
126 tooltip.Append(commentNumElement.Value);
127 tooltip.AppendLine();
130 var mylistCounterElement = thumbElement.Element("mylist_counter");
131 if (mylistCounterElement != null)
133 tooltip.Append(Properties.Resources.NiconicoInfoText6);
134 tooltip.Append(mylistCounterElement.Value);
135 tooltip.AppendLine();
138 return tooltip.ToString();