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.
23 using System.Net.Http;
24 using System.Runtime.Serialization.Json;
25 using System.Text.RegularExpressions;
26 using System.Threading;
27 using System.Threading.Tasks;
30 using System.Xml.Linq;
31 using System.Xml.XPath;
33 namespace OpenTween.Thumbnail.Services
35 class FoursquareCheckin : IThumbnailService
37 public static readonly Regex UrlPatternRegex =
38 new Regex(@"^https?://foursquare\.com/.+?/checkin/(?<checkin_id>[0-9a-z]+)(?:\?s=(?<signature>[^&]+))?");
40 public static readonly string ApiBase = "https://api.foursquare.com/v2";
42 protected readonly HttpClient http;
44 public FoursquareCheckin()
49 public FoursquareCheckin(HttpClient http)
51 this.http = http ?? MyCommon.CreateHttpClient();
54 public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
56 if (!AppendSettingDialog.Instance.IsPreviewFoursquare)
59 // ツイートに位置情報が付与されている場合は何もしない
60 if (post.PostGeo.Lat != 0 || post.PostGeo.Lng != 0)
63 var match = UrlPatternRegex.Match(url);
68 var checkinIdGroup = match.Groups["checkin_id"];
69 var signatureGroup = match.Groups["signature"];
73 // Foursquare のベニュー情報を取得
74 // 参照: https://developer.foursquare.com/docs/venues/venues
76 var query = HttpUtility.ParseQueryString(string.Empty);
77 query["client_id"] = ApplicationSettings.FoursquareClientId;
78 query["client_secret"] = ApplicationSettings.FoursquareClientSecret;
79 query["v"] = "20140419"; // https://developer.foursquare.com/overview/versioning
81 if (signatureGroup.Success)
82 query["signature"] = signatureGroup.Value;
84 var apiUrl = new Uri(ApiBase + "/checkins/" + checkinIdGroup.Value + "?" + query);
86 using (var response = await this.http.GetAsync(apiUrl, token).ConfigureAwait(false))
88 response.EnsureSuccessStatusCode();
90 var jsonBytes = await response.Content.ReadAsByteArrayAsync()
91 .ConfigureAwait(false);
93 var location = ParseIntoLocation(jsonBytes);
97 var map = MapThumb.GetDefaultInstance();
99 return new ThumbnailInfo(this.http)
101 ImageUrl = map.CreateMapLinkUrl(location.Latitude, location.Longitude),
102 ThumbnailUrl = map.CreateStaticMapUrl(location.Latitude, location.Longitude),
107 catch (HttpRequestException) { }
112 internal static GlobalLocation ParseIntoLocation(byte[] jsonBytes)
114 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max))
116 var xElm = XElement.Load(jsonReader);
118 var locationElm = xElm.XPathSelectElement("/response/checkin/venue/location");
121 if (locationElm == null)
124 // 月など、地球以外の星の座標である場合
125 var planetElm = locationElm.Element("planet");
126 if (planetElm != null && planetElm.Value != "earth")
129 return new GlobalLocation
131 Latitude = double.Parse(locationElm.Element("lat").Value),
132 Longitude = double.Parse(locationElm.Element("lng").Value),