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.
23 using System.Collections.Generic;
25 using System.Net.Http;
26 using System.Runtime.Serialization.Json;
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;
35 namespace OpenTween.Thumbnail.Services
37 class ImgAzyobuziNet : IThumbnailService, IDisposable
39 protected string[] ApiHosts = {
40 "https://img.azyobuzi.net/api/",
41 "https://img.opentween.org/api/",
44 protected string[] ExcludedServiceNames =
52 protected string ApiBase;
53 protected IEnumerable<Regex> UrlRegex = null;
54 protected Timer UpdateTimer;
56 protected HttpClient http
57 => this.localHttpClient ?? Networking.Http;
59 private readonly HttpClient localHttpClient;
61 private readonly object LockObj = new object();
63 public ImgAzyobuziNet(bool autoupdate)
64 : this(null, autoupdate)
68 public ImgAzyobuziNet(HttpClient http)
69 : this(http, autoupdate: false)
73 public ImgAzyobuziNet(HttpClient http, bool autoupdate)
75 this.UpdateTimer = new Timer(async _ => await this.LoadRegexAsync());
76 this.AutoUpdate = autoupdate;
79 this.DisabledInDM = true;
81 this.localHttpClient = http;
84 public bool AutoUpdate
86 get => this._AutoUpdate;
90 this.StartAutoUpdate();
92 this.StopAutoUpdate();
94 this._AutoUpdate = value;
97 private bool _AutoUpdate = false;
100 /// img.azyobizi.net によるサムネイル情報の取得を有効にするか
102 public bool Enabled { get; set; }
105 /// ダイレクトメッセージに含まれる画像URLに対して img.azyobuzi.net を使用しない
107 public bool DisabledInDM { get; set; }
109 protected void StartAutoUpdate()
110 => this.UpdateTimer.Change(0, 30 * 60 * 1000); // 30分おきに更新
112 protected void StopAutoUpdate()
113 => this.UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
115 public async Task LoadRegexAsync()
117 foreach (var host in this.ApiHosts)
121 var result = await this.LoadRegexAsync(host)
122 .ConfigureAwait(false);
137 this.UrlRegex = null;
142 public async Task<bool> LoadRegexAsync(string apiBase)
146 var jsonBytes = await this.FetchRegexAsync(apiBase)
147 .ConfigureAwait(false);
149 using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max);
150 var xElm = XElement.Load(jsonReader);
152 if (xElm.Element("error") != null)
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))
162 this.ApiBase = apiBase;
167 catch (HttpRequestException) { } // サーバーが2xx以外のステータスコードを返した場合
168 catch (OperationCanceledException) { } // リクエストがタイムアウトした場合
169 catch (XmlException) { } // サーバーが不正なJSONを返した場合
174 protected virtual async Task<byte[]> FetchRegexAsync(string apiBase)
176 using var cts = new CancellationTokenSource(millisecondsDelay: 1000);
177 using var response = await this.http.GetAsync(apiBase + "regex.json", cts.Token)
178 .ConfigureAwait(false);
180 response.EnsureSuccessStatusCode();
182 return await response.Content.ReadAsByteArrayAsync()
183 .ConfigureAwait(false);
186 public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
188 return Task.Run(() =>
193 if (this.DisabledInDM && post != null && post.IsDm)
198 if (this.UrlRegex == null)
201 foreach (var regex in this.UrlRegex)
203 if (regex.IsMatch(url))
205 return new ThumbnailInfo
208 ThumbnailImageUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
209 FullSizeImageUrl = this.ApiBase + "redirect?size=full&uri=" + Uri.EscapeDataString(url),
220 public virtual void Dispose()
221 => this.UpdateTimer.Dispose();