Created
August 18, 2015 08:40
-
-
Save KzoNag/8613b98467d92a4adaa4 to your computer and use it in GitHub Desktop.
TwitterAPIを利用するためのアセット"Let's Tweet in Unity!"で検索APIを使うための拡張
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Text; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
namespace Twitter | |
{ | |
public delegate void CommonCallback(bool success, string response); | |
public partial class API | |
{ | |
public enum Method | |
{ | |
GET, | |
POST | |
} | |
private const string ApiBaseURL = "https://api.twitter.com/1.1/{0}.json"; | |
/// <summary> | |
/// クエリを指定してツイートを検索 | |
/// </summary> | |
/// <param name="query">検索クエリ.</param> | |
/// <param name="consumerKey">コンシューマキー</param> | |
/// <param name="consumerSecret">コンシューマシークレット</param> | |
/// <param name="response">Twitter.API.GetAccessTokenで取得したアクセストークン</param> | |
/// <param name="callback">検索結果を受け取るコールバック</param> | |
/// <example> | |
/// | |
/// /*使用例*/ | |
/// | |
/// Twitter.AccessTokenResponse m_AccessTokenResponse; // 事前にTwitter.API.GetAccessTokenでアクセストークンを取得しておく | |
/// | |
/// void SerchByHashTag() | |
/// { | |
/// // ハッシュタグ "#Unity" で検索(終了するとOnSearchTweetsResponseが実行される) | |
/// StartCoroutine(Twitter.API.SearchTweets("#Unity", "CONSUMER_KEY", "CONSUMER_SECRET", m_AccessTokenResponse, OnSearchTweetsResponse)); | |
/// } | |
/// | |
/// void OnSearchTweetsResponse(bool success, string response) | |
/// { | |
/// // responseに検索結果のJSON文字列が入ってくるので解析して各ツイートのテキストを得る | |
/// } | |
/// | |
/// </example> | |
public static IEnumerator SearchTweets(string query, string consumerKey, string consumerSecret, AccessTokenResponse response, CommonCallback callback) | |
{ | |
var parameters = new Dictionary<string, string>(); | |
parameters["q"] = query; | |
return CommonRequest("search/tweets", Method.GET, parameters, consumerKey, consumerSecret, response, callback); | |
} | |
private static IEnumerator CommonRequest(string api, Method method, Dictionary<string, string> parameters, string consumerKey, string consumerSecret, AccessTokenResponse response, CommonCallback callback) | |
{ | |
string baseURL = string.Format(ApiBaseURL, api); | |
string url = baseURL; | |
byte[] data = null; | |
if (parameters == null) | |
{ | |
parameters = new Dictionary<string, string>(); | |
} | |
if (method == Method.GET) | |
{ | |
url = BuildURL(url, parameters); | |
} | |
if (method == Method.POST) | |
{ | |
WWWForm form = new WWWForm(); | |
foreach (var pair in parameters) | |
{ | |
form.AddField(pair.Key, pair.Value); | |
} | |
data = form.data; | |
} | |
var headers = new Hashtable(); | |
headers["Authorization"] = GetHeaderWithAccessToken(method.ToString(), url, consumerKey, consumerSecret, response, parameters); | |
WWW web = new WWW(url, data, headers); | |
yield return web; | |
if (!string.IsNullOrEmpty(web.error)) | |
{ | |
Debug.Log(string.Format("{0} failed. {1}\n{2}", api, web.error, web.text)); | |
callback(false, web.error); | |
} | |
else | |
{ | |
string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value; | |
if (!string.IsNullOrEmpty(error)) | |
{ | |
Debug.Log(string.Format("{0} failed. {1}\n{2}", api, web.error, web.text)); | |
callback(false, web.text); | |
} | |
else | |
{ | |
callback(true, web.text); | |
} | |
} | |
} | |
private static string BuildURL(string baseURL, Dictionary<string, string> parameters) | |
{ | |
StringBuilder sb = new StringBuilder(baseURL); | |
if (parameters != null && parameters.Count > 0) | |
{ | |
sb.Append("?"); | |
foreach (var pair in parameters) | |
{ | |
sb.Append(WWW.EscapeURL(pair.Key)).Append("=").Append(WWW.EscapeURL(pair.Value)).Append("&"); | |
} | |
sb.Remove(sb.Length - 1, 1); | |
} | |
return sb.ToString(); | |
} | |
private static void PrintJson(JsonData json) | |
{ | |
if (json.IsArray) | |
{ | |
for (int i = 0; i < json.Count; ++i) | |
{ | |
PrintJson(json[i]); | |
} | |
} | |
else if (json.IsObject) | |
{ | |
foreach (var key in json.Keys) | |
{ | |
PrintJson(json[key]); | |
} | |
} | |
else | |
{ | |
Debug.Log(json.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment