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;
29 using System.Text.RegularExpressions;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using OpenTween.Models;
33 using OpenTween.Thumbnail.Services;
35 namespace OpenTween.Thumbnail
37 class ThumbnailGenerator
39 public static List<IThumbnailService> Services { get; protected set; }
41 internal static ImgAzyobuziNet ImgAzyobuziNetInstance { get; private set; }
43 static ThumbnailGenerator()
44 => ThumbnailGenerator.Services = new List<IThumbnailService>();
46 public static void InitializeGenerator()
48 ImgAzyobuziNetInstance = new ImgAzyobuziNet(autoupdate: true);
50 ThumbnailGenerator.Services = new List<IThumbnailService>
55 // twitter.com/tweet_video
56 new TwitterComVideo(),
71 new SimpleThumbnailService(@"^https?://.*(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$", "${0}"),
74 ImgAzyobuziNetInstance,
77 new SimpleThumbnailService(
78 @"^https?://(?:i\.)?imgur\.com/(\w+)(?:\..{3})?$",
79 "https://img.imgur.com/${1}l.jpg",
80 "https://img.imgur.com/${1}.jpg"),
83 new MetaThumbnailService(@"^https?://(www\.)?twitpic\.com/.+$"),
86 new MetaThumbnailService(@"^https?://(?:www\.)?mobypicture.com/user/\w+/view/\d+$"),
89 new SimpleThumbnailService(
90 @"^https?://movapic\.com/pic/(\w+)$",
91 "http://image.movapic.com/pic/s_${1}.jpeg",
92 "http://image.movapic.com/pic/m_${1}.jpeg"),
95 new SimpleThumbnailService(
96 @"^https?://f\.hatena\.ne\.jp/(([a-z])[a-z0-9_-]{1,30}[a-z0-9])/((\d{8})\d+)$",
97 "http://img.f.hatena.ne.jp/images/fotolife/${2}/${1}/${4}/${3}_120.jpg",
98 "http://img.f.hatena.ne.jp/images/fotolife/${2}/${1}/${4}/${3}.jpg"),
101 new SimpleThumbnailService(
102 @"^https?://(?:seiga\.nicovideo\.jp/seiga/|nico\.ms/)im(?<id>\d+)",
103 "http://lohas.nicoseiga.jp/thumb/${id}q?",
104 "http://lohas.nicoseiga.jp/thumb/${id}l?"),
110 new MetaThumbnailService(@"^https?://www\.flickr\.com/.+$"),
113 new SimpleThumbnailService(
114 @"^https?://photozou\.jp/photo/show/(?<userId>[0-9]+)/(?<photoId>[0-9]+)",
115 "https://photozou.jp/p/thumb/${photoId}",
116 "https://photozou.jp/p/img/${photoId}"),
119 new MetaThumbnailService(@"^https?://piapro\.jp/(?:content/[0-9a-z]+|t/[0-9a-zA-Z_\-]+)$"),
125 new SimpleThumbnailService(@"^https?://www\.(mypix\.jp|shamoji\.info)/app\.php/picture/(?<contentId>[0-9a-z]+)", "${0}/thumb.jpg"),
128 new SimpleThumbnailService(@"^https?://ow\.ly/i/(\w+)$", "http://static.ow.ly/photos/thumb/${1}.jpg"),
131 new SimpleThumbnailService(@"^https?://c[0-9]+\.cdn[0-9]+\.cloudfiles\.rackspacecloud\.com/[a-z_0-9]+", "${0}"),
134 new SimpleThumbnailService(
135 @"^https?://(?:instagram.com|instagr\.am|i\.instagram\.com|www\.instagram\.com)/p/.+/",
137 "${0}media/?size=l"),
140 new FoursquareCheckin(),
146 new SimpleThumbnailService(@"^https?://tuna\.be/t/(?<entryId>[a-zA-Z0-9\.\-_]+)$", "https://tuna.be/show/thumb/${entryId}"),
149 new SimpleThumbnailService(@"^https?://gifmagazine\.net/post_images/(\d+)", "https://img.gifmagazine.net/gifmagazine/images/${1}/original.gif"),
152 new MetaThumbnailService(@"^https?://soundcloud.com/[\w-]+/[\w-]+$"),
154 // Gyazo (参考: http://qiita.com/uiureo/items/9ea55b07dff28a322a9e)
155 new SimpleThumbnailService(@"^https?://gyazo\.com/([a-zA-Z0-9]+)/?$", "https://gyazo.com/${1}/raw"),
159 public static async Task<IEnumerable<ThumbnailInfo>> GetThumbnailsAsync(PostClass post, CancellationToken token)
161 var thumbnails = new List<ThumbnailInfo>();
163 var expandedUrls = Enumerable.Concat(
164 post.GetExpandedUrls(), post.Media.Select(x => x.Url));
166 foreach (var expandedUrl in expandedUrls)
168 var thumbInfo = await ThumbnailGenerator.GetThumbnailInfoAsync(expandedUrl, post, token)
169 .ConfigureAwait(false);
171 if (thumbInfo != null)
172 thumbnails.Add(thumbInfo);
174 token.ThrowIfCancellationRequested();
177 if (post.PostGeo != null)
179 var map = MapThumb.GetDefaultInstance();
180 var thumb = await map.GetThumbnailInfoAsync(post.PostGeo.Value)
181 .ConfigureAwait(false);
182 thumbnails.Add(thumb);
188 public static async Task<ThumbnailInfo?> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
190 foreach (var generator in ThumbnailGenerator.Services)
192 var result = await generator.GetThumbnailInfoAsync(url, post, token)
193 .ConfigureAwait(false);
198 token.ThrowIfCancellationRequested();