• Stars
    star
    1,143
  • Rank 40,777 (Top 0.9 %)
  • Language
    Go
  • License
    MIT License
  • Created over 11 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Go client library for the Twitter 1.1 API

Anaconda

Build Status Build Status GoDoc

Anaconda is a simple, transparent Go package for accessing version 1.1 of the Twitter API.

Successful API queries return native Go structs that can be used immediately, with no need for type assertions.

Examples

Authentication

If you already have the access token (and secret) for your user (Twitter provides this for your own account on the developer portal), creating the client is simple:

api := anaconda.NewTwitterApiWithCredentials("your-access-token", "your-access-token-secret", "your-consumer-key", "your-consumer-secret")

Queries

Queries are conducted using a pointer to an authenticated TwitterApi struct. In v1.1 of Twitter's API, all requests should be authenticated.

searchResult, _ := api.GetSearch("golang", nil)
for _ , tweet := range searchResult.Statuses {
    fmt.Println(tweet.Text)
}

Certain endpoints allow separate optional parameter; if desired, these can be passed as the final parameter.

//Perhaps we want 30 values instead of the default 15
v := url.Values{}
v.Set("count", "30")
result, err := api.GetSearch("golang", v)

(Remember that url.Values is equivalent to a map[string][]string, if you find that more convenient notation when specifying values). Otherwise, nil suffices.

Streaming

Anaconda supports the Streaming APIs. You can use PublicStream* or UserStream API methods. A go loop is started an gives you an stream that sends interface{} objects through it's chan C Objects which you can cast into a tweet, event and more.

v := url.Values{}
s := api.UserStream(v)

for t := range s.C {
  switch v := t.(type) {
  case anaconda.Tweet:
    fmt.Printf("%-15s: %s\n", v.User.ScreenName, v.Text)
  case anaconda.EventTweet:
    switch v.Event.Event {
    case "favorite":
      sn := v.Source.ScreenName
      tw := v.TargetObject.Text
      fmt.Printf("Favorited by %-15s: %s\n", sn, tw)
    case "unfavorite":
      sn := v.Source.ScreenName
      tw := v.TargetObject.Text
      fmt.Printf("UnFavorited by %-15s: %s\n", sn, tw)
    }
  }
}

Endpoints

Anaconda implements most of the endpoints defined in the Twitter API documentation. For clarity, in most cases, the function name is simply the name of the HTTP method and the endpoint (e.g., the endpoint GET /friendships/incoming is provided by the function GetFriendshipsIncoming).

In a few cases, a shortened form has been chosen to make life easier (for example, retweeting is simply the function Retweet)

Error Handling, Rate Limiting, and Throttling

Error Handling

Twitter errors are returned as an ApiError, which satisfies the error interface and can be treated as a vanilla error. However, it also contains the additional information returned by the Twitter API that may be useful in deciding how to proceed after encountering an error.

If you make queries too quickly, you may bump against Twitter's rate limits. If this happens, anaconda automatically retries the query when the rate limit resets, using the X-Rate-Limit-Reset header that Twitter provides to determine how long to wait.

In other words, users of the anaconda library should not need to handle rate limiting errors themselves; this is handled seamlessly behind-the-scenes. If an error is returned by a function, another form of error must have occurred (which can be checked by using the fields provided by the ApiError struct).

(If desired, this feature can be turned off by calling ReturnRateLimitError(true).)

Throttling

Anaconda now supports automatic client-side throttling of queries to avoid hitting the Twitter rate-limit.

This is currently off by default; however, it may be turned on by default in future versions of the library, as the implementation is improved.

To set a delay between queries, use the SetDelay method:

api.SetDelay(10 * time.Second)

Delays are set specific to each TwitterApi struct, so queries that use different users' access credentials are completely independent.

To turn off automatic throttling, set the delay to 0:

api.SetDelay(0 * time.Second)

Query Queue Persistence

If your code creates a NewTwitterApi in a regularly called function, you'll need to call .Close() on the API struct to clear the queryQueue and allow the goroutine to exit. Otherwise you could see goroutine and therefor heap memory leaks in long-running applications.

Google App Engine

Since Google App Engine doesn't make the standard http.Transport available, it's necessary to tell Anaconda to use a different client context.

api = anaconda.NewTwitterApi("", "")
c := appengine.NewContext(r)
api.HttpClient.Transport = &urlfetch.Transport{Context: c}

License

Anaconda is free software licensed under the MIT/X11 license. Details provided in the LICENSE file.

More Repositories

1

gojson

Automatically generate Go (golang) struct definitions from example JSON
Go
2,655
star
2

gitgo

A Go implementation of Git functions
Go
345
star
3

koro

A Bengali (เฆฌเฆพเฆ‚เฆฒเฆพ) version of the Go compiler and toolchain
Go
80
star
4

docker-without-docker

A presentation about Docker, systemd, and containerization, given at the Recurse Center
78
star
5

go-for-pythonists

Presentation given at New York Python Meetup
75
star
6

tokenbucket

A Go implementation of the token bucket scheduling algorithm
Go
39
star
7

go-angular-jetpack

A sample webapp that uses both Go and Angular.js
Go
34
star
8

go-server-bootstrap

A simple sample Go web server with authentication (via OAuth, using Clef.io)
CSS
24
star
9

go-yo

A yo-yo for email, written in Go
Go
24
star
10

electrovim

JavaScript
19
star
11

pluto

Pluto is not a Planet
Go
19
star
12

journalctl-go

A Go client library for reading the systemd journal
Go
12
star
13

go-for-statistical-programming

A presentation given at the NYC Open Statistical Programming Meetup on 8/19/2013
Go
12
star
14

readinglevel

A Go package for determining the reading level of a passage of text
Go
9
star
15

emojibot

Go
9
star
16

goxml

Automatically generate golang struct definitions from example XML
Go
9
star
17

PySq

Python
9
star
18

goangel

A Go client library for the AngelList API
Go
9
star
19

Planet-hackNY

Planet HackNY
JavaScript
8
star
20

ncnyt

All the news that's fit to print()
Python
8
star
21

geheimschreiber

A Go implementation of the Geheimschreiber ("G-writer")
Go
8
star
22

nginx-ssl-docker

A simple Docker image for running nginx with SSL
Nginx
7
star
23

go-react-jetpack

Go
7
star
24

gopherbot

An IRC bot. Currently "gophrbot" on #hackny on Freenode.
Go
6
star
25

intro-to-scipy-and-numpy

A talk that was given on Nov. 14, 2013 at Columbia University.
6
star
26

docker-offlineimap-fastmail

A basic Docker container for running offlineimap with Fastmail
Shell
6
star
27

erowidparser

A quick and dirty parser for the Erowid archives
Go
6
star
28

docker-offlineimap

A Docker container for syncrhonizing email messages over IMAP in the Maildir format
Shell
5
star
29

mtwerk

A Go client library for Amazon Mechanical Turk. The "fun" alternative to mturk.
Go
5
star
30

terrorists-to-communists

JavaScript
5
star
31

duckduckbang

An add-on for adding touch-friendly !bang buttons to the DuckDuckGo search results page
JavaScript
4
star
32

MLang

4
star
33

docker-mediatomb

A Docker container for running Mediatomb
4
star
34

hackboston-webapp

Go
4
star
35

python-for-data-science

Presentation: "Python for Data Science"
4
star
36

InstaVote

InstaVote implements instant-runoff voting using Python
Python
4
star
37

lobbyistsfromlastnight

Python
4
star
38

hackny-masters-introduction-to-go

A talk given at the HackNY Masters conference on getting started with Go
Go
4
star
39

cs5830-lab2

Go
3
star
40

docker-hackerschool

A presentation about Docker, Ansible, and systemd given at Hacker School
3
star
41

vc-for-hackers

A presentation given to the 2013 hackNY fellows
3
star
42

veneur-docker

A vanilla Dockerfile and sample systemd unit for running Veneur
Shell
3
star
43

goimports-log-bug

Go
3
star
44

StatHomeworkHelper

Implements statistical formulas commonly referenced in homework for statistics classes
Python
3
star
45

mgo

A git mirror of labix.org/v2/mgo
Go
3
star
46

hackers-guide-to-privacy-and-security

3
star
47

PlayThatRacket

3
star
48

hackboston-scraping

Go
3
star
49

otterandjen

Go
3
star
50

FreeSeat

Seating Chart Generator
Common Lisp
3
star
51

textcorpora

A Go helper package that provides an interface for various corpora used in text analysis
Go
3
star
52

go.crypto

A git mirror of code.google.com/p/go.crypto/bcrypt
Go
3
star
53

Babar

Babar is a Ruby wrapper for the Toodledo API
Ruby
3
star
54

twitter-follower-logger

Go
3
star
55

erowidcoin

Go
2
star
56

docker-cockatrice

A Docker container for running Cockatrice, a Qt4 application
Shell
2
star
57

crypto-mathematical-and-applied

2
star
58

toodlego

A Go client library for the Toodledo API
Go
2
star
59

znc-kibana-playbooks

2
star
60

git-practices

Some basic git practices for new users to get started quickly
2
star
61

ReadabilityParser

Python
2
star
62

humble-interfaces-gophercon-india-2016

2
star
63

hacker-forum-get-going-with-go

A presentation given at the Hacker Union Hacker Forum on 2 December 2013 at the Huffington Post.
Go
2
star
64

PitAI

Go
2
star
65

Pysq-dev

Python wrapper for Foursquare
Python
2
star
66

gojson-web

An experimental port of gojson to the browser, using gopherjs
JavaScript
2
star
67

PKGBUILDS

PKGBUILD files for AUR packages on Arch Linux
Shell
2
star
68

symmetric-api-testing-goruco-2016

2
star
69

rc-webhook-server

Go
2
star
70

building-planet-hackny-in-go

A talk given on Nov 20, 2013 at Columbia University
Go
2
star
71

notifybuild

Go
2
star
72

fourthquare

A Lisp wrapper for the foursquare API
Common Lisp
2
star
73

android-tutorial

Sample application from https://developer.android.com/training/index.html
Java
2
star
74

tdigest-rust

Rust
1
star
75

recurse-dokku

Shell
1
star
76

rustphrases

Rust
1
star
77

gopkgrss

Mirror of jteeuwen/go-pkg-rss
Go
1
star
78

cs5560-hw3

Python
1
star
79

ncmpcpp-0.6-docker

A Docker image for the latest version of ncmpcpp that supports filtering
Shell
1
star
80

full-stack-go-client-slides

Part II of a workshop on developing full-stack Go applications (native web, mobile, & desktop)
Go
1
star
81

beamer-sample-presentation

1
star
82

godeckbrew

Go
1
star
83

gilt-webapp

Go
1
star
84

GOctober

JavaScript
1
star
85

prezent

Alternate themes for the go presentation tool
CSS
1
star