OSDN Git Service

6f688b073725a2ceba4d8932d7261263c353535c
[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.Net.Http;
31 using System.Text;
32 using System.Text.RegularExpressions;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using System.Xml;
36 using System.Xml.Linq;
37 using OpenTween.Connection;
38 using OpenTween.Models;
39
40 namespace OpenTween.Thumbnail.Services
41 {
42     class Nicovideo : IThumbnailService
43     {
44         public static readonly Regex UrlPatternRegex =
45             new Regex(@"^https?://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$");
46
47         public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
48         {
49             var match = Nicovideo.UrlPatternRegex.Match(url);
50             if (!match.Success)
51                 return null;
52
53             try
54             {
55                 var requestUri = new Uri("http://www.nicovideo.jp/api/getthumbinfo/" + match.Groups["id"].Value);
56                 var responseText = await Networking.Http.GetStringAsync(requestUri);
57
58                 var xdoc = XDocument.Parse(responseText);
59
60                 var responseElement = xdoc.Element("nicovideo_thumb_response");
61                 if (responseElement == null || responseElement.Attribute("status").Value != "ok")
62                     return null;
63
64                 var thumbElement = responseElement.Element("thumb");
65                 if (thumbElement == null)
66                     return null;
67
68                 var thumbUrlElement = thumbElement.Element("thumbnail_url");
69                 if (thumbUrlElement == null)
70                     return null;
71
72                 return new ThumbnailInfo
73                 {
74                     MediaPageUrl = url,
75                     ThumbnailImageUrl = thumbUrlElement.Value,
76                     TooltipText = BuildTooltip(thumbElement),
77                     IsPlayable = true,
78                 };
79             }
80             catch (HttpRequestException) { }
81
82             return null;
83         }
84
85         internal static string BuildTooltip(XElement thumbElement)
86         {
87             var tooltip = new StringBuilder(200);
88
89             var titleElement = thumbElement.Element("title");
90             if (titleElement != null)
91             {
92                 tooltip.Append(Properties.Resources.NiconicoInfoText1);
93                 tooltip.Append(titleElement.Value);
94                 tooltip.AppendLine();
95             }
96
97             var lengthElement = thumbElement.Element("length");
98             if (lengthElement != null)
99             {
100                 tooltip.Append(Properties.Resources.NiconicoInfoText2);
101                 tooltip.Append(lengthElement.Value);
102                 tooltip.AppendLine();
103             }
104
105             var firstRetrieveElement = thumbElement.Element("first_retrieve");
106             DateTime firstRetrieveDate;
107             if (firstRetrieveElement != null && DateTime.TryParse(firstRetrieveElement.Value, out firstRetrieveDate))
108             {
109                 tooltip.Append(Properties.Resources.NiconicoInfoText3);
110                 tooltip.Append(firstRetrieveDate.ToString());
111                 tooltip.AppendLine();
112             }
113
114             var viewCounterElement = thumbElement.Element("view_counter");
115             if (viewCounterElement != null)
116             {
117                 tooltip.Append(Properties.Resources.NiconicoInfoText4);
118                 tooltip.Append(viewCounterElement.Value);
119                 tooltip.AppendLine();
120             }
121
122             var commentNumElement = thumbElement.Element("comment_num");
123             if (commentNumElement != null)
124             {
125                 tooltip.Append(Properties.Resources.NiconicoInfoText5);
126                 tooltip.Append(commentNumElement.Value);
127                 tooltip.AppendLine();
128             }
129
130             var mylistCounterElement = thumbElement.Element("mylist_counter");
131             if (mylistCounterElement != null)
132             {
133                 tooltip.Append(Properties.Resources.NiconicoInfoText6);
134                 tooltip.Append(mylistCounterElement.Value);
135                 tooltip.AppendLine();
136             }
137
138             return tooltip.ToString();
139         }
140     }
141 }