• R/O
  • HTTP
  • SSH
  • HTTPS

open-tween: Commit

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


Commit MetaInfo

Revisionc8dadbf3bee2c24b1ee7740906f2fa0bfbe47b80 (tree)
Time2022-01-26 02:03:15
Authorupsilon <kim.upsilon@bucy...>
CommiterGitHub

Log Message

Merge pull request #90 from opentween/update-youtube-pattern

YouTubeのサムネイルを表示するURLのパターンを追加

Change Summary

Incremental Difference

--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/YoutubeTest.cs
@@ -0,0 +1,44 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2022 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.Threading.Tasks;
27+using Xunit;
28+
29+namespace OpenTween.Thumbnail.Services
30+{
31+ public class YoutubeTest
32+ {
33+ [Theory]
34+ [InlineData("https://www.youtube.com/watch?v=aaaaa", "aaaaa")]
35+ [InlineData("https://www.youtube.com/watch?v=aaaaa#hoge", "aaaaa")]
36+ [InlineData("https://www.youtube.com/watch?feature=youtu.be&v=aaaaa&app=desktop", "aaaaa")]
37+ [InlineData("https://m.youtube.com/watch?v=aaaaa", "aaaaa")]
38+ [InlineData("https://youtu.be/aaaaa", "aaaaa")]
39+ [InlineData("https://youtu.be/aaaaa?t=123", "aaaaa")]
40+ [InlineData("https://www.youtube.com/channel/aaaaa", null)] // チャンネルページ
41+ public void GetVideoIdFromUrl_Test(string testUrl, string expected)
42+ => Assert.Equal(expected, Youtube.GetVideoIdFromUrl(testUrl));
43+ }
44+}
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -2,7 +2,7 @@
22
33 ==== Ver 2.4.4-dev(2019/xx/xx)
44 * CHG: pic.twitter.com の画像URLのフォーマット変更に対応
5- * CHG: Instagramのサムネイルを表示するURLのパターンを追加
5+ * CHG: Instagram, Youtubeのサムネイルを表示するURLのパターンを追加
66 * FIX: DMの添付画像をブラウザで開く場合に使用するURLを変更
77 * FIX: 大文字アルファベットを含むハッシュタグがユーザー情報画面で正しくリンク化されない不具合を修正 (thx @naminodarie!)
88
--- a/OpenTween/Thumbnail/Services/Youtube.cs
+++ b/OpenTween/Thumbnail/Services/Youtube.cs
@@ -30,10 +30,11 @@ using System;
3030 using System.Collections.Generic;
3131 using System.Linq;
3232 using System.Text;
33+using System.Text.RegularExpressions;
3334 using System.Threading;
3435 using System.Threading.Tasks;
36+using System.Web;
3537 using System.Xml;
36-using System.Text.RegularExpressions;
3738 using OpenTween.Models;
3839
3940 namespace OpenTween.Thumbnail.Services
@@ -41,17 +42,16 @@ namespace OpenTween.Thumbnail.Services
4142 class Youtube : IThumbnailService
4243 {
4344 public static readonly Regex UrlPatternRegex =
44- new Regex(@"^https?://(?:((?:www|m)\.youtube\.com)|(youtu\.be))/(watch\?v=)?(?<videoid>([\w\-]+))");
45+ new Regex(@"^https?://(?:www\.youtube\.com|m\.youtube\.com|youtu\.be)/");
4546
4647 public override Task<ThumbnailInfo?> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
4748 {
4849 return Task.Run(() =>
4950 {
50- var match = Youtube.UrlPatternRegex.Match(url);
51- if (!match.Success)
51+ var videoId = Youtube.GetVideoIdFromUrl(url);
52+ if (videoId == null)
5253 return null;
5354
54- var videoId = match.Groups["videoid"].Value;
5555 var imgUrl = "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
5656
5757 return new ThumbnailInfo
@@ -63,5 +63,29 @@ namespace OpenTween.Thumbnail.Services
6363 };
6464 }, token);
6565 }
66+
67+ public static string? GetVideoIdFromUrl(string urlStr)
68+ {
69+ if (!Youtube.UrlPatternRegex.IsMatch(urlStr))
70+ return null;
71+
72+ var url = new Uri(urlStr);
73+ switch (url.Host)
74+ {
75+ case "www.youtube.com":
76+ case "m.youtube.com":
77+ {
78+ if (url.AbsolutePath != "/watch")
79+ return null;
80+
81+ var query = HttpUtility.ParseQueryString(url.Query);
82+ return query["v"];
83+ }
84+ case "youtu.be":
85+ return url.AbsolutePath.Substring(1);
86+ default:
87+ return null;
88+ }
89+ }
6690 }
6791 }
Show on old repository browser