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

Support for Collection API #159

Open
wants to merge 4 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
82 changes: 82 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package anaconda

type Collection struct {
Name string
UserId string `json:"user_id"`
CollectionUrl string `json:"collection_url"`
Description string
Url string
Visibility string
TimelineOrder string `json:"timeline_order"`
CollectionType string `json:"collection_type"`
}

type CollectionListResult struct {
Objects struct {
Users map[string]User
Timelines map[string]Collection
}
Response struct {
Results []struct {
TimelineId string `json:"timeline_id"`
}
Cursors struct {
NextCursor string `json:"next_cursor"`
}
}
}

// GET collections/show
// Also used by POST collections/create
// Also used by POST collections/update
type CollectionShowResult struct {
Objects struct {
Users map[string]User
Timelines map[string]Collection
}
Response struct {
TimelineId string `json:"timeline_id"`
}
}

type CollectionEntriesResult struct {
Objects struct {
Timelines map[string]Collection
}
Tweets []Tweet
Response struct {
Position struct {
MaxPosition string `json:"max_position"`
MinPosition string `json:"min_position"`
WasTruncated bool `json:"was_truncated"`
}
Timeline []struct {
FeatureContext string `json:"feature_context"`
Tweet struct {
Id string
SortIndex string `json:"sort_index"`
}
}
TimelineId string `json:"timeline_id"`
}
}

type CollectionDestroyResult struct {
Destroyed bool
}

// POST collections/entries/add
// Also used by POST collections/entries/remove
// Also used by POST collections/entries/move
type CollectionEntryAddResult struct {
Objects struct{}
Response struct {
Errors []struct {
Change struct {
Op string
TweetId string `json:"tweet_id"`
}
Reason string
}
}
}
90 changes: 90 additions & 0 deletions collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package anaconda

import (
"fmt"
"net/url"
)

func (a TwitterApi) GetCollectionListByUserId(userId int64, v url.Values) (result CollectionListResult, err error) {
v = cleanValues(v)
v.Set("user_id", fmt.Sprintf("%d", userId))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/list.json", v, &result, _GET, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) GetCollectionListByScreenName(screenName string, v url.Values) (result CollectionListResult, err error) {
v = cleanValues(v)
v.Set("screen_name", screenName)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/list.json", v, &result, _GET, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) GetCollectionShow(id string, v url.Values) (result CollectionShowResult, err error) {
v = cleanValues(v)
v.Set("id", id)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/show.json", v, &result, _GET, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) GetCollectionEntries(id string, v url.Values) (result CollectionEntriesResult, err error) {
v = cleanValues(v)
v.Set("id", id)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/entries.json", v, &result, _GET, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) CreateCollection(name string, v url.Values) (result CollectionShowResult, err error) {
v = cleanValues(v)
v.Set("name", name)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/create.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) UpdateCollection(id string, v url.Values) (result CollectionShowResult, err error) {
v = cleanValues(v)
v.Set("id", id)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/update.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) DestroyCollection(id string, v url.Values) (result CollectionDestroyResult, err error) {
v = cleanValues(v)
v.Set("id", id)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/destroy.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) AddEntryToCollection(id string, tweetId int64, v url.Values) (result CollectionEntryAddResult, err error) {
v = cleanValues(v)
v.Set("id", id)
v.Set("tweet_id", fmt.Sprintf("%d", tweetId))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/entries/add.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) RemoveEntryFromCollection(id string, tweetId int64, v url.Values) (result CollectionEntryAddResult, err error) {
v = cleanValues(v)
v.Set("id", id)
v.Set("tweet_id", fmt.Sprintf("%d", tweetId))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/entries/remove.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}

func (a TwitterApi) MoveEntryFromCollection(id string, tweetId, relativeTo int64, v url.Values) (result CollectionEntryAddResult, err error) {
v = cleanValues(v)
v.Set("id", id)
v.Set("tweet_id", fmt.Sprintf("%d", tweetId))
v.Set("relative_to", fmt.Sprintf("%d", relativeTo))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/collections/entries/move.json", v, &result, _POST, response_ch}
return result, (<-response_ch).err
}
10 changes: 10 additions & 0 deletions twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,22 @@ func SetConsumerKey(consumer_key string) {
oauthCredentials.Token = consumer_key
}

//GetConsumerKey will return the consumer key set by SetConsumerSecret.
func GetConsumerKey() string {
return oauthClient.Credentials.Token
}

//SetConsumerSecret will set the application-specific secret used in the initial OAuth process
//This secret is listed on https://dev.twitter.com/apps/YOUR_APP_ID/show
func SetConsumerSecret(consumer_secret string) {
oauthCredentials.Secret = consumer_secret
}

//GetConsumerSecret will return the consumer secret set by SetConsumerKey.
func GetConsumerSecret() string {
return oauthClient.Credentials.Secret
}

// ReturnRateLimitError specifies behavior when the Twitter API returns a rate-limit error.
// If set to true, the query will fail and return the error instead of automatically queuing and
// retrying the query when the rate limit expires
Expand Down