• R/O
  • HTTP
  • SSH
  • HTTPS

open-tween: Commit

開発に使用するリポジトリ


Commit MetaInfo

Revision5bd22d86e4d082ddf81130c0d05fd249b9d5007b (tree)
Time2020-01-19 08:12:41
AuthorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

Change Summary

Incremental Difference

--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/PbsTwimgComTest.cs
@@ -0,0 +1,126 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2020 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.Text;
26+using System.Text.RegularExpressions;
27+using System.Threading;
28+using System.Threading.Tasks;
29+using OpenTween.Models;
30+using Xunit;
31+
32+namespace OpenTween.Thumbnail.Services
33+{
34+ public class PbsTwimgComTest
35+ {
36+ [Fact]
37+ public void ModernUrlPattern_Test()
38+ {
39+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
40+ var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
41+
42+ Assert.True(matches.Success);
43+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
44+ Assert.Equal("jpg", matches.Groups["format"].Value);
45+ }
46+
47+ [Fact]
48+ public void ModernUrlPattern_UnorderedTest()
49+ {
50+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?name=large&format=jpg";
51+ var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
52+
53+ Assert.True(matches.Success);
54+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
55+ Assert.Equal("jpg", matches.Groups["format"].Value);
56+ }
57+
58+ [Fact]
59+ public void ModernUrlPattern_LegacyUrlTest()
60+ {
61+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg:large";
62+ var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
63+
64+ Assert.False(matches.Success);
65+ }
66+
67+ [Fact]
68+ public void LegacyUrlPattern_Test()
69+ {
70+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg";
71+ var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
72+
73+ Assert.True(matches.Success);
74+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
75+ Assert.Equal("jpg", matches.Groups["format"].Value);
76+ }
77+
78+ [Fact]
79+ public void LegacyUrlPattern_SizeNameTest()
80+ {
81+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg:large";
82+ var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
83+
84+ Assert.True(matches.Success);
85+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
86+ Assert.Equal("jpg", matches.Groups["format"].Value);
87+ }
88+
89+ [Fact]
90+ public void LegacyUrlPattern_ModernUrlTest()
91+ {
92+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
93+ var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
94+
95+ Assert.False(matches.Success);
96+ }
97+
98+ [Fact]
99+ public async Task GetThumbnailInfoAsync_ModernUrlTest()
100+ {
101+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
102+
103+ var service = new PbsTwimgCom();
104+ var thumb = await service.GetThumbnailInfoAsync(mediaUrl, new PostClass(), CancellationToken.None)
105+ .ConfigureAwait(false);
106+
107+ Assert.NotNull(thumb);
108+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large", thumb!.ThumbnailImageUrl);
109+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=orig", thumb!.FullSizeImageUrl);
110+ }
111+
112+ [Fact]
113+ public async Task GetThumbnailInfoAsync_LegacyUrlTest()
114+ {
115+ var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg";
116+
117+ var service = new PbsTwimgCom();
118+ var thumb = await service.GetThumbnailInfoAsync(mediaUrl, new PostClass(), CancellationToken.None)
119+ .ConfigureAwait(false);
120+
121+ Assert.NotNull(thumb);
122+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large", thumb!.ThumbnailImageUrl);
123+ Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=orig", thumb!.FullSizeImageUrl);
124+ }
125+ }
126+}
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -1,6 +1,7 @@
11 更新履歴
22
33 ==== Ver 2.4.4-dev(2019/xx/xx)
4+ * CHG: pic.twitter.com の画像URLのフォーマット変更に対応
45
56 ==== Ver 2.4.3(2019/12/06)
67 * FIX: 関連発言表示のタブを読み込み中に閉じるとエラーが発生する不具合を修正
--- a/OpenTween/Thumbnail/Services/PbsTwimgCom.cs
+++ b/OpenTween/Thumbnail/Services/PbsTwimgCom.cs
@@ -32,28 +32,52 @@ using OpenTween.Models;
3232
3333 namespace OpenTween.Thumbnail.Services
3434 {
35- class PbsTwimgCom : SimpleThumbnailService
35+ class PbsTwimgCom : IThumbnailService
3636 {
37- public static readonly string UrlPattern = @"^(https?://pbs\.twimg\.com/[^:]+)(?:\:.+)?$";
37+ public static readonly Regex ModernUrlPattern =
38+ new Regex(@"^(?<base_url>https?://pbs\.twimg\.com/[^:.]+)\?([^&]+?&)?format=(?<format>[A-Za-z]+)");
3839
39- public PbsTwimgCom()
40- : base(UrlPattern, "${1}", "${1}:orig")
41- {
42- }
40+ public static readonly Regex LegacyUrlPattern =
41+ new Regex(@"^(?<base_url>https?://pbs\.twimg\.com/[^.]+)\.(?<format>[A-Za-z]+)(?:\:.+)?$");
42+
43+ public override Task<ThumbnailInfo?> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
44+ => Task.FromResult(this.GetThumbnailInfo(url, post, token));
4345
44- public override async Task<ThumbnailInfo?> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
46+ private ThumbnailInfo? GetThumbnailInfo(string url, PostClass post, CancellationToken token)
4547 {
46- var thumb = await base.GetThumbnailInfoAsync(url, post, token)
47- .ConfigureAwait(false);
48+ string baseUrl;
49+ string format;
4850
49- if (thumb == null)
51+ // 現状の Twitter API から返る media_url_https は LegacyUrlPettern の形式になっているが、
52+ // どちらの形式でも動くようにする
53+ if (ModernUrlPattern.Match(url) is { Success: true } matchesModern)
54+ {
55+ baseUrl = matchesModern.Groups["base_url"].Value;
56+ format = matchesModern.Groups["format"].Value;
57+ }
58+ else if (LegacyUrlPattern.Match(url) is { Success: true } matchesLegacy)
59+ {
60+ baseUrl = matchesLegacy.Groups["base_url"].Value;
61+ format = matchesLegacy.Groups["format"].Value;
62+ }
63+ else
64+ {
5065 return null;
66+ }
67+
68+ var mediaOrig = $"{baseUrl}?format={format}&name=orig";
69+ var mediaLarge = $"{baseUrl}?format={format}&name=large";
5170
5271 var media = post.Media.FirstOrDefault(x => x.Url == url);
53- if (media != null)
72+ var altText = media?.AltText;
73+
74+ var thumb = new ThumbnailInfo
5475 {
55- thumb.TooltipText = media.AltText;
56- }
76+ MediaPageUrl = mediaOrig,
77+ ThumbnailImageUrl = mediaLarge,
78+ TooltipText = altText,
79+ FullSizeImageUrl = mediaOrig,
80+ };
5781
5882 return thumb;
5983 }
Show on old repository browser