1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
25 using System.Collections.Generic;
27 using System.Net.Http;
28 using System.Runtime.Serialization.Json;
30 using System.Xml.Linq;
31 using System.Text.RegularExpressions;
32 using System.Threading;
33 using System.Threading.Tasks;
34 using OpenTween.Connection;
35 using OpenTween.Models;
37 namespace OpenTween.Thumbnail.Services
39 class ImgAzyobuziNet : IThumbnailService, IDisposable
41 protected string[] ApiHosts = {
42 "https://img.azyobuzi.net/api/",
43 "https://img.opentween.org/api/",
46 protected string[] ExcludedServiceNames =
54 protected string? ApiBase;
55 protected IEnumerable<Regex>? UrlRegex = null;
56 protected Timer UpdateTimer;
58 protected HttpClient http
59 => this.localHttpClient ?? Networking.Http;
61 private readonly HttpClient? localHttpClient;
63 private readonly object LockObj = new object();
65 public ImgAzyobuziNet(bool autoupdate)
66 : this(null, autoupdate)
70 public ImgAzyobuziNet(HttpClient? http)
71 : this(http, autoupdate: false)
75 public ImgAzyobuziNet(HttpClient? http, bool autoupdate)
77 this.UpdateTimer = new Timer(async _ => await this.LoadRegexAsync());
78 this.AutoUpdate = autoupdate;
81 this.DisabledInDM = true;
83 this.localHttpClient = http;
86 public bool AutoUpdate
88 get => this._AutoUpdate;
92 this.StartAutoUpdate();
94 this.StopAutoUpdate();
96 this._AutoUpdate = value;
99 private bool _AutoUpdate = false;
102 /// img.azyobizi.net によるサムネイル情報の取得を有効にするか
104 public bool Enabled { get; set; }
107 /// ダイレクトメッセージに含まれる画像URLに対して img.azyobuzi.net を使用しない
109 public bool DisabledInDM { get; set; }
111 protected void StartAutoUpdate()
112 => this.UpdateTimer.Change(0, 30 * 60 * 1000); // 30分おきに更新
114 protected void StopAutoUpdate()
115 => this.UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
117 public async Task LoadRegexAsync()
119 foreach (var host in this.ApiHosts)
123 var result = await this.LoadRegexAsync(host)
124 .ConfigureAwait(false);
139 this.UrlRegex = null;
144 public async Task<bool> LoadRegexAsync(string apiBase)
148 var jsonBytes = await this.FetchRegexAsync(apiBase)
149 .ConfigureAwait(false);
151 using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max);
152 var xElm = XElement.Load(jsonReader);
154 if (xElm.Element("error") != null)
159 this.UrlRegex = xElm.Elements("item")
160 .Where(x => !this.ExcludedServiceNames.Contains(x.Element("name").Value))
161 .Select(e => new Regex(e.Element("regex").Value, RegexOptions.IgnoreCase))
164 this.ApiBase = apiBase;
169 catch (HttpRequestException) { } // サーバーが2xx以外のステータスコードを返した場合
170 catch (OperationCanceledException) { } // リクエストがタイムアウトした場合
171 catch (XmlException) { } // サーバーが不正なJSONを返した場合
176 protected virtual async Task<byte[]> FetchRegexAsync(string apiBase)
178 using var cts = new CancellationTokenSource(millisecondsDelay: 1000);
179 using var response = await this.http.GetAsync(apiBase + "regex.json", cts.Token)
180 .ConfigureAwait(false);
182 response.EnsureSuccessStatusCode();
184 return await response.Content.ReadAsByteArrayAsync()
185 .ConfigureAwait(false);
188 public override Task<ThumbnailInfo?> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
190 return Task.Run(() =>
195 if (this.DisabledInDM && post != null && post.IsDm)
200 if (this.UrlRegex == null)
203 foreach (var regex in this.UrlRegex)
205 if (regex.IsMatch(url))
207 return new ThumbnailInfo
210 ThumbnailImageUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
211 FullSizeImageUrl = this.ApiBase + "redirect?size=full&uri=" + Uri.EscapeDataString(url),
222 public virtual void Dispose()
223 => this.UpdateTimer.Dispose();