OSDN Git Service

SimpleThumbnailService/MetaThumbnailService 以外で使用する正規表現を各クラス内に埋め込む
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 26 Apr 2014 07:20:40 +0000 (16:20 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 26 Apr 2014 09:45:00 +0000 (18:45 +0900)
OpenTween.Tests/Thumbnail/Services/TinamiTest.cs
OpenTween/Thumbnail/Services/Nicovideo.cs
OpenTween/Thumbnail/Services/PhotoShareShortlink.cs
OpenTween/Thumbnail/Services/Pixiv.cs
OpenTween/Thumbnail/Services/Tinami.cs
OpenTween/Thumbnail/Services/Tumblr.cs
OpenTween/Thumbnail/Services/ViaMe.cs
OpenTween/Thumbnail/Services/Vimeo.cs
OpenTween/Thumbnail/Services/Youtube.cs
OpenTween/Thumbnail/ThumbnailGenerator.cs

index 1febd14..a8ac1da 100644 (file)
@@ -41,8 +41,8 @@ namespace OpenTween.Thumbnail.Services
         {
             public string FakeXml { get; set; }
 
-            public TestTinami(string pattern)
-                : base(null, pattern)
+            public TestTinami()
+                : base(null)
             {
             }
 
@@ -55,7 +55,7 @@ namespace OpenTween.Thumbnail.Services
         [Fact]
         public async Task ApiTest()
         {
-            var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$");
+            var service = new TestTinami();
 
             service.FakeXml = @"<?xml version='1.0' encoding='utf-8' ?>
 <rsp stat='ok'>
@@ -83,7 +83,7 @@ namespace OpenTween.Thumbnail.Services
         [Fact]
         public async Task ApiErrorTest()
         {
-            var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$");
+            var service = new TestTinami();
 
             service.FakeXml = @"<?xml version='1.0' encoding='utf-8'?>
 <rsp stat='user_only'>
index 14639b0..a2c1bb8 100644 (file)
@@ -37,18 +37,14 @@ namespace OpenTween.Thumbnail.Services
 {
     class Nicovideo : IThumbnailService
     {
-        protected readonly Regex regex;
-
-        public Nicovideo(string pattern)
-        {
-            this.regex = new Regex(pattern);
-        }
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"^http://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$");
 
         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
             return Task.Run(() =>
             {
-                var match = this.regex.Match(url);
+                var match = Nicovideo.UrlPatternRegex.Match(url);
                 if (!match.Success)
                     return null;
 
index 0398be4..be7e9ae 100644 (file)
@@ -32,20 +32,16 @@ namespace OpenTween.Thumbnail.Services
 {
     class PhotoShareShortlink : IThumbnailService
     {
-        protected Regex regex;
-
-        public PhotoShareShortlink(string pattern)
-        {
-            this.regex = new Regex(pattern);
-        }
+        public static readonly Regex UrlPatternRegex = new Regex(@"^http://bctiny\.com/p(\w+)$");
 
         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
             return Task.Run(() =>
             {
-                var match = this.regex.Match(url);
+                var match = PhotoShareShortlink.UrlPatternRegex.Match(url);
 
-                if (!match.Success) return null;
+                if (!match.Success)
+                    return null;
 
                 return new ThumbnailInfo
                 {
index 81c9167..09aa622 100644 (file)
@@ -33,8 +33,11 @@ namespace OpenTween.Thumbnail.Services
 {
     class Pixiv : MetaThumbnailService
     {
-        public Pixiv(HttpClient http, string urlPattern)
-            : base(http, urlPattern)
+        public static readonly string UrlPattern =
+            @"^http://www\.pixiv\.net/(member_illust|index)\.php\?(?=.*mode=(medium|big))(?=.*illust_id=(?<illustId>[0-9]+)).*$";
+
+        public Pixiv(HttpClient http)
+            : base(http, Pixiv.UrlPattern)
         {
         }
 
index e5ceddc..9496c33 100644 (file)
@@ -35,18 +35,19 @@ namespace OpenTween.Thumbnail.Services
 {
     class Tinami : IThumbnailService
     {
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$");
+
         protected readonly HttpClient http;
-        protected readonly Regex regex;
 
-        public Tinami(HttpClient http, string urlPattern)
+        public Tinami(HttpClient http)
         {
             this.http = http;
-            this.regex = new Regex(urlPattern);
         }
 
         public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
-            var match = this.regex.Match(url);
+            var match = Tinami.UrlPatternRegex.Match(url);
             if (!match.Success)
                 return null;
 
index 8a6acc6..08ac70e 100644 (file)
@@ -37,18 +37,14 @@ namespace OpenTween.Thumbnail.Services
 {
     class Tumblr : IThumbnailService
     {
-        protected readonly Regex regex;
-
-        public Tumblr(string urlPattern)
-        {
-            this.regex = new Regex(urlPattern);
-        }
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"(?<base>http://.+?\.tumblr\.com/)post/(?<postID>[0-9]+)(/(?<subject>.+?)/)?");
 
         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
             return Task.Run(() =>
             {
-                var match = this.regex.Match(url);
+                var match = Tumblr.UrlPatternRegex.Match(url);
                 if (!match.Success)
                     return null;
 
index 95fb716..72054d5 100644 (file)
@@ -37,18 +37,19 @@ namespace OpenTween.Thumbnail.Services
 {
     class ViaMe : IThumbnailService
     {
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"^https?://via\.me/-(\w+)$");
+
         protected readonly HttpClient http;
-        protected readonly Regex regex;
 
-        public ViaMe(HttpClient http, string urlPattern)
+        public ViaMe(HttpClient http)
         {
             this.http = http;
-            this.regex = new Regex(urlPattern);
         }
 
         public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
-            var match = this.regex.Match(url);
+            var match = ViaMe.UrlPatternRegex.Match(url);
             if (!match.Success)
                 return null;
 
index d59a394..78b11ca 100644 (file)
@@ -37,18 +37,19 @@ namespace OpenTween.Thumbnail.Services
 {
     class Vimeo : IThumbnailService
     {
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"http://vimeo\.com/(?<postID>[0-9]+)");
+
         protected readonly HttpClient http;
-        protected readonly Regex regex;
 
-        public Vimeo(HttpClient http, string urlPattern)
+        public Vimeo(HttpClient http)
         {
             this.http = http;
-            this.regex = new Regex(urlPattern);
         }
 
         public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
-            var match = this.regex.Match(url);
+            var match = Vimeo.UrlPatternRegex.Match(url);
             if (!match.Success)
                 return null;
 
index 420f112..8516b30 100644 (file)
@@ -37,18 +37,14 @@ namespace OpenTween.Thumbnail.Services
 {
     class Youtube : IThumbnailService
     {
-        protected readonly Regex regex;
-
-        public Youtube(string urlPattern)
-        {
-            this.regex = new Regex(urlPattern);
-        }
+        public static readonly Regex UrlPatternRegex =
+            new Regex(@"^http://(?:(www\.youtube\.com)|(youtu\.be))/(watch\?v=)?(?<videoid>([\w\-]+))");
 
         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
         {
             return Task.Run(() =>
             {
-                var match = this.regex.Match(url);
+                var match = Youtube.UrlPatternRegex.Match(url);
                 if (!match.Success)
                     return null;
 
index 817a978..1c18902 100644 (file)
@@ -103,7 +103,7 @@ namespace OpenTween.Thumbnail
                 new SimpleThumbnailService(@"^http://(?:www\.)?bcphotoshare\.com/photos/\d+/(\d+)$", "http://images.bcphotoshare.com/storages/${1}/thumb180.jpg"),
 
                 // PhotoShare
-                new PhotoShareShortlink(@"^http://bctiny\.com/p(\w+)$"),
+                new PhotoShareShortlink(),
 
                 // img.ly
                 new SimpleThumbnailService(@"^http://img\.ly/(\w+)$",
@@ -116,10 +116,10 @@ namespace OpenTween.Thumbnail
                     "http://twitgoo.com/${1}/img"),
 
                 // youtube
-                new Youtube(@"^http://(?:(www\.youtube\.com)|(youtu\.be))/(watch\?v=)?(?<videoid>([\w\-]+))"),
+                new Youtube(),
 
                 // ニコニコ動画
-                new Nicovideo(@"^http://(?:(www|ext)\.nicovideo\.jp/watch|nico\.ms)/(?<id>(?:sm|nm)?[0-9]+)(\?.+)?$"),
+                new Nicovideo(),
 
                 // ニコニコ静画
                 new SimpleThumbnailService(
@@ -128,7 +128,7 @@ namespace OpenTween.Thumbnail
                     "http://lohas.nicoseiga.jp/thumb/${id}l?"),
 
                 // pixiv
-                new Pixiv(http, @"^http://www\.pixiv\.net/(member_illust|index)\.php\?(?=.*mode=(medium|big))(?=.*illust_id=(?<illustId>[0-9]+)).*$"),
+                new Pixiv(http),
 
                 // flickr
                 new MetaThumbnailService(http, @"^http://www\.flickr\.com/.+$"),
@@ -143,7 +143,7 @@ namespace OpenTween.Thumbnail
                 new MetaThumbnailService(http, @"^http://piapro\.jp/(?:content/[0-9a-z]+|t/[0-9a-zA-Z_\-]+)$"),
 
                 // Tumblr
-                new Tumblr(@"(?<base>http://.+?\.tumblr\.com/)post/(?<postID>[0-9]+)(/(?<subject>.+?)/)?"),
+                new Tumblr(),
 
                 // ついっぷるフォト
                 new SimpleThumbnailService(@"^http://p\.twipple\.jp/(?<contentId>[0-9a-z]+)", "http://p.twipple.jp/show/large/${contentId}"),
@@ -155,7 +155,7 @@ namespace OpenTween.Thumbnail
                 new SimpleThumbnailService(@"^http://ow\.ly/i/(\w+)$", "http://static.ow.ly/photos/thumb/${1}.jpg"),
 
                 // vimeo
-                new Vimeo(http, @"http://vimeo\.com/(?<postID>[0-9]+)"),
+                new Vimeo(http),
 
                 // cloudfiles
                 new SimpleThumbnailService(@"^http://c[0-9]+\.cdn[0-9]+\.cloudfiles\.rackspacecloud\.com/[a-z_0-9]+", "${0}"),
@@ -176,7 +176,7 @@ namespace OpenTween.Thumbnail
                 new FoursquareCheckin(http),
 
                 // TINAMI
-                new Tinami(http, @"^http://www\.tinami\.com/view/(?<ContentId>\d+)$"),
+                new Tinami(http),
 
                 // pic.twitter.com
                 new SimpleThumbnailService(
@@ -197,7 +197,7 @@ namespace OpenTween.Thumbnail
                     "${0}.jpg"),
 
                 // via.me
-                new ViaMe(http, @"^https?://via\.me/-(\w+)$"),
+                new ViaMe(http),
 
                 // tuna.be
                 new SimpleThumbnailService(@"^http://tuna\.be/t/(?<entryId>[a-zA-Z0-9\.\-_]+)$", "http://tuna.be/show/thumb/${entryId}"),