OSDN Git Service

TweenMain.http を削除, HttpConnection.GlobalHttpClient をなるべく使用する
[opentween/open-tween.git] / OpenTween / Thumbnail / Services / MetaThumbnailService.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Net;
26 using System.Net.Http;
27 using System.Text;
28 using System.Text.RegularExpressions;
29 using System.Threading;
30 using System.Threading.Tasks;
31
32 namespace OpenTween.Thumbnail.Services
33 {
34     /// <summary>
35     /// og:image や twitter:image をスクレイピングしてサムネイルURLを抽出する
36     /// </summary>
37     class MetaThumbnailService : IThumbnailService
38     {
39         protected static Regex metaPattern = new Regex("<meta (name|property)=[\"'](?<name>.+?)[\"'] (content|value)=[\"'](?<content>.+?)[\"']");
40         protected static string[] propertyNames = { "twitter:image", "og:image" };
41
42         protected HttpClient http
43         {
44             get { return this.localHttpClient ?? HttpConnection.GlobalHttpClient; }
45         }
46         private readonly HttpClient localHttpClient;
47
48         protected readonly Regex regex;
49
50         public MetaThumbnailService(string urlPattern)
51             : this(null, urlPattern)
52         {
53         }
54
55         public MetaThumbnailService(HttpClient http, string urlPattern)
56         {
57             this.localHttpClient = http;
58             this.regex = new Regex(urlPattern);
59         }
60
61         public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
62         {
63             if (!this.regex.IsMatch(url))
64                 return null;
65
66             try
67             {
68                 var content = await this.FetchImageUrlAsync(url, token)
69                     .ConfigureAwait(false);
70
71                 var thumbnailUrl = this.GetThumbnailUrl(content);
72                 if (string.IsNullOrEmpty(thumbnailUrl)) return null;
73
74                 return new ThumbnailInfo
75                 {
76                     ImageUrl = url,
77                     ThumbnailUrl = thumbnailUrl,
78                     TooltipText = null,
79                 };
80             }
81             catch (HttpRequestException) { }
82
83             return null;
84         }
85
86         protected virtual string GetThumbnailUrl(string html)
87         {
88             var matches = MetaThumbnailService.metaPattern.Matches(html);
89
90             foreach (Match match in matches)
91             {
92                 var propertyName = match.Groups["name"].Value;
93                 if (MetaThumbnailService.propertyNames.Contains(propertyName))
94                 {
95                     return match.Groups["content"].Value;
96                 }
97             }
98
99             return null;
100         }
101
102         protected virtual async Task<string> FetchImageUrlAsync(string url, CancellationToken token)
103         {
104             using (var response = await this.http.GetAsync(url, token).ConfigureAwait(false))
105             {
106                 response.EnsureSuccessStatusCode();
107
108                 return await response.Content.ReadAsStringAsync()
109                     .ConfigureAwait(false);
110             }
111         }
112     }
113 }