Search for Tweets https://dev.twitter.com/rest/reference/get/search/tweets Contact UsGeocode can be limited to s with geo attributes geocode Use arguments 緯度,経度,距離km format.Ruby#!/usr/bin/env ruby
# coding: utf-8
gem 'twitter', '>= 5.0.0' # twitter gem のバージョンが 5 以上を指定
require 'twitter'
client = Twitter::REST::Client.new(
API のアクセストークンを登録
consumer_key: 'TWITTER_CONSUMER_KEY',
consumer_secret: 'TWITTER_CONSUMER_SECRET',
access_token: 'ACCESS_TOKEN',
access_token_secret: 'ACCESS_TOKEN_SECRET'
)
since_id = nil
loop do
tweets = client.search(
"love", # 検索キーワードを "love" に
geocode: "40.7033121,-73.979681,10km", # around New York
count: 100, # 読み込みツイート数(API 上限が100)
since_id: since_id # 同じツイートを取得しないようにツイートの読み込み開始 id を指定
)
since_id = tweets.first.id if tweets.first
tweets.take(100).reverse.each do |tw|
next if tw.geo.lat.nil? # ツイートに緯度が含まれていない場合には次のツイートへ
# client.search の引数に geocode を入れる場合には不要
puts "#{tw.place.name}, #{tw.place.country} (#{tw.geo.lat}, #{tw.geo.long}) @ #{tw.created_at}\n@#{tw.user.screen_name} (@ #{tw.user.location}): #{tw.text}\n---"
# ツイート検索結果を1ツイート2行にして出力
# 都市名, 国名 (緯度, 経度) @ ツイート時刻
# @アカウント名 (@ アカウントの登録地名): ツイート本文
end
sleep 10 #10秒毎に繰り返す
end
a little bit of script commentary,tw information is included in the instance. Same as the contents returned by JSON from the API.tw.place.name JSON "place" "name" tw.place.country "place" tw.geo.lat the first of "coordinates" in "geo", tw.geo.long the second of "coordinates" of "geo" is returned.If geocode is specified, the search keyword is OK for empty characters. (Reference: https://dev.twitter.com/rest/public/search )If you look at JSON in other languages than Ruby, you can get a la .Example of JSON returned by API:{
"created_at",Tue Feb 03 14:36:14 +0000 2015",
"id":562620584031318018,
"id_str",562620584031318018",
Please take care of your health carefully boy! Love you all
Android
ncated": false,
"in_reply_to_status_id":null,
"in_reply_to_status_id_str":null,
"in_reply_to_user_id":2483888760,
"in_reply_to_user_id_str",2483888760",
"in_reply_to_screen_namehzhztttao",",
"user": {
"id":246864766,
"id_str",246864766",
"name",tsinychong",
"screen_name",tsinychong",
Hotel Reservation System _ 2017 sabahhotels.com. All rights reserved _ 連絡先
"profile_location",
"description ladyI am a Malaysian Chinese lady"
"url": null,
"entities":{"description":{"urls":[]}},
"protected": false,
"followers_count":6,
"friends_count":72,
"listed_count":0,
"created_at",Thu Feb 03 16:36:39 +0000 2011",
"favourites_count":7,
"utc_offset":28800,
"time_zone",Kuala Lumpur",
"geo_enabled": true,
"verified": false,
"statuses_count":1261,
lang",en
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color",EBEBEBEB"
"profile_background_image_url.http://abs.twimg.com/images/themes/theme7/bg.gif",
"profile_background_image_url_https://abs.twimg.com/images/themes/theme7/bg.gif",
"profile_background_tile": false, "profile_image_url.http://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png",
"profile_image_url_https://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png",
"profile_link_color",990000",
"profile_sidebar_border_color",DFDFDFDF",
"profile_sidebar_fill_color",F3F3F3",
"profile_text_color",333333333",
"profile_use_background_image": true,
"default_profile": false,
"default_profile_image": true,
"following": false,
"follow_request_sent": false,
"notifications": false
},
"geo":{"type]Point]coordinates":[4.262577,117.9022693]},
"coordinates":{"type]Point]coordinates":[117.9022693,4.262577]},
"place": {
"idebeeb42e1fdfb3cfe4",
"urlapihttps://api.twitter.com/1.1/geo/id/eeb42e1fdfb3cfe4.json",
"place_type",city",
"name",Tawau",
"full_name",Tawau",
"country_codeMYMY",
"country",Malaysia",
"contained_within":[],
"bounding_box": {
"type",Polygon",
"coordinates":
[116.9334819,4.1657558],
[118.3555885,4.1657558],
[118.3555885,4.7962486],
[116.9334819,4.7962486]
]
},
"attributes": {}
},
"contributors": null,
"re"count":0,
"favorite_count",
"entities": {
"hashtags":[{"text]GetWellSoonTao]indices":[13,28]}],
"]bols":[],
"user_mentions":
"screen_namehzhztttao",",
"nameEXEXO-TAO"
"id":2483888760,
"id_str",2483888760",
"indices":[0,12]
},
"urls":[]
},
"favorited": false,
"re:ed": false,
lang"zh
} else {
Example:Tawau, Malaysia (4.262577, 117.9022693) @ 2015-02-03 23:36:14 +0900
@tsinychong (@ Tawau, Sabah, East Malaysia): @hztttao___ #GetWellSoonTao 孩子,我们都知道你有多幺的努力工作,但是健康更重要啊!真的心疼你们!Please take care your health carefully boy! Love you all
Ulu Kelang, Malaysia (3.1958138, 101.7770204) @ 2015-02-03 23:36:27 +0900
@ayieshcasanova (@ ): I love you so much - sent to myself
Kau pehal - reply back to myself
Sebab aku sayang diri sendiri la kau masih hidup sampai skrg . K gila
Quixeramobim, Brasil (0.0, 0.0) @ 2015-02-03 23:36:27 +0900
@jackinhanj (@ Quixeramobim/ce): A Tuesday with much love that clip ♥ #FourFiveSecondsOnVEVO @rihanna
Quilpué, Chile (-33.0484477, -71.4708018) @ 2015-02-03 23:37:03 +0900
@PereiraCote (@ Quilpué-Chile): #HappyBDayJustinTimberlake i love you!!!! :'3 #Mirrors mi cancion favorita de el :)
Île-de-France, France (0.0, 0.0) @ 2015-02-03 23:38:11 +0900
@allxflouis (@ France): @Real_Liam_Payne
Ur story is the most touching i've never heard
I cry when I remember what you went through
U r our warrior. Love ya x1
Merkez, Türkiye (0.0, 0.0) @ 2015-02-03 23:38:12 +0900
@gltenmer (@ ): you make me happy @justinbieber ,please.
FOLLOW ME ,PLEASE.
I am TURKISH BELİEBER.
SEE ME ,PLZ.♥
I LOVE U SO MUCH♥♥
x3.078
Lecce, Italia (0.0, 0.0) @ 2015-02-03 23:40:18 +0900
@Cinzia268 (@ Collepasso): Love me like you do.
Liloy, Republika ng Pilipinas (8.1198248, 122.6804241) @ 2015-02-03 23:42:01 +0900
@tonetmurcia (@ Murcia Spain): like what..while watching Unforgettable Love there's a certain question that kept on poking my head..jeez I feel you Nina
Shanghai, 中华人民共和国 (31.21431856, 121.55950506) @ 2015-02-03 23:43:54 +0900
@lilannash (@ Shanghai): #pureandwhole spring new dishes: I already love all of them. This is my favourite place after my… http://t.co/jKD9ndRfqW
Los Angeles, United States (0.0, 0.0) @ 2015-02-03 23:45:16 +0900
@awarde (@ ): @chankpc @CNM_JLin_Vids i love how the NY media basically said that we know r hands have been tied with the Lakers
geocode if not specifiedhttps://api.twitter.com/1.1/search/tweets.json?q=love&count=100&since_id=(since_id)geocodehttps://api.twitter.com/1.1/search/tweets.json?q=love&geocode=40.7033121,-73.979681,10km&count=100&since_id=(since_id)I think it will be the same as (since_id).In addition, you can see many 実行s with longitude (0.0, 0.0). The geocode search specification is probably calculated from the la。, so it may be missing from the search result if the geocode condition is included in the 位置 that contains the city.Please note.{{ data.meta.length_formatted }}GeolocalizationThe search API will first attempt to find conducts which have lat/long within the queried geocode, and in case of not having success, it will attempt to find s created by users whose profile location can be reverse geocoded into a lat/long within the queried geocode, meaning that is possible to receive s which do not include lat/long information. https://dev.twitter.com/rest/public/search In geocode search also uses profile location information.