OSDN Git Service

8a6acc61a756f325ed66e0da8130b1d2f92d7c1e
[opentween/open-tween.git] / OpenTween / Thumbnail / Services / Tumblr.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Text.RegularExpressions;
32 using System.Threading;
33 using System.Threading.Tasks;
34 using System.Xml;
35
36 namespace OpenTween.Thumbnail.Services
37 {
38     class Tumblr : IThumbnailService
39     {
40         protected readonly Regex regex;
41
42         public Tumblr(string urlPattern)
43         {
44             this.regex = new Regex(urlPattern);
45         }
46
47         public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
48         {
49             return Task.Run(() =>
50             {
51                 var match = this.regex.Match(url);
52                 if (!match.Success)
53                     return null;
54
55                 var apiUrl = match.Result("${base}api/read?id=${postID}");
56
57                 var http = new HttpVarious();
58                 var src = "";
59                 string imgurl = null;
60                 string errmsg;
61                 if (http.GetData(apiUrl, null, out src, 0, out errmsg, ""))
62                 {
63                     var xdoc = new XmlDocument();
64                     try
65                     {
66                         xdoc.LoadXml(src);
67
68                         var type = xdoc.SelectSingleNode("/tumblr/posts/post").Attributes["type"].Value;
69                         if (type == "photo")
70                         {
71                             imgurl = xdoc.SelectSingleNode("/tumblr/posts/post/photo-url").InnerText;
72                         }
73                         else
74                         {
75                             errmsg = "PostType:" + type;
76                             return null;
77                         }
78                     }
79                     catch (Exception)
80                     {
81                         return null;
82                     }
83
84                     return new ThumbnailInfo
85                     {
86                         ImageUrl = url,
87                         ThumbnailUrl = imgurl,
88                         TooltipText = null,
89                     };
90                 }
91                 return null;
92             }, token);
93         }
94     }
95 }