OSDN Git Service

using var を使用する
[opentween/open-tween.git] / OpenTween / Thumbnail / Services / ImgAzyobuziNet.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.Http;
26 using System.Runtime.Serialization.Json;
27 using System.Xml;
28 using System.Xml.Linq;
29 using System.Text.RegularExpressions;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using OpenTween.Connection;
33 using OpenTween.Models;
34
35 namespace OpenTween.Thumbnail.Services
36 {
37     class ImgAzyobuziNet : IThumbnailService, IDisposable
38     {
39         protected string[] ApiHosts = {
40             "https://img.azyobuzi.net/api/",
41             "https://img.opentween.org/api/",
42         };
43
44         protected string[] ExcludedServiceNames =
45         {
46             "Instagram",
47             "Twitter",
48             "Tumblr",
49             "Gyazo",
50         };
51
52         protected string ApiBase;
53         protected IEnumerable<Regex> UrlRegex = null;
54         protected Timer UpdateTimer;
55
56         protected HttpClient http
57             => this.localHttpClient ?? Networking.Http;
58
59         private readonly HttpClient localHttpClient;
60
61         private readonly object LockObj = new object();
62
63         public ImgAzyobuziNet(bool autoupdate)
64             : this(null, autoupdate)
65         {
66         }
67
68         public ImgAzyobuziNet(HttpClient http)
69             : this(http, autoupdate: false)
70         {
71         }
72
73         public ImgAzyobuziNet(HttpClient http, bool autoupdate)
74         {
75             this.UpdateTimer = new Timer(async _ => await this.LoadRegexAsync());
76             this.AutoUpdate = autoupdate;
77
78             this.Enabled = true;
79             this.DisabledInDM = true;
80
81             this.localHttpClient = http;
82         }
83
84         public bool AutoUpdate
85         {
86             get => this._AutoUpdate;
87             set
88             {
89                 if (value)
90                     this.StartAutoUpdate();
91                 else
92                     this.StopAutoUpdate();
93
94                 this._AutoUpdate = value;
95             }
96         }
97         private bool _AutoUpdate = false;
98
99         /// <summary>
100         /// img.azyobizi.net によるサムネイル情報の取得を有効にするか
101         /// </summary>
102         public bool Enabled { get; set; }
103
104         /// <summary>
105         /// ダイレクトメッセージに含まれる画像URLに対して img.azyobuzi.net を使用しない
106         /// </summary>
107         public bool DisabledInDM { get; set; }
108
109         protected void StartAutoUpdate()
110             => this.UpdateTimer.Change(0, 30 * 60 * 1000); // 30分おきに更新
111
112         protected void StopAutoUpdate()
113             => this.UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
114
115         public async Task LoadRegexAsync()
116         {
117             foreach (var host in this.ApiHosts)
118             {
119                 try
120                 {
121                     var result = await this.LoadRegexAsync(host)
122                         .ConfigureAwait(false);
123
124                     if (result) return;
125                 }
126                 catch (Exception)
127                 {
128 #if DEBUG
129                     throw;
130 #endif
131                 }
132             }
133
134             // どのサーバーも使用できない場合
135             lock (this.LockObj)
136             {
137                 this.UrlRegex = null;
138                 this.ApiBase = null;
139             }
140         }
141
142         public async Task<bool> LoadRegexAsync(string apiBase)
143         {
144             try
145             {
146                 var jsonBytes = await this.FetchRegexAsync(apiBase)
147                     .ConfigureAwait(false);
148
149                 using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max);
150                 var xElm = XElement.Load(jsonReader);
151
152                 if (xElm.Element("error") != null)
153                     return false;
154
155                 lock (this.LockObj)
156                 {
157                     this.UrlRegex = xElm.Elements("item")
158                         .Where(x => !this.ExcludedServiceNames.Contains(x.Element("name").Value))
159                         .Select(e => new Regex(e.Element("regex").Value, RegexOptions.IgnoreCase))
160                         .ToArray();
161
162                     this.ApiBase = apiBase;
163                 }
164
165                 return true;
166             }
167             catch (HttpRequestException) { } // サーバーが2xx以外のステータスコードを返した場合
168             catch (OperationCanceledException) { } // リクエストがタイムアウトした場合
169             catch (XmlException) { } // サーバーが不正なJSONを返した場合
170
171             return false;
172         }
173
174         protected virtual async Task<byte[]> FetchRegexAsync(string apiBase)
175         {
176             using var cts = new CancellationTokenSource(millisecondsDelay: 1000);
177             using var response = await this.http.GetAsync(apiBase + "regex.json", cts.Token)
178                 .ConfigureAwait(false);
179
180             response.EnsureSuccessStatusCode();
181
182             return await response.Content.ReadAsByteArrayAsync()
183                 .ConfigureAwait(false);
184         }
185
186         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
187         {
188             return Task.Run(() =>
189             {
190                 if (!this.Enabled)
191                     return null;
192
193                 if (this.DisabledInDM && post != null && post.IsDm)
194                     return null;
195
196                 lock (this.LockObj)
197                 {
198                     if (this.UrlRegex == null)
199                         return null;
200
201                     foreach (var regex in this.UrlRegex)
202                     {
203                         if (regex.IsMatch(url))
204                         {
205                             return new ThumbnailInfo
206                             {
207                                 MediaPageUrl = url,
208                                 ThumbnailImageUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
209                                 FullSizeImageUrl = this.ApiBase + "redirect?size=full&uri=" + Uri.EscapeDataString(url),
210                                 TooltipText = null,
211                             };
212                         }
213                     }
214                 }
215
216                 return null;
217             }, token);
218         }
219
220         public virtual void Dispose()
221             => this.UpdateTimer.Dispose();
222     }
223 }