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.Collections.Generic;
24 using System.Net.Http;
25 using System.Runtime.Serialization.Json;
26 using System.Text.RegularExpressions;
27 using System.Threading;
28 using System.Threading.Tasks;
31 using System.Xml.Linq;
32 using System.Xml.XPath;
34 namespace OpenTween.Thumbnail.Services
36 class FoursquareCheckin : IThumbnailService
38 public static readonly Regex UrlPatternRegex =
39 new Regex(@"^https?://foursquare\.com/.+?/checkin/(?<checkin_id>[0-9a-z]+)(?:\?s=(?<signature>[^&]+))?");
41 public static readonly string ApiBase = "https://api.foursquare.com/v2";
43 protected readonly HttpClient http;
45 public FoursquareCheckin(HttpClient http)
50 public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
52 // ツイートに位置情報が付与されている場合は何もしない
53 if (post.PostGeo.Lat != 0 || post.PostGeo.Lng != 0)
56 var match = UrlPatternRegex.Match(url);
61 var checkinIdGroup = match.Groups["checkin_id"];
62 var signatureGroup = match.Groups["signature"];
66 // Foursquare のベニュー情報を取得
67 // 参照: https://developer.foursquare.com/docs/venues/venues
69 var query = new Dictionary<string, string>
71 {"client_id", ApplicationSettings.FoursquareClientId},
72 {"client_secret", ApplicationSettings.FoursquareClientSecret},
73 {"v", "20140419"}, // https://developer.foursquare.com/overview/versioning
76 if (signatureGroup.Success)
77 query["signature"] = signatureGroup.Value;
79 var apiUrl = new Uri(ApiBase + "/checkins/" + checkinIdGroup.Value + "?" + MyCommon.BuildQueryString(query));
81 using (var response = await this.http.GetAsync(apiUrl, token).ConfigureAwait(false))
83 response.EnsureSuccessStatusCode();
85 var jsonBytes = await response.Content.ReadAsByteArrayAsync()
86 .ConfigureAwait(false);
88 var location = ParseIntoLocation(jsonBytes);
92 var map = MapThumb.GetDefaultInstance();
94 return new ThumbnailInfo
96 ImageUrl = map.CreateMapLinkUrl(location.Latitude, location.Longitude),
97 ThumbnailUrl = map.CreateStaticMapUrl(location.Latitude, location.Longitude),
102 catch (HttpRequestException) { }
107 internal static GlobalLocation ParseIntoLocation(byte[] jsonBytes)
109 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max))
111 var xElm = XElement.Load(jsonReader);
113 var locationElm = xElm.XPathSelectElement("/response/checkin/venue/location");
116 if (locationElm == null)
119 // 月など、地球以外の星の座標である場合
120 var planetElm = locationElm.Element("planet");
121 if (planetElm != null && planetElm.Value != "earth")
124 return new GlobalLocation
126 Latitude = double.Parse(locationElm.Element("lat").Value),
127 Longitude = double.Parse(locationElm.Element("lng").Value),