🍃このブログは移転しました。
3秒後、自動的に移動します・・・。

TwitterのAPIについてのメモ

なんか作ってみたいと思ってるので、そのメモ。

大きく分けて2つのAPIがあるみたい。

APIの種類

その他、サイトに貼れるウィジェットやったり、ツイートボタンやったりも紹介されてます。

参考:Documentation | Twitter Developers

Streaming API

こっちを使うつもりはないのであんまり見てませんが。

The Twitter Streaming API allows high-throughput near-realtime access to various subsets of public and protected Twitter data.
Developers are strongly encouraged to read all of the documentation linked to by this document thoroughly.

Twitterのリアルタイムな情報を利用できます、ってなこっちゃ。

タイムラインをリアルタイムに取得して反映・・みたいな仕様にする場合は、こっちがよさげ。

HTTP Basic Authentication or OAuth is required to access the streaming API methods.
A client must, for Basic Authentication, provide the credentials of a valid Twitter account, or for OAuth, sign the request appropriately.

ただ使うには認証が必要です。

REST API

RESTについては最近知ったとこなので、いまいちな解釈かもしれませんがー。
いわゆるHttpでリクエストして結果をもらう、とか「いわゆるWebのAPI」っていうのは大抵コレなんやろうか。

参考:REST API Resources | Twitter Developers

取得できる内容は、以下の分類。

  • Timelines
  • Tweets
  • Search
  • Direct Messages
  • Friends & Followers
  • Users
  • Suggested Users
  • Favorites
  • Lists
  • Accounts
  • Notification
  • Saved Searches
  • Local Trends
  • Places & Geo
  • Trends
  • Block
  • Spam Reporting
  • OAuth
  • Help
  • Legal
  • Deprecated

多いわ!w

なにはともあれ、Twitterの全てがココに!
それぞれGETかPOSTかどっちもか、できます。

はてさて。

REST -Search API

今回狙ってるのはこいつ!

参考:GET search | Twitter Developers
参考:Using the Twitter Search API | Twitter Developers
参考:Tweet Entities | Twitter Developers

だいたいこの3ページに知りたいことは全て書いてあるはず。

取得結果

JSONかatomかJSONPの形式から。

クエリ

1つ目と2つ目の参考URL参照。
include_entitiesをtrueにすると、参考URL3つ目にあるように、Tweet Entitiesなるものも取得可能。

Tweet text can potentially mention other users or lists, but also contain URLs, media, hashtags...
Instead of parsing the text yourself to try to extract those entities, you can use the entities attribute that contains this parsed and structured data.

メタデータってやつですね。
つぶやきの補足データ:リンクを含んでるか、ハッシュタグがあるか、などなど。

これは使えそうー!

利用制限

  • 全ての結果を検索できるわけではなく、6〜9日前くらいまでしかデータはありません。
  • 1週間より前のつぶやきは検索できません。
  • クエリが複雑すぎると、エラーになります。
  • 中には取得されなりつぶやきもあったりします。(それが嫌ならStreaming APIを使ってね!)
  • 他のREST APIと違って、時間あたりのリクエスト回数ではなく、クエリの複雑性のみで制限がかかります。

利用のコツ

  • きっちりエンコードしてからリクエストする。
  • since_idパラメータを使う。
  • パラメータは10コ以内にする。
  • UAを付与してリクエストする。

result_type

mixed / Popular / Recent
これがいまいちよくわかんない。

つぶやきには人気のもの、最近のものっていう独自の分類があって、そういったバイアスを含んだ結果を返すかどうか、ってことなんかな?


以上、メモでした。