Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for reverse geocode lookup #173

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion json/statuses/show.json?id=404409873170841600

This file was deleted.

1 change: 0 additions & 1 deletion json/statuses/show.json?id=738567564641599489

This file was deleted.

46 changes: 46 additions & 0 deletions rate_limit_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package anaconda

import "net/url"

type RateLimitSearchResult struct {
RateLimitContext struct {
AccessToken string `json:"access_token"`
} `json:"rate_limit_context"`
Resources struct {
Geo struct {
Similar_places struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
} `json:"/geo/similar_places"`
Place_id struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
} `json:"/geo/id/:place_id"`
Reverse_geocode struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
} `json:"/geo/reverse_geocode"`
Search struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
} `json:"/geo/search"`
} `json:"geo"`
Search struct {
Tweets struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
} `json:"/search/tweets"`
} `json:"search"`
} `json:"resources"`
}

func (a TwitterApi) GetRateLimitStatus(v url.Values) (r RateLimitSearchResult, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/application/rate_limit_status.json", v, &r, _GET, response_ch}
return r, (<-response_ch).err
}
58 changes: 58 additions & 0 deletions reverse_geocode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package anaconda

import "net/url"

type GeoReverseSearchResult struct {
Result struct {
Places []struct {
Id string `json:"id"`
URL string `json:"url"`
PlaceType string `json:"place_type"`
Name string `json:"name"`
FullName string `json:"full_name"`
CountryCode string `json:"country_code"`
Country string `json:"country"`
ContainedWithin []struct {
ID string `json:"id"`
URL string `json:"url"`
PlaceType string `json:"place_type"`
Name string `json:"name"`
FullName string `json:"full_name"`
CountryCode string `json:"country_code"`
Country string `json:"country"`
Centroid []float64 `json:"centroid"`
BoundingBox struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
} `json:"bounding_box"`
Attributes struct {
} `json:"attributes"`
} `json:"contained_within"`
Centroid []float64 `json:"centroid"`
BoundingBox struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
} `json:"bounding_box"`
Attributes struct {
} `json:"attributes"`
} `json:"places"`
} `json:"result"`
Query struct {
URL string `json:"url"`
Type string `json:"type"`
Params struct {
Accuracy float64 `json:"accuracy"`
Granularity string `json:"granularity"`
Coordinates struct {
Coordinates []float64 `json:"coordinates"`
Type string `json:"type"`
} `json:"coordinates"`
} `json:"params"`
} `json:"query"`
}

func (a TwitterApi) GetPlaces(v url.Values) (p GeoReverseSearchResult, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/geo/reverse_geocode.json", v, &p, _GET, response_ch}
return p, (<-response_ch).err
}
28 changes: 28 additions & 0 deletions twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import (

"github.com/ChimeraCoder/tokenbucket"
"github.com/garyburd/go-oauth/oauth"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)

const (
Expand Down Expand Up @@ -121,6 +123,32 @@ func NewTwitterApi(access_token string, access_token_secret string) *TwitterApi
return c
}

//NewTwitterApi takes an user-specific access token and secret and returns a TwitterApi struct for that user.
//The TwitterApi struct can be used for accessing any of the endpoints available.
func NewTwitterAppApi(consumer_key string, consumer_secret string) *TwitterApi {
//TODO figure out how much to buffer this channel
//A non-buffered channel will cause blocking when multiple queries are made at the same time

config := &clientcredentials.Config{ClientID: consumer_key,
ClientSecret: consumer_secret,
TokenURL: "https://api.twitter.com/oauth2/token"}

// OAuth2 http.Client will automatically authorize Requests
httpClient := config.Client(oauth2.NoContext)

queue := make(chan query)
c := &TwitterApi{
queryQueue: queue,
bucket: nil,
returnRateLimitError: false,
HttpClient: httpClient,
Log: silentLogger{},
baseUrl: BaseUrl,
}
go c.throttledQuery()
return c
}

//SetConsumerKey will set the application-specific consumer_key used in the initial OAuth process
//This key is listed on https://dev.twitter.com/apps/YOUR_APP_ID/show
func SetConsumerKey(consumer_key string) {
Expand Down